board.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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://www.rt-thread.org/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.h"
  17. #include "board.h"
  18. static void rt_hw_console_init(void);
  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. #ifdef VECT_TAB_RAM
  33. /* Set the Vector Table base location at 0x20000000 */
  34. NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  35. #else /* VECT_TAB_FLASH */
  36. /* Set the Vector Table base location at 0x08000000 */
  37. NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  38. #endif
  39. }
  40. /*******************************************************************************
  41. * Function Name : SysTick_Configuration
  42. * Description : Configures the SysTick for OS tick.
  43. * Input : None
  44. * Output : None
  45. * Return : None
  46. *******************************************************************************/
  47. void SysTick_Configuration(void)
  48. {
  49. RCC_ClocksTypeDef rcc_clocks;
  50. rt_uint32_t cnts;
  51. RCC_GetClocksFreq(&rcc_clocks);
  52. cnts = (rt_uint32_t)rcc_clocks.HCLK_Frequency / RT_TICK_PER_SECOND;
  53. SysTick_Config(cnts);
  54. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  55. }
  56. /**
  57. * This is the timer interrupt service routine.
  58. *
  59. */
  60. void rt_hw_timer_handler(void)
  61. {
  62. /* enter interrupt */
  63. rt_interrupt_enter();
  64. rt_tick_increase();
  65. /* leave interrupt */
  66. rt_interrupt_leave();
  67. }
  68. /**
  69. * This function will initial STM32 board.
  70. */
  71. void rt_hw_board_init()
  72. {
  73. /* NVIC Configuration */
  74. NVIC_Configuration();
  75. /* Configure the SysTick */
  76. SysTick_Configuration();
  77. rt_hw_console_init();
  78. }
  79. #if STM32_CONSOLE_USART == 1
  80. #define CONSOLE_TX_PIN GPIO_Pin_9
  81. #define CONSOLE_RX_PIN GPIO_Pin_10
  82. #define CONSOLE_GPIO GPIOA
  83. #define CONSOLE_USART USART1
  84. #define CONSOLE_RCC RCC_APB2Periph_USART1
  85. #define CONSOLE_RCC_GPIO RCC_APB2Periph_GPIOA
  86. #elif STM32_CONSOLE_USART == 2
  87. #if defined(STM32F10X_LD) || defined(STM32F10X_MD) || defined(STM32F10X_CL)
  88. #define CONSOLE_TX_PIN GPIO_Pin_5
  89. #define CONSOLE_RX_PIN GPIO_Pin_6
  90. #define CONSOLE_GPIO GPIOD
  91. #define CONSOLE_RCC RCC_APB1Periph_USART2
  92. #define CONSOLE_RCC_GPIO RCC_APB2Periph_GPIOD
  93. #elif defined(STM32F10X_HD)
  94. #define CONSOLE_TX_PIN GPIO_Pin_2
  95. #define CONSOLE_RX_PIN GPIO_Pin_3
  96. #define CONSOLE_GPIO GPIOA
  97. #define CONSOLE_RCC RCC_APB1Periph_USART2
  98. #define CONSOLE_RCC_GPIO RCC_APB2Periph_GPIOA
  99. #endif
  100. #define CONSOLE_USART USART2
  101. #elif STM32_CONSOLE_USART == 2
  102. #define CONSOLE_RX_PIN GPIO_Pin_11
  103. #define CONSOLE_TX_PIN GPIO_Pin_10
  104. #define CONSOLE_GPIO GPIOB
  105. #define CONSOLE_USART USART3
  106. #define CONSOLE_RCC RCC_APB1Periph_USART3
  107. #define CONSOLE_RCC_GPIO RCC_APB2Periph_GPIOB
  108. #endif
  109. /* init console to support rt_kprintf */
  110. static void rt_hw_console_init()
  111. {
  112. #if STM32_CONSOLE_USART == 0
  113. #else
  114. /* Enable GPIOx clock */
  115. RCC_APB2PeriphClockCmd(CONSOLE_RCC_GPIO, ENABLE);
  116. #if STM32_CONSOLE_USART == 1
  117. /* Enable USART1 and GPIOA clocks */
  118. RCC_APB2PeriphClockCmd(CONSOLE_RCC, ENABLE);
  119. #else
  120. RCC_APB1PeriphClockCmd(CONSOLE_RCC, ENABLE);
  121. #endif
  122. #if (STM32_CONSOLE_USART == 2) && (defined(STM32F10X_LD) || defined(STM32F10X_MD) || defined(STM32F10X_CL))
  123. /* Enable AFIO clock */
  124. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  125. /* Enable the USART2 Pins Software Remapping */
  126. GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
  127. #endif
  128. /* GPIO configuration */
  129. {
  130. GPIO_InitTypeDef GPIO_InitStructure;
  131. /* Configure USART Tx as alternate function push-pull */
  132. GPIO_InitStructure.GPIO_Pin = CONSOLE_TX_PIN;
  133. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  134. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  135. GPIO_Init(CONSOLE_GPIO, &GPIO_InitStructure);
  136. /* Configure USART Rx as input floating */
  137. GPIO_InitStructure.GPIO_Pin = CONSOLE_RX_PIN;
  138. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  139. GPIO_Init(CONSOLE_GPIO, &GPIO_InitStructure);
  140. }
  141. /* USART configuration */
  142. {
  143. USART_InitTypeDef USART_InitStructure;
  144. /* USART configured as follow:
  145. - BaudRate = 115200 baud
  146. - Word Length = 8 Bits
  147. - One Stop Bit
  148. - No parity
  149. - Hardware flow control disabled (RTS and CTS signals)
  150. - Receive and transmit enabled
  151. - USART Clock disabled
  152. - USART CPOL: Clock is active low
  153. - USART CPHA: Data is captured on the middle
  154. - USART LastBit: The clock pulse of the last data bit is not output to
  155. the SCLK pin
  156. */
  157. USART_InitStructure.USART_BaudRate = 115200;
  158. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  159. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  160. USART_InitStructure.USART_Parity = USART_Parity_No;
  161. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  162. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  163. USART_Init(CONSOLE_USART, &USART_InitStructure);
  164. /* Enable USART */
  165. USART_Cmd(CONSOLE_USART, ENABLE);
  166. }
  167. #endif
  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 (!(CONSOLE_USART->SR & USART_FLAG_TXE));
  178. CONSOLE_USART->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. #if STM32_CONSOLE_USART == 0
  188. /* no console */
  189. #else
  190. while (*str)
  191. {
  192. rt_hw_console_putc (*str++);
  193. }
  194. #endif
  195. }
  196. /*@}*/