static void accel_data_report_worker(void *arg) { struct sensor_accel_info *info; struct report_info *rinfo; struct report_info_data *rinfo_data; struct sensor_event_data *event_data; struct timespec ts; uint16_t payload_size; payload_size = (PRESSURE_READING_NUM * sizeof(struct sensor_event_data)) + (REPORTING_SENSORS * sizeof(struct report_info)); info = arg; if (info->callback) { rinfo_data = malloc(sizeof(struct report_info_data) + payload_size); if (!rinfo_data) goto out; rinfo_data->num_sensors_reporting = REPORTING_SENSORS; rinfo = rinfo_data->reportinfo; rinfo->id = info->sensor_id; rinfo->flags = 0; event_data = (struct sensor_event_data *)&rinfo->data_payload[0]; up_rtc_gettime(&ts); rinfo->reference_time = timespec_to_nsec(&ts); gb_debug("[%u.%03u]\n", ts.tv_sec, (ts.tv_nsec / 1000000)); #ifdef BATCH_PROCESS_ENABLED /* * Batch sensor data values and its time_deltas * until max fifo event count */ #else /* Single sensor event data */ rinfo->readings = PRESSURE_READING_NUM; event_data->time_delta = 0; event_data->data_value[0] = data++; event_data->data_value[1] = data++; event_data->data_value[2] = data++; #endif gb_debug("report sensor: %d\n", rinfo->id); info->callback(info->sensor_id, rinfo_data, payload_size); free(rinfo_data); } out: /* cancel any work and reset ourselves */ if (!work_available(&info->data_report_work)) work_cancel(LPWORK, &info->data_report_work); /* if not already scheduled, schedule start */ if (work_available(&info->data_report_work)) work_queue(LPWORK, &info->data_report_work, accel_data_report_worker, info, MSEC2TICK(1200)); }
static int sensor_accel_op_flush(struct device *dev, uint8_t id) { struct sensor_event_data *event_data; struct sensor_accel_info *info; struct report_info_data *rinfo_data; struct report_info *rinfo; struct timespec ts; uint16_t payload_size; payload_size = (PRESSURE_READING_NUM * sizeof(struct sensor_event_data)) + (REPORTING_SENSORS * sizeof(struct report_info)); gb_debug("%s:\n", __func__); if (!dev || !device_get_private(dev)) { return -EINVAL; } info = device_get_private(dev); if (info->callback) { rinfo_data = malloc(sizeof(struct report_info) + payload_size); if (!rinfo_data) return -ENOMEM; rinfo_data->num_sensors_reporting = REPORTING_SENSORS; rinfo = rinfo_data->reportinfo; rinfo->id = info->sensor_id; event_data = (struct sensor_event_data *)&rinfo->data_payload[0]; up_rtc_gettime(&ts); rinfo->reference_time = timespec_to_nsec(&ts); gb_debug("[%u.%03u]\n", ts.tv_sec, (ts.tv_nsec / 1000000)); rinfo->flags = REPORT_INFO_FLAG_FLUSHING | REPORT_INFO_FLAG_FLUSH_COMPLETE; #ifdef BATCH_PROCESS_ENABLED /* * Batch sensor data values and its time_deltas * until max fifo event count */ #else /* Single sensor event data */ rinfo->readings = PRESSURE_READING_NUM; event_data->time_delta = 0; event_data->data_value[0] = data++; event_data->data_value[1] = data++; event_data->data_value[2] = data++; #endif info->callback(info->sensor_id, rinfo_data, payload_size); free(rinfo_data); } return OK; }
static int stm32_rtc_alarm(time_t tv_sec, time_t tv_nsec, bool exti) { struct timespec alarmtime; int ret; /* Configure to receive RTC Alarm EXTI interrupt */ if (exti) { /* TODO: Make sure that that is no pending EXTI interrupt */ (void)stm32_exti_alarm(true, true, true, stm32_alarm_exti); } /* Configure the RTC alarm to Auto Wake the system */ (void)up_rtc_gettime(&alarmtime); alarmtime.tv_sec += tv_sec; alarmtime.tv_nsec += tv_nsec; /* The tv_nsec value must not exceed 1,000,000,000. That * would be an invalid time. */ if (alarmtime.tv_nsec >= NSEC_PER_SEC) { /* Carry to the seconds */ alarmtime.tv_sec++; alarmtime.tv_nsec -= NSEC_PER_SEC; } /* Set the alarm */ g_alarmwakeup = false; ret = stm32_rtc_setalarm(&alarmtime, stm32_alarmcb); if (ret < 0) { lldbg("Warning: The alarm is already set\n"); } return ret; }
static inline int clock_basetime(FAR struct timespec *tp) { /* Get the complete time from the hi-res RTC. */ return up_rtc_gettime(tp); }
static inline void clock_basetime(FAR struct timespec *tp) { /* Get the complete time from the hi-res RTC. */ (void)up_rtc_gettime(tp); }
int clock_gettime(clockid_t clock_id, struct timespec *tp) { #ifdef CONFIG_SYSTEM_TIME64 uint64_t msecs; uint64_t secs; uint64_t nsecs; #else uint32_t msecs; uint32_t secs; uint32_t nsecs; #endif int ret = OK; sdbg("clock_id=%d\n", clock_id); DEBUGASSERT(tp != NULL); /* CLOCK_REALTIME - POSIX demands this to be present. This is the wall * time clock. */ #ifdef CONFIG_RTC if (clock_id == CLOCK_REALTIME || clock_id == CLOCK_ACTIVETIME) #else if (clock_id == CLOCK_REALTIME) #endif { /* Do we have a high-resolution RTC that can provie us with the time? */ #ifdef CONFIG_RTC_HIRES if (g_rtc_enabled && clock_id != CLOCK_ACTIVETIME) { /* Yes.. Get the hi-resolution time from the RTC */ ret = up_rtc_gettime(tp); } else #endif { /* Get the elapsed time since power up (in milliseconds) biased * as appropriate. */ msecs = MSEC_PER_TICK * (g_system_timer - g_tickbias); sdbg("msecs = %d g_tickbias=%d\n", (int)msecs, (int)g_tickbias); /* Get the elapsed time in seconds and nanoseconds. */ secs = msecs / MSEC_PER_SEC; nsecs = (msecs - (secs * MSEC_PER_SEC)) * NSEC_PER_MSEC; sdbg("secs = %d + %d nsecs = %d + %d\n", (int)msecs, (int)g_basetime.tv_sec, (int)nsecs, (int)g_basetime.tv_nsec); /* Add the base time to this. */ secs += (uint32_t)g_basetime.tv_sec; nsecs += (uint32_t)g_basetime.tv_nsec; /* Handle carry to seconds. */ if (nsecs > NSEC_PER_SEC) { uint32_t dwCarrySecs = nsecs / NSEC_PER_SEC; secs += dwCarrySecs; nsecs -= (dwCarrySecs * NSEC_PER_SEC); } /* And return the result to the caller. */ tp->tv_sec = (time_t)secs; tp->tv_nsec = (long)nsecs; } sdbg("Returning tp=(%d,%d)\n", (int)tp->tv_sec, (int)tp->tv_nsec); } else { sdbg("Returning ERROR\n"); errno = EINVAL; ret = ERROR; } return ret; }
int sif_main(int argc, char *argv[]) { if (argc >= 2) { if (!strcmp(argv[1], "init")) { return sif_init(); } else if (!strcmp(argv[1], "gpio") && argc == 4) { vsn_sif.gpio[0] = atoi(argv[2]); vsn_sif.gpio[1] = atoi(argv[3]); sif_gpio1_update(); sif_gpio2_update(); printf("GPIO States: %2x %2x\n", vsn_sif.gpio[0], vsn_sif.gpio[1] ); return 0; } else if (!strcmp(argv[1], "pwr") && argc == 3) { int val = atoi(argv[2]); STM32_TIM_SETCOMPARE(vsn_sif.tim8, GPIO_OUT_PWRPWM_TIM8_CH, val); return 0; } else if (!strcmp(argv[1], "time") && argc == 3) { struct timespec t_set; t_set.tv_sec = atoi(argv[2]); clock_settime(CLOCK_REALTIME, &t_set); } else if (!strcmp(argv[1], "free") ) { uint16_t page = 0, stpage = 0xFFFF; int status; do { status = up_progmem_ispageerased(page++); /* Is this beginning of new free space section */ if (status == 0) { if (stpage == 0xFFFF) stpage = page-1; } else if (status != 0) { if (stpage != 0xFFFF) { printf("Free Range:\t%d\t-\t%d\n", stpage, page-2); stpage = 0xFFFF; } } } while (status >= 0); return 0; } else if (!strcmp(argv[1], "erase") && argc == 3 ) { int page = atoi(argv[2]); printf("Erase result: %d\n", up_progmem_erasepage(page) ); return 0; } else if (!strcmp(argv[1], "flash") && argc == 3 ) { uint16_t page = atoi(argv[2]); uint32_t addr = page * up_progmem_pagesize(page); printf("Write result: %d (writing to address %xh)\n", up_progmem_write( addr, "Test", 4 ), addr); return 0; } else if (!strcmp(argv[1], "i2c") && argc == 3) { int val = atoi(argv[2]); I2C_SETFREQUENCY(vsn_sif.i2c1, 100000); struct lis331dl_dev_s * lis = lis331dl_init(vsn_sif.i2c1, val); if (lis) { const struct lis331dl_vector_s * a; int i; uint32_t time_stamp = clock_systimer(); /* Set to 400 Hz : 3 = 133 Hz/axis */ lis331dl_setconversion(lis, false, true); /* Sample some values */ for (i=0; i<1000; ) { if ( (a = lis331dl_getreadings(lis)) ) { i++; printf("%d %d %d\n", a->x, a->y, a->z); } else if (errno != 11) { printf("Readings errno %d\n", errno); break; } } printf("Time diff = %d\n", clock_systimer() - time_stamp); lis331dl_deinit(lis); } else printf("Exit point: errno=%d\n", errno); return 0; } else if (!strcmp(argv[1], "cc")) { struct cc1101_dev_s * cc; uint8_t buf[64]; int sta; cc = cc1101_init(vsn_sif.spi2, CC1101_PIN_GDO0, GPIO_CC1101_GDO0, &cc1101_rfsettings_ISM1_868MHzGFSK100kbps); if (cc) { /* Work-around: enable falling edge, event and interrupt */ stm32_gpiosetevent(GPIO_CC1101_GDO0, false, true, true, cc1101_eventcb); /* Enable clock to ARM PLL, allowing to speed-up to 72 MHz */ cc1101_setgdo(cc, CC1101_PIN_GDO2, CC1101_GDO_CLK_XOSC3); cc1101_setchannel(cc, 0); /* AV Test Hex, receive on that channel */ cc1101_receive(cc); /* Enter RX mode */ while(1) { fflush(stdout); sta = cc1101_read(cc, buf, 64); if (sta > 0) { printf("Received %d bytes: rssi=%d [dBm], LQI=%d (CRC %s)\n", sta, cc1101_calcRSSIdBm(buf[sta-2]), buf[sta-1]&0x7F, (buf[sta-1]&0x80)?"OK":"BAD"); cc1101_write(cc, buf, 61); cc1101_send(cc); printf("Packet send back\n"); cc1101_receive(cc); } } } } } fprintf(stderr, "%s:\tinit\n\tgpio\tA B\n\tpwr\tval\n", argv[0]); struct timespec t_active; clock_gettime(CLOCK_ACTIVETIME, &t_active); fprintf(stderr, "rtc time = %u / %u, active = %u / %u, time / systick = %u / %u\n", up_rtc_gettime(), up_rtc_getclock(), t_active.tv_sec, t_active.tv_nsec, time(NULL), clock_systimer() ); return -1; }
int clock_gettime(clockid_t clock_id, struct timespec *tp) { #ifndef CONFIG_SYSTEM_UTC uint32_t msecs; uint32_t secs; uint32_t nsecs; #endif int ret = OK; sdbg("clock_id=%d\n", clock_id); /* CLOCK_REALTIME - POSIX demands this to be present. This is the wall * time clock. */ if (clock_id == CLOCK_REALTIME && tp) { #ifndef CONFIG_SYSTEM_UTC /* Get the elapsed time since power up (in milliseconds) biased * as appropriate. */ msecs = MSEC_PER_TICK * (clock_systimer() - g_tickbias); sdbg("msecs = %d g_tickbias=%d\n", (int)msecs, (int)g_tickbias); /* Get the elapsed time in seconds and nanoseconds. */ secs = msecs / MSEC_PER_SEC; nsecs = (msecs - (secs * MSEC_PER_SEC)) * NSEC_PER_MSEC; sdbg("secs = %d + %d nsecs = %d + %d\n", (int)msecs, (int)g_basetime.tv_sec, (int)nsecs, (int)g_basetime.tv_nsec); /* Add the base time to this. */ secs += (uint32_t)g_basetime.tv_sec; nsecs += (uint32_t)g_basetime.tv_nsec; /* Handle carry to seconds. */ if (nsecs > NSEC_PER_SEC) { uint32_t dwCarrySecs = nsecs / NSEC_PER_SEC; secs += dwCarrySecs; nsecs -= (dwCarrySecs * NSEC_PER_SEC); } /* And return the result to the caller. */ tp->tv_sec = (time_t)secs; tp->tv_nsec = (long)nsecs; #else /* if CONFIG_SYSTEM_UTC=y */ #ifdef CONFIG_RTC if (g_rtc_enabled) { tp->tv_sec = up_rtc_gettime(); tp->tv_nsec = (up_rtc_getclock() & (RTC_CLOCKS_PER_SEC-1) ) * (1000000000/TICK_PER_SEC); } else #endif { tp->tv_sec = g_system_utc; tp->tv_nsec = g_tickcount * (1000000000/TICK_PER_SEC); } #endif sdbg("Returning tp=(%d,%d)\n", (int)tp->tv_sec, (int)tp->tv_nsec); } /* CLOCK_ACTIVETIME is non-standard. Returns active UTC time, which is * disabled during power down modes. Unit is 1 second. */ #ifdef CONFIG_RTC else if (clock_id == CLOCK_ACTIVETIME && g_rtc_enabled && tp) { tp->tv_sec = g_system_utc; tp->tv_nsec = g_tickcount * (1000000000/TICK_PER_SEC); } #endif else { sdbg("Returning ERROR\n"); errno = EINVAL; ret = ERROR; } return ret; }