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