board.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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-17 ZYH first implementation
  23. */
  24. #include <rtthread.h>
  25. #include "board.h"
  26. #include "drv_mpu.h"
  27. #include "drv_sdram.h"
  28. #include <rthw.h>
  29. /**
  30. * @addtogroup STM32
  31. */
  32. static void SystemClock_Config(void)
  33. {
  34. RCC_OscInitTypeDef RCC_OscInitStruct;
  35. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  36. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
  37. /**Configure the main internal regulator output voltage
  38. */
  39. __HAL_RCC_PWR_CLK_ENABLE();
  40. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  41. /**Initializes the CPU, AHB and APB busses clocks
  42. */
  43. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
  44. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  45. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  46. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  47. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  48. RCC_OscInitStruct.PLL.PLLM = 25;
  49. RCC_OscInitStruct.PLL.PLLN = 432;
  50. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  51. RCC_OscInitStruct.PLL.PLLQ = 9;
  52. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  53. /**Activate the Over-Drive mode
  54. */
  55. HAL_PWREx_EnableOverDrive();
  56. /**Initializes the CPU, AHB and APB busses clocks
  57. */
  58. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  59. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  60. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  61. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  62. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  63. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  64. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7);
  65. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART1
  66. |RCC_PERIPHCLK_USART6|RCC_PERIPHCLK_UART4
  67. |RCC_PERIPHCLK_UART5|RCC_PERIPHCLK_UART7
  68. |RCC_PERIPHCLK_SDMMC2|RCC_PERIPHCLK_CLK48;
  69. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  70. PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  71. PeriphClkInitStruct.Uart4ClockSelection = RCC_UART4CLKSOURCE_PCLK1;
  72. PeriphClkInitStruct.Uart5ClockSelection = RCC_UART5CLKSOURCE_PCLK1;
  73. PeriphClkInitStruct.Usart6ClockSelection = RCC_USART6CLKSOURCE_PCLK2;
  74. PeriphClkInitStruct.Uart7ClockSelection = RCC_UART7CLKSOURCE_PCLK1;
  75. PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48SOURCE_PLL;
  76. PeriphClkInitStruct.Sdmmc2ClockSelection = RCC_SDMMC2CLKSOURCE_CLK48;
  77. HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
  78. }
  79. void SysTick_Handler(void)
  80. {
  81. /* enter interrupt */
  82. rt_interrupt_enter();
  83. rt_tick_increase();
  84. /* leave interrupt */
  85. rt_interrupt_leave();
  86. }
  87. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  88. {
  89. /**Configure the Systick interrupt time
  90. */
  91. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
  92. /**Configure the Systick
  93. */
  94. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  95. /* SysTick_IRQn interrupt configuration */
  96. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  97. return HAL_OK;
  98. }
  99. uint32_t HAL_GetTick(void)
  100. {
  101. return rt_tick_get() * 1000 / RT_TICK_PER_SECOND;
  102. }
  103. void HAL_Delay(__IO uint32_t Delay)
  104. {
  105. rt_thread_delay(Delay * 1000 / RT_TICK_PER_SECOND);
  106. }
  107. void HAL_SuspendTick(void)
  108. {
  109. /* we should not suspend tick */
  110. }
  111. void HAL_ResumeTick(void)
  112. {
  113. /* we should not resume tick */
  114. }
  115. #if defined(BSP_USING_SDRAM) && defined(RT_USING_MEMHEAP_AS_HEAP)
  116. static struct rt_memheap system_heap;
  117. #endif
  118. void HAL_MspInit(void)
  119. {
  120. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  121. /* System interrupt init*/
  122. /* MemoryManagement_IRQn interrupt configuration */
  123. HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0);
  124. /* BusFault_IRQn interrupt configuration */
  125. HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0);
  126. /* UsageFault_IRQn interrupt configuration */
  127. HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0);
  128. /* SVCall_IRQn interrupt configuration */
  129. HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0);
  130. /* DebugMonitor_IRQn interrupt configuration */
  131. HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0);
  132. /* PendSV_IRQn interrupt configuration */
  133. HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
  134. /* SysTick_IRQn interrupt configuration */
  135. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  136. }
  137. /**
  138. * This function will initial STM32 board.
  139. */
  140. void rt_hw_board_init()
  141. {
  142. /* Configure the MPU attributes as Write Through */
  143. bsp_mpu_hw_init();
  144. /* Enable I-Cache-------------------------------------------------------------*/
  145. rt_hw_cpu_icache_enable();
  146. /* Enable D-Cache-------------------------------------------------------------*/
  147. rt_hw_cpu_dcache_enable();
  148. /* STM32F7xx HAL library initialization:
  149. - Configure the Flash ART accelerator on ITCM interface
  150. - Configure the Systick to generate an interrupt each 1 msec
  151. - Set NVIC Group Priority to 4
  152. - Global MSP (MCU Support Package) initialization
  153. */
  154. /* Configure the system clock @ 216 Mhz */
  155. SystemClock_Config();
  156. HAL_Init();
  157. #ifdef RT_USING_HEAP
  158. #if defined(BSP_USING_SDRAM) && defined(RT_USING_MEMHEAP_AS_HEAP)
  159. bsp_sdram_hw_init();
  160. rt_system_heap_init((void *)SDRAM_BEGIN, (void *)SDRAM_END);
  161. rt_memheap_init(&system_heap, "sram", (void *)HEAP_BEGIN, HEAP_SIZE);
  162. #else
  163. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  164. #endif
  165. #endif
  166. #ifdef RT_USING_COMPONENTS_INIT
  167. rt_components_board_init();
  168. #endif
  169. #ifdef RT_USING_CONSOLE
  170. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  171. #endif
  172. }