board.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 HSI clock source and pll
  54. * HSI = 8; sysclk = 8/2 * 12 = 48MHZ
  55. * sysclk source is pllclk
  56. * AHB prescaler is 1, HCLK = SYSCKL = SystemCoreClock = 48MHZ
  57. */
  58. static void RCC_Configuration(void)
  59. {
  60. RCC_DeInit();
  61. /* setup HSI */
  62. RCC_HSICmd(ENABLE);
  63. /* Configure PLL source is HSI */
  64. RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_12);
  65. RCC_PLLCmd(ENABLE);
  66. /* Configure SYSCLK source is PLL */
  67. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  68. /* Conigure AHB prescaler value is 1 */
  69. RCC_HCLKConfig(RCC_SYSCLK_Div1);
  70. /* Delay for RCC setup */
  71. Delay(0x3FFFF);
  72. /* Update SystemCoreClock value from RCC configure */
  73. SystemCoreClockUpdate();
  74. }
  75. #ifdef PRINT_RCC_FREQ_INFO
  76. /**
  77. * print RCC freq information
  78. *
  79. * for example:
  80. *
  81. * SYSCLK_Frequency is 48000000HZ
  82. * PCLK_Frequency is 48000000HZ
  83. * HCLK_Frequency is 48000000HZ
  84. * CECCLK_Frequency is 32786HZ
  85. * ADCCLK_Frequency is 14000000HZ
  86. * USART1CLK_Frequency is 48000000HZ
  87. * I2C1CLK_Frequency is 8000000HZ
  88. * SystemCoreClock is 48000000HZ
  89. *
  90. */
  91. void print_rcc_freq_info(void)
  92. {
  93. RCC_ClocksTypeDef RCC_ClockFreq;
  94. RCC_GetClocksFreq(&RCC_ClockFreq);
  95. rt_kprintf("\nSYSCLK_Frequency is %dHZ", RCC_ClockFreq.SYSCLK_Frequency);
  96. rt_kprintf("\nPCLK_Frequency is %dHZ", RCC_ClockFreq.PCLK_Frequency);
  97. rt_kprintf("\nHCLK_Frequency is %dHZ", RCC_ClockFreq.HCLK_Frequency);
  98. rt_kprintf("\nCECCLK_Frequency is %dHZ", RCC_ClockFreq.CECCLK_Frequency);
  99. rt_kprintf("\nADCCLK_Frequency is %dHZ", RCC_ClockFreq.ADCCLK_Frequency);
  100. rt_kprintf("\nUSART1CLK_Frequency is %dHZ", RCC_ClockFreq.USART1CLK_Frequency);
  101. rt_kprintf("\nI2C1CLK_Frequency is %dHZ", RCC_ClockFreq.I2C1CLK_Frequency);
  102. rt_kprintf("\nSystemCoreClock is %dHZ\n", SystemCoreClock);
  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. /* Initial usart deriver, and set console device */
  128. rt_hw_usart_init();
  129. #ifdef RT_USING_CONSOLE
  130. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  131. #endif
  132. /* Print RCC freq info */
  133. #ifdef PRINT_RCC_FREQ_INFO
  134. print_rcc_freq_info();
  135. #endif
  136. /* Call components board initial (use INIT_BOARD_EXPORT()) */
  137. #ifdef RT_USING_COMPONENTS_INIT
  138. rt_components_board_init();
  139. #endif
  140. }
  141. /*@}*/