board.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <rtthread.h>
  2. #include "stm32f10x.h"
  3. /*******************************************************************************
  4. * Function Name : NVIC_Configuration
  5. * Description : Configures the nested vectored interrupt controller.
  6. * Input : None
  7. * Output : None
  8. * Return : None
  9. *******************************************************************************/
  10. void NVIC_Configuration(void)
  11. {
  12. #ifdef VECT_TAB_RAM
  13. /* Set the Vector Table base location at 0x20000000 */
  14. NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  15. #else /* VECT_TAB_FLASH */
  16. /* Set the Vector Table base location at 0x08000000 */
  17. NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  18. #endif
  19. }
  20. /*******************************************************************************
  21. * Function Name : SysTick_Configuration
  22. * Description : Configures the SysTick for OS tick.
  23. * Input : None
  24. * Output : None
  25. * Return : None
  26. *******************************************************************************/
  27. void SysTick_Configuration(void)
  28. {
  29. RCC_ClocksTypeDef rcc_clocks;
  30. rt_uint32_t cnts;
  31. RCC_GetClocksFreq(&rcc_clocks);
  32. cnts = (rt_uint32_t)rcc_clocks.HCLK_Frequency / RT_TICK_PER_SECOND;
  33. SysTick_Config(cnts);
  34. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  35. }
  36. /* init console to support rt_kprintf */
  37. static void rt_hw_console_init()
  38. {
  39. /* Enable USART1 and GPIOA clocks */
  40. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  41. /* GPIO configuration */
  42. {
  43. GPIO_InitTypeDef GPIO_InitStructure;
  44. /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  45. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  46. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  47. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  48. GPIO_Init(GPIOA, &GPIO_InitStructure);
  49. /* Configure USART1 Rx (PA.10) as input floating */
  50. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  51. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  52. GPIO_Init(GPIOA, &GPIO_InitStructure);
  53. }
  54. /* USART configuration */
  55. {
  56. USART_InitTypeDef USART_InitStructure;
  57. /* USART1 configured as follow:
  58. - BaudRate = 115200 baud
  59. - Word Length = 8 Bits
  60. - One Stop Bit
  61. - No parity
  62. - Hardware flow control disabled (RTS and CTS signals)
  63. - Receive and transmit enabled
  64. - USART Clock disabled
  65. - USART CPOL: Clock is active low
  66. - USART CPHA: Data is captured on the middle
  67. - USART LastBit: The clock pulse of the last data bit is not output to
  68. the SCLK pin
  69. */
  70. USART_InitStructure.USART_BaudRate = 115200;
  71. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  72. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  73. USART_InitStructure.USART_Parity = USART_Parity_No;
  74. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  75. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  76. USART_Init(USART1, &USART_InitStructure);
  77. /* Enable USART1 */
  78. USART_Cmd(USART1, ENABLE);
  79. }
  80. }
  81. /* write one character to serial, must not trigger interrupt */
  82. static void rt_hw_console_putc(const char c)
  83. {
  84. /*
  85. to be polite with serial console add a line feed
  86. to the carriage return character
  87. */
  88. if (c=='\n')rt_hw_console_putc('\r');
  89. while (!(USART1->SR & USART_FLAG_TXE));
  90. USART1->DR = (c & 0x1FF);
  91. }
  92. /**
  93. * This function is used by rt_kprintf to display a string on console.
  94. *
  95. * @param str the displayed string
  96. */
  97. void rt_hw_console_output(const char* str)
  98. {
  99. while (*str)
  100. {
  101. rt_hw_console_putc (*str++);
  102. }
  103. }
  104. void board_init(void)
  105. {
  106. /* NVIC Configuration */
  107. NVIC_Configuration();
  108. /* Configure the SysTick */
  109. SysTick_Configuration();
  110. rt_hw_console_init();
  111. }
  112. /**
  113. * This is the timer interrupt service routine.
  114. *
  115. */
  116. extern void rt_hw_interrupt_thread_switch(void);
  117. void rt_hw_timer_handler(void)
  118. {
  119. /* enter interrupt */
  120. rt_interrupt_enter();
  121. rt_tick_increase();
  122. /* leave interrupt */
  123. rt_interrupt_leave();
  124. rt_hw_interrupt_thread_switch();
  125. }