board.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. * set priority group:
  41. * 2 bits for pre-emption priority
  42. * 2 bits for subpriority
  43. */
  44. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  45. }
  46. extern void rt_hw_interrupt_thread_switch(void);
  47. /**
  48. * This is the timer interrupt service routine.
  49. *
  50. */
  51. void rt_hw_timer_handler(void)
  52. {
  53. /* enter interrupt */
  54. rt_interrupt_enter();
  55. rt_tick_increase();
  56. /* leave interrupt */
  57. rt_interrupt_leave();
  58. }
  59. /* NAND Flash */
  60. #include "fsmc_nand.h"
  61. static void all_device_reset(void)
  62. {
  63. /* RESET */
  64. /* DM9000A PE5 */
  65. /* LCD PF10 */
  66. /* SPI-FLASH PA3 */
  67. /* CS */
  68. /* DM9000A FSMC_NE4 PG12 */
  69. /* LCD FSMC_NE2 PG9 */
  70. /* SPI_FLASH PA4 */
  71. /* CODEC PC5 */
  72. /* TOUCH PC4 */
  73. GPIO_InitTypeDef GPIO_InitStructure;
  74. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOE
  75. | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG,ENABLE);
  76. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  77. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  78. /* SPI_FLASH CS */
  79. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  80. GPIO_Init(GPIOA,&GPIO_InitStructure);
  81. GPIO_SetBits(GPIOA,GPIO_Pin_4);
  82. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  83. /* CODEC && TOUCH CS */
  84. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
  85. GPIO_Init(GPIOC,&GPIO_InitStructure);
  86. GPIO_SetBits(GPIOC,GPIO_Pin_4 | GPIO_Pin_5);
  87. /* DM9000A RESET */
  88. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  89. GPIO_Init(GPIOE,&GPIO_InitStructure);
  90. GPIO_ResetBits(GPIOE,GPIO_Pin_5);
  91. /* LCD RESET */
  92. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  93. GPIO_Init(GPIOF,&GPIO_InitStructure);
  94. GPIO_ResetBits(GPIOF,GPIO_Pin_10);
  95. /* SPI_FLASH RESET */
  96. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  97. GPIO_Init(GPIOA,&GPIO_InitStructure);
  98. GPIO_ResetBits(GPIOA,GPIO_Pin_3);
  99. /* FSMC GPIO configure */
  100. {
  101. GPIO_InitTypeDef GPIO_InitStructure;
  102. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF
  103. | RCC_APB2Periph_GPIOG, ENABLE);
  104. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
  105. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  106. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  107. /*
  108. FSMC_D0 ~ FSMC_D3
  109. PD14 FSMC_D0 PD15 FSMC_D1 PD0 FSMC_D2 PD1 FSMC_D3
  110. */
  111. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_14 | GPIO_Pin_15;
  112. GPIO_Init(GPIOD,&GPIO_InitStructure);
  113. /*
  114. FSMC_D4 ~ FSMC_D12
  115. PE7 ~ PE15 FSMC_D4 ~ FSMC_D12
  116. */
  117. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10
  118. | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  119. GPIO_Init(GPIOE,&GPIO_InitStructure);
  120. /* FSMC_D13 ~ FSMC_D15 PD8 ~ PD10 */
  121. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
  122. GPIO_Init(GPIOD,&GPIO_InitStructure);
  123. /*
  124. FSMC_A0 ~ FSMC_A5 FSMC_A6 ~ FSMC_A9
  125. PF0 ~ PF5 PF12 ~ PF15
  126. */
  127. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3
  128. | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  129. GPIO_Init(GPIOF,&GPIO_InitStructure);
  130. /* FSMC_A10 ~ FSMC_A15 PG0 ~ PG5 */
  131. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
  132. GPIO_Init(GPIOG,&GPIO_InitStructure);
  133. /* FSMC_A16 ~ FSMC_A18 PD11 ~ PD13 */
  134. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
  135. GPIO_Init(GPIOD,&GPIO_InitStructure);
  136. /* RD-PD4 WR-PD5 */
  137. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
  138. GPIO_Init(GPIOD,&GPIO_InitStructure);
  139. /* NBL0-PE0 NBL1-PE1 */
  140. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  141. GPIO_Init(GPIOE,&GPIO_InitStructure);
  142. /* NE1/NCE2 */
  143. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  144. GPIO_Init(GPIOD,&GPIO_InitStructure);
  145. /* NE2 */
  146. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  147. GPIO_Init(GPIOG,&GPIO_InitStructure);
  148. /* NE3 */
  149. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  150. GPIO_Init(GPIOG,&GPIO_InitStructure);
  151. /* NE4 */
  152. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  153. GPIO_Init(GPIOG,&GPIO_InitStructure);
  154. }
  155. /* FSMC GPIO configure */
  156. GPIO_SetBits(GPIOE,GPIO_Pin_5); /* DM9000A */
  157. GPIO_SetBits(GPIOF,GPIO_Pin_10); /* LCD */
  158. GPIO_SetBits(GPIOA,GPIO_Pin_3); /* SPI_FLASH */
  159. }
  160. /**
  161. * This function will initial STM32 Radio board.
  162. */
  163. extern void FSMC_SRAM_Init(void);
  164. void rt_hw_board_init()
  165. {
  166. //NAND_IDTypeDef NAND_ID;
  167. /* Configure the system clocks */
  168. SystemInit();
  169. all_device_reset();
  170. /* NVIC Configuration */
  171. NVIC_Configuration();
  172. /* Configure the SysTick */
  173. SysTick_Config( SystemFrequency_SysClk / RT_TICK_PER_SECOND );
  174. /* Console Initialization*/
  175. rt_hw_console_init();
  176. rt_kprintf("\r\n\r\nSystemInit......\r\n");
  177. /* FSMC Initialization */
  178. //FSMC_NAND_Init();
  179. /* NAND read ID command */
  180. //FSMC_NAND_ReadID(&NAND_ID);
  181. //rt_kprintf("Read the NAND ID:%02X%02X%02X%02X",NAND_ID.Maker_ID,NAND_ID.Device_ID,NAND_ID.Third_ID,NAND_ID.Fourth_ID);
  182. /* SRAM init */
  183. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
  184. FSMC_SRAM_Init();
  185. /* memtest */
  186. {
  187. unsigned char * p_extram = (unsigned char *)0x68000000;
  188. unsigned int temp;
  189. rt_kprintf("\r\nmem testing....");
  190. for(temp=0; temp<0x80000; temp++)
  191. {
  192. *p_extram++ = (unsigned char)temp;
  193. }
  194. p_extram = (unsigned char *)0x68000000;
  195. for(temp=0; temp<0x80000; temp++)
  196. {
  197. if( *p_extram++ != (unsigned char)temp )
  198. {
  199. rt_kprintf("\rmemtest fail @ %08X\r\nsystem halt!!!!!",(unsigned int)p_extram);
  200. while(1);
  201. }
  202. }
  203. rt_kprintf("\rmem test pass!!\r\n");
  204. }/* memtest */
  205. {
  206. /* PC6 for SDCard Rst */
  207. GPIO_InitTypeDef GPIO_InitStructure;
  208. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  209. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  210. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  211. GPIO_Init(GPIOC,&GPIO_InitStructure);
  212. GPIO_SetBits(GPIOC,GPIO_Pin_6);
  213. }
  214. /* SPI1 config */
  215. {
  216. GPIO_InitTypeDef GPIO_InitStructure;
  217. SPI_InitTypeDef SPI_InitStructure;
  218. /* Enable SPI1 Periph clock */
  219. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
  220. | RCC_APB2Periph_AFIO | RCC_APB2Periph_SPI1,
  221. ENABLE);
  222. /* Configure SPI1 pins: PA5-SCK, PA6-MISO and PA7-MOSI */
  223. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  224. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  225. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  226. GPIO_Init(GPIOA, &GPIO_InitStructure);
  227. /*------------------------ SPI1 configuration ------------------------*/
  228. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI_Direction_1Line_Tx;
  229. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  230. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  231. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  232. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  233. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  234. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;/* 72M/64=1.125M */
  235. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  236. SPI_InitStructure.SPI_CRCPolynomial = 7;
  237. SPI_I2S_DeInit(SPI1);
  238. SPI_Init(SPI1, &SPI_InitStructure);
  239. /* Enable SPI_MASTER */
  240. SPI_Cmd(SPI1, ENABLE);
  241. SPI_CalculateCRC(SPI1, DISABLE);
  242. }
  243. }/* rt_hw_board_init */
  244. #if STM32_CONSOLE_USART == 1
  245. #define CONSOLE_RX_PIN GPIO_Pin_9
  246. #define CONSOLE_TX_PIN GPIO_Pin_10
  247. #define CONSOLE_GPIO GPIOA
  248. #define CONSOLE_USART USART1
  249. #elif STM32_CONSOLE_USART == 2
  250. #if defined(STM32_LD) || defined(STM32_MD)
  251. #define CONSOLE_RX_PIN GPIO_Pin_6
  252. #define CONSOLE_TX_PIN GPIO_Pin_5
  253. #define CONSOLE_GPIO GPIOD
  254. #elif defined(STM32_HD)
  255. #define CONSOLE_RX_PIN GPIO_Pin_3
  256. #define CONSOLE_TX_PIN GPIO_Pin_2
  257. #define CONSOLE_GPIO GPIOA
  258. #endif
  259. #define CONSOLE_USART USART2
  260. #elif STM32_CONSOLE_USART == 2
  261. #define CONSOLE_RX_PIN GPIO_Pin_11
  262. #define CONSOLE_TX_PIN GPIO_Pin_10
  263. #define CONSOLE_GPIO GPIOB
  264. #define CONSOLE_USART USART3
  265. #endif
  266. /* init console to support rt_kprintf */
  267. static void rt_hw_console_init(void)
  268. {
  269. /* Enable USART1 and GPIOA clocks */
  270. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1
  271. | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC
  272. | RCC_APB2Periph_GPIOF, ENABLE);
  273. #if STM32_CONSOLE_USART == 0
  274. #else
  275. /* GPIO configuration */
  276. {
  277. GPIO_InitTypeDef GPIO_InitStructure;
  278. /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  279. GPIO_InitStructure.GPIO_Pin = CONSOLE_RX_PIN;
  280. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  281. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  282. GPIO_Init(CONSOLE_GPIO, &GPIO_InitStructure);
  283. /* Configure USART1 Rx (PA.10) as input floating */
  284. GPIO_InitStructure.GPIO_Pin = CONSOLE_TX_PIN;
  285. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  286. GPIO_Init(CONSOLE_GPIO, &GPIO_InitStructure);
  287. }
  288. /* USART configuration */
  289. {
  290. USART_InitTypeDef USART_InitStructure;
  291. /* USART configured as follow:
  292. - BaudRate = 115200 baud
  293. - Word Length = 8 Bits
  294. - One Stop Bit
  295. - No parity
  296. - Hardware flow control disabled (RTS and CTS signals)
  297. - Receive and transmit enabled
  298. - USART Clock disabled
  299. - USART CPOL: Clock is active low
  300. - USART CPHA: Data is captured on the middle
  301. - USART LastBit: The clock pulse of the last data bit is not output to
  302. the SCLK pin
  303. */
  304. USART_InitStructure.USART_BaudRate = 115200;
  305. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  306. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  307. USART_InitStructure.USART_Parity = USART_Parity_No;
  308. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  309. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  310. USART_Init(CONSOLE_USART, &USART_InitStructure);
  311. /* Enable USART1 */
  312. USART_Cmd(CONSOLE_USART, ENABLE);
  313. }
  314. #endif
  315. }
  316. /* write one character to serial, must not trigger interrupt */
  317. static void rt_hw_console_putc(const char c)
  318. {
  319. /*
  320. to be polite with serial console add a line feed
  321. to the carriage return character
  322. */
  323. if (c=='\n')rt_hw_console_putc('\r');
  324. while (!(CONSOLE_USART->SR & USART_FLAG_TXE));
  325. CONSOLE_USART->DR = (c & 0x1FF);
  326. }
  327. /**
  328. * This function is used by rt_kprintf to display a string on console.
  329. *
  330. * @param str the displayed string
  331. */
  332. void rt_hw_console_output(const char* str)
  333. {
  334. #if STM32_CONSOLE_USART == 0
  335. /* no console */
  336. #else
  337. while (*str)
  338. {
  339. rt_hw_console_putc (*str++);
  340. }
  341. #endif
  342. }
  343. /*@}*/