DeepPass / mDot_AT_firmware

Dependencies:   MTS-Serial libmDot mbed-rtos mbed-src

Fork of mDot_AT_firmware by MultiTech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wakeup.c Source File

wakeup.c

00001 #include "stm32f4xx_hal.h"
00002 #include "wakeup.h"
00003 
00004 
00005 static int rtc_inited = 0;
00006 static uint32_t timeout = 10;
00007 
00008 uint32_t wakeup_init(uint32_t seconds) {
00009     RCC_OscInitTypeDef RCC_OscInitStruct;
00010     uint32_t rtc_freq = 0;
00011 
00012     if (rtc_inited) {
00013         if (timeout != seconds)   {
00014             timeout = seconds;
00015             HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, timeout, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
00016         }
00017         return 0;
00018     }
00019     rtc_inited = 1;
00020 
00021     RtcHandle.Instance = RTC;
00022 
00023     // Enable Power clock
00024     __PWR_CLK_ENABLE();
00025 
00026     // Enable access to Backup domain
00027     HAL_PWR_EnableBkUpAccess();
00028 
00029     uint32_t count = RTC_ReadBackupRegister(RTC_BKP_DR0);
00030     printf("UPLINK: %lu\r\n", count);
00031 
00032 
00033     // Reset Backup domain
00034     __HAL_RCC_BACKUPRESET_FORCE();
00035     __HAL_RCC_BACKUPRESET_RELEASE();
00036 
00037     // Enable LSE Oscillator
00038     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
00039     RCC_OscInitStruct.PLL.PLLState   = RCC_PLL_NONE; /* Mandatory, otherwise the PLL is reconfigured! */
00040     RCC_OscInitStruct.LSEState       = RCC_LSE_ON; /* External 32.768 kHz clock on OSC_IN/OSC_OUT */
00041     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) {
00042         // Connect LSE to RTC
00043         __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE);
00044         __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
00045         rtc_freq = LSE_VALUE;
00046     } else {
00047         // Enable LSI clock
00048         RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
00049         RCC_OscInitStruct.PLL.PLLState   = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
00050         RCC_OscInitStruct.LSEState       = RCC_LSE_OFF;
00051         RCC_OscInitStruct.LSIState       = RCC_LSI_ON;
00052         if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
00053             //error("RTC error: LSI clock initialization failed.");
00054         }
00055         // Connect LSI to RTC
00056         __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI);
00057         __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
00058         // [TODO] This value is LSI typical value. To be measured precisely using a timer input capture
00059         rtc_freq = 32000;
00060     }
00061 
00062     // Enable RTC
00063     __HAL_RCC_RTC_ENABLE();
00064 
00065     RtcHandle.Init.HourFormat     = RTC_HOURFORMAT_24;
00066     RtcHandle.Init.AsynchPrediv   = 127;
00067     RtcHandle.Init.SynchPrediv    = (rtc_freq / 128) - 1;
00068     RtcHandle.Init.OutPut         = RTC_OUTPUT_DISABLE;
00069     RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
00070     RtcHandle.Init.OutPutType     = RTC_OUTPUT_TYPE_OPENDRAIN;
00071 
00072     if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
00073         //error("RTC error: RTC initialization failed.");
00074     }
00075 
00076     timeout = seconds;
00077     HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, timeout, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
00078 
00079     return count;
00080 }
00081 
00082 void wakeup_clear(void)
00083 {
00084     __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&RtcHandle, RTC_FLAG_WUTF);
00085 }
00086 
00087 uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR)
00088 {
00089     __IO uint32_t tmp = 0;
00090 
00091     // Check the parameters
00092     assert_param(IS_RTC_BKP(RTC_BKP_DR));
00093     tmp = RTC_BASE + 0x50;
00094     tmp += (RTC_BKP_DR * 4);
00095     // Read the specified register
00096     return (*(__IO uint32_t *)tmp);
00097 }
00098 
00099 void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data)
00100 {
00101     __IO uint32_t tmp = 0;
00102 
00103     // Check the parameters/
00104     assert_param(IS_RTC_BKP(RTC_BKP_DR));
00105     tmp = RTC_BASE + 0x50;
00106     tmp += (RTC_BKP_DR * 4);
00107     // Write the specified register/
00108     *(__IO uint32_t *)tmp = (uint32_t)Data;
00109 }
00110 
00111 
00112 uint8_t TM_WATCHDOG_Init(uint16_t reload) {
00113     uint8_t result = 0;
00114 
00115     /* Check if the system has resumed from IWDG reset */
00116     if (RCC->CSR & (0x20000000)) {
00117         /* Reset by IWDG */
00118         result = 1;
00119 
00120         /* Clear reset flags */
00121         RCC->CSR |= RCC_CSR_RMVF;
00122     }
00123 
00124     /* Enable write access to IWDG_PR and IWDG_RLR registers */
00125     IWDG->KR = 0x5555;
00126 
00127     /* IWDG counter clock: LSI/32 = 1024Hz */
00128     IWDG->PR = 0x03;
00129 
00130     reload = 4095;
00131 
00132     printf("WATCHDOG RELOAD: %lu\r\n", reload);
00133     /* Set reload */
00134     IWDG->RLR = reload;
00135 
00136     /* Reload IWDG counter */
00137     IWDG->KR = 0xAAAA;
00138 
00139     /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
00140     IWDG->KR = 0xCCCC;
00141 
00142     /* Return status */
00143     return result;
00144 }
00145 
00146 void TM_WATCHDOG_Disable(void) {
00147     IWDG->KR = 0x0000;
00148 }
00149 
00150 void TM_WATCHDOG_Reset(void) {
00151     /* Reload IWDG counter */
00152     IWDG->KR = 0xAAAA;
00153 }