board.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "stm32f4xx.h"
  18. #include "board.h"
  19. #include "usart.h"
  20. #include "stm32f4xx_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_OscInitTypeDef RCC_OscInitStruct;
  43. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  44. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
  45. __HAL_RCC_PWR_CLK_ENABLE();
  46. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  47. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  48. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  49. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  50. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  51. RCC_OscInitStruct.PLL.PLLM = 8;
  52. RCC_OscInitStruct.PLL.PLLN = 336;
  53. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  54. RCC_OscInitStruct.PLL.PLLQ = 7;
  55. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  56. {
  57. Error_Handler();
  58. }
  59. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  60. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  61. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  62. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  63. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  64. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  65. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  66. {
  67. Error_Handler();
  68. }
  69. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LTDC;
  70. PeriphClkInitStruct.PLLSAI.PLLSAIN = 260;
  71. PeriphClkInitStruct.PLLSAI.PLLSAIR = 2;
  72. PeriphClkInitStruct.PLLSAIDivR = RCC_PLLSAIDIVR_2;
  73. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  74. {
  75. Error_Handler();
  76. }
  77. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  78. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  79. /* SysTick_IRQn interrupt configuration */
  80. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  81. }
  82. /**
  83. * This is the timer interrupt service routine.
  84. *
  85. */
  86. void SysTick_Handler(void)
  87. {
  88. /* enter interrupt */
  89. rt_interrupt_enter();
  90. /* tick for HAL Library */
  91. HAL_IncTick();
  92. rt_tick_increase();
  93. /* leave interrupt */
  94. rt_interrupt_leave();
  95. }
  96. /* re-implementat tick interface for STM32 HAL */
  97. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  98. {
  99. /*Configure the SysTick to have interrupt in 1ms time basis*/
  100. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/RT_TICK_PER_SECOND);
  101. /*Configure the SysTick IRQ priority */
  102. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0);
  103. /* Return function status */
  104. return HAL_OK;
  105. }
  106. void HAL_Delay(__IO uint32_t Delay)
  107. {
  108. rt_thread_delay(Delay);
  109. }
  110. /**
  111. * This function will initial STM32 board.
  112. */
  113. void rt_hw_board_init()
  114. {
  115. HAL_Init();
  116. SystemClock_Config();
  117. #ifdef RT_USING_COMPONENTS_INIT
  118. rt_components_board_init();
  119. #else
  120. stm32_hw_usart_init();
  121. #endif
  122. #ifdef RT_USING_CONSOLE
  123. rt_console_set_device(CONSOLE_DEVICE);
  124. #endif
  125. }
  126. /*@}*/