board.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 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://openlab.rt-thread.com/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-08-23 Bernard first implementation
  13. */
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #include "stm32f10x_lib.h"
  17. static void rt_hw_console_init(void);
  18. /**
  19. * @addtogroup STM32
  20. */
  21. /*@{*/
  22. ErrorStatus HSEStartUpStatus;
  23. /*******************************************************************************
  24. * Function Name : RCC_Configuration
  25. * Description : Configures the different system clocks.
  26. * Input : None
  27. * Output : None
  28. * Return : None
  29. *******************************************************************************/
  30. void RCC_Configuration(void)
  31. {
  32. /* RCC system reset(for debug purpose) */
  33. RCC_DeInit();
  34. /* Enable HSE */
  35. RCC_HSEConfig(RCC_HSE_ON);
  36. /* Wait till HSE is ready */
  37. HSEStartUpStatus = RCC_WaitForHSEStartUp();
  38. if(HSEStartUpStatus == SUCCESS)
  39. {
  40. /* HCLK = SYSCLK */
  41. RCC_HCLKConfig(RCC_SYSCLK_Div1);
  42. /* PCLK2 = HCLK */
  43. RCC_PCLK2Config(RCC_HCLK_Div1);
  44. /* PCLK1 = HCLK/2 */
  45. RCC_PCLK1Config(RCC_HCLK_Div2);
  46. /* Flash 2 wait state */
  47. FLASH_SetLatency(FLASH_Latency_2);
  48. /* Enable Prefetch Buffer */
  49. FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  50. /* PLLCLK = 8MHz * 9 = 72 MHz */
  51. RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
  52. /* Enable PLL */
  53. RCC_PLLCmd(ENABLE);
  54. /* Wait till PLL is ready */
  55. while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) ;
  56. /* Select PLL as system clock source */
  57. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  58. /* Wait till PLL is used as system clock source */
  59. while(RCC_GetSYSCLKSource() != 0x08) ;
  60. }
  61. }
  62. /*******************************************************************************
  63. * Function Name : NVIC_Configuration
  64. * Description : Configures Vector Table base location.
  65. * Input : None
  66. * Output : None
  67. * Return : None
  68. *******************************************************************************/
  69. void NVIC_Configuration(void)
  70. {
  71. #ifdef VECT_TAB_RAM
  72. /* Set the Vector Table base location at 0x20000000 */
  73. NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  74. #else /* VECT_TAB_FLASH */
  75. /* Set the Vector Table base location at 0x08000000 */
  76. NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  77. #endif
  78. }
  79. /*******************************************************************************
  80. * Function Name : SysTick_Configuration
  81. * Description : Configures the SysTick for OS tick.
  82. * Input : None
  83. * Output : None
  84. * Return : None
  85. *******************************************************************************/
  86. void SysTick_Configuration(void)
  87. {
  88. RCC_ClocksTypeDef rcc_clocks;
  89. rt_uint32_t cnts;
  90. RCC_GetClocksFreq(&rcc_clocks);
  91. cnts = (rt_uint32_t)rcc_clocks.HCLK_Frequency / RT_TICK_PER_SECOND;
  92. SysTick_SetReload(cnts);
  93. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  94. SysTick_CounterCmd(SysTick_Counter_Enable);
  95. SysTick_ITConfig(ENABLE);
  96. }
  97. extern void rt_hw_interrupt_thread_switch(void);
  98. /**
  99. * This is the timer interrupt service routine.
  100. *
  101. */
  102. void rt_hw_timer_handler(void)
  103. {
  104. /* enter interrupt */
  105. rt_interrupt_enter();
  106. rt_tick_increase();
  107. /* leave interrupt */
  108. rt_interrupt_leave();
  109. rt_hw_interrupt_thread_switch();
  110. }
  111. /**
  112. * This function will initial STM32 board.
  113. */
  114. void rt_hw_board_init()
  115. {
  116. /* Configure the system clocks */
  117. RCC_Configuration();
  118. /* NVIC Configuration */
  119. NVIC_Configuration();
  120. /* Configure the SysTick */
  121. SysTick_Configuration();
  122. rt_hw_console_init();
  123. }
  124. /* init console to support rt_kprintf */
  125. static void rt_hw_console_init()
  126. {
  127. /* Enable USART1 and GPIOA clocks */
  128. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  129. /* GPIO configuration */
  130. {
  131. GPIO_InitTypeDef GPIO_InitStructure;
  132. /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  133. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  134. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  135. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  136. GPIO_Init(GPIOA, &GPIO_InitStructure);
  137. /* Configure USART1 Rx (PA.10) as input floating */
  138. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  139. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  140. GPIO_Init(GPIOA, &GPIO_InitStructure);
  141. }
  142. /* USART configuration */
  143. {
  144. USART_InitTypeDef USART_InitStructure;
  145. /* USART1 configured as follow:
  146. - BaudRate = 115200 baud
  147. - Word Length = 8 Bits
  148. - One Stop Bit
  149. - No parity
  150. - Hardware flow control disabled (RTS and CTS signals)
  151. - Receive and transmit enabled
  152. - USART Clock disabled
  153. - USART CPOL: Clock is active low
  154. - USART CPHA: Data is captured on the middle
  155. - USART LastBit: The clock pulse of the last data bit is not output to
  156. the SCLK pin
  157. */
  158. USART_InitStructure.USART_BaudRate = 115200;
  159. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  160. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  161. USART_InitStructure.USART_Parity = USART_Parity_No;
  162. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  163. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  164. USART_Init(USART1, &USART_InitStructure);
  165. /* Enable USART1 */
  166. USART_Cmd(USART1, ENABLE);
  167. }
  168. }
  169. /* write one character to serial, must not trigger interrupt */
  170. static void rt_hw_console_putc(const char c)
  171. {
  172. /*
  173. to be polite with serial console add a line feed
  174. to the carriage return character
  175. */
  176. if (c=='\n')rt_hw_console_putc('\r');
  177. while (!(USART1->SR & USART_FLAG_TXE));
  178. USART1->DR = (c & 0x1FF);
  179. }
  180. /**
  181. * This function is used by rt_kprintf to display a string on console.
  182. *
  183. * @param str the displayed string
  184. */
  185. void rt_hw_console_output(const char* str)
  186. {
  187. while (*str)
  188. {
  189. rt_hw_console_putc (*str++);
  190. }
  191. }
  192. /*@}*/