board.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. /**
  20. * @addtogroup STM32
  21. */
  22. /*@{*/
  23. /*******************************************************************************
  24. * Function Name : NVIC_Configuration
  25. * Description : Configures Vector Table base location.
  26. * Input : None
  27. * Output : None
  28. * Return : None
  29. *******************************************************************************/
  30. void NVIC_Configuration(void)
  31. {
  32. // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  33. }
  34. /**
  35. * @brief Inserts a delay time.
  36. * @param nCount: specifies the delay time length.
  37. * @retval None
  38. */
  39. static void Delay(__IO uint32_t nCount)
  40. {
  41. /* Decrement nCount value */
  42. while (nCount != 0)
  43. {
  44. nCount--;
  45. }
  46. }
  47. /**
  48. * This RCC initial for system.
  49. * use HSI clock source and pll
  50. * HSI = 8; sysclk = 8/2 * 12 = 48MHZ
  51. * sysclk source is pllclk
  52. * AHB prescaler is 1, HCLK = SYSCKL = SystemCoreClock = 48MHZ
  53. */
  54. static void RCC_Configuration(void)
  55. {
  56. RCC_DeInit();
  57. /* setup HSI */
  58. RCC_HSICmd(ENABLE);
  59. /* Configure PLL source is HSI */
  60. RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_12);
  61. RCC_PLLCmd(ENABLE);
  62. /* Configure SYSCLK source is PLL */
  63. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  64. /* Conigure AHB prescaler value is 1 */
  65. RCC_HCLKConfig(RCC_SYSCLK_Div1);
  66. /* Delay for RCC setup */
  67. Delay(0x3FFFF);
  68. /* Update SystemCoreClock value from RCC configure */
  69. SystemCoreClockUpdate();
  70. }
  71. #ifdef PRINT_RCC_FREQ_INFO
  72. /**
  73. * print RCC freq information
  74. *
  75. * for example:
  76. *
  77. * SYSCLK_Frequency is 48000000HZ
  78. * PCLK_Frequency is 48000000HZ
  79. * HCLK_Frequency is 48000000HZ
  80. * CECCLK_Frequency is 32786HZ
  81. * ADCCLK_Frequency is 14000000HZ
  82. * USART1CLK_Frequency is 48000000HZ
  83. * I2C1CLK_Frequency is 8000000HZ
  84. * SystemCoreClock is 48000000HZ
  85. *
  86. */
  87. void print_rcc_freq_info(void)
  88. {
  89. RCC_ClocksTypeDef RCC_ClockFreq;
  90. RCC_GetClocksFreq(&RCC_ClockFreq);
  91. rt_kprintf("\nSYSCLK_Frequency is %dHZ", RCC_ClockFreq.SYSCLK_Frequency);
  92. rt_kprintf("\nPCLK_Frequency is %dHZ", RCC_ClockFreq.PCLK_Frequency);
  93. rt_kprintf("\nHCLK_Frequency is %dHZ", RCC_ClockFreq.HCLK_Frequency);
  94. rt_kprintf("\nCECCLK_Frequency is %dHZ", RCC_ClockFreq.CECCLK_Frequency);
  95. rt_kprintf("\nADCCLK_Frequency is %dHZ", RCC_ClockFreq.ADCCLK_Frequency);
  96. rt_kprintf("\nUSART1CLK_Frequency is %dHZ", RCC_ClockFreq.USART1CLK_Frequency);
  97. rt_kprintf("\nI2C1CLK_Frequency is %dHZ", RCC_ClockFreq.I2C1CLK_Frequency);
  98. rt_kprintf("\nSystemCoreClock is %dHZ\n", SystemCoreClock);
  99. }
  100. #endif
  101. /**
  102. * This is the timer interrupt service routine.
  103. *
  104. */
  105. void SysTick_Handler(void)
  106. {
  107. /* enter interrupt */
  108. rt_interrupt_enter();
  109. rt_tick_increase();
  110. /* leave interrupt */
  111. rt_interrupt_leave();
  112. }
  113. /**
  114. * This function will initial STM32 board.
  115. */
  116. void rt_hw_board_init()
  117. {
  118. /* NVIC Configuration */
  119. NVIC_Configuration();
  120. /* Configure the SysTick */
  121. RCC_Configuration();
  122. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  123. /* Initial usart deriver, and set console device */
  124. rt_hw_usart_init();
  125. #ifdef RT_USING_CONSOLE
  126. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  127. #endif
  128. /* Print RCC freq info */
  129. #ifdef PRINT_RCC_FREQ_INFO
  130. print_rcc_freq_info();
  131. #endif
  132. /* Call components board initial (use INIT_BOARD_EXPORT()) */
  133. #ifdef RT_USING_COMPONENTS_INIT
  134. rt_components_board_init();
  135. #endif
  136. }
  137. /*@}*/