board.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <stm32l4xx.h>
  14. #include "board.h"
  15. #include "usart.h"
  16. #include "stm32l4xx_hal.h"
  17. void _init(void)
  18. {
  19. }
  20. /**
  21. * @brief This function is executed in case of error occurrence.
  22. * @param None
  23. * @retval None
  24. */
  25. void Error_Handler(void)
  26. {
  27. /* USER CODE BEGIN Error_Handler */
  28. /* User can add his own implementation to report the HAL error return state */
  29. while(1)
  30. {
  31. }
  32. /* USER CODE END Error_Handler */
  33. }
  34. /** System Clock Configuration
  35. */
  36. void SystemClock_Config(void)
  37. {
  38. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  39. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  40. /* MSI is enabled after System reset, activate PLL with MSI as source */
  41. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  42. RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  43. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  44. RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  45. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  46. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  47. RCC_OscInitStruct.PLL.PLLM = 1;
  48. RCC_OscInitStruct.PLL.PLLN = 40;
  49. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  50. RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  51. RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  52. if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  53. {
  54. /* Initialization Error */
  55. Error_Handler();
  56. }
  57. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  58. clocks dividers */
  59. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  60. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  61. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  62. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  63. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  64. if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  65. {
  66. /* Initialization Error */
  67. Error_Handler();
  68. }
  69. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/RT_TICK_PER_SECOND);
  70. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  71. /* SysTick_IRQn interrupt configuration */
  72. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  73. }
  74. /**
  75. * This is the timer interrupt service routine.
  76. *
  77. */
  78. void SysTick_Handler(void)
  79. {
  80. /* enter interrupt */
  81. rt_interrupt_enter();
  82. rt_tick_increase();
  83. /* leave interrupt */
  84. rt_interrupt_leave();
  85. }
  86. /* re-implement tick interface for STM32 HAL */
  87. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  88. {
  89. /* Return function status */
  90. return HAL_OK;
  91. }
  92. uint32_t HAL_GetTick(void)
  93. {
  94. return rt_tick_get() * 1000 / RT_TICK_PER_SECOND;
  95. }
  96. void HAL_SuspendTick(void)
  97. {
  98. }
  99. void HAL_ResumeTick(void)
  100. {
  101. }
  102. void HAL_Delay(__IO uint32_t Delay)
  103. {
  104. }
  105. /**
  106. * This function will initial STM32 board.
  107. */
  108. void rt_hw_board_init()
  109. {
  110. /* NVIC Configuration */
  111. #define NVIC_VTOR_MASK 0x3FFFFF80
  112. #ifdef VECT_TAB_RAM
  113. /* Set the Vector Table base location at 0x10000000 */
  114. SCB->VTOR = (0x10000000 & NVIC_VTOR_MASK);
  115. #else /* VECT_TAB_FLASH */
  116. /* Set the Vector Table base location at 0x08000000 */
  117. SCB->VTOR = (0x08000000 & NVIC_VTOR_MASK);
  118. #endif
  119. HAL_Init();
  120. SystemClock_Config();
  121. stm32_hw_usart_init();
  122. #ifdef RT_USING_CONSOLE
  123. rt_console_set_device(CONSOLE_DEVICE);
  124. #endif
  125. #ifdef RT_USING_COMPONENTS_INIT
  126. rt_components_board_init();
  127. #endif
  128. }
  129. /*@}*/