PageRenderTime 28ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/FWLib/examples/IWDG/main.c

https://github.com/zaurus04/cortexm3
C | 281 lines | 111 code | 47 blank | 123 comment | 10 complexity | 08d685fc601536ee7b73cf56ad0031a8 MD5 | raw file
  1. /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
  2. * File Name : main.c
  3. * Author : MCD Application Team
  4. * Version : V2.0.3
  5. * Date : 09/22/2008
  6. * Description : Main program body.
  7. ********************************************************************************
  8. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  9. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  10. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  11. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  12. * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  13. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  14. *******************************************************************************/
  15. /* Includes ------------------------------------------------------------------*/
  16. #include "stm32f10x_lib.h"
  17. #include "platform_config.h"
  18. /* Private typedef -----------------------------------------------------------*/
  19. /* Private define ------------------------------------------------------------*/
  20. /* Private macro -------------------------------------------------------------*/
  21. /* Private variables ---------------------------------------------------------*/
  22. ErrorStatus HSEStartUpStatus;
  23. /* Private function prototypes -----------------------------------------------*/
  24. void RCC_Configuration(void);
  25. void NVIC_Configuration(void);
  26. void GPIO_Configuration(void);
  27. void EXTI_Configuration(void);
  28. void SysTick_Configuration(void);
  29. void Delay(vu32 nCount);
  30. /* Private functions ---------------------------------------------------------*/
  31. /*******************************************************************************
  32. * Function Name : main
  33. * Description : Main program.
  34. * Input : None
  35. * Output : None
  36. * Return : None
  37. *******************************************************************************/
  38. int main(void)
  39. {
  40. #ifdef DEBUG
  41. debug();
  42. #endif
  43. /* System Clocks Configuration */
  44. RCC_Configuration();
  45. /* GPIO configuration */
  46. GPIO_Configuration();
  47. /* Check if the system has resumed from IWDG reset */
  48. if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET)
  49. {/* IWDGRST flag set */
  50. /* Set GPIO_LED pin 6 */
  51. GPIO_SetBits(GPIO_LED, GPIO_Pin_6);
  52. /* Clear reset flags */
  53. RCC_ClearFlag();
  54. }
  55. else
  56. {/* IWDGRST flag is not set */
  57. /* Reset GPIO_LED pin 6 */
  58. GPIO_ResetBits(GPIO_LED, GPIO_Pin_6);
  59. }
  60. /* Configure Key Button EXTI Line to generate an interrupt on falling edge */
  61. EXTI_Configuration();
  62. /* NVIC configuration */
  63. NVIC_Configuration();
  64. /* Configure SysTick to generate an interrupt each 250ms */
  65. SysTick_Configuration();
  66. /* IWDG timeout equal to 280 ms (the timeout may varies due to LSI frequency
  67. dispersion) */
  68. /* Enable write access to IWDG_PR and IWDG_RLR registers */
  69. IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
  70. /* IWDG counter clock: 40KHz(LSI) / 32 = 1.25 KHz */
  71. IWDG_SetPrescaler(IWDG_Prescaler_32);
  72. /* Set counter reload value to 349 */
  73. IWDG_SetReload(349);
  74. /* Reload IWDG counter */
  75. IWDG_ReloadCounter();
  76. /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
  77. IWDG_Enable();
  78. while (1)
  79. {}
  80. }
  81. /*******************************************************************************
  82. * Function Name : RCC_Configuration
  83. * Description : Configures the different system clocks.
  84. * Input : None
  85. * Output : None
  86. * Return : None
  87. *******************************************************************************/
  88. void RCC_Configuration(void)
  89. {
  90. /* RCC system reset(for debug purpose) */
  91. RCC_DeInit();
  92. /* Enable HSE */
  93. RCC_HSEConfig(RCC_HSE_ON);
  94. /* Wait till HSE is ready */
  95. HSEStartUpStatus = RCC_WaitForHSEStartUp();
  96. if (HSEStartUpStatus == SUCCESS)
  97. {
  98. /* Enable Prefetch Buffer */
  99. FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  100. /* Flash 0 wait state */
  101. FLASH_SetLatency(FLASH_Latency_0);
  102. /* HCLK = SYSCLK */
  103. RCC_HCLKConfig(RCC_SYSCLK_Div1);
  104. /* PCLK2 = HCLK */
  105. RCC_PCLK2Config(RCC_HCLK_Div1);
  106. /* PCLK1 = HCLK */
  107. RCC_PCLK1Config(RCC_HCLK_Div1);
  108. /* Select HSE as system clock source */
  109. RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
  110. /* Wait till HSE is used as system clock source */
  111. while (RCC_GetSYSCLKSource() != 0x04)
  112. {}
  113. }
  114. /* Enable Key Button GPIO Port, GPIO_LED and AFIO clock */
  115. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_KEY_BUTTON | RCC_APB2Periph_GPIO_LED
  116. | RCC_APB2Periph_AFIO, ENABLE);
  117. }
  118. /*******************************************************************************
  119. * Function Name : GPIO_Configuration
  120. * Description : Configures the different GPIO ports.
  121. * Input : None
  122. * Output : None
  123. * Return : None
  124. *******************************************************************************/
  125. void GPIO_Configuration(void)
  126. {
  127. GPIO_InitTypeDef GPIO_InitStructure;
  128. /* Configure GPIO_LED pin 6 and pin 7 as Output push-pull */
  129. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  130. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  131. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  132. GPIO_Init(GPIO_LED, &GPIO_InitStructure);
  133. /* Configure Key Button GPIO Pin as input floating (Key Button EXTI Line) */
  134. GPIO_InitStructure.GPIO_Pin = GPIO_PIN_KEY_BUTTON;
  135. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  136. GPIO_Init(GPIO_KEY_BUTTON, &GPIO_InitStructure);
  137. }
  138. /*******************************************************************************
  139. * Function Name : EXTI_Configuration
  140. * Description : Configures EXTI Line9.
  141. * Input : None
  142. * Output : None
  143. * Return : None
  144. *******************************************************************************/
  145. void EXTI_Configuration(void)
  146. {
  147. EXTI_InitTypeDef EXTI_InitStructure;
  148. /* Connect Key Button EXTI Line to Key Button GPIO Pin */
  149. GPIO_EXTILineConfig(GPIO_PORT_SOURCE_KEY_BUTTON, GPIO_PIN_SOURCE_KEY_BUTTON);
  150. /* Configure Key Button EXTI Line to generate an interrupt on falling edge */
  151. EXTI_ClearITPendingBit(EXTI_LINE_KEY_BUTTON);
  152. EXTI_InitStructure.EXTI_Line = EXTI_LINE_KEY_BUTTON;
  153. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  154. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  155. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  156. EXTI_Init(&EXTI_InitStructure);
  157. }
  158. /*******************************************************************************
  159. * Function Name : NVIC_Configuration
  160. * Description : Configure the nested vectored interrupt controller.
  161. * Input : None
  162. * Output : None
  163. * Return : None
  164. *******************************************************************************/
  165. void NVIC_Configuration(void)
  166. {
  167. NVIC_InitTypeDef NVIC_InitStructure;
  168. #ifdef VECT_TAB_RAM
  169. /* Set the Vector Table base location at 0x20000000 */
  170. NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  171. #else /* VECT_TAB_FLASH */
  172. /* Set the Vector Table base location at 0x08000000 */
  173. NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  174. #endif
  175. /* Configure one bit for preemption priority */
  176. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  177. /* Enable the EXTI9_5 Interrupt */
  178. NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
  179. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  180. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  181. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  182. NVIC_Init(&NVIC_InitStructure);
  183. /* Set SysTick interrupt vector Preemption Priority to 1 */
  184. NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 1, 0);
  185. }
  186. /*******************************************************************************
  187. * Function Name : Delay
  188. * Description : Inserts a delay time.
  189. * Input : nCount: specifies the delay time length.
  190. * Output : None
  191. * Return : None
  192. *******************************************************************************/
  193. void Delay(vu32 nCount)
  194. {
  195. for (; nCount != 0; nCount--);
  196. }
  197. #ifdef DEBUG
  198. /*******************************************************************************
  199. * Function Name : assert_failed
  200. * Description : Reports the name of the source file and the source line number
  201. * where the assert_param error has occurred.
  202. * Input : - file: pointer to the source file name
  203. * - line: assert_param error line source number
  204. * Output : None
  205. * Return : None
  206. *******************************************************************************/
  207. void assert_failed(u8* file, u32 line)
  208. {
  209. /* User can add his own implementation to report the file name and line number,
  210. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  211. /* Infinite loop */
  212. while (1)
  213. {}
  214. }
  215. #endif
  216. /*******************************************************************************
  217. * Function Name : SysTick_Configuration
  218. * Description : Configures SysTick to generate an interrupt each 250ms.
  219. * Input : None
  220. * Output : None
  221. * Return : None
  222. *******************************************************************************/
  223. void SysTick_Configuration(void)
  224. {
  225. /* Select HCLK/8 as SysTick clock source */
  226. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
  227. /* SysTick interrupt each 250ms with counter clock equal to 1MHz */
  228. SysTick_SetReload(250000);
  229. /* Enable the SysTick Counter */
  230. SysTick_CounterCmd(SysTick_Counter_Enable);
  231. /* Enable the SysTick Interrupt */
  232. SysTick_ITConfig(ENABLE);
  233. }
  234. /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/