STM32 V5 source code
guowenxue
2018-02-04 785deec23b4cb1e7c4c4d81eb808f195adb1d98a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/****************************************************************************
*   Copyright: (C)2014 Î人ÁèÔÆÇ¶ÈëʽʵÑéÊÒ www.emblinux.com
*      Author: GuoWenxue<guowenxue@gmail.com> QQ: 281143292
* Description: ·Ü¶·STM32v5¿ª·¢°å´®¿Ú²Ù×÷º¯Êý½Ó¿Ú,printf¹³×Óº¯Êýfputc
*   ChangeLog:
*        °æ±¾ºÅ     ÈÕÆÚ       ×÷Õß      ËµÃ÷
*        V1.0.0  2014.08.25  GuoWenxue   ·¢²¼¸Ã°æ±¾
****************************************************************************/
#include "stm32f10x.h"
#include "stm32f10x_usart.h"
#include "stm32v5_uart.h"
#include <stdio.h>
 
  /* USARTx configured as follow:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
void USART_Config(USART_TypeDef* USARTx)
{
    USART_InitTypeDef USART_InitStructure;
    
  /* Configure USART1 */    
  USART_InitStructure.USART_BaudRate = 115200;                        //ËÙÂÊ115200bps
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;        //Êý¾Ýλ8λ
  USART_InitStructure.USART_StopBits = USART_StopBits_1;            //ֹͣλ1λ
  USART_InitStructure.USART_Parity = USART_Parity_No;                //ÎÞУÑéλ
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;   //ÎÞÓ²¼þÁ÷¿Ø
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;                    //ÊÕ·¢Ä£Ê½
  USART_Init(USARTx, &USART_InitStructure);                            //ÅäÖô®¿Ú²ÎÊýº¯Êý
 
#if 0    
  /* Enable USARTx Receive and Transmit interrupts */
  USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);          //ʹÄܽÓÊÕÖжÏ
  USART_ITConfig(USARTx, USART_IT_TXE, ENABLE);                        //ʹÄÜ·¢ËÍ»º³å¿ÕÖжϠ  
#endif
    
  /* Enable the USARTx */
  USART_Cmd(USARTx, ENABLE);    
}
 
void dbg_setup_uart(void)
{
    GPIO_InitTypeDef   GPIO_InitStructure;    
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE); 
 
  /* Ä¬Èϸ´Óù¦ÄÜ */    
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;                      //USART1 TX
    GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;             //¸´ÓÃÍÆÍìÊä³ö
  GPIO_Init(GPIOA, &GPIO_InitStructure);        
    
  /* ¸´Óù¦ÄܵÄÊäÈëÒý½Å±ØÐëÅäÖÃΪÊäÈëģʽ£¨¸¡¿Õ/ÉÏÀ­/ÏÂÀ­µÄÒ»ÖÖ£©*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;                  //USART1 RX
    GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  //¸´Óø¡¿ÕÊäÈë
  GPIO_Init(GPIOA, &GPIO_InitStructure);                       
    
  USART_Config(DBG_UART);
 
  USART_SendData(DBG_UART, '\n'); 
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);     
}
 
int fputc(int ch, FILE *f) 
    if('\n' == ch)
    {
       USART_SendData(DBG_UART, (uint8_t) '\r'); 
     while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)  ; 
    }
        
  USART_SendData(DBG_UART, (uint8_t) ch); 
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); 
    
  return ch; 
}