board.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * 2017-10-20 ZYH emmm...setup for HAL Libraries
  14. * 2017-11-15 ZYH update to 3.0.0
  15. */
  16. #include <rthw.h>
  17. #include <rtthread.h>
  18. #include "board.h"
  19. /**
  20. * @addtogroup STM32
  21. */
  22. /*@{*/
  23. void HAL_MspInit(void)
  24. {
  25. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  26. /* System interrupt init*/
  27. __HAL_RCC_AFIO_CLK_ENABLE();
  28. /* MemoryManagement_IRQn interrupt configuration */
  29. HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0);
  30. /* BusFault_IRQn interrupt configuration */
  31. HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0);
  32. /* UsageFault_IRQn interrupt configuration */
  33. HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0);
  34. /* SVCall_IRQn interrupt configuration */
  35. HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0);
  36. /* DebugMonitor_IRQn interrupt configuration */
  37. HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0);
  38. /* PendSV_IRQn interrupt configuration */
  39. HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
  40. /* SysTick_IRQn interrupt configuration */
  41. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  42. /**DISABLE: JTAG-DP Disabled and SW-DP Disabled**/
  43. __HAL_AFIO_REMAP_SWJ_NOJTAG();
  44. }
  45. void SystemClock_Config(void)
  46. {
  47. rt_err_t ret = RT_EOK;
  48. RCC_OscInitTypeDef RCC_OscInitStruct;
  49. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  50. #if !defined(RT_USING_HSI)
  51. /* Initializes the CPU, AHB and APB busses clocks */
  52. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  53. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  54. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  55. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  56. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  57. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  58. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  59. ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  60. RT_ASSERT(ret == HAL_OK);
  61. /* Initializes the CPU, AHB and APB busses clocks */
  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_DIV2;
  67. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  68. ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
  69. RT_ASSERT(ret == HAL_OK);
  70. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
  71. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  72. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  73. #else
  74. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  75. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  76. RCC_OscInitStruct.HSICalibrationValue = 16;
  77. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  78. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  79. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  80. ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  81. RT_ASSERT(ret == HAL_OK);
  82. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
  83. RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  84. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  85. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  86. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  87. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  88. ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
  89. RT_ASSERT(ret == HAL_OK);
  90. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/RT_TICK_PER_SECOND);
  91. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  92. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  93. #endif
  94. }
  95. /**
  96. * This is the timer interrupt service routine.
  97. *
  98. */
  99. void SysTick_Handler(void)
  100. {
  101. /* enter interrupt */
  102. rt_interrupt_enter();
  103. HAL_IncTick();
  104. rt_tick_increase();
  105. /* leave interrupt */
  106. rt_interrupt_leave();
  107. }
  108. /**
  109. * This function will initial STM32 board.
  110. */
  111. void rt_hw_board_init(void)
  112. {
  113. HAL_Init();
  114. SystemClock_Config();
  115. #ifdef RT_USING_HEAP
  116. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  117. #endif
  118. #ifdef RT_USING_COMPONENTS_INIT
  119. rt_components_board_init();
  120. #endif
  121. #ifdef RT_USING_CONSOLE
  122. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  123. #endif
  124. }
  125. /**
  126. * This function will delay for some us.
  127. *
  128. * @param us the delay time of us
  129. */
  130. void rt_hw_us_delay(rt_uint32_t us)
  131. {
  132. rt_uint32_t delta;
  133. us = us * (SysTick->LOAD / (1000000 / RT_TICK_PER_SECOND));
  134. delta = SysTick->VAL;
  135. if (delta < us)
  136. {
  137. /* wait current OSTick left time gone */
  138. while (SysTick->VAL < us);
  139. us -= delta;
  140. delta = SysTick->LOAD;
  141. }
  142. while (delta - SysTick->VAL < us);
  143. }