board.c 3.8 KB

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