void user_init(void) { ETS_UART_INTR_DISABLE(); UART_SetBaudrate(UART0, BIT_RATE_9600); UART_ResetFifo(UART0); UART_SetBaudrate(UART1, BIT_RATE_115200); UART_ResetFifo(UART1); flash_param_init(); flash_param = flash_param_get(); emsRxBuf = allocateRcvMsgBuff(); uart_init(BIT_RATE_9600, BIT_RATE_115200); rtc_clock_calibration = system_rtc_clock_cali_proc(); // get RTC clock period os_printf("rtc_clock_calibration: %0x\n", rtc_clock_calibration >>12 ); os_printf("system_get_rtc_time: %d\n", system_get_rtc_time()); os_printf("system_get_time: %d\n", system_get_time()); serverInit(flash_param->port); wifi_set_sleep_type(LIGHT_SLEEP_T); system_os_task(recvTask, recvTaskPrio, recvTaskQueue, recvTaskQueueLen); ETS_UART_INTR_ENABLE(); }
/** * The main entry point in an ESP8266 application. * It is where the logic of ESP8266 starts. */ void user_init() { system_timer_reinit(); // use microsecond os_timer_* // Initialize the UART devices int defaultBaudRate = 9600; #ifdef DEFAULT_CONSOLE_BAUDRATE defaultBaudRate = DEFAULT_CONSOLE_BAUDRATE; #endif uart_init(defaultBaudRate, defaultBaudRate); //uart_init(BIT_RATE_9600, BIT_RATE_9600); os_delay_us(1000); // make sure there's a gap on uart output UART_SetPrintPort(1); system_set_os_print(1); os_printf("\n\n\n\n"); os_delay_us(1000); // Dump the restart exception information. dumpRestart(); os_printf("Heap: %d\n", system_get_free_heap_size()); os_printf("Variables: %d @%dea = %dbytes\n", JSVAR_CACHE_SIZE, sizeof(JsVar), JSVAR_CACHE_SIZE * sizeof(JsVar)); os_printf("Time sys=%u rtc=%u\n", system_get_time(), system_get_rtc_time()); // Register the ESP8266 initialization callback. system_init_done_cb(initDone); os_timer_setfn(&mainLoopSuspendTimer, enableMainLoop, NULL); }
int luaopen_tmr( lua_State *L ){ int i; luaL_rometatable(L, "tmr.timer", (void *)tmr_dyn_map); for(i=0; i<NUM_TMR; i++){ alarm_timers[i].lua_ref = LUA_NOREF; alarm_timers[i].self_ref = LUA_REFNIL; alarm_timers[i].mode = TIMER_MODE_OFF; os_timer_disarm(&alarm_timers[i].os); } last_rtc_time=system_get_rtc_time(); // Right now is time 0 last_rtc_time_us=0; os_timer_disarm(&rtc_timer); os_timer_setfn(&rtc_timer, rtc_callback, NULL); os_timer_arm(&rtc_timer, 1000, 1); SWTIMER_REG_CB(rtc_callback, SWTIMER_RESUME); //The function rtc_callback calls the a function that calibrates the SoftRTC for drift in the esp8266's clock. //My guess: after the duration of light_sleep there's bound to be some drift in the clock, so a calibration is due. SWTIMER_REG_CB(alarm_timer_common, SWTIMER_RESUME); //The function alarm_timer_common handles timers created by the developer via tmr.create(). //No reason not to resume the timers, so resume em'. return 0; }
void meminfo(void) { os_printf("free heap size=%u, system time=%u, rtc time=%u \n", system_get_free_heap_size(), system_get_time(), system_get_rtc_time()); }
void rtc_timer_update_cb(void *arg){ uint64_t t = (uint64_t)system_get_rtc_time(); uint64_t delta = (t>=cur_count)?(t - cur_count):(0x100000000 + t - cur_count); // NODE_ERR("%x\n",t); cur_count = t; unsigned c = system_rtc_clock_cali_proc(); uint64_t itg = c >> 12; uint64_t dec = c & 0xFFF; rtc_us += (delta*itg + ((delta*dec)>>12)); // TODO: store rtc_us to rtc memory. }
/* * Called when SDK initialization is finished */ void sdk_init_done_cb() { srand(system_get_rtc_time()); #if !defined(ESP_ENABLE_HW_WATCHDOG) ets_wdt_disable(); #endif pp_soft_wdt_stop(); /* Schedule SJS initialization (`sjs_init()`) */ os_timer_disarm(&startcmd_timer); os_timer_setfn(&startcmd_timer, sjs_init, NULL); os_timer_arm(&startcmd_timer, 0, 0); }
/* * Called when SDK initialization is finished */ void sdk_init_done_cb() { srand(system_get_rtc_time()); #if !defined(ESP_ENABLE_HW_WATCHDOG) && !defined(RTOS_TODO) ets_wdt_disable(); #endif pp_soft_wdt_stop(); #ifndef RTOS_SDK /* Schedule SJS initialization (`sjs_init()`) */ os_timer_disarm(&startcmd_timer); os_timer_setfn(&startcmd_timer, sjs_init, NULL); os_timer_arm(&startcmd_timer, 500, 0); #else rtos_dispatch_initialize(); #endif }
static void ICACHE_FLASH_ATTR resetBtnTimerCb(void *arg) { static int resetCnt=0; if (!GPIO_INPUT_GET(BTNGPIO)) { resetCnt++; } else { if (resetCnt>=6) { //3 sec pressed wifi_station_disconnect(); wifi_set_opmode(0x3); //reset to AP+STA mode os_printf("Reset to AP mode. Restarting system...\n"); system_restart(); } else if (resetCnt>=2) { //1 sec pressed //print rtc_time to comport os_printf("rtc_time: %d", system_get_rtc_time() * system_rtc_clock_cali_proc()); } resetCnt=0; } }
void rtc_timer_update_cb(void *arg){ uint32_t t = (uint32_t)system_get_rtc_time(); uint32_t delta = 0; if(t>=cur_count){ delta = t-cur_count; }else{ delta = 0xFFFFFFF - cur_count + t + 1; } // uint64_t delta = (t>=cur_count)?(t - cur_count):(0x100000000 + t - cur_count); // NODE_ERR("%x\n",t); cur_count = t; uint32_t c = system_rtc_clock_cali_proc(); uint32_t itg = c >> 12; // ~=5 uint32_t dec = c & 0xFFF; // ~=2ff rtc_10ms += (delta*itg + ((delta*dec)>>12)) / 10000; // TODO: store rtc_10ms to rtc memory. }
// This returns the number of microseconds uptime. Note that it relies on the rtc clock, // which is notoriously temperature dependent inline static uint64_t rtc_timer_update(bool do_calibration){ if (do_calibration || rtc_time_cali==0) rtc_time_cali=system_rtc_clock_cali_proc(); uint32_t current = system_get_rtc_time(); uint32_t since_last=current-last_rtc_time; // This will transparently deal with wraparound uint32_t us_since_last=rtc2usec(since_last); uint64_t now=last_rtc_time_us+us_since_last; // Only update if at least 100ms has passed since we last updated. // This prevents the rounding errors in rtc2usec from accumulating if (us_since_last>=100000) { last_rtc_time=current; last_rtc_time_us=now; } return now; }
void ICACHE_FLASH_ATTR show_sysinfo() { uint32 chipid = system_get_chip_id(); uint32 heepsize = system_get_free_heap_size(); uint32 rtctime = system_get_rtc_time(); uint32 systime = system_get_time(); os_printf("\n\nSDK version: [%s]\n", system_get_sdk_version()); os_printf("SYSTEM INIT OVER\n"); os_printf("==========SYS INFO==========\n"); system_print_meminfo(); os_printf("CHIP ID: [%d]\n", chipid); os_printf("HEAP SIZE: [%d]\n", heepsize); os_printf("RTC TIME: [%d]\n", rtctime); os_printf("SYS TIME: [%d]\n", systime); os_printf("==========SYS INFO==========\n"); }
int luaopen_tmr( lua_State *L ){ int i; luaL_rometatable(L, "tmr.timer", (void *)tmr_dyn_map); for(i=0; i<NUM_TMR; i++){ alarm_timers[i].lua_ref = LUA_NOREF; alarm_timers[i].self_ref = LUA_REFNIL; alarm_timers[i].mode = TIMER_MODE_OFF; ets_timer_disarm(&alarm_timers[i].os); } last_rtc_time=system_get_rtc_time(); // Right now is time 0 last_rtc_time_us=0; ets_timer_disarm(&rtc_timer); ets_timer_setfn(&rtc_timer, rtc_callback, NULL); ets_timer_arm_new(&rtc_timer, 1000, 1, 1); return 0; }