board.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 STM32SKY
  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. static void FSMC_SRAM_Init(void)
  112. {
  113. #define REG32(x) (*(volatile unsigned long*)(x))
  114. /* enable FSMC clock */
  115. REG32(0x40021014) = 0x114;
  116. /* enable GPIOD, GPIOE, GPIOF and GPIOG clocks */
  117. REG32(0x40021018) = 0x1e0;
  118. /* SRAM Data lines, NOE and NWE configuration */
  119. REG32(0x40011400) = 0x44BB44BB;
  120. REG32(0x40011404) = 0xBBBBBBBB;
  121. REG32(0x40011800) = 0xB44444BB;
  122. REG32(0x40011804) = 0xBBBBBBBB;
  123. REG32(0x40011C00) = 0x44BBBBBB;
  124. REG32(0x40011C04) = 0xBBBB4444;
  125. REG32(0x40012000) = 0x44BBBBBB;
  126. REG32(0x40012004) = 0x44444B44;
  127. /* FSMC Configuration (enable FSMC Bank1_SRAM Bank) */
  128. REG32(0xA0000010) = 0x00001011;
  129. REG32(0xA0000014) = 0x00000200;
  130. }
  131. /**
  132. * This function will initial STM32 board.
  133. */
  134. void rt_hw_board_init()
  135. {
  136. /* Configure the system clocks */
  137. RCC_Configuration();
  138. /* NVIC Configuration */
  139. NVIC_Configuration();
  140. /* Configure the SysTick */
  141. SysTick_Configuration();
  142. /* Configure SRAM on the board */
  143. FSMC_SRAM_Init();
  144. rt_hw_console_init();
  145. }
  146. /* init console to support rt_kprintf */
  147. static void rt_hw_console_init()
  148. {
  149. /* Enable USART1 and GPIOA clocks */
  150. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  151. /* GPIO configuration */
  152. {
  153. GPIO_InitTypeDef GPIO_InitStructure;
  154. /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  155. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  156. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  157. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  158. GPIO_Init(GPIOA, &GPIO_InitStructure);
  159. /* Configure USART1 Rx (PA.10) as input floating */
  160. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  161. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  162. GPIO_Init(GPIOA, &GPIO_InitStructure);
  163. }
  164. /* USART configuration */
  165. {
  166. USART_InitTypeDef USART_InitStructure;
  167. /* USART1 configured as follow:
  168. - BaudRate = 115200 baud
  169. - Word Length = 8 Bits
  170. - One Stop Bit
  171. - No parity
  172. - Hardware flow control disabled (RTS and CTS signals)
  173. - Receive and transmit enabled
  174. - USART Clock disabled
  175. - USART CPOL: Clock is active low
  176. - USART CPHA: Data is captured on the middle
  177. - USART LastBit: The clock pulse of the last data bit is not output to
  178. the SCLK pin
  179. */
  180. USART_InitStructure.USART_BaudRate = 115200;
  181. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  182. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  183. USART_InitStructure.USART_Parity = USART_Parity_No;
  184. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  185. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  186. USART_Init(USART1, &USART_InitStructure);
  187. /* Enable USART1 */
  188. USART_Cmd(USART1, ENABLE);
  189. }
  190. }
  191. /* write one character to serial, must not trigger interrupt */
  192. static void rt_hw_console_putc(const char c)
  193. {
  194. /*
  195. to be polite with serial console add a line feed
  196. to the carriage return character
  197. */
  198. if (c=='\n')rt_hw_console_putc('\r');
  199. while (!(USART1->SR & USART_FLAG_TXE));
  200. USART1->DR = (c & 0x1FF);
  201. }
  202. /**
  203. * This function is used by rt_kprintf to display a string on console.
  204. *
  205. * @param str the displayed string
  206. */
  207. void rt_hw_console_output(const char* str)
  208. {
  209. while (*str)
  210. {
  211. rt_hw_console_putc (*str++);
  212. }
  213. }
  214. /*@}*/