1
0

board.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 result;
  48. RCC_OscInitTypeDef RCC_OscInitStruct;
  49. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  50. /**Initializes the CPU, AHB and APB busses clocks
  51. */
  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. result = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  60. RT_ASSERT(result == HAL_OK);
  61. /**Initializes the CPU, AHB and APB busses clocks
  62. */
  63. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | 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. result = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
  69. RT_ASSERT(result == HAL_OK);
  70. /**Configure the Systick interrupt time
  71. */
  72. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
  73. /**Configure the Systick
  74. */
  75. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  76. /* SysTick_IRQn interrupt configuration */
  77. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  78. }
  79. /**
  80. * This is the timer interrupt service routine.
  81. *
  82. */
  83. void SysTick_Handler(void)
  84. {
  85. /* enter interrupt */
  86. rt_interrupt_enter();
  87. HAL_IncTick();
  88. rt_tick_increase();
  89. /* leave interrupt */
  90. rt_interrupt_leave();
  91. }
  92. /**
  93. * This function will initial STM32 board.
  94. */
  95. void rt_hw_board_init(void)
  96. {
  97. HAL_Init();
  98. SystemClock_Config();
  99. #ifdef RT_USING_HEAP
  100. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  101. #endif
  102. #ifdef RT_USING_COMPONENTS_INIT
  103. rt_components_board_init();
  104. #endif
  105. #ifdef RT_USING_CONSOLE
  106. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  107. #endif
  108. }