board.c 3.8 KB

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