board.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. */
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #include "stm32f10x.h"
  17. #include "stm32f10x_fsmc.h"
  18. #include "board.h"
  19. static void rt_hw_console_init(void);
  20. /**
  21. * @addtogroup STM32
  22. */
  23. /*@{*/
  24. /*******************************************************************************
  25. * Function Name : NVIC_Configuration
  26. * Description : Configures Vector Table base location.
  27. * Input : None
  28. * Output : None
  29. * Return : None
  30. *******************************************************************************/
  31. void NVIC_Configuration(void)
  32. {
  33. #ifdef VECT_TAB_RAM
  34. /* Set the Vector Table base location at 0x20000000 */
  35. NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  36. #else /* VECT_TAB_FLASH */
  37. /* Set the Vector Table base location at 0x08000000 */
  38. NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  39. #endif
  40. }
  41. /*******************************************************************************
  42. * Function Name : SysTick_Configuration
  43. * Description : Configures the SysTick for OS tick.
  44. * Input : None
  45. * Output : None
  46. * Return : None
  47. *******************************************************************************/
  48. void SysTick_Configuration(void)
  49. {
  50. RCC_ClocksTypeDef rcc_clocks;
  51. rt_uint32_t cnts;
  52. RCC_GetClocksFreq(&rcc_clocks);
  53. cnts = (rt_uint32_t)rcc_clocks.HCLK_Frequency / RT_TICK_PER_SECOND;
  54. SysTick_Config(cnts);
  55. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  56. }
  57. #if STM32_EXT_SRAM
  58. #define Bank1_SRAM3_ADDR ((u32)0x68000000)
  59. void EXT_SRAM_Configuration(void)
  60. {
  61. FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
  62. FSMC_NORSRAMTimingInitTypeDef p;
  63. GPIO_InitTypeDef GPIO_InitStructure;
  64. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
  65. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOG | RCC_APB2Periph_GPIOE |
  66. RCC_APB2Periph_GPIOF, ENABLE);
  67. /*-- GPIO Configuration ------------------------------------------------------*/
  68. /* SRAM Data lines configuration */
  69. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 | GPIO_Pin_9 |
  70. GPIO_Pin_10 | GPIO_Pin_14 | GPIO_Pin_15;
  71. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  72. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  73. GPIO_Init(GPIOD, &GPIO_InitStructure);
  74. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
  75. GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
  76. GPIO_Pin_15;
  77. GPIO_Init(GPIOE, &GPIO_InitStructure);
  78. /* SRAM Address lines configuration */
  79. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
  80. GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13 |
  81. GPIO_Pin_14 | GPIO_Pin_15;
  82. GPIO_Init(GPIOF, &GPIO_InitStructure);
  83. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
  84. GPIO_Pin_4 | GPIO_Pin_5;
  85. GPIO_Init(GPIOG, &GPIO_InitStructure);
  86. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
  87. GPIO_Init(GPIOD, &GPIO_InitStructure);
  88. /* NOE and NWE configuration */
  89. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 |GPIO_Pin_5;
  90. GPIO_Init(GPIOD, &GPIO_InitStructure);
  91. /* NE3 NE4 configuration */
  92. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_12;
  93. GPIO_Init(GPIOG, &GPIO_InitStructure);
  94. /* NBL0, NBL1 configuration */
  95. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  96. GPIO_Init(GPIOE, &GPIO_InitStructure);
  97. /*-- FSMC Configuration ------------------------------------------------------*/
  98. p.FSMC_AddressSetupTime = 0;
  99. p.FSMC_AddressHoldTime = 0;
  100. p.FSMC_DataSetupTime = 2;
  101. p.FSMC_BusTurnAroundDuration = 0;
  102. p.FSMC_CLKDivision = 0;
  103. p.FSMC_DataLatency = 0;
  104. p.FSMC_AccessMode = FSMC_AccessMode_A;
  105. FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM3;
  106. FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  107. FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  108. FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  109. FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  110. FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  111. FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  112. FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  113. FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  114. FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  115. FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  116. FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  117. FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
  118. FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
  119. FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
  120. /* Enable FSMC Bank1_SRAM Bank */
  121. FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE);
  122. }
  123. #endif
  124. /**
  125. * This is the timer interrupt service routine.
  126. *
  127. */
  128. void rt_hw_timer_handler(void)
  129. {
  130. /* enter interrupt */
  131. rt_interrupt_enter();
  132. rt_tick_increase();
  133. /* leave interrupt */
  134. rt_interrupt_leave();
  135. }
  136. /**
  137. * This function will initial STM32 board.
  138. */
  139. void rt_hw_board_init()
  140. {
  141. /* NVIC Configuration */
  142. NVIC_Configuration();
  143. /* Configure the SysTick */
  144. SysTick_Configuration();
  145. #if STM32_EXT_SRAM
  146. EXT_SRAM_Configuration();
  147. #endif
  148. rt_hw_console_init();
  149. }
  150. #if STM32_CONSOLE_USART == 1
  151. #define CONSOLE_TX_PIN GPIO_Pin_9
  152. #define CONSOLE_RX_PIN GPIO_Pin_10
  153. #define CONSOLE_GPIO GPIOA
  154. #define CONSOLE_USART USART1
  155. #define CONSOLE_RCC RCC_APB2Periph_USART1
  156. #define CONSOLE_RCC_GPIO RCC_APB2Periph_GPIOA
  157. #elif STM32_CONSOLE_USART == 2
  158. #if defined(STM32F10X_LD) || defined(STM32F10X_MD) || defined(STM32F10X_CL)
  159. #define CONSOLE_TX_PIN GPIO_Pin_5
  160. #define CONSOLE_RX_PIN GPIO_Pin_6
  161. #define CONSOLE_GPIO GPIOD
  162. #define CONSOLE_RCC RCC_APB1Periph_USART2
  163. #define CONSOLE_RCC_GPIO RCC_APB2Periph_GPIOD
  164. #elif defined(STM32F10X_HD)
  165. #define CONSOLE_TX_PIN GPIO_Pin_2
  166. #define CONSOLE_RX_PIN GPIO_Pin_3
  167. #define CONSOLE_GPIO GPIOA
  168. #define CONSOLE_RCC RCC_APB1Periph_USART2
  169. #define CONSOLE_RCC_GPIO RCC_APB2Periph_GPIOA
  170. #endif
  171. #define CONSOLE_USART USART2
  172. #elif STM32_CONSOLE_USART == 2
  173. #define CONSOLE_RX_PIN GPIO_Pin_11
  174. #define CONSOLE_TX_PIN GPIO_Pin_10
  175. #define CONSOLE_GPIO GPIOB
  176. #define CONSOLE_USART USART3
  177. #define CONSOLE_RCC RCC_APB1Periph_USART3
  178. #define CONSOLE_RCC_GPIO RCC_APB2Periph_GPIOB
  179. #endif
  180. /* init console to support rt_kprintf */
  181. static void rt_hw_console_init()
  182. {
  183. #if STM32_CONSOLE_USART == 0
  184. #else
  185. /* Enable GPIOx clock */
  186. RCC_APB2PeriphClockCmd(CONSOLE_RCC_GPIO, ENABLE);
  187. #if STM32_CONSOLE_USART == 1
  188. /* Enable USART1 and GPIOA clocks */
  189. RCC_APB2PeriphClockCmd(CONSOLE_RCC, ENABLE);
  190. #else
  191. RCC_APB1PeriphClockCmd(CONSOLE_RCC, ENABLE);
  192. #endif
  193. #if (STM32_CONSOLE_USART == 2) && (defined(STM32F10X_LD) || defined(STM32F10X_MD) || defined(STM32F10X_CL))
  194. /* Enable AFIO clock */
  195. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  196. /* Enable the USART2 Pins Software Remapping */
  197. GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
  198. #endif
  199. /* GPIO configuration */
  200. {
  201. GPIO_InitTypeDef GPIO_InitStructure;
  202. /* Configure USART Tx as alternate function push-pull */
  203. GPIO_InitStructure.GPIO_Pin = CONSOLE_TX_PIN;
  204. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  205. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  206. GPIO_Init(CONSOLE_GPIO, &GPIO_InitStructure);
  207. /* Configure USART Rx as input floating */
  208. GPIO_InitStructure.GPIO_Pin = CONSOLE_RX_PIN;
  209. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  210. GPIO_Init(CONSOLE_GPIO, &GPIO_InitStructure);
  211. }
  212. /* USART configuration */
  213. {
  214. USART_InitTypeDef USART_InitStructure;
  215. /* USART configured as follow:
  216. - BaudRate = 115200 baud
  217. - Word Length = 8 Bits
  218. - One Stop Bit
  219. - No parity
  220. - Hardware flow control disabled (RTS and CTS signals)
  221. - Receive and transmit enabled
  222. - USART Clock disabled
  223. - USART CPOL: Clock is active low
  224. - USART CPHA: Data is captured on the middle
  225. - USART LastBit: The clock pulse of the last data bit is not output to
  226. the SCLK pin
  227. */
  228. USART_InitStructure.USART_BaudRate = 115200;
  229. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  230. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  231. USART_InitStructure.USART_Parity = USART_Parity_No;
  232. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  233. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  234. USART_Init(CONSOLE_USART, &USART_InitStructure);
  235. /* Enable USART */
  236. USART_Cmd(CONSOLE_USART, ENABLE);
  237. }
  238. #endif
  239. }
  240. /* write one character to serial, must not trigger interrupt */
  241. static void rt_hw_console_putc(const char c)
  242. {
  243. /*
  244. to be polite with serial console add a line feed
  245. to the carriage return character
  246. */
  247. if (c=='\n')rt_hw_console_putc('\r');
  248. while (!(CONSOLE_USART->SR & USART_FLAG_TXE));
  249. CONSOLE_USART->DR = (c & 0x1FF);
  250. }
  251. /**
  252. * This function is used by rt_kprintf to display a string on console.
  253. *
  254. * @param str the displayed string
  255. */
  256. void rt_hw_console_output(const char* str)
  257. {
  258. #if STM32_CONSOLE_USART == 0
  259. /* no console */
  260. #else
  261. while (*str)
  262. {
  263. rt_hw_console_putc (*str++);
  264. }
  265. #endif
  266. }
  267. /*@}*/