board.c 3.7 KB

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