STM32 V5 source code
guowenxue
2018-05-16 8a8715e9d87d63c5908f8e00d1b5fe3595e41c41
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/****************************************************************************
*   Copyright: (C)2018 Î人ÁèÔÆÎïÍøÖÇ¿ÆÊµÑéÊÒ www.iot-yun.com
*      Author: GuoWenxue<guowenxue@gmail.com> QQ: 281143292
* Description: ·Ü¶·STM32v5¿ª·¢°å ´®¿Ú²Ù×÷º¯Êý½Ó¿Ú,printf¹³×Óº¯Êýfputc
*   ChangeLog:
*        °æ±¾ºÅ     ÈÕÆÚ       ×÷Õß      ËµÃ÷
*        V1.0.0  2018.05.11  GuoWenxue   ·¢²¼¸Ã°æ±¾
****************************************************************************/
 
#include <stdio.h>
#include "stm32f10x_usart.h"
#include "stm32v5_usart.h"
 
static USART_TypeDef* debug_usart=USART1; /* Ä¬ÈÏʹÓô®¿Ú1×÷Ϊµ÷ÊÔ´®¿Ú */
 
 
/* º¯Êý˵Ã÷:  ÅäÖô®¿ÚGPIO¿ÚºÍʱÖÓ£»
 * ²ÎÊý˵Ã÷:  whichÖ¸¶¨ÒªÅäÖõĴ®¿Ú,ÆäÖµÓ¦¸ÃΪ USART_PORT1 »ò USART_PORT2
 * ·µ»ØÖµ:    ÎÞ
 */
void init_usart_gpio(int which)
{
    GPIO_InitTypeDef   GPIO_InitStructure;    
    
    /* Ê¹ÄÜUSART1ËùÓÃGPIO¹Ü½ÅʱÖӺʹ®¿Ú¹¦ÄÜʱÖÓ */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 
    
    if( USART_PORT2 == which )
        RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    else
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
 
 
    if( USART_PORT2 == which )
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;              /* USART2 TXD Á¬ PA2 */
    else
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;              /* USART1 TXD Á¬ PA9 */
 
    GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;            /* ¸´ÓÃÍÆÍìÊä³ö */
  GPIO_Init(GPIOA, &GPIO_InitStructure);        
    
    if( USART_PORT2 == which )
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;                 /* USART2 RXD Á¬ PA3 */
    else
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;            /* USART1 RXD Á¬ PA10 */
    
    GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; /* ¸´Óø¡¿ÕÊäÈë */
    
  GPIO_Init(GPIOA, &GPIO_InitStructure);        
}
 
 
/* º¯Êý˵Ã÷:  ÅäÖô®¿ÚµÄ²¨ÌØÂÊ¡¢Êý¾Ýλ¡¢ÆæÅ¼Ð£Ñéλ¡¢Í£Ö¹Î»¡¢Á÷¿ØµÈ£»
 * ²ÎÊý˵Ã÷:  USARTxÖ¸¶¨ÒªÅäÖõĴ®¿Ú,ÆäÖµÓ¦¸ÃΪ USART1 »ò USART2
 *            baudrate: ²¨ÌØÂÊ,ÆäֵΪ: 115200,9600,4800,2400,1200....
 * ·µ»ØÖµ:    ÎÞ
 */
void config_usart(USART_TypeDef* USARTx, uint32_t baudrate)
{
    USART_InitTypeDef USART_InitStructure;
    
  /* Configure USART1 */    
  USART_InitStructure.USART_BaudRate = baudrate;                        //²¨ÌØÂÊ
  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);    
}
 
/* º¯Êý˵Ã÷: Í¨¹ýÖ¸¶¨´®¿Ú·¢ËÍÒ»¸ö×Ö·û 
 * ²ÎÊý˵Ã÷: usartÖ¸¶¨Äĸö´®¿Ú, USART1 »ò USART2
 *           ch ÊÇÒª·¢Ë͵Ä×Ö·û
 * ·µ»ØÖµ:   ÎÞ
 */
__inline void usart_putchar(USART_TypeDef* usart, uint8_t ch)
{
 
  USART_SendData(usart, ch); 
  while (USART_GetFlagStatus(usart, USART_FLAG_TC) == RESET)
        ;     
}
 
/* º¯Êý˵Ã÷: ³õʼ»¯µ÷ÊÔ´®¿Ú,²¢Ö¸¶¨ÏàÓ¦µÄ´®¿ÚΪµ÷ÊÔ´®¿Ú
 * ²ÎÊý˵Ã÷: which²ÎÊýÖ¸¶¨Ê¹ÓÃÄĸö´®¿Ú, ÆäÖµÓ¦¸ÃÊÇ USART_PORT1 »ò USART_PORT2 
 * ·µ»ØÖµ£º  ÎÞ
 */
void init_dbg_uart(int which, uint32_t baudrate)
{
    /* ÉèÖõ÷ÊÔ´®¿Ú */
    debug_usart = USART_PORT2==which ? USART2 : USART1;  
    
    /* ³õʼ»¯ USART1µÄGPIO¿Ú */ 
    init_usart_gpio(which);   
    
    /* ÅäÖô®¿ÚͨÐŵIJ¨ÌØÂÊ¡¢Êý¾Ýλ¡¢ÆæÅ¼Ð£Ñéλ¡¢Í£Ö¹Î»µÈ */
  config_usart(debug_usart, baudrate);  
 
    /* Êä³öÒ»¸ö»Ø³µ, secureCRTÈí¼þ²Ù×÷´®¿ÚµÄʱºòÐèÒª */
    usart_putchar(debug_usart, '\n');
}
 
/* º¯Êý˵Ã÷: Íùµ÷ÊÔ´®¿Ú·¢ËÍ×Ö·û´®
 * ²ÎÊý˵Ã÷: str Ö¸ÏòÒª·¢Ë͵Ä×Ö·û´®ÄÚÈÝ
 * ·µ»ØÖµ£º  ÎÞ
 */
void usart_puts(const char *str)
{
    if( !str )
        return ;
    
    for( ; *str!='\0'; str++)
    {
        /* windowsÏ»»ÐзûÊÇ\r\n,Èç¹ûÅöµ½\nÔòÔÚÇ°ÃæÌí¼Ó\r */
        if('\n' == *str)  
        {
            usart_putchar(debug_usart, '\r');
        }        
        usart_putchar(debug_usart, *str);
    }
}
 
/* º¯Êý˵Ã÷: printfº¯ÊýʵÏֵĹ³×Óº¯Êý
 * ²ÎÊý˵Ã÷: ch: Òª·¢Ë͵Ä×Ö·û     FILE *f: Î´Ê¹ÓÃ
 * ·µ»ØÖµ£º  ·¢Ë͵Ä×Ö·û
 */
int fputc(int ch, FILE *f) 
    /* windowsÏ»»ÐзûÊÇ\r\n,Èç¹ûÅöµ½\nÔòÔÚÇ°ÃæÌí¼Ó\r */
    if('\n' == ch)
    {
       usart_putchar(debug_usart, '\r');
    }
        
  usart_putchar(debug_usart, ch);
  return ch; 
}