board.c 4.1 KB

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