board.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-09-22 Bernard add board.h to this bsp
  13. */
  14. #include <rtthread.h>
  15. #include "board.h"
  16. /**
  17. * @addtogroup STM32
  18. */
  19. /*@{*/
  20. static void SystemClock_Config(void)
  21. {
  22. RCC_OscInitTypeDef RCC_OscInitStruct;
  23. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  24. #ifdef RT_USING_RTC
  25. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
  26. #endif
  27. /**Configure the main internal regulator output voltage
  28. */
  29. __HAL_RCC_PWR_CLK_ENABLE();
  30. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  31. /**Initializes the CPU, AHB and APB busses clocks
  32. */
  33. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  34. #ifdef RT_USING_RTC
  35. RCC_OscInitStruct.OscillatorType |= RCC_OSCILLATORTYPE_LSI;
  36. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  37. #endif
  38. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  39. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  40. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  41. RCC_OscInitStruct.PLL.PLLM = (HSE_VALUE/1000000UL);//Get 1M clock
  42. RCC_OscInitStruct.PLL.PLLN = (HCLK_VALUE/1000000UL)*2;//Get 2*HCLK_VALUE
  43. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;//Get HCLK_VALUE
  44. RCC_OscInitStruct.PLL.PLLQ = RCC_OscInitStruct.PLL.PLLN/48;//Get 48M Clock
  45. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  46. {
  47. while(1)
  48. {}
  49. }
  50. /**Initializes the CPU, AHB and APB busses clocks
  51. */
  52. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  53. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  54. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  55. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  56. #if (RT_HSE_HCLK <= 42000000UL)
  57. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  58. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  59. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  60. {
  61. while(1)
  62. {}
  63. }
  64. #elif (RT_HSE_HCLK <= 84000000UL)
  65. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  66. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  67. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  68. {
  69. while(1)
  70. {}
  71. }
  72. #elif (RT_HSE_HCLK <= 168000000UL)
  73. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  74. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  75. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  76. {
  77. while(1)
  78. {}
  79. }
  80. #else
  81. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV8;
  82. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
  83. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
  84. {
  85. while(1)
  86. {}
  87. }
  88. #endif
  89. #ifdef RT_USING_RTC
  90. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  91. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  92. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  93. {
  94. while(1)
  95. {}
  96. }
  97. #endif
  98. }
  99. /**
  100. * This is the timer interrupt service routine.
  101. *
  102. */
  103. void SysTick_Handler(void)
  104. {
  105. /* enter interrupt */
  106. rt_interrupt_enter();
  107. /* tick for HAL Library */
  108. HAL_IncTick();
  109. rt_tick_increase();
  110. /* leave interrupt */
  111. rt_interrupt_leave();
  112. }
  113. /* re-implementat tick interface for STM32 HAL */
  114. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  115. {
  116. /*Configure the SysTick to have interrupt in 1ms time basis*/
  117. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
  118. /*Configure the SysTick IRQ priority */
  119. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority , 0);
  120. /* Return function status */
  121. return HAL_OK;
  122. }
  123. void HAL_Delay(__IO uint32_t Delay)
  124. {
  125. rt_thread_delay(Delay);
  126. }
  127. void HAL_SuspendTick(void)
  128. {
  129. /* we should not suspend tick */
  130. }
  131. void HAL_ResumeTick(void)
  132. {
  133. /* we should not resume tick */
  134. }
  135. void HAL_MspInit(void)
  136. {
  137. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  138. /* System interrupt init*/
  139. /* MemoryManagement_IRQn interrupt configuration */
  140. HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0);
  141. /* BusFault_IRQn interrupt configuration */
  142. HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0);
  143. /* UsageFault_IRQn interrupt configuration */
  144. HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0);
  145. /* SVCall_IRQn interrupt configuration */
  146. HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0);
  147. /* DebugMonitor_IRQn interrupt configuration */
  148. HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0);
  149. /* PendSV_IRQn interrupt configuration */
  150. HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
  151. /* SysTick_IRQn interrupt configuration */
  152. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  153. }
  154. /**
  155. * This function will initial STM32 board.
  156. */
  157. void rt_hw_board_init()
  158. {
  159. /* Configure the system clock @ 84 Mhz */
  160. SystemClock_Config();
  161. HAL_Init();
  162. #ifdef RT_USING_HEAP
  163. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  164. #endif
  165. #ifdef RT_USING_COMPONENTS_INIT
  166. rt_components_board_init();
  167. #endif
  168. #ifdef RT_USING_CONSOLE
  169. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  170. #endif
  171. }
  172. /*@}*/