board.c 4.0 KB

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