board.c 3.5 KB

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