board.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. GPIO_SetBits(GPIOE,GPIO_Pin_5); /* DM9000A */
  100. GPIO_SetBits(GPIOF,GPIO_Pin_10); /* LCD */
  101. GPIO_SetBits(GPIOA,GPIO_Pin_3); /* SPI_FLASH */
  102. }
  103. /**
  104. * This function will initial STM32 Radio board.
  105. */
  106. extern void FSMC_SRAM_Init(void);
  107. void rt_hw_board_init()
  108. {
  109. NAND_IDTypeDef NAND_ID;
  110. /* Configure the system clocks */
  111. SystemInit();
  112. all_device_reset();
  113. /* NVIC Configuration */
  114. NVIC_Configuration();
  115. /* Configure the SysTick */
  116. SysTick_Config( SystemFrequency_SysClk / RT_TICK_PER_SECOND );
  117. /* Console Initialization*/
  118. rt_hw_console_init();
  119. rt_kprintf("\r\n\r\nSystemInit......\r\n");
  120. /* FSMC Initialization */
  121. FSMC_NAND_Init();
  122. /* NAND read ID command */
  123. FSMC_NAND_ReadID(&NAND_ID);
  124. 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);
  125. /* SRAM init */
  126. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
  127. FSMC_SRAM_Init();
  128. /* memtest */
  129. {
  130. unsigned char * p_extram = (unsigned char *)0x68000000;
  131. unsigned int temp;
  132. rt_kprintf("\r\nmem testing....");
  133. for(temp=0; temp<0x80000; temp++)
  134. {
  135. *p_extram++ = (unsigned char)temp;
  136. }
  137. p_extram = (unsigned char *)0x68000000;
  138. for(temp=0; temp<0x80000; temp++)
  139. {
  140. if( *p_extram++ != (unsigned char)temp )
  141. {
  142. rt_kprintf("\rmemtest fail @ %08X\r\nsystem halt!!!!!",(unsigned int)p_extram);
  143. while(1);
  144. }
  145. }
  146. rt_kprintf("\rmem test pass!!\r\n");
  147. }/* memtest */
  148. {
  149. /* PC6 for SDCard Rst */
  150. GPIO_InitTypeDef GPIO_InitStructure;
  151. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  152. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  153. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  154. GPIO_Init(GPIOC,&GPIO_InitStructure);
  155. GPIO_SetBits(GPIOC,GPIO_Pin_6);
  156. }
  157. /* SPI1 config */
  158. {
  159. GPIO_InitTypeDef GPIO_InitStructure;
  160. SPI_InitTypeDef SPI_InitStructure;
  161. /* Enable SPI1 Periph clock */
  162. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
  163. | RCC_APB2Periph_AFIO | RCC_APB2Periph_SPI1,
  164. ENABLE);
  165. /* Configure SPI1 pins: PA5-SCK, PA6-MISO and PA7-MOSI */
  166. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  167. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  168. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  169. GPIO_Init(GPIOA, &GPIO_InitStructure);
  170. /*------------------------ SPI1 configuration ------------------------*/
  171. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI_Direction_1Line_Tx;
  172. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  173. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  174. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  175. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  176. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  177. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;/* 72M/64=1.125M */
  178. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  179. SPI_InitStructure.SPI_CRCPolynomial = 7;
  180. SPI_I2S_DeInit(SPI1);
  181. SPI_Init(SPI1, &SPI_InitStructure);
  182. /* Enable SPI_MASTER */
  183. SPI_Cmd(SPI1, ENABLE);
  184. SPI_CalculateCRC(SPI1, DISABLE);
  185. }
  186. }/* rt_hw_board_init */
  187. #if STM32_CONSOLE_USART == 1
  188. #define CONSOLE_RX_PIN GPIO_Pin_9
  189. #define CONSOLE_TX_PIN GPIO_Pin_10
  190. #define CONSOLE_GPIO GPIOA
  191. #define CONSOLE_USART USART1
  192. #elif STM32_CONSOLE_USART == 2
  193. #if defined(STM32_LD) || defined(STM32_MD)
  194. #define CONSOLE_RX_PIN GPIO_Pin_6
  195. #define CONSOLE_TX_PIN GPIO_Pin_5
  196. #define CONSOLE_GPIO GPIOD
  197. #elif defined(STM32_HD)
  198. #define CONSOLE_RX_PIN GPIO_Pin_3
  199. #define CONSOLE_TX_PIN GPIO_Pin_2
  200. #define CONSOLE_GPIO GPIOA
  201. #endif
  202. #define CONSOLE_USART USART2
  203. #elif STM32_CONSOLE_USART == 2
  204. #define CONSOLE_RX_PIN GPIO_Pin_11
  205. #define CONSOLE_TX_PIN GPIO_Pin_10
  206. #define CONSOLE_GPIO GPIOB
  207. #define CONSOLE_USART USART3
  208. #endif
  209. /* init console to support rt_kprintf */
  210. static void rt_hw_console_init(void)
  211. {
  212. /* Enable USART1 and GPIOA clocks */
  213. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1
  214. | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC
  215. | RCC_APB2Periph_GPIOF, ENABLE);
  216. #if STM32_CONSOLE_USART == 0
  217. #else
  218. /* GPIO configuration */
  219. {
  220. GPIO_InitTypeDef GPIO_InitStructure;
  221. /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  222. GPIO_InitStructure.GPIO_Pin = CONSOLE_RX_PIN;
  223. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  224. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  225. GPIO_Init(CONSOLE_GPIO, &GPIO_InitStructure);
  226. /* Configure USART1 Rx (PA.10) as input floating */
  227. GPIO_InitStructure.GPIO_Pin = CONSOLE_TX_PIN;
  228. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  229. GPIO_Init(CONSOLE_GPIO, &GPIO_InitStructure);
  230. }
  231. /* USART configuration */
  232. {
  233. USART_InitTypeDef USART_InitStructure;
  234. /* USART configured as follow:
  235. - BaudRate = 115200 baud
  236. - Word Length = 8 Bits
  237. - One Stop Bit
  238. - No parity
  239. - Hardware flow control disabled (RTS and CTS signals)
  240. - Receive and transmit enabled
  241. - USART Clock disabled
  242. - USART CPOL: Clock is active low
  243. - USART CPHA: Data is captured on the middle
  244. - USART LastBit: The clock pulse of the last data bit is not output to
  245. the SCLK pin
  246. */
  247. USART_InitStructure.USART_BaudRate = 115200;
  248. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  249. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  250. USART_InitStructure.USART_Parity = USART_Parity_No;
  251. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  252. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  253. USART_Init(CONSOLE_USART, &USART_InitStructure);
  254. /* Enable USART1 */
  255. USART_Cmd(CONSOLE_USART, ENABLE);
  256. }
  257. #endif
  258. }
  259. /* write one character to serial, must not trigger interrupt */
  260. static void rt_hw_console_putc(const char c)
  261. {
  262. /*
  263. to be polite with serial console add a line feed
  264. to the carriage return character
  265. */
  266. if (c=='\n')rt_hw_console_putc('\r');
  267. while (!(CONSOLE_USART->SR & USART_FLAG_TXE));
  268. CONSOLE_USART->DR = (c & 0x1FF);
  269. }
  270. /**
  271. * This function is used by rt_kprintf to display a string on console.
  272. *
  273. * @param str the displayed string
  274. */
  275. void rt_hw_console_output(const char* str)
  276. {
  277. #if STM32_CONSOLE_USART == 0
  278. /* no console */
  279. #else
  280. while (*str)
  281. {
  282. rt_hw_console_putc (*str++);
  283. }
  284. #endif
  285. }
  286. /*@}*/