board.c 8.8 KB

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