board.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. if (HAL_RCC_OscConfig(&OscInit) != HAL_OK)
  64. {
  65. RT_ASSERT(RT_NULL);
  66. }
  67. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  68. clocks dividers */
  69. ClkInit.ClockType = RCC_CLOCKTYPE_SYSCLK |
  70. RCC_CLOCKTYPE_HCLK |
  71. RCC_CLOCKTYPE_PCLK1 |
  72. RCC_CLOCKTYPE_PCLK2;
  73. ClkInit.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  74. ClkInit.AHBCLKDivider = RCC_SYSCLK_DIV1;
  75. ClkInit.APB1CLKDivider = RCC_HCLK_DIV1;
  76. ClkInit.APB2CLKDivider = RCC_HCLK_DIV1;
  77. if (HAL_RCC_ClockConfig(&ClkInit, FLASH_LATENCY_1) != HAL_OK)
  78. {
  79. RT_ASSERT(RT_NULL);
  80. }
  81. }
  82. #ifdef PRINT_RCC_FREQ_INFO
  83. /**
  84. * print RCC freq information
  85. *
  86. * for example:
  87. *
  88. * SYSCLK_Frequency is 48000000HZ
  89. * PCLK_Frequency is 48000000HZ
  90. * HCLK_Frequency is 48000000HZ
  91. * CECCLK_Frequency is 32786HZ
  92. * ADCCLK_Frequency is 14000000HZ
  93. * USART1CLK_Frequency is 48000000HZ
  94. * I2C1CLK_Frequency is 8000000HZ
  95. * SystemCoreClock is 48000000HZ
  96. *
  97. */
  98. void print_rcc_freq_info(void)
  99. {
  100. rt_uint32_t clkval;
  101. clkval = HAL_RCC_GetSysClockFreq();//sysclk
  102. rt_kprintf("\nSYSCLK_Frequency is %dHZ", clkval);
  103. clkval = HAL_RCC_GetHCLKFreq(); //Hclk
  104. rt_kprintf("\nHCLK_Frequency is %dHZ", clkval);
  105. clkval = HAL_RCC_GetPCLK1Freq(); //pclk1
  106. rt_kprintf("\nPCLK1_Frequency is %dHZ", clkval);
  107. clkval = HAL_RCC_GetPCLK2Freq(); //pclk2
  108. rt_kprintf("\nPCLK2_Frequency is %dHZ", clkval);
  109. }
  110. #endif
  111. /**
  112. * This is the timer interrupt service routine.
  113. *
  114. */
  115. void SysTick_Handler(void)
  116. {
  117. /* enter interrupt */
  118. rt_interrupt_enter();
  119. rt_tick_increase();
  120. /* leave interrupt */
  121. rt_interrupt_leave();
  122. }
  123. /**
  124. * This function will initial STM32 board.
  125. */
  126. void rt_hw_board_init()
  127. {
  128. /* NVIC Configuration */
  129. NVIC_Configuration();
  130. /* Configure the SysTick */
  131. RCC_Configuration();
  132. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  133. /* Call components board initial (use INIT_BOARD_EXPORT()) */
  134. #ifdef RT_USING_COMPONENTS_INIT
  135. rt_components_board_init();
  136. #endif
  137. #ifdef RT_USING_CONSOLE
  138. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  139. #endif
  140. /* Print RCC freq info */
  141. #ifdef PRINT_RCC_FREQ_INFO
  142. print_rcc_freq_info();
  143. #endif
  144. }
  145. long cmd_reset(int argc, char** argv)
  146. {
  147. HAL_NVIC_SystemReset();
  148. return 0;
  149. }
  150. FINSH_FUNCTION_EXPORT_ALIAS(cmd_reset, __cmd_reset, Reset Board.);
  151. /*@}*/