board.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-05-14 ZYH first implementation
  23. */
  24. #include <stdint.h>
  25. #include <rthw.h>
  26. #include <rtthread.h>
  27. #include "board.h"
  28. #ifdef BSP_USING_HSI
  29. #error Can not using HSI on this bsp
  30. #endif
  31. static void SystemClock_Config(void)
  32. {
  33. RCC_OscInitTypeDef RCC_OscInitStruct;
  34. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  35. RCC_PeriphCLKInitTypeDef PeriphClkInit;
  36. /**Initializes the CPU, AHB and APB busses clocks
  37. */
  38. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_MSI;
  39. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  40. RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  41. RCC_OscInitStruct.MSICalibrationValue = 0;
  42. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  43. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  44. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  45. RCC_OscInitStruct.PLL.PLLM = 1;
  46. RCC_OscInitStruct.PLL.PLLN = 40;
  47. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  48. RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  49. RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  50. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  51. /**Initializes the CPU, AHB and APB busses clocks
  52. */
  53. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  54. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  55. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  56. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  57. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  58. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  59. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4);
  60. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2
  61. |RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_UART4
  62. |RCC_PERIPHCLK_UART5|RCC_PERIPHCLK_LPUART1
  63. |RCC_PERIPHCLK_USB;
  64. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  65. PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  66. PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
  67. PeriphClkInit.Uart4ClockSelection = RCC_UART4CLKSOURCE_PCLK1;
  68. PeriphClkInit.Uart5ClockSelection = RCC_UART5CLKSOURCE_PCLK1;
  69. PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
  70. PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1;
  71. PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_MSI;
  72. PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
  73. PeriphClkInit.PLLSAI1.PLLSAI1N = 24;
  74. PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV7;
  75. PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
  76. PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
  77. PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_48M2CLK;
  78. HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
  79. /**Configure the main internal regulator output voltage
  80. */
  81. HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  82. }
  83. void SysTick_Handler(void)
  84. {
  85. /* enter interrupt */
  86. rt_interrupt_enter();
  87. rt_tick_increase();
  88. /* leave interrupt */
  89. rt_interrupt_leave();
  90. }
  91. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  92. {
  93. /**Configure the Systick interrupt time
  94. */
  95. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
  96. /**Configure the Systick
  97. */
  98. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  99. /* SysTick_IRQn interrupt configuration */
  100. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  101. return HAL_OK;
  102. }
  103. uint32_t HAL_GetTick(void)
  104. {
  105. return rt_tick_get() * 1000 / RT_TICK_PER_SECOND;
  106. }
  107. void HAL_Delay(__IO uint32_t Delay)
  108. {
  109. rt_thread_delay(Delay * 1000 / RT_TICK_PER_SECOND);
  110. }
  111. void HAL_SuspendTick(void)
  112. {
  113. /* we should not suspend tick */
  114. }
  115. void HAL_ResumeTick(void)
  116. {
  117. /* we should not resume tick */
  118. }
  119. void HAL_MspInit(void)
  120. {
  121. __HAL_RCC_SYSCFG_CLK_ENABLE();
  122. __HAL_RCC_PWR_CLK_ENABLE();
  123. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  124. /* System interrupt init*/
  125. /* MemoryManagement_IRQn interrupt configuration */
  126. HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0);
  127. /* BusFault_IRQn interrupt configuration */
  128. HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0);
  129. /* UsageFault_IRQn interrupt configuration */
  130. HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0);
  131. /* SVCall_IRQn interrupt configuration */
  132. HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0);
  133. /* DebugMonitor_IRQn interrupt configuration */
  134. HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0);
  135. /* PendSV_IRQn interrupt configuration */
  136. HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
  137. }
  138. /**
  139. * This function will initial STM32 board.
  140. */
  141. void rt_hw_board_init(void)
  142. {
  143. /* Configure the system clock @ 80 Mhz */
  144. SystemClock_Config();
  145. HAL_Init();
  146. #ifdef RT_USING_HEAP
  147. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  148. #endif
  149. #ifdef RT_USING_COMPONENTS_INIT
  150. rt_components_board_init();
  151. #endif
  152. #ifdef RT_USING_CONSOLE
  153. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  154. #endif
  155. }
  156. /*@}*/