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