board.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, RT-Thread Development 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-09-22 Bernard add board.h to this bsp
  13. */
  14. #include <rtthread.h>
  15. #include "board.h"
  16. /**
  17. * @addtogroup STM32
  18. */
  19. /*@{*/
  20. static void SystemClock_Config(void)
  21. {
  22. RCC_OscInitTypeDef RCC_OscInitStruct;
  23. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  24. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
  25. /**Configure the main internal regulator output voltage
  26. */
  27. __HAL_RCC_PWR_CLK_ENABLE();
  28. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  29. /**Initializes the CPU, AHB and APB busses clocks
  30. */
  31. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  32. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  33. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  34. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  35. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  36. RCC_OscInitStruct.PLL.PLLM = 4;
  37. RCC_OscInitStruct.PLL.PLLN = 168;
  38. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  39. RCC_OscInitStruct.PLL.PLLQ = 7;
  40. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  41. {
  42. while(1)
  43. {}
  44. }
  45. /**Initializes the CPU, AHB and APB busses clocks
  46. */
  47. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  48. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  49. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  50. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  51. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  52. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  53. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  54. {
  55. while(1)
  56. {}
  57. }
  58. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  59. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  60. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  61. {
  62. while(1)
  63. {}
  64. }
  65. }
  66. /**
  67. * This is the timer interrupt service routine.
  68. *
  69. */
  70. void SysTick_Handler(void)
  71. {
  72. /* enter interrupt */
  73. rt_interrupt_enter();
  74. /* tick for HAL Library */
  75. HAL_IncTick();
  76. rt_tick_increase();
  77. /* leave interrupt */
  78. rt_interrupt_leave();
  79. }
  80. /* re-implementat tick interface for STM32 HAL */
  81. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  82. {
  83. /*Configure the SysTick to have interrupt in 1ms time basis*/
  84. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
  85. /*Configure the SysTick IRQ priority */
  86. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority , 0);
  87. /* Return function status */
  88. return HAL_OK;
  89. }
  90. void HAL_Delay(__IO uint32_t Delay)
  91. {
  92. rt_thread_delay(Delay);
  93. }
  94. void HAL_SuspendTick(void)
  95. {
  96. /* we should not suspend tick */
  97. }
  98. void HAL_ResumeTick(void)
  99. {
  100. /* we should not resume tick */
  101. }
  102. void HAL_MspInit(void)
  103. {
  104. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  105. /* System interrupt init*/
  106. /* MemoryManagement_IRQn interrupt configuration */
  107. HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0);
  108. /* BusFault_IRQn interrupt configuration */
  109. HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0);
  110. /* UsageFault_IRQn interrupt configuration */
  111. HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0);
  112. /* SVCall_IRQn interrupt configuration */
  113. HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0);
  114. /* DebugMonitor_IRQn interrupt configuration */
  115. HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0);
  116. /* PendSV_IRQn interrupt configuration */
  117. HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
  118. /* SysTick_IRQn interrupt configuration */
  119. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  120. }
  121. /**
  122. * This function will initial STM32 board.
  123. */
  124. void rt_hw_board_init()
  125. {
  126. /* Configure the system clock @ 84 Mhz */
  127. SystemClock_Config();
  128. HAL_Init();
  129. #ifdef RT_USING_HEAP
  130. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  131. #endif
  132. #ifdef RT_USING_COMPONENTS_INIT
  133. rt_components_board_init();
  134. #endif
  135. #ifdef RT_USING_CONSOLE
  136. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  137. #endif
  138. }
  139. /*@}*/