board.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2009-01-05 Bernard first implementation
  9. * 2013-11-15 bright add RCC initial and print RCC freq function
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #ifdef RT_USING_FINSH
  14. #include <finsh.h>
  15. #endif
  16. #include "board.h"
  17. #include "usart.h"
  18. /**
  19. * @addtogroup STM32
  20. */
  21. /*@{*/
  22. /*******************************************************************************
  23. * Function Name : NVIC_Configuration
  24. * Description : Configures Vector Table base location.
  25. * Input : None
  26. * Output : None
  27. * Return : None
  28. *******************************************************************************/
  29. void NVIC_Configuration(void)
  30. {
  31. // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  32. }
  33. /**
  34. * This RCC initial for system.
  35. * use HSE clock source
  36. * HSE = 20MHZ; sysclk = 20MHZ
  37. * sysclk source is HSE
  38. * AHB prescaler is 1, HCLK = SYSCKL = SystemCoreClock = 20MHZ
  39. */
  40. static void RCC_Configuration(void)
  41. {
  42. RCC_ClkInitTypeDef ClkInit = {0};
  43. RCC_OscInitTypeDef OscInit = {0};
  44. HAL_RCC_DeInit();
  45. /* Enable HSI Oscillator and Activate PLL with HSI as source */
  46. OscInit.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  47. OscInit.HSIState = RCC_HSI_ON;
  48. OscInit.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  49. OscInit.PLL.PLLState = RCC_PLL_ON;
  50. OscInit.PLL.PLLDIV = RCC_PLLDIV_2;
  51. OscInit.PLL.PLLMUL = RCC_PLLMUL_4;
  52. OscInit.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  53. if (HAL_RCC_OscConfig(&OscInit) != HAL_OK)
  54. {
  55. RT_ASSERT(RT_NULL);
  56. }
  57. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  58. clocks dividers */
  59. ClkInit.ClockType = RCC_CLOCKTYPE_SYSCLK |
  60. RCC_CLOCKTYPE_HCLK |
  61. RCC_CLOCKTYPE_PCLK1 |
  62. RCC_CLOCKTYPE_PCLK2;
  63. ClkInit.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  64. ClkInit.AHBCLKDivider = RCC_SYSCLK_DIV1;
  65. ClkInit.APB1CLKDivider = RCC_HCLK_DIV1;
  66. ClkInit.APB2CLKDivider = RCC_HCLK_DIV1;
  67. if (HAL_RCC_ClockConfig(&ClkInit, FLASH_LATENCY_1) != HAL_OK)
  68. {
  69. RT_ASSERT(RT_NULL);
  70. }
  71. }
  72. #ifdef PRINT_RCC_FREQ_INFO
  73. /**
  74. * print RCC freq information
  75. *
  76. * for example:
  77. *
  78. * SYSCLK_Frequency is 48000000HZ
  79. * PCLK_Frequency is 48000000HZ
  80. * HCLK_Frequency is 48000000HZ
  81. * CECCLK_Frequency is 32786HZ
  82. * ADCCLK_Frequency is 14000000HZ
  83. * USART1CLK_Frequency is 48000000HZ
  84. * I2C1CLK_Frequency is 8000000HZ
  85. * SystemCoreClock is 48000000HZ
  86. *
  87. */
  88. void print_rcc_freq_info(void)
  89. {
  90. rt_uint32_t clkval;
  91. clkval = HAL_RCC_GetSysClockFreq();//sysclk
  92. rt_kprintf("\nSYSCLK_Frequency is %dHZ", clkval);
  93. clkval = HAL_RCC_GetHCLKFreq(); //Hclk
  94. rt_kprintf("\nHCLK_Frequency is %dHZ", clkval);
  95. clkval = HAL_RCC_GetPCLK1Freq(); //pclk1
  96. rt_kprintf("\nPCLK1_Frequency is %dHZ", clkval);
  97. clkval = HAL_RCC_GetPCLK2Freq(); //pclk2
  98. rt_kprintf("\nPCLK2_Frequency is %dHZ", clkval);
  99. }
  100. #endif
  101. /**
  102. * This is the timer interrupt service routine.
  103. *
  104. */
  105. void SysTick_Handler(void)
  106. {
  107. /* enter interrupt */
  108. rt_interrupt_enter();
  109. rt_tick_increase();
  110. /* leave interrupt */
  111. rt_interrupt_leave();
  112. }
  113. /**
  114. * This function will initial STM32 board.
  115. */
  116. void rt_hw_board_init()
  117. {
  118. /* NVIC Configuration */
  119. NVIC_Configuration();
  120. /* Configure the SysTick */
  121. RCC_Configuration();
  122. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  123. /* Call components board initial (use INIT_BOARD_EXPORT()) */
  124. #ifdef RT_USING_COMPONENTS_INIT
  125. rt_components_board_init();
  126. #endif
  127. #ifdef RT_USING_CONSOLE
  128. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  129. #endif
  130. /* Print RCC freq info */
  131. #ifdef PRINT_RCC_FREQ_INFO
  132. print_rcc_freq_info();
  133. #endif
  134. }
  135. long cmd_reset(int argc, char** argv)
  136. {
  137. HAL_NVIC_SystemReset();
  138. return 0;
  139. }
  140. FINSH_FUNCTION_EXPORT_ALIAS(cmd_reset, __cmd_reset, Reset Board);
  141. /*@}*/