37 #if !defined(_GNU_SOURCE)
44 #if !(defined(_WIN32) || defined(WIN32) || defined(__MINGW32__))
64 #ifdef PIOS_INCLUDE_RTC
65 #include "pios_rtc_priv.h"
69 #include "manualcontrolsettings.h"
73 #if defined(PIOS_INCLUDE_SYS)
74 static bool debug_fpe=
false;
76 #define MAX_SPI_BUSES 16
78 bool are_realtime =
false;
80 #ifdef PIOS_INCLUDE_SPI
81 static int num_spi = 0;
90 static HwSharedPortTypesOptions rcvr_proto = HWSHARED_PORTTYPES_SBUS;
93 #ifdef PIOS_INCLUDE_I2C
94 static char mag_orientation = 255;
96 static int num_i2c = 0;
107 static void Usage(
char *cmdName) {
108 printf(
"usage: %s [-f] [-r] [-m orientation] [-p proto] [-s spibase]\n"
109 "\t\t[-d drvname:bus:id] [-l logfile] [-I i2cdev] [-i drvname:bus]\n"
110 "\t\t[-g port] [-c confflash] [-x time] -!\n"
112 #
if !(defined(_WIN32) || defined(WIN32) || defined(__MINGW32__))
113 "\t-f\t\t\tEnables floating point exception trapping mode\n"
116 "\t-r\t\t\tGoes realtime and pins all memory (requires root)\n"
118 "\t-!\t\t\tUse a fake clock timebase gated by gcs/simsensors\n"
119 "\t-l log\t\t\tWrites simulation data to a log\n"
120 "\t-g port\t\t\tStarts FlightGear driver on port\n"
122 "\t-y\t\t\tUse an external simulator (drhil yasim)\n"
124 #
if !(defined(_WIN32) || defined(WIN32) || defined(__MINGW32__))
125 "\t-x time\t\t\tExit after time seconds\n"
127 "\t-S drvname:serialpath\tStarts a serial driver on serialpath\n"
128 "\t\t\tAvailable drivers: gps msp lighttelemetry telemetry omnip\n\n"
130 "\t-p proto\t\tSpecify a flyingpio rcvr protocol\n"
131 "\t\t\tAvailable protocols: dsm hottsumd hottsumh sbus ppm\n"
132 "\t\t\t\t\tsrxl ibus\n\n"
133 "\t-s spibase\t\tConfigures a SPI interface on the base path\n"
134 "\t-d drvname:bus:id\tStarts driver drvname on bus/id\n"
135 "\t\t\tAvailable drivers: bmm150 bmx055 flyingpio ms5611\n\n"
138 "\t-m orientation\t\tSets the orientation of an external mag\n"
139 "\t-I i2cdev\t\tConfigures an I2C interface on i2cdev\n"
140 "\t-i drvname:bus\t\tStarts a driver instance on bus\n"
141 "\t\t\tAvailable drivers: px4flow hmc5883 hmc5983 bmp280 ms5611\n\n"
143 "\t-c confflash\t\tspecify a filename to store config flash\n"
150 #ifdef PIOS_INCLUDE_OMNIP
154 #define SERIAL_BUF_LEN 384
155 static int handle_serial_device(
const char *optarg) {
158 strncpy(arg_copy, optarg,
sizeof(arg_copy));
159 arg_copy[
sizeof(arg_copy)-1] = 0;
163 char *drv_name = strtok_r(arg_copy,
":", &saveptr);
164 if (drv_name == NULL)
goto fail;
166 char *ser_path = strtok_r(NULL,
":", &saveptr);
167 if (ser_path == NULL)
goto fail;
175 port = strtol(ser_path, &endptr, 10);
182 if ((port > 0) && (port < 65536) && !(*endptr)) {
185 virtserial_cfg =
PIOS_malloc(
sizeof(*virtserial_cfg));
187 virtserial_cfg->
ip =
"0.0.0.0";
191 printf(
"Can't init PIOS_TCP\n");
197 }
else if (!strcmp(ser_path,
"stdio")) {
199 orig_stdout,
true)) {
200 printf(
"Can't init stdio-serial\n");
207 printf(
"Can't init serial\n");
215 SERIAL_BUF_LEN, SERIAL_BUF_LEN)) {
216 printf(
"Can't init PIOS_COM\n");
220 if (!strcmp(drv_name,
"gps")) {
223 }
else if (!strcmp(drv_name,
"msp")) {
226 }
else if (!strcmp(drv_name,
"lighttelemetry")) {
229 }
else if (!strcmp(drv_name,
"telemetry")) {
231 #ifdef PIOS_INCLUDE_OMNIP
232 }
else if (!strcmp(drv_name,
"omnip")) {
236 printf(
"Can't init OmniP radar\n");
241 printf(
"Unknown serial driver %s\n", drv_name);
250 #ifdef PIOS_INCLUDE_I2C
251 static int handle_i2c_device(
const char *optarg) {
254 strncpy(arg_copy, optarg,
sizeof(arg_copy));
255 arg_copy[
sizeof(arg_copy) - 1] = 0;
259 char *drv_name = strtok_r(arg_copy,
":", &saveptr);
260 if (drv_name == NULL) {
264 char *bus_num_str = strtok_r(NULL,
":", &saveptr);
265 if (bus_num_str == NULL) {
269 int bus_num = atoi(bus_num_str);
270 if ((bus_num < 0) || (bus_num >= num_i2c)) {
274 if (!strcmp(drv_name,
"px4flow")) {
280 }
else if (!strcmp(drv_name,
"hmc5883")) {
287 }
else if (!strcmp(drv_name,
"hmc5983")) {
289 mag_orientation, i2c_devs + bus_num,
293 }
else if (!strcmp(drv_name,
"bmp280")) {
295 i2c_devs + bus_num, NULL)) {
298 }
else if (!strcmp(drv_name,
"ms5611")) {
300 i2c_devs + bus_num, NULL)) {
313 #ifdef PIOS_INCLUDE_SPI
314 static int handle_rcvr_protocol(
const char *optarg) {
315 if (!strcmp(optarg,
"dsm")) {
316 rcvr_proto = HWSHARED_PORTTYPES_DSM;
317 }
else if (!strcmp(optarg,
"hottsumd")) {
318 rcvr_proto = HWSHARED_PORTTYPES_HOTTSUMD;
319 }
else if (!strcmp(optarg,
"hottsumh")) {
320 rcvr_proto = HWSHARED_PORTTYPES_HOTTSUMH;
321 }
else if (!strcmp(optarg,
"sbus")) {
322 rcvr_proto = HWSHARED_PORTTYPES_SBUS;
323 }
else if (!strcmp(optarg,
"ppm")) {
324 rcvr_proto = HWSHARED_PORTTYPES_PPM;
325 }
else if (!strcmp(optarg,
"srxl")) {
326 rcvr_proto = HWSHARED_PORTTYPES_SRXL;
327 }
else if (!strcmp(optarg,
"ibus")) {
328 rcvr_proto = HWSHARED_PORTTYPES_IBUS;
336 static int handle_device(
const char *optarg) {
339 strncpy(arg_copy, optarg,
sizeof(arg_copy));
340 arg_copy[
sizeof(arg_copy)-1] = 0;
344 char *drv_name = strtok_r(arg_copy,
":", &saveptr);
345 if (drv_name == NULL)
goto fail;
347 char *bus_num_str = strtok_r(NULL,
":", &saveptr);
348 if (bus_num_str == NULL)
goto fail;
350 char *dev_num_str = strtok_r(NULL,
":", &saveptr);
351 if (dev_num_str == NULL)
goto fail;
353 int bus_num = atoi(bus_num_str);
354 if ((bus_num < 0) || (bus_num >= num_spi)) {
358 int dev_num = atoi(dev_num_str);
363 if (!strcmp(drv_name,
"ms5611")) {
374 }
else if (!strcmp(drv_name,
"bmx055")) {
379 bzero(bmx055_cfg,
sizeof(*bmx055_cfg));
384 }
else if (!strcmp(drv_name,
"bmm150")) {
389 bzero(bmm150_cfg,
sizeof(*bmm150_cfg));
391 if (mag_orientation == 255) {
401 }
else if (!strcmp(drv_name,
"flyingpio")) {
436 static void go_realtime() {
440 int rc = mlockall(MCL_CURRENT | MCL_FUTURE);
451 cpu_set_t allowable_cpus;
453 CPU_ZERO(&allowable_cpus);
455 CPU_SET(0, &allowable_cpus);
457 rc = sched_setaffinity(0,
sizeof(allowable_cpus), &allowable_cpus);
460 perror(
"sched_setaffinity");
466 struct sched_param sch_p = {
470 rc = sched_setscheduler(0, SCHED_RR, &sch_p);
473 perror(
"sched_setscheduler");
479 printf(
"Only can do realtime stuff on Linux\n");
484 static int saved_argc;
485 static char **saved_argv;
487 #ifdef PIOS_INCLUDE_SIMSENSORS_YASIM
497 bool hw_argseen =
true;
499 while ((opt = getopt(argc, argv,
"!yfrx:g:l:s:d:S:I:i:m:c:p:")) != -1) {
501 #ifdef PIOS_INCLUDE_SIMSENSORS_YASIM
507 PIOS_Thread_FakeClock_Tick();
517 printf(
"Realtime must be before hw\n");
528 printf(
"Couldn't open logfile %s\n",
538 printf(
"Couldn't init fileout com layer\n");
545 if (handle_serial_device(optarg)) {
546 printf(
"Couldn't init device\n");
551 #ifdef PIOS_INCLUDE_I2C
557 printf(
"Mag orientation must be before hw\n");
561 mag_orientation = strtol(optarg, &endptr, 10);
563 if (!endptr || (*endptr !=
'\0')) {
564 printf(
"Invalid mag orientation\n");
575 printf(
"Couldn't init I2C\n");
583 if (handle_i2c_device(optarg)) {
584 printf(
"Couldn't init i2c device\n");
590 #ifdef PIOS_INCLUDE_SPI
592 if (handle_device(optarg)) {
593 printf(
"Couldn't init device\n");
612 printf(
"Couldn't init SPI\n");
623 printf(
"Proto must be before hw\n");
627 if (handle_rcvr_protocol(optarg)) {
628 printf(
"Invalid receiver proto\n");
636 uint16_t port = atoi(optarg);
641 printf(
"Couldn't init FGear\n");
648 #if !(defined(_WIN32) || defined(WIN32) || defined(__MINGW32__))
651 int timeout = atoi(optarg);
677 #if !(defined(_WIN32) || defined(WIN32) || defined(__MINGW32__))
678 static void sigint_handler(
int signum, siginfo_t *siginfo,
void *ucontext)
680 printf(
"\nSIGINT received. Shutting down\n");
684 static void sigfpe_handler(
int signum, siginfo_t *siginfo,
void *ucontext)
686 printf(
"\nSIGFPE received. OMG!\n");
695 orig_stdout = dup(STDOUT_FILENO);
697 if (orig_stdout < 0) {
702 if (dup2(STDERR_FILENO, STDOUT_FILENO) < 0) {
707 setbuf(stderr, NULL);
714 printf(
"HW serial number-- hex: %s\n", ser_text);
719 #ifdef PIOS_INCLUDE_RTC
721 printf(
"Pseudo-RTC started\n");
724 #if !(defined(_WIN32) || defined(WIN32) || defined(__MINGW32__))
725 struct sigaction sa_int = {
726 .sa_sigaction = sigint_handler,
727 .sa_flags = SA_SIGINFO,
730 int rc = sigaction(SIGINT, &sa_int, NULL);
734 struct sigaction sa_fpe = {
735 .sa_sigaction = sigfpe_handler,
736 .sa_flags = SA_SIGINFO,
739 rc = sigaction(SIGFPE, &sa_fpe, NULL);
745 feenableexcept(FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW |
749 printf(
"UNABLE TO DBEUG FPE ON OSX!\n");
771 int num = saved_argc;
778 for (i = 0; i < num; i++) {
779 argv[
i] = saved_argv[
i];
788 for (i = STDERR_FILENO+1; i < 1024; i++) {
792 execvp(argv[0], argv);
799 static inline uint64_t find_ser_in_buf(
const char *buf)
801 char *serial = strstr(buf,
"Serial");
809 while ((*serial) && (isblank(*serial))) {
813 if (*serial !=
':') {
819 while (isblank(*serial)) {
825 uint64_t ret = strtoull(serial, &endptr, 16);
831 if (*endptr !=
'\n' && (!*endptr)) {
855 fd = open(
"/proc/cpuinfo", O_RDONLY);
858 len = read(fd, buf,
sizeof(buf) - 1);
863 ret = find_ser_in_buf((
const char *) buf);
875 array[
i] = ret >> 56;
884 fd = open(
"/sys/class/dmi/id/board_serial", O_RDONLY);
887 len = read(fd, buf,
sizeof(buf) - 1);
891 if (buf[len-1] ==
'\n') {
896 memcpy(array, buf, len);
936 static inline char nibble_to_hex(uint8_t c)
965 str[i * 2] = nibble_to_hex(array[i] >> 4);
966 str[i * 2 + 1] = nibble_to_hex(array[i] & 0xf);
const struct pios_com_driver pios_serial_com_driver
Main PiOS header to include all the compiled in PiOS options.
int32_t PIOS_BMX055_SPI_Init(pios_bmx055_dev_t *dev, pios_spi_t spi_id, uint32_t slave_gyro, uint32_t slave_accel, const struct pios_bmx055_cfg *cfg)
Initialize the BMX-xxxx 6/9-axis sensor on SPI.
uint32_t temperature_interleaving
How many samples of pressure for each temperature measurement.
int PIOS_HAL_ConfigureExternalBaro(HwSharedExtBaroOptions baro, pios_i2c_t *i2c_id, const struct pios_i2c_adapter_cfg *i2c_cfg)
int32_t PIOS_BMM150_SPI_Init(pios_bmm150_dev_t *dev, pios_spi_t spi_id, uint32_t slave_mag, const struct pios_bmm150_cfg *cfg)
Initialize the BMM-xxxx 6/9-axis sensor on SPI.
#define PIOS_FILEOUT_TX_BUFFER_SIZE
int32_t PIOS_FLIGHTGEAR_Init(flightgear_dev_t *dev, uint16_t port)
int32_t PIOS_MS5611_SPI_Init(pios_spi_t spi_id, uint32_t slave_num, const struct pios_ms5611_cfg *cfg)
FlightGear driver header.
void PIOS_Modules_Enable(enum pios_modules module)
void * PIOS_malloc(size_t size)
int PIOS_HAL_ConfigureExternalMag(HwSharedMagOptions mag, HwSharedMagOrientationOptions orientation, pios_i2c_t *i2c_id, const struct pios_i2c_adapter_cfg *i2c_cfg)
#define PIOS_SYS_SERIAL_NUM_BINARY_LEN
void PIOS_Flash_Posix_SetFName(const char *name)
#define PIOS_COM_TELEM_USB
Configuration structure for the MS5611 driver.
int32_t PIOS_I2C_Init(pios_i2c_t *i2c_id, const char *path)
enum pios_ms5611_osr oversampling
#define PIOS_SYS_SERIAL_NUM_ASCII_LEN
#define PIOS_COM_LIGHTTELEMETRY
pios_i2c_t external_i2c_adapter_id
const struct pios_adc_driver pios_flyingpio_adc_driver
void sha1_init(SHA1_CTX *ctx)
int32_t PIOS_RCVR_Init(uintptr_t *rcvr_id, const struct pios_rcvr_driver *driver, const uintptr_t lower_id)
struct pios_i2c_adapter * pios_i2c_t
int32_t PIOS_SPI_Init(pios_spi_t *spi_dev, const struct pios_spi_cfg *cfg)
int32_t PIOS_FLYINGPIO_SPI_Init(pios_flyingpio_dev_t *dev, pios_spi_t spi_id, uint32_t slave_idx, uint8_t protocol)
Initialize the FlyingPIO programmed-IO expander.
struct pios_flyingpio_dev * pios_flyingpio_dev_t
int32_t PIOS_OMNIP_Init(omnip_dev_t *dev, const struct pios_com_driver *driver, uintptr_t lower_id)
Allocate and initialise OMNIP device.
SERIAL private definitions.
void PIOS_RTC_Init(const struct pios_rtc_cfg *cfg)
int32_t PIOS_SYS_Reset(void)
size_t PIOS_SYS_OsStackUnused(void)
int32_t PIOS_SERIAL_InitFromFd(uintptr_t *serial_id, int readfd, int writefd, bool dont_touch_line)
struct pios_bmx055_dev * pios_bmx055_dev_t
void sha1_update(SHA1_CTX *ctx, const uint8_t data[], size_t len)
int32_t PIOS_FILEOUT_Init(uintptr_t *fileout_id, const char *filename, const char *mode)
int32_t PIOS_SYS_SerialNumberGet(char str[PIOS_SYS_SERIAL_NUM_ASCII_LEN+1])
int32_t PIOS_TCP_Init(uintptr_t *tcp_id, const struct pios_tcp_cfg *cfg)
const struct pios_com_driver pios_fileout_com_driver
int32_t PIOS_COM_Init(uintptr_t *com_id, const struct pios_com_driver *driver, uintptr_t lower_id, uint16_t rx_buffer_len, uint16_t tx_buffer_len)
struct pios_bmm150_dev * pios_bmm150_dev_t
struct omnip_dev_s * omnip_dev_t
#define DONT_BUILD_IF(COND, MSG)
size_t PIOS_SYS_IrqStackUnused(void)
void sha1_final(SHA1_CTX *ctx, uint8_t hash[])
int32_t PIOS_ADC_Init(uintptr_t *adc_id, const struct pios_adc_driver *driver, uintptr_t lower_id)
int printf(const char *format,...)
const struct pios_com_driver pios_tcp_com_driver
void PIOS_SYS_Args(int argc, char *argv[])
void PIOS_HAL_SetReceiver(int receiver_type, uintptr_t value)
FILEOUT private definitions.
const struct pios_rcvr_driver pios_flyingpio_rcvr_driver
#define PIOS_Assert(test)
int32_t PIOS_SERIAL_Init(uintptr_t *serial_id, const char *path)
#define PIOS_INCLUDE_SIMSENSORS_YASIM
enum pios_bmm150_orientation orientation
USART private definitions.
int32_t PIOS_SYS_SerialNumberGetBinary(uint8_t array[PIOS_SYS_SERIAL_NUM_BINARY_LEN])