board.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /*******************************************************************************
  41. * Function Name : SysTick_Configuration
  42. * Description : Configures the SysTick for OS tick.
  43. * Input : None
  44. * Output : None
  45. * Return : None
  46. *******************************************************************************/
  47. void SysTick_Configuration(void)
  48. {
  49. RCC_ClocksTypeDef rcc_clocks;
  50. rt_uint32_t cnts;
  51. RCC_GetClocksFreq(&rcc_clocks);
  52. cnts = (rt_uint32_t)rcc_clocks.HCLK_Frequency / RT_TICK_PER_SECOND;
  53. SysTick_Config(cnts);
  54. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  55. }
  56. /**
  57. * This is the timer interrupt service routine.
  58. *
  59. */
  60. void rt_hw_timer_handler(void)
  61. {
  62. rt_tick_increase();
  63. }
  64. /**
  65. * This function will initial STM32 board.
  66. */
  67. void rt_hw_board_init()
  68. {
  69. /* NVIC Configuration */
  70. NVIC_Configuration();
  71. /* Configure the SysTick */
  72. SysTick_Configuration();
  73. rt_hw_console_init();
  74. }
  75. /* init console to support rt_kprintf */
  76. static void rt_hw_console_init()
  77. {
  78. /* Enable USART1 and GPIOA clocks */
  79. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  80. /* GPIO configuration */
  81. {
  82. GPIO_InitTypeDef GPIO_InitStructure;
  83. /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  84. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  85. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  86. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  87. GPIO_Init(GPIOA, &GPIO_InitStructure);
  88. /* Configure USART1 Rx (PA.10) as input floating */
  89. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  90. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  91. GPIO_Init(GPIOA, &GPIO_InitStructure);
  92. }
  93. /* USART configuration */
  94. {
  95. USART_InitTypeDef USART_InitStructure;
  96. /* USART1 configured as follow:
  97. - BaudRate = 115200 baud
  98. - Word Length = 8 Bits
  99. - One Stop Bit
  100. - No parity
  101. - Hardware flow control disabled (RTS and CTS signals)
  102. - Receive and transmit enabled
  103. - USART Clock disabled
  104. - USART CPOL: Clock is active low
  105. - USART CPHA: Data is captured on the middle
  106. - USART LastBit: The clock pulse of the last data bit is not output to
  107. the SCLK pin
  108. */
  109. USART_InitStructure.USART_BaudRate = 115200;
  110. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  111. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  112. USART_InitStructure.USART_Parity = USART_Parity_No;
  113. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  114. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  115. USART_Init(USART1, &USART_InitStructure);
  116. /* Enable USART1 */
  117. USART_Cmd(USART1, ENABLE);
  118. }
  119. }
  120. /* write one character to serial, must not trigger interrupt */
  121. static void rt_hw_console_putc(const char c)
  122. {
  123. /*
  124. to be polite with serial console add a line feed
  125. to the carriage return character
  126. */
  127. if (c=='\n')rt_hw_console_putc('\r');
  128. while (!(USART1->SR & USART_FLAG_TXE));
  129. USART1->DR = (c & 0x1FF);
  130. }
  131. /**
  132. * This function is used by rt_kprintf to display a string on console.
  133. *
  134. * @param str the displayed string
  135. */
  136. void rt_hw_console_output(const char* str)
  137. {
  138. while (*str)
  139. {
  140. rt_hw_console_putc (*str++);
  141. }
  142. }
  143. /*@}*/