board.c 5.4 KB

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