board.c 4.1 KB

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