board.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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-01-05 Bernard first implementation
  9. */
  10. #include <stdint.h>
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include "stm32f4xx.h"
  14. #include "board.h"
  15. #include "stm32f4xx_hal.h"
  16. void _init(void)
  17. {
  18. }
  19. /**
  20. * @brief This function is executed in case of error occurrence.
  21. * @param None
  22. * @retval None
  23. */
  24. void Error_Handler(void)
  25. {
  26. /* USER CODE BEGIN Error_Handler */
  27. /* User can add his own implementation to report the HAL error return state */
  28. while(1)
  29. {
  30. }
  31. /* USER CODE END Error_Handler */
  32. }
  33. /** System Clock Configuration
  34. */
  35. void SystemClock_Config(void)
  36. {
  37. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  38. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  39. __HAL_RCC_PWR_CLK_ENABLE();
  40. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  41. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  42. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  43. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  44. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  45. RCC_OscInitStruct.PLL.PLLM = 8;
  46. RCC_OscInitStruct.PLL.PLLN = 360;
  47. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  48. RCC_OscInitStruct.PLL.PLLQ = 8;
  49. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  50. {
  51. RT_ASSERT(RT_NULL);
  52. }
  53. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  54. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  55. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  56. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  57. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  58. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  59. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  60. {
  61. RT_ASSERT(RT_NULL);
  62. }
  63. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/RT_TICK_PER_SECOND);
  64. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  65. /* SysTick_IRQn interrupt configuration */
  66. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  67. }
  68. /**
  69. * This is the timer interrupt service routine.
  70. *
  71. */
  72. void SysTick_Handler(void)
  73. {
  74. /* enter interrupt */
  75. rt_interrupt_enter();
  76. rt_tick_increase();
  77. /* leave interrupt */
  78. rt_interrupt_leave();
  79. }
  80. /* re-implement tick interface for STM32 HAL */
  81. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  82. {
  83. /* Return function status */
  84. return HAL_OK;
  85. }
  86. uint32_t HAL_GetTick(void)
  87. {
  88. return rt_tick_get() * 1000 / RT_TICK_PER_SECOND;
  89. }
  90. void HAL_SuspendTick(void)
  91. {
  92. }
  93. void HAL_ResumeTick(void)
  94. {
  95. }
  96. void HAL_Delay(__IO uint32_t Delay)
  97. {
  98. Delay *= 1000;
  99. while (Delay--)
  100. {
  101. Delay = Delay;
  102. }
  103. }
  104. /**
  105. * This function will initial STM32 board.
  106. */
  107. void rt_hw_board_init()
  108. {
  109. /* NVIC Configuration */
  110. #define NVIC_VTOR_MASK 0x3FFFFF80
  111. #ifdef VECT_TAB_RAM
  112. /* Set the Vector Table base location at 0x10000000 */
  113. SCB->VTOR = (0x10000000 & NVIC_VTOR_MASK);
  114. #else /* VECT_TAB_FLASH */
  115. /* Set the Vector Table base location at 0x08000000 */
  116. SCB->VTOR = (0x08000000 & NVIC_VTOR_MASK);
  117. #endif
  118. HAL_Init();
  119. SystemClock_Config();
  120. #ifdef RT_USING_COMPONENTS_INIT
  121. rt_components_board_init();
  122. #endif
  123. #ifdef RT_USING_CONSOLE
  124. rt_console_set_device(CONSOLE_DEVICE);
  125. #endif
  126. }
  127. /*@}*/