board.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. */
  15. #include <rthw.h>
  16. #include <rtthread.h>
  17. #include "board.h"
  18. /**
  19. * @addtogroup STM32
  20. */
  21. /*@{*/
  22. void HAL_MspInit(void)
  23. {
  24. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  25. /* System interrupt init*/
  26. __HAL_RCC_AFIO_CLK_ENABLE();
  27. /* MemoryManagement_IRQn interrupt configuration */
  28. HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0);
  29. /* BusFault_IRQn interrupt configuration */
  30. HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0);
  31. /* UsageFault_IRQn interrupt configuration */
  32. HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0);
  33. /* SVCall_IRQn interrupt configuration */
  34. HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0);
  35. /* DebugMonitor_IRQn interrupt configuration */
  36. HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0);
  37. /* PendSV_IRQn interrupt configuration */
  38. HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
  39. /* SysTick_IRQn interrupt configuration */
  40. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  41. /**DISABLE: JTAG-DP Disabled and SW-DP Disabled
  42. */
  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() / 1000);
  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_SERIAL
  97. rt_hw_usart_init();
  98. #endif
  99. #ifdef RT_USING_PIN
  100. rt_hw_pin_init();
  101. #endif
  102. #ifdef RT_USING_CONSOLE
  103. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  104. #endif
  105. }
  106. /*@}*/