/STM32F0xx_StdPeriph_Lib_V1.0.0/Project/STM32F0xx_StdPeriph_Examples/RTC/RTC_LSI/main.c

https://github.com/rurume/stm32-test · C · 276 lines · 107 code · 50 blank · 119 comment · 8 complexity · 0827150f376ad6de339dd7171f37c54d MD5 · raw file

  1. /**
  2. ******************************************************************************
  3. * @file RTC/RTC_LSI/main.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 18-May-2012
  7. * @brief Main program body
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "main.h"
  29. /** @addtogroup STM32F0xx_StdPeriph_Examples
  30. * @{
  31. */
  32. /** @addtogroup RTC_LSI
  33. * @{
  34. */
  35. /* Private typedef -----------------------------------------------------------*/
  36. /* Private define ------------------------------------------------------------*/
  37. /* Private macro -------------------------------------------------------------*/
  38. /* Private variables ---------------------------------------------------------*/
  39. RTC_InitTypeDef RTC_InitStructure;
  40. __IO uint32_t LsiFreq = 0;
  41. __IO uint32_t CaptureNumber = 0, PeriodValue = 0;
  42. /* Private function prototypes -----------------------------------------------*/
  43. void RTC_Config(void);
  44. uint32_t GetLSIFrequency(void);
  45. /* Private functions ---------------------------------------------------------*/
  46. /**
  47. * @brief Main program.
  48. * @param None
  49. * @retval None
  50. */
  51. int main(void)
  52. {
  53. /*!< At this stage the microcontroller clock setting is already configured,
  54. this is done through SystemInit() function which is called from startup
  55. file (startup_stm32f0xx.s) before to branch to application main.
  56. To reconfigure the default setting of SystemInit() function, refer to
  57. system_stm32f0xx.c file
  58. */
  59. /* Initialize LEDs mounted on STM320518-EVAL board --------------------------*/
  60. STM_EVAL_LEDInit(LED1);
  61. STM_EVAL_LEDInit(LED2);
  62. STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO);
  63. /* RTC Configuration -------------------------------------------------------*/
  64. RTC_Config();
  65. /* Wait Until KEY BUTTON is pressed */
  66. while(STM_EVAL_PBGetState(BUTTON_KEY) != RESET)
  67. {
  68. }
  69. /* Get the LSI frequency: TIM14 is used to measure the LSI frequency */
  70. LsiFreq = GetLSIFrequency();
  71. /* Turn on LED2 */
  72. STM_EVAL_LEDOn(LED2);
  73. /* Calendar Configuration */
  74. RTC_InitStructure.RTC_AsynchPrediv = 99;
  75. RTC_InitStructure.RTC_SynchPrediv = (LsiFreq/100) - 1;
  76. RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
  77. RTC_Init(&RTC_InitStructure);
  78. /* Infinite loop */
  79. while (1)
  80. {
  81. }
  82. }
  83. /**
  84. * @brief Configure the RTC peripheral by selecting the clock source.
  85. * @param None
  86. * @retval None
  87. */
  88. void RTC_Config(void)
  89. {
  90. RTC_TimeTypeDef RTC_TimeStructure;
  91. NVIC_InitTypeDef NVIC_InitStructure;
  92. EXTI_InitTypeDef EXTI_InitStructure;
  93. RTC_AlarmTypeDef RTC_AlarmStructure;
  94. /* Enable the PWR clock */
  95. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  96. /* Allow access to RTC */
  97. PWR_BackupAccessCmd(ENABLE);
  98. /* LSI used as RTC source clock */
  99. /* The RTC Clock may varies due to LSI frequency dispersion. */
  100. /* Enable the LSI OSC */
  101. RCC_LSICmd(ENABLE);
  102. /* Wait till LSI is ready */
  103. while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
  104. {
  105. }
  106. /* Select the RTC Clock Source */
  107. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
  108. /* Enable the RTC Clock */
  109. RCC_RTCCLKCmd(ENABLE);
  110. /* Wait for RTC APB registers synchronisation */
  111. RTC_WaitForSynchro();
  112. /* Calendar Configuration */
  113. RTC_InitStructure.RTC_AsynchPrediv = 99;
  114. RTC_InitStructure.RTC_SynchPrediv = 399; /* (40KHz / 100) - 1 = 399*/
  115. RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
  116. RTC_Init(&RTC_InitStructure);
  117. /* EXTI configuration *******************************************************/
  118. EXTI_ClearITPendingBit(EXTI_Line17);
  119. EXTI_InitStructure.EXTI_Line = EXTI_Line17;
  120. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  121. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  122. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  123. EXTI_Init(&EXTI_InitStructure);
  124. /* Enable the RTC Wakeup Interrupt */
  125. NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
  126. NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  127. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  128. NVIC_Init(&NVIC_InitStructure);
  129. /* Set the alarm X+5s */
  130. RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
  131. RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = 0x00;
  132. RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = 0x00;
  133. RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = 0x01;
  134. RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;
  135. RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
  136. RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;
  137. RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure);
  138. RTC_ITConfig(RTC_IT_ALRA, ENABLE);
  139. /* Enable the alarm */
  140. RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
  141. /* Set the time to 00h 00mn 00s AM */
  142. RTC_TimeStructure.RTC_H12 = RTC_H12_AM;
  143. RTC_TimeStructure.RTC_Hours = 0x00;
  144. RTC_TimeStructure.RTC_Minutes = 0x00;
  145. RTC_TimeStructure.RTC_Seconds = 0x00;
  146. RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
  147. }
  148. /**
  149. * @brief Configures TIM14 to measure the LSI oscillator frequency.
  150. * @param None
  151. * @retval LSI Frequency
  152. */
  153. uint32_t GetLSIFrequency(void)
  154. {
  155. NVIC_InitTypeDef NVIC_InitStructure;
  156. TIM_ICInitTypeDef TIM_ICInitStructure;
  157. RCC_ClocksTypeDef RCC_ClockFreq;
  158. /* TIM14 configuration *******************************************************/
  159. /* Enable TIM14 clock */
  160. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
  161. /* Reset TIM14 registers */
  162. TIM_DeInit(TIM14);
  163. /* Configure TIM14 prescaler */
  164. TIM_PrescalerConfig(TIM14, 0, TIM_PSCReloadMode_Immediate);
  165. /* Connect internally the TIM14_CH1 to the RTC clock output */
  166. TIM_RemapConfig(TIM14, TIM14_RTC_CLK);
  167. /* TIM14 configuration: Input Capture mode ---------------------
  168. The reference clock(LSE or external) is connected to TIM14 CH1
  169. The Rising edge is used as active edge,
  170. The TIM14 CCR1 is used to compute the frequency value
  171. ------------------------------------------------------------ */
  172. TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
  173. TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  174. TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  175. TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8;
  176. TIM_ICInitStructure.TIM_ICFilter = 0x0;
  177. TIM_ICInit(TIM14, &TIM_ICInitStructure);
  178. /* Enable the TIM14 global Interrupt */
  179. NVIC_InitStructure.NVIC_IRQChannel = TIM14_IRQn;
  180. NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  181. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  182. NVIC_Init(&NVIC_InitStructure);
  183. /* Enable TIM14 counter */
  184. TIM_Cmd(TIM14, ENABLE);
  185. /* Reset the flags */
  186. TIM14->SR = 0;
  187. /* Enable the CC1 Interrupt Request */
  188. TIM_ITConfig(TIM14, TIM_IT_CC1, ENABLE);
  189. /* Wait until the TIM14 get 2 LSI edges (refer to TIM14_IRQHandler() in
  190. stm32F0xx_it.c file) ******************************************************/
  191. while(CaptureNumber != 2)
  192. {
  193. }
  194. /* Deinitialize the TIM14 peripheral registers to their default reset values */
  195. TIM_DeInit(TIM14);
  196. /* Compute the LSI frequency, depending on TIM14 input clock frequency (PCLK1)*/
  197. /* Get SYSCLK, HCLK and PCLKx frequency */
  198. RCC_GetClocksFreq(&RCC_ClockFreq);
  199. /* PCLK1 prescaler equal to 1 => TIMCLK = PCLK1 */
  200. return ((RCC_ClockFreq.PCLK_Frequency / PeriodValue) * 8);
  201. }
  202. #ifdef USE_FULL_ASSERT
  203. /**
  204. * @brief Reports the name of the source file and the source line number
  205. * where the assert_param error has occurred.
  206. * @param file: pointer to the source file name
  207. * @param line: assert_param error line source number
  208. * @retval None
  209. */
  210. void assert_failed(uint8_t* file, uint32_t line)
  211. {
  212. /* User can add his own implementation to report the file name and line number,
  213. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  214. /* Infinite loop */
  215. while (1)
  216. {
  217. }
  218. }
  219. #endif
  220. /**
  221. * @}
  222. */
  223. /**
  224. * @}
  225. */
  226. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/