1
0

board.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, 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. * 2009-01-05 Bernard first implementation
  23. * 2017-08-25 LongfeiMa transplantation for stm32h7xx
  24. */
  25. #include <rtthread.h>
  26. #include "board.h"
  27. #include "sram.h"
  28. #include "drv_mpu.h"
  29. #include "drv_led.h"
  30. #include "drv_usart.h"
  31. /**
  32. * @addtogroup STM32
  33. */
  34. /**
  35. * @brief System Clock Configuration
  36. * The system Clock is configured as follow :
  37. * System Clock source = PLL (HSE BYPASS)
  38. * SYSCLK(Hz) = 400000000 (CPU Clock)
  39. * HCLK(Hz) = 200000000 (AXI and AHBs Clock)
  40. * AHB Prescaler = 2
  41. * D1 APB3 Prescaler = 2 (APB3 Clock 100MHz)
  42. * D2 APB1 Prescaler = 2 (APB1 Clock 100MHz)
  43. * D2 APB2 Prescaler = 2 (APB2 Clock 100MHz)
  44. * D3 APB4 Prescaler = 2 (APB4 Clock 100MHz)
  45. * HSE Frequency(Hz) = 8000000
  46. * PLL_M = 4
  47. * PLL_N = 400
  48. * PLL_P = 2
  49. * PLL_Q = 4
  50. * PLL_R = 2
  51. * VDD(V) = 3.3
  52. * Flash Latency(WS) = 4
  53. * @param None
  54. * @retval None
  55. */
  56. static void SystemClock_Config(void)
  57. {
  58. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  59. RCC_OscInitTypeDef RCC_OscInitStruct;
  60. HAL_StatusTypeDef ret = HAL_OK;
  61. /*!< Supply configuration update enable */
  62. MODIFY_REG(PWR->CR3, PWR_CR3_SCUEN, 0);
  63. /* The voltage scaling allows optimizing the power consumption when the device is
  64. clocked below the maximum system frequency, to update the voltage scaling value
  65. regarding system frequency refer to product datasheet. */
  66. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  67. while ((PWR->D3CR & (PWR_D3CR_VOSRDY)) != PWR_D3CR_VOSRDY) {}
  68. /* Enable D2 domain SRAM3 Clock (0x30040000 AXI)*/
  69. __HAL_RCC_D2SRAM3_CLK_ENABLE();
  70. /* Enable HSE Oscillator and activate PLL with HSE as source */
  71. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  72. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  73. RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
  74. RCC_OscInitStruct.CSIState = RCC_CSI_OFF;
  75. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  76. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  77. RCC_OscInitStruct.PLL.PLLM = 4;
  78. RCC_OscInitStruct.PLL.PLLN = 400;
  79. RCC_OscInitStruct.PLL.PLLP = 2;
  80. RCC_OscInitStruct.PLL.PLLR = 2;
  81. RCC_OscInitStruct.PLL.PLLQ = 4;
  82. RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  83. RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
  84. ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  85. if(ret != HAL_OK)
  86. {
  87. while (1) { ; }
  88. }
  89. /* Select PLL as system clock source and configure bus clocks dividers */
  90. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 | \
  91. RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1);
  92. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  93. RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  94. RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  95. RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  96. RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  97. RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  98. RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  99. ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4);
  100. if(ret != HAL_OK)
  101. {
  102. while (1) { ; }
  103. }
  104. /*activate CSI clock mondatory for I/O Compensation Cell*/
  105. __HAL_RCC_CSI_ENABLE() ;
  106. /* Enable SYSCFG clock mondatory for I/O Compensation Cell */
  107. __HAL_RCC_SYSCFG_CLK_ENABLE() ;
  108. /* Enables the I/O Compensation Cell */
  109. HAL_EnableCompensationCell();
  110. }
  111. /**
  112. * @brief CPU L1-Cache enable.
  113. * @param None
  114. * @retval None
  115. */
  116. static void CPU_CACHE_Enable(void)
  117. {
  118. // /* Enable branch prediction */
  119. // SCB->CCR |= (1 << 18);
  120. // __DSB();
  121. /* Enable I-Cache */
  122. SCB_EnableICache();
  123. /* Enable D-Cache */
  124. SCB_EnableDCache();
  125. }
  126. /**
  127. * This is the timer interrupt service routine.
  128. *
  129. */
  130. void SysTick_Handler(void)
  131. {
  132. /* enter interrupt */
  133. rt_interrupt_enter();
  134. /* tick for HAL Library */
  135. HAL_IncTick();
  136. rt_tick_increase();
  137. /* leave interrupt */
  138. rt_interrupt_leave();
  139. }
  140. /* re-implementat tick interface for STM32 HAL */
  141. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  142. {
  143. /*Configure the SysTick to have interrupt in 1ms time basis*/
  144. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/RT_TICK_PER_SECOND);
  145. /*Configure the SysTick IRQ priority */
  146. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0);
  147. /* Return function status */
  148. return HAL_OK;
  149. }
  150. void HAL_Delay(__IO uint32_t Delay)
  151. {
  152. rt_thread_delay(Delay);
  153. }
  154. void HAL_SuspendTick(void)
  155. {
  156. /* we should not suspend tick */
  157. }
  158. void HAL_ResumeTick(void)
  159. {
  160. /* we should not resume tick */
  161. }
  162. /**
  163. * This function will initial STM32 board.
  164. */
  165. void rt_hw_board_init()
  166. {
  167. /* Configure the MPU attributes as Write Through */
  168. mpu_init();
  169. /* Enable the CPU Cache */
  170. CPU_CACHE_Enable();
  171. /* STM32F7xx HAL library initialization:
  172. - Configure the Flash ART accelerator on ITCM interface
  173. - Configure the Systick to generate an interrupt each 1 msec
  174. - Set NVIC Group Priority to 4
  175. - Global MSP (MCU Support Package) initialization
  176. */
  177. HAL_Init();
  178. /* Configure the system clock @ 200 Mhz */
  179. SystemClock_Config();
  180. /* init systick */
  181. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  182. /* set pend exception priority */
  183. NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
  184. #ifdef RT_USING_COMPONENTS_INIT
  185. rt_components_board_init();
  186. #endif
  187. #ifdef RT_USING_EXT_SDRAM
  188. rt_system_heap_init((void*)EXT_SDRAM_BEGIN, (void*)EXT_SDRAM_END);
  189. sram_init();
  190. #else
  191. rt_system_heap_init((void*)HEAP_BEGIN, (void*)HEAP_END);
  192. #endif
  193. #ifdef RT_USING_CONSOLE
  194. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  195. #endif
  196. }
  197. /*@}*/