board.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-01-05 Bernard first implementation
  23. */
  24. #include <stdint.h>
  25. #include <rthw.h>
  26. #include <rtthread.h>
  27. #include <stm32l4xx.h>
  28. #include "board.h"
  29. #include "usart.h"
  30. #include "stm32l4xx_hal.h"
  31. void _init(void)
  32. {
  33. }
  34. /**
  35. * @brief This function is executed in case of error occurrence.
  36. * @param None
  37. * @retval None
  38. */
  39. void Error_Handler(void)
  40. {
  41. /* USER CODE BEGIN Error_Handler */
  42. /* User can add his own implementation to report the HAL error return state */
  43. while(1)
  44. {
  45. }
  46. /* USER CODE END Error_Handler */
  47. }
  48. /** System Clock Configuration
  49. */
  50. void SystemClock_Config(void)
  51. {
  52. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  53. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  54. /* MSI is enabled after System reset, activate PLL with MSI as source */
  55. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  56. RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  57. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  58. RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  59. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  60. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  61. RCC_OscInitStruct.PLL.PLLM = 1;
  62. RCC_OscInitStruct.PLL.PLLN = 40;
  63. RCC_OscInitStruct.PLL.PLLR = 2;
  64. RCC_OscInitStruct.PLL.PLLP = 7;
  65. RCC_OscInitStruct.PLL.PLLQ = 4;
  66. if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  67. {
  68. /* Initialization Error */
  69. Error_Handler();
  70. }
  71. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  72. clocks dividers */
  73. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  74. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  75. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  76. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  77. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  78. if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  79. {
  80. /* Initialization Error */
  81. Error_Handler();
  82. }
  83. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/RT_TICK_PER_SECOND);
  84. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  85. /* SysTick_IRQn interrupt configuration */
  86. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  87. }
  88. /**
  89. * This is the timer interrupt service routine.
  90. *
  91. */
  92. void SysTick_Handler(void)
  93. {
  94. /* enter interrupt */
  95. rt_interrupt_enter();
  96. rt_tick_increase();
  97. /* leave interrupt */
  98. rt_interrupt_leave();
  99. }
  100. /* re-implement tick interface for STM32 HAL */
  101. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  102. {
  103. /* Return function status */
  104. return HAL_OK;
  105. }
  106. uint32_t HAL_GetTick(void)
  107. {
  108. return rt_tick_get() * 1000 / RT_TICK_PER_SECOND;
  109. }
  110. void HAL_SuspendTick(void)
  111. {
  112. }
  113. void HAL_ResumeTick(void)
  114. {
  115. }
  116. void HAL_Delay(__IO uint32_t Delay)
  117. {
  118. }
  119. /**
  120. * This function will initial STM32 board.
  121. */
  122. void rt_hw_board_init()
  123. {
  124. /* NVIC Configuration */
  125. #define NVIC_VTOR_MASK 0x3FFFFF80
  126. #ifdef VECT_TAB_RAM
  127. /* Set the Vector Table base location at 0x10000000 */
  128. SCB->VTOR = (0x10000000 & NVIC_VTOR_MASK);
  129. #else /* VECT_TAB_FLASH */
  130. /* Set the Vector Table base location at 0x08000000 */
  131. SCB->VTOR = (0x08000000 & NVIC_VTOR_MASK);
  132. #endif
  133. HAL_Init();
  134. SystemClock_Config();
  135. #ifdef RT_USING_COMPONENTS_INIT
  136. rt_components_board_init();
  137. #endif
  138. #ifdef RT_USING_CONSOLE
  139. rt_console_set_device(CONSOLE_DEVICE);
  140. #endif
  141. }
  142. /*@}*/