board.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. */
  24. #include <rtthread.h>
  25. #include "board.h"
  26. #include "sram.h"
  27. #include "drv_mpu.h"
  28. /**
  29. * @addtogroup STM32
  30. */
  31. /**
  32. * @brief System Clock Configuration
  33. * The system Clock is configured as follow :
  34. * System Clock source = PLL (HSE)
  35. * SYSCLK(Hz) = 200000000
  36. * HCLK(Hz) = 200000000
  37. * AHB Prescaler = 1
  38. * APB1 Prescaler = 4
  39. * APB2 Prescaler = 2
  40. * HSE Frequency(Hz) = 25000000
  41. * PLL_M = 25
  42. * PLL_N = 400
  43. * PLL_P = 2
  44. * PLLSAI_N = 384
  45. * PLLSAI_P = 8
  46. * VDD(V) = 3.3
  47. * Main regulator output voltage = Scale1 mode
  48. * Flash Latency(WS) = 6
  49. * @param None
  50. * @retval None
  51. */
  52. static void SystemClock_Config(void)
  53. {
  54. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  55. RCC_OscInitTypeDef RCC_OscInitStruct;
  56. HAL_StatusTypeDef ret = HAL_OK;
  57. /* Enable HSE Oscillator and activate PLL with HSE as source */
  58. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  59. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  60. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  61. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  62. RCC_OscInitStruct.PLL.PLLM = 25;
  63. RCC_OscInitStruct.PLL.PLLN = 400;
  64. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  65. RCC_OscInitStruct.PLL.PLLQ = 8;
  66. ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  67. if(ret != HAL_OK)
  68. {
  69. while (1) { ; }
  70. }
  71. ret = HAL_PWREx_EnableOverDrive();
  72. if (ret != HAL_OK)
  73. {
  74. while (1) { ; }
  75. }
  76. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  77. clocks dividers */
  78. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK |\
  79. RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  80. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  81. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  82. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  83. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  84. ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_6);
  85. if (ret != HAL_OK)
  86. {
  87. while (1) { ; }
  88. }
  89. }
  90. /**
  91. * @brief CPU L1-Cache enable.
  92. * @param None
  93. * @retval None
  94. */
  95. static void CPU_CACHE_Enable(void)
  96. {
  97. /* Enable branch prediction */
  98. SCB->CCR |= (1 << 18);
  99. __DSB();
  100. /* Enable I-Cache */
  101. SCB_EnableICache();
  102. /* Enable D-Cache */
  103. SCB_EnableDCache();
  104. }
  105. /**
  106. * This is the timer interrupt service routine.
  107. *
  108. */
  109. void SysTick_Handler(void)
  110. {
  111. /* enter interrupt */
  112. rt_interrupt_enter();
  113. /* tick for HAL Library */
  114. HAL_IncTick();
  115. rt_tick_increase();
  116. /* leave interrupt */
  117. rt_interrupt_leave();
  118. }
  119. /* re-implementat tick interface for STM32 HAL */
  120. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  121. {
  122. /*Configure the SysTick to have interrupt in 1ms time basis*/
  123. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/RT_TICK_PER_SECOND);
  124. /*Configure the SysTick IRQ priority */
  125. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0);
  126. /* Return function status */
  127. return HAL_OK;
  128. }
  129. void HAL_Delay(__IO uint32_t Delay)
  130. {
  131. rt_thread_delay(Delay);
  132. }
  133. void HAL_SuspendTick(void)
  134. {
  135. /* we should not suspend tick */
  136. }
  137. void HAL_ResumeTick(void)
  138. {
  139. /* we should not resume tick */
  140. }
  141. /**
  142. * This function will initial STM32 board.
  143. */
  144. void rt_hw_board_init()
  145. {
  146. /* Configure the MPU attributes as Write Through */
  147. mpu_init();
  148. /* Enable the CPU Cache */
  149. CPU_CACHE_Enable();
  150. /* STM32F7xx HAL library initialization:
  151. - Configure the Flash ART accelerator on ITCM interface
  152. - Configure the Systick to generate an interrupt each 1 msec
  153. - Set NVIC Group Priority to 4
  154. - Global MSP (MCU Support Package) initialization
  155. */
  156. HAL_Init();
  157. /* Configure the system clock @ 200 Mhz */
  158. SystemClock_Config();
  159. /* init systick */
  160. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  161. /* set pend exception priority */
  162. NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
  163. #ifdef RT_USING_COMPONENTS_INIT
  164. rt_components_board_init();
  165. #endif
  166. #ifdef RT_USING_EXT_SDRAM
  167. rt_system_heap_init((void*)EXT_SDRAM_BEGIN, (void*)EXT_SDRAM_END);
  168. sram_init();
  169. #else
  170. rt_system_heap_init((void*)HEAP_BEGIN, (void*)HEAP_END);
  171. #endif
  172. #ifdef RT_USING_CONSOLE
  173. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  174. #endif
  175. }
  176. /*@}*/