board.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. * Function Name : FSMC_SRAM_Init
  113. * Description : Configures the FSMC and GPIOs to interface with the SRAM memory.
  114. * This function must be called before any write/read operation
  115. * on the SRAM.
  116. * Input : None
  117. * Output : None
  118. * Return : None
  119. *******************************************************************************/
  120. void FSMC_SRAM_Init(void)
  121. {
  122. FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
  123. FSMC_NORSRAMTimingInitTypeDef p;
  124. GPIO_InitTypeDef GPIO_InitStructure;
  125. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOG | RCC_APB2Periph_GPIOE |
  126. RCC_APB2Periph_GPIOF, ENABLE);
  127. /*-- GPIO Configuration ------------------------------------------------------*/
  128. /* SRAM Data lines configuration */
  129. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 | GPIO_Pin_9 |
  130. GPIO_Pin_10 | GPIO_Pin_14 | GPIO_Pin_15;
  131. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  132. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  133. GPIO_Init(GPIOD, &GPIO_InitStructure);
  134. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
  135. GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
  136. GPIO_Pin_15;
  137. GPIO_Init(GPIOE, &GPIO_InitStructure);
  138. /* SRAM Address lines configuration */
  139. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
  140. GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13 |
  141. GPIO_Pin_14 | GPIO_Pin_15;
  142. GPIO_Init(GPIOF, &GPIO_InitStructure);
  143. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
  144. GPIO_Pin_4 | GPIO_Pin_5;
  145. GPIO_Init(GPIOG, &GPIO_InitStructure);
  146. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
  147. GPIO_Init(GPIOD, &GPIO_InitStructure);
  148. /* NOE and NWE configuration */
  149. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 |GPIO_Pin_5;
  150. GPIO_Init(GPIOD, &GPIO_InitStructure);
  151. /* NE3 configuration */
  152. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  153. GPIO_Init(GPIOG, &GPIO_InitStructure);
  154. /* NBL0, NBL1 configuration */
  155. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  156. GPIO_Init(GPIOE, &GPIO_InitStructure);
  157. /*-- FSMC Configuration ------------------------------------------------------*/
  158. p.FSMC_AddressSetupTime = 0;
  159. p.FSMC_AddressHoldTime = 0;
  160. p.FSMC_DataSetupTime = 2;
  161. p.FSMC_BusTurnAroundDuration = 0;
  162. p.FSMC_CLKDivision = 0;
  163. p.FSMC_DataLatency = 0;
  164. p.FSMC_AccessMode = FSMC_AccessMode_A;
  165. FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM3;
  166. FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  167. FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  168. FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  169. FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  170. FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  171. FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  172. FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  173. FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  174. FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  175. FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  176. FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  177. FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
  178. FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
  179. FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
  180. /* Enable FSMC Bank1_SRAM Bank */
  181. FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE);
  182. }
  183. /*******************************************************************************
  184. * Function Name : FSMC_NOR_Init
  185. * Description : Configures the FSMC and GPIOs to interface with the NOR memory.
  186. * This function must be called before any write/read operation
  187. * on the NOR.
  188. * Input : None
  189. * Output : None
  190. * Return : None
  191. *******************************************************************************/
  192. void FSMC_NOR_Init(void)
  193. {
  194. FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
  195. FSMC_NORSRAMTimingInitTypeDef p;
  196. GPIO_InitTypeDef GPIO_InitStructure;
  197. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
  198. RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE);
  199. /*-- GPIO Configuration ------------------------------------------------------*/
  200. /* NOR Data lines configuration */
  201. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 | GPIO_Pin_9 |
  202. GPIO_Pin_10 | GPIO_Pin_14 | GPIO_Pin_15;
  203. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  204. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  205. GPIO_Init(GPIOD, &GPIO_InitStructure);
  206. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
  207. GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 |
  208. GPIO_Pin_14 | GPIO_Pin_15;
  209. GPIO_Init(GPIOE, &GPIO_InitStructure);
  210. /* NOR Address lines configuration */
  211. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
  212. GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13 |
  213. GPIO_Pin_14 | GPIO_Pin_15;
  214. GPIO_Init(GPIOF, &GPIO_InitStructure);
  215. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 |
  216. GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
  217. GPIO_Init(GPIOG, &GPIO_InitStructure);
  218. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
  219. GPIO_Init(GPIOD, &GPIO_InitStructure);
  220. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6;
  221. GPIO_Init(GPIOE, &GPIO_InitStructure);
  222. /* NOE and NWE configuration */
  223. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
  224. GPIO_Init(GPIOD, &GPIO_InitStructure);
  225. /* NE2 configuration */
  226. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  227. GPIO_Init(GPIOG, &GPIO_InitStructure);
  228. /*-- FSMC Configuration ----------------------------------------------------*/
  229. p.FSMC_AddressSetupTime = 0x03;
  230. p.FSMC_AddressHoldTime = 0x00;
  231. p.FSMC_DataSetupTime = 0x04;
  232. p.FSMC_BusTurnAroundDuration = 0x00;
  233. p.FSMC_CLKDivision = 0x00;
  234. p.FSMC_DataLatency = 0x00;
  235. p.FSMC_AccessMode = FSMC_AccessMode_B;
  236. FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;
  237. FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  238. FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR;
  239. FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  240. FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  241. FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  242. FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  243. FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  244. FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  245. FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  246. FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  247. FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  248. FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
  249. FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
  250. FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
  251. /* Enable FSMC Bank1_NOR Bank */
  252. FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE);
  253. }
  254. /**
  255. * This function will initial STM32 board.
  256. */
  257. void rt_hw_board_init()
  258. {
  259. /* Configure the system clocks */
  260. RCC_Configuration();
  261. /* NVIC Configuration */
  262. NVIC_Configuration();
  263. /* SRAM init */
  264. FSMC_SRAM_Init();
  265. /* Configure the SysTick */
  266. SysTick_Configuration();
  267. rt_hw_console_init();
  268. }
  269. /* init console to support rt_kprintf */
  270. static void rt_hw_console_init()
  271. {
  272. /* Enable USART1 and GPIOA clocks */
  273. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  274. /* GPIO configuration */
  275. {
  276. GPIO_InitTypeDef GPIO_InitStructure;
  277. /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  278. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  279. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  280. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  281. GPIO_Init(GPIOA, &GPIO_InitStructure);
  282. /* Configure USART1 Rx (PA.10) as input floating */
  283. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  284. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  285. GPIO_Init(GPIOA, &GPIO_InitStructure);
  286. }
  287. /* USART configuration */
  288. {
  289. USART_InitTypeDef USART_InitStructure;
  290. /* USART1 configured as follow:
  291. - BaudRate = 115200 baud
  292. - Word Length = 8 Bits
  293. - One Stop Bit
  294. - No parity
  295. - Hardware flow control disabled (RTS and CTS signals)
  296. - Receive and transmit enabled
  297. - USART Clock disabled
  298. - USART CPOL: Clock is active low
  299. - USART CPHA: Data is captured on the middle
  300. - USART LastBit: The clock pulse of the last data bit is not output to
  301. the SCLK pin
  302. */
  303. USART_InitStructure.USART_BaudRate = 115200;
  304. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  305. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  306. USART_InitStructure.USART_Parity = USART_Parity_No;
  307. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  308. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  309. USART_Init(USART1, &USART_InitStructure);
  310. /* Enable USART1 */
  311. USART_Cmd(USART1, ENABLE);
  312. }
  313. }
  314. /* write one character to serial, must not trigger interrupt */
  315. static void rt_hw_console_putc(const char c)
  316. {
  317. /*
  318. to be polite with serial console add a line feed
  319. to the carriage return character
  320. */
  321. if (c=='\n')rt_hw_console_putc('\r');
  322. while (!(USART1->SR & USART_FLAG_TXE));
  323. USART1->DR = (c & 0x1FF);
  324. }
  325. /**
  326. * This function is used by rt_kprintf to display a string on console.
  327. *
  328. * @param str the displayed string
  329. */
  330. void rt_hw_console_output(const char* str)
  331. {
  332. while (*str)
  333. {
  334. rt_hw_console_putc (*str++);
  335. }
  336. }
  337. /*@}*/