board.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /**
  19. * @addtogroup STM32
  20. */
  21. /*@{*/
  22. /*******************************************************************************
  23. * Function Name : NVIC_Configuration
  24. * Description : Configures Vector Table base location.
  25. * Input : None
  26. * Output : None
  27. * Return : None
  28. *******************************************************************************/
  29. void NVIC_Configuration(void)
  30. {
  31. #ifdef VECT_TAB_RAM
  32. /* Set the Vector Table base location at 0x20000000 */
  33. NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  34. #else /* VECT_TAB_FLASH */
  35. /* Set the Vector Table base location at 0x08000000 */
  36. NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  37. #endif
  38. /*
  39. * set priority group:
  40. * 2 bits for pre-emption priority
  41. * 2 bits for subpriority
  42. */
  43. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  44. }
  45. extern void rt_hw_interrupt_thread_switch(void);
  46. /**
  47. * This is the timer interrupt service routine.
  48. *
  49. */
  50. void rt_hw_timer_handler(void)
  51. {
  52. /* enter interrupt */
  53. rt_interrupt_enter();
  54. rt_tick_increase();
  55. /* leave interrupt */
  56. rt_interrupt_leave();
  57. }
  58. /* NAND Flash */
  59. #include "fsmc_nand.h"
  60. static void all_device_reset(void)
  61. {
  62. /* RESET */
  63. /* DM9000A PE5 */
  64. /* LCD PF10 */
  65. /* SPI-FLASH PA3 */
  66. /* CS */
  67. /* DM9000A FSMC_NE4 PG12 */
  68. /* LCD FSMC_NE2 PG9 */
  69. /* SPI_FLASH PA4 */
  70. /* CODEC PC5 */
  71. /* TOUCH PC4 */
  72. GPIO_InitTypeDef GPIO_InitStructure;
  73. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOE
  74. | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG,ENABLE);
  75. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  76. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  77. /* SPI_FLASH CS */
  78. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  79. GPIO_Init(GPIOA,&GPIO_InitStructure);
  80. GPIO_SetBits(GPIOA,GPIO_Pin_4);
  81. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  82. /* CODEC && TOUCH CS */
  83. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
  84. GPIO_Init(GPIOC,&GPIO_InitStructure);
  85. GPIO_SetBits(GPIOC,GPIO_Pin_4 | GPIO_Pin_5);
  86. /* DM9000A RESET */
  87. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  88. GPIO_Init(GPIOE,&GPIO_InitStructure);
  89. GPIO_ResetBits(GPIOE,GPIO_Pin_5);
  90. /* LCD RESET */
  91. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  92. GPIO_Init(GPIOF,&GPIO_InitStructure);
  93. GPIO_ResetBits(GPIOF,GPIO_Pin_10);
  94. /* SPI_FLASH RESET */
  95. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  96. GPIO_Init(GPIOA,&GPIO_InitStructure);
  97. GPIO_ResetBits(GPIOA,GPIO_Pin_3);
  98. /* FSMC GPIO configure */
  99. {
  100. GPIO_InitTypeDef GPIO_InitStructure;
  101. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF
  102. | RCC_APB2Periph_GPIOG, ENABLE);
  103. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
  104. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  105. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  106. /*
  107. FSMC_D0 ~ FSMC_D3
  108. PD14 FSMC_D0 PD15 FSMC_D1 PD0 FSMC_D2 PD1 FSMC_D3
  109. */
  110. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_14 | GPIO_Pin_15;
  111. GPIO_Init(GPIOD,&GPIO_InitStructure);
  112. /*
  113. FSMC_D4 ~ FSMC_D12
  114. PE7 ~ PE15 FSMC_D4 ~ FSMC_D12
  115. */
  116. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10
  117. | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  118. GPIO_Init(GPIOE,&GPIO_InitStructure);
  119. /* FSMC_D13 ~ FSMC_D15 PD8 ~ PD10 */
  120. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
  121. GPIO_Init(GPIOD,&GPIO_InitStructure);
  122. /*
  123. FSMC_A0 ~ FSMC_A5 FSMC_A6 ~ FSMC_A9
  124. PF0 ~ PF5 PF12 ~ PF15
  125. */
  126. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3
  127. | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  128. GPIO_Init(GPIOF,&GPIO_InitStructure);
  129. /* FSMC_A10 ~ FSMC_A15 PG0 ~ PG5 */
  130. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
  131. GPIO_Init(GPIOG,&GPIO_InitStructure);
  132. /* FSMC_A16 ~ FSMC_A18 PD11 ~ PD13 */
  133. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
  134. GPIO_Init(GPIOD,&GPIO_InitStructure);
  135. /* RD-PD4 WR-PD5 */
  136. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
  137. GPIO_Init(GPIOD,&GPIO_InitStructure);
  138. /* NBL0-PE0 NBL1-PE1 */
  139. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  140. GPIO_Init(GPIOE,&GPIO_InitStructure);
  141. /* NE1/NCE2 */
  142. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  143. GPIO_Init(GPIOD,&GPIO_InitStructure);
  144. /* NE2 */
  145. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  146. GPIO_Init(GPIOG,&GPIO_InitStructure);
  147. /* NE3 */
  148. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  149. GPIO_Init(GPIOG,&GPIO_InitStructure);
  150. /* NE4 */
  151. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  152. GPIO_Init(GPIOG,&GPIO_InitStructure);
  153. }
  154. /* FSMC GPIO configure */
  155. GPIO_SetBits(GPIOE,GPIO_Pin_5); /* DM9000A */
  156. GPIO_SetBits(GPIOF,GPIO_Pin_10); /* LCD */
  157. GPIO_SetBits(GPIOA,GPIO_Pin_3); /* SPI_FLASH */
  158. }
  159. /**
  160. * This function will initial STM32 Radio board.
  161. */
  162. extern void FSMC_SRAM_Init(void);
  163. void rt_hw_board_init()
  164. {
  165. //NAND_IDTypeDef NAND_ID;
  166. /* Configure the system clocks */
  167. SystemInit();
  168. all_device_reset();
  169. /* NVIC Configuration */
  170. NVIC_Configuration();
  171. /* Configure the SysTick */
  172. SysTick_Config( SystemFrequency_SysClk / RT_TICK_PER_SECOND );
  173. /* Console Initialization*/
  174. rt_hw_usart_init();
  175. rt_console_set_device("uart1");
  176. rt_kprintf("\r\n\r\nSystemInit......\r\n");
  177. /* SRAM init */
  178. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
  179. FSMC_SRAM_Init();
  180. /* memtest */
  181. {
  182. unsigned char * p_extram = (unsigned char *)0x68000000;
  183. unsigned int temp;
  184. rt_kprintf("\r\nmem testing....");
  185. for(temp=0; temp<0x80000; temp++)
  186. {
  187. *p_extram++ = (unsigned char)temp;
  188. }
  189. p_extram = (unsigned char *)0x68000000;
  190. for(temp=0; temp<0x80000; temp++)
  191. {
  192. if( *p_extram++ != (unsigned char)temp )
  193. {
  194. rt_kprintf("\rmemtest fail @ %08X\r\nsystem halt!!!!!",(unsigned int)p_extram);
  195. while(1);
  196. }
  197. }
  198. rt_kprintf("\rmem test pass!!\r\n");
  199. }/* memtest */
  200. {
  201. /* PC6 for SDCard Rst */
  202. GPIO_InitTypeDef GPIO_InitStructure;
  203. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  204. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  205. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  206. GPIO_Init(GPIOC,&GPIO_InitStructure);
  207. GPIO_SetBits(GPIOC,GPIO_Pin_6);
  208. }
  209. /* SPI1 config */
  210. {
  211. GPIO_InitTypeDef GPIO_InitStructure;
  212. SPI_InitTypeDef SPI_InitStructure;
  213. /* Enable SPI1 Periph clock */
  214. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
  215. | RCC_APB2Periph_AFIO | RCC_APB2Periph_SPI1,
  216. ENABLE);
  217. /* Configure SPI1 pins: PA5-SCK, PA6-MISO and PA7-MOSI */
  218. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  219. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  220. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  221. GPIO_Init(GPIOA, &GPIO_InitStructure);
  222. /*------------------------ SPI1 configuration ------------------------*/
  223. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI_Direction_1Line_Tx;
  224. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  225. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  226. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  227. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  228. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  229. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;/* 72M/64=1.125M */
  230. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  231. SPI_InitStructure.SPI_CRCPolynomial = 7;
  232. SPI_I2S_DeInit(SPI1);
  233. SPI_Init(SPI1, &SPI_InitStructure);
  234. /* Enable SPI_MASTER */
  235. SPI_Cmd(SPI1, ENABLE);
  236. SPI_CalculateCRC(SPI1, DISABLE);
  237. }
  238. }/* rt_hw_board_init */
  239. /*@}*/