board.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. RCC_OscInitTypeDef RCC_OscInitStruct;
  48. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  49. /**Initializes the CPU, AHB and APB busses clocks
  50. */
  51. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  52. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  53. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  54. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  55. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  56. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  57. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  58. RT_ASSERT(HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK);
  59. /**Initializes the CPU, AHB and APB busses clocks
  60. */
  61. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  62. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  63. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  64. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  65. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  66. RT_ASSERT(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) == HAL_OK);
  67. /**Configure the Systick interrupt time
  68. */
  69. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
  70. /**Configure the Systick
  71. */
  72. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  73. /* SysTick_IRQn interrupt configuration */
  74. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  75. }
  76. /**
  77. * This is the timer interrupt service routine.
  78. *
  79. */
  80. void SysTick_Handler(void)
  81. {
  82. /* enter interrupt */
  83. rt_interrupt_enter();
  84. HAL_IncTick();
  85. rt_tick_increase();
  86. /* leave interrupt */
  87. rt_interrupt_leave();
  88. }
  89. /**
  90. * This function will initial STM32 board.
  91. */
  92. void rt_hw_board_init(void)
  93. {
  94. HAL_Init();
  95. SystemClock_Config();
  96. #ifdef RT_USING_HEAP
  97. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  98. #endif
  99. #ifdef RT_USING_COMPONENTS_INIT
  100. rt_components_board_init();
  101. #endif
  102. #ifdef RT_USING_CONSOLE
  103. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  104. #endif
  105. }