board.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-09-22 Bernard add board.h to this bsp
  13. */
  14. #include <rtthread.h>
  15. #include "board.h"
  16. /**
  17. * @addtogroup STM32
  18. */
  19. /*@{*/
  20. /**
  21. * @brief System Clock Configuration
  22. * The system Clock is configured as follow :
  23. * System Clock source = PLL (HSI)
  24. * SYSCLK(Hz) = 84000000
  25. * HCLK(Hz) = 84000000
  26. * AHB Prescaler = 1
  27. * APB1 Prescaler = 2
  28. * APB2 Prescaler = 1
  29. * HSI Frequency(Hz) = 16000000
  30. * PLL_M = 16
  31. * PLL_N = 336
  32. * PLL_P = 4
  33. * PLL_Q = 7
  34. * VDD(V) = 3.3
  35. * Main regulator output voltage = Scale2 mode
  36. * Flash Latency(WS) = 2
  37. * @param None
  38. * @retval None
  39. */
  40. static void SystemClock_Config(void)
  41. {
  42. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  43. RCC_OscInitTypeDef RCC_OscInitStruct;
  44. /* Enable Power Control clock */
  45. __HAL_RCC_PWR_CLK_ENABLE();
  46. /* The voltage scaling allows optimizing the power consumption when the device is
  47. clocked below the maximum system frequency, to update the voltage scaling value
  48. regarding system frequency refer to product datasheet. */
  49. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  50. /* Enable HSI Oscillator and activate PLL with HSI as source */
  51. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  52. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  53. RCC_OscInitStruct.HSICalibrationValue = 0x10;
  54. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  55. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  56. RCC_OscInitStruct.PLL.PLLM = 16;
  57. RCC_OscInitStruct.PLL.PLLN = 336;
  58. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  59. RCC_OscInitStruct.PLL.PLLQ = 7;
  60. if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  61. {
  62. while(1)
  63. {
  64. ;
  65. }
  66. }
  67. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  68. clocks dividers */
  69. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  70. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  71. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  72. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  73. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  74. if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  75. {
  76. while (1)
  77. {
  78. ;
  79. }
  80. }
  81. }
  82. /**
  83. * This is the timer interrupt service routine.
  84. *
  85. */
  86. void SysTick_Handler(void)
  87. {
  88. /* enter interrupt */
  89. rt_interrupt_enter();
  90. /* tick for HAL Library */
  91. HAL_IncTick();
  92. rt_tick_increase();
  93. /* leave interrupt */
  94. rt_interrupt_leave();
  95. }
  96. /* re-implementat tick interface for STM32 HAL */
  97. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  98. {
  99. /*Configure the SysTick to have interrupt in 1ms time basis*/
  100. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/RT_TICK_PER_SECOND);
  101. /*Configure the SysTick IRQ priority */
  102. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0);
  103. /* Return function status */
  104. return HAL_OK;
  105. }
  106. void HAL_Delay(__IO uint32_t Delay)
  107. {
  108. rt_thread_delay(Delay);
  109. }
  110. void HAL_SuspendTick(void)
  111. {
  112. /* we should not suspend tick */
  113. }
  114. void HAL_ResumeTick(void)
  115. {
  116. /* we should not resume tick */
  117. }
  118. /**
  119. * This function will initial STM32 board.
  120. */
  121. void rt_hw_board_init()
  122. {
  123. /* STM32F7xx HAL library initialization:
  124. - Configure the Flash ART accelerator on ITCM interface
  125. - Configure the Systick to generate an interrupt each 1 msec
  126. - Set NVIC Group Priority to 4
  127. - Global MSP (MCU Support Package) initialization
  128. */
  129. HAL_Init();
  130. /* Configure the system clock @ 200 Mhz */
  131. SystemClock_Config();
  132. #ifdef RT_USING_HEAP
  133. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  134. #endif
  135. #ifdef RT_USING_COMPONENTS_INIT
  136. rt_components_board_init();
  137. #endif
  138. #ifdef RT_USING_CONSOLE
  139. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  140. #endif
  141. }
  142. /*@}*/