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; }