/** Initialize the low power ticker * */ void lp_ticker_init(void) { lptmr_config_t lptmrConfig; if (lp_ticker_inited) { return; } lp_ticker_inited = true; /* Setup low resolution clock - RTC */ if (!rtc_isenabled()) { rtc_init(); RTC_DisableInterrupts(RTC, kRTC_AlarmInterruptEnable | kRTC_SecondsInterruptEnable); RTC_StartTimer(RTC); } RTC->TAR = 0; /* Write clears the IRQ flag */ NVIC_ClearPendingIRQ(RTC_IRQn); NVIC_SetVector(RTC_IRQn, (uint32_t)rtc_isr); NVIC_EnableIRQ(RTC_IRQn); /* Setup high resolution clock - LPTMR */ LPTMR_GetDefaultConfig(&lptmrConfig); /* Use 32kHz drive */ CLOCK_SetXtal32Freq(OSC32K_CLK_HZ); lptmrConfig.prescalerClockSource = kLPTMR_PrescalerClock_2; LPTMR_Init(LPTMR0, &lptmrConfig); LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable); NVIC_ClearPendingIRQ(LPTMR0_IRQn); NVIC_SetVector(LPTMR0_IRQn, (uint32_t)lptmr_isr); EnableIRQ(LPTMR0_IRQn); }
time_t rtc_read(void) { // NOTE: After boot, RTC time registers are not synced immediately, about 1 sec latency. // RTC time got (through RTC_GetDateAndTime()) in this sec would be last-synced and incorrect. // NUC472/M453: Known issue // M487: Fixed if (! rtc_isenabled()) { rtc_init(); } S_RTC_TIME_DATA_T rtc_datetime; RTC_GetDateAndTime(&rtc_datetime); struct tm timeinfo; // Convert struct tm to S_RTC_TIME_DATA_T timeinfo.tm_year = rtc_datetime.u32Year - YEAR0; timeinfo.tm_mon = rtc_datetime.u32Month - 1; timeinfo.tm_mday = rtc_datetime.u32Day; timeinfo.tm_wday = rtc_datetime.u32DayOfWeek; timeinfo.tm_hour = rtc_datetime.u32Hour; if (rtc_datetime.u32TimeScale == RTC_CLOCK_12 && rtc_datetime.u32AmPm == RTC_PM) { timeinfo.tm_hour += 12; } timeinfo.tm_min = rtc_datetime.u32Minute; timeinfo.tm_sec = rtc_datetime.u32Second; // Convert to timestamp time_t t; if (_rtc_maketime(&timeinfo, &t, RTC_FULL_LEAP_YEAR_SUPPORT) == false) { return 0; } return t; }
void rtc_write(time_t t) { if (! rtc_isenabled()) { rtc_init(); } // Convert timestamp to struct tm struct tm timeinfo; if (_rtc_localtime(t, &timeinfo, RTC_FULL_LEAP_YEAR_SUPPORT) == false) { return; } S_RTC_TIME_DATA_T rtc_datetime; // Convert S_RTC_TIME_DATA_T to struct tm rtc_datetime.u32Year = timeinfo.tm_year + YEAR0; rtc_datetime.u32Month = timeinfo.tm_mon + 1; rtc_datetime.u32Day = timeinfo.tm_mday; rtc_datetime.u32DayOfWeek = timeinfo.tm_wday; rtc_datetime.u32Hour = timeinfo.tm_hour; rtc_datetime.u32Minute = timeinfo.tm_min; rtc_datetime.u32Second = timeinfo.tm_sec; rtc_datetime.u32TimeScale = RTC_CLOCK_24; // NOTE: Timing issue with write to RTC registers. This delay is empirical, not rational. RTC_SetDateAndTime(&rtc_datetime); wait_us(100); }
void rtc_init(void) { if (rtc_isenabled()) { return; } RTC_Open(NULL); }
void cmd_time(int argc, char **argv) { struct tm timeinfo; read_locoltime(&timeinfo); printf("Time as a custom formatted string = %d-%d-%d %d:%d:%d wday:%d yday:%d\n", timeinfo.tm_year, timeinfo.tm_mon, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min,timeinfo.tm_sec,timeinfo.tm_wday,timeinfo.tm_yday); printf("==============rtc_isenabled : %d============\n", rtc_isenabled()); }
void uart_start_h() { #define UART_SOCKET_STACK_SIZE 512 #define HAPNotifyHandle_STACK_SIZE 512*4 #define UART_SOCKET_PRIORITY 1 TaskHandle_t uart_socket_handle = NULL; TaskHandle_t HAPNotifyHandle_handle = NULL; const TickType_t xDelay = 10000; eTaskState TaskState; xSemaphoreHandle uart_socket_sema = NULL; xSemaphoreHandle HAPNotifyHandle_sema = NULL; struct tm timeinfo; //printf("==============[uart_start_h]rtc_isenabled : %d============\n", rtc_isenabled()); //vTaskDelay(xDelay*5); //printf("uart start\n"); vSemaphoreCreateBinary(uart_socket_sema); vSemaphoreCreateBinary(HAPNotifyHandle_sema); if(xTaskCreate(uart_socket_example, "uart_socket", UART_SOCKET_STACK_SIZE*2, &uart_socket_sema, UART_SOCKET_PRIORITY, &uart_socket_handle) != pdPASS) uart_printf("%s xTaskCreate failed", __FUNCTION__); if(xTaskCreate(HAPNotifyHandle, "HAPNotifyHandle", HAPNotifyHandle_STACK_SIZE, &HAPNotifyHandle_sema, UART_SOCKET_PRIORITY, &HAPNotifyHandle_handle) != pdPASS) uart_printf("%s xTaskCreate failed", __FUNCTION__); while(1) { vTaskDelay(xDelay); if (xSemaphoreTake(uart_socket_sema, ( TickType_t ) 2000) == pdTRUE) { //uart_printf("========uart_socket_right go on=========\n"); #if 0 read_locoltime(&timeinfo); printf("Time as a custom formatted string = %d-%d-%d %d:%d:%d wday:%d yday:%d\n", timeinfo.tm_year, timeinfo.tm_mon, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min,timeinfo.tm_sec,timeinfo.tm_wday,timeinfo.tm_yday); printf("==============rtc_isenabled : %d============\n", rtc_isenabled()); #endif } else { uart_printf("========uart_socket_wrong reset=========\n"); vTaskDelete(uart_socket_handle); uart_close(uart_socket); //uart_close(uart_socket_send); if(xTaskCreate(uart_socket_example, "uart_socket", UART_SOCKET_STACK_SIZE, &uart_socket_sema, UART_SOCKET_PRIORITY, &uart_socket_handle) != pdPASS) uart_printf("%s xTaskCreate failed", __FUNCTION__); } if (xSemaphoreTake(HAPNotifyHandle_sema, ( TickType_t ) 4000) != pdTRUE) { uart_printf("========HAPNotifyHandle reset=========\n"); vTaskDelete(HAPNotifyHandle_handle); if(xTaskCreate(HAPNotifyHandle, "HAPNotifyHandle", HAPNotifyHandle_STACK_SIZE, &HAPNotifyHandle_sema, UART_SOCKET_PRIORITY, &HAPNotifyHandle_handle) != pdPASS) uart_printf("%s xTaskCreate failed", __FUNCTION__); } } }
void rtc_init(void) { if (rtc_isenabled()) { return; } SIM_HAL_EnableRtcClock(SIM_BASE, 0U); RTC_HAL_Init(RTC_BASE); RTC_HAL_Enable(RTC_BASE); RTC_HAL_EnableCounter(RTC_BASE, true); }
void rtc_init(void) { RCC_OscInitTypeDef RCC_OscInitStruct; #if RTC_LSI if (rtc_inited) return; rtc_inited = 1; #endif RtcHandle.Instance = RTC; #if !RTC_LSI // Enable LSE Oscillator RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT RCC_OscInitStruct.LSIState = RCC_LSI_OFF; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly // Connect LSE to RTC __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); } else { error("Cannot initialize RTC with LSE\n"); } #else // Enable Power clock __PWR_CLK_ENABLE(); // Enable access to Backup domain HAL_PWR_EnableBkUpAccess(); // Reset Backup domain __HAL_RCC_BACKUPRESET_FORCE(); __HAL_RCC_BACKUPRESET_RELEASE(); // Enable LSI clock RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! RCC_OscInitStruct.LSEState = RCC_LSE_OFF; RCC_OscInitStruct.LSIState = RCC_LSI_ON; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { error("Cannot initialize RTC with LSI\n"); } // Connect LSI to RTC __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); #endif // Enable RTC __HAL_RCC_RTC_ENABLE(); RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { error("RTC error: RTC initialization failed."); } #if DEVICE_LOWPOWERTIMER #if RTC_LSI rtc_write(0); #else if (!rtc_isenabled()) { rtc_write(0); } #endif NVIC_ClearPendingIRQ(RTC_IRQn); NVIC_DisableIRQ(RTC_IRQn); NVIC_SetVector(RTC_IRQn, (uint32_t)RTC_IRQHandler); NVIC_EnableIRQ(RTC_IRQn); #endif }