board.c 4.3 KB

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