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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/****************************************************************************
*   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 "misc.h"
#include "stm32f10x.h"
#include "stm32v5_usart.h"
 
static USART_TypeDef*     debug_usart=USART1; /* Ä¬ÈÏʹÓô®¿Ú1×÷Ϊµ÷ÊÔ´®¿Ú */
 
uint8_t                   g_RxBuf[RXBUF_SIZE];
uint8_t                   g_RxLen = 0;
uint8_t                   g_RxEnd_Flag = 0;
 
 
/* º¯Êý˵Ã÷:  ÅäÖô®¿ÚGPIO¿ÚºÍʱÖÓ£»
 * ²ÎÊý˵Ã÷:  whichÖ¸¶¨ÒªÅäÖõĴ®¿Ú,ÆäÖµÓ¦¸ÃΪ USART_PORT1 »ò USART_PORT2
 * ·µ»ØÖµ:    ÎÞ
 */
void init_usart_gpio(int which)
{
    GPIO_InitTypeDef   GPIO_InitStructure;    
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, 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);        
}
 
 
/* º¯Êý˵Ã÷:  ÅäÖô®¿ÚµÄ²¨ÌØÂÊ¡¢Êý¾Ýλ¡¢ÆæÅ¼Ð£Ñéλ¡¢Í£Ö¹Î»¡¢Á÷¿ØµÈ£»
 * ²ÎÊý˵Ã÷:  whichÖ¸¶¨ÒªÅäÖõĴ®¿Ú,ÆäÖµÓ¦¸ÃΪ USART_PORT1 »ò USART_PORT2
 *            baudrate: ²¨ÌØÂÊ,ÆäֵΪ: 115200,9600,4800,2400,1200....
 *            rxirq: ÊÇ·ñʹÄܽÓÊÕÖжÏ, 1: Ê¹ÄÜ  0: ²»Ê¹ÄÜ
 * ·µ»ØÖµ:    ÎÞ
 */
void config_usart(uint8_t which, uint32_t baudrate, uint8_t  rxirq)
{
    USART_InitTypeDef    USART_InitStructure;
    USART_TypeDef*       USARTx = NULL;
    NVIC_InitTypeDef     NVIC_InitStructure;
    
    /* ¸ù¾Ý´®¿Ú²»Í¬ÉèÖÃÏàÓ¦²ÎÊý */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    if( USART_PORT2 == which )
    {
        USARTx = USART2;
        NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    }
    else
    {
        USARTx = USART1;            
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    }    
    
  /* ÅäÖÃÏàÓ¦´®¿Ú Êý¾Ýλ¡¢ÆæÅ¼Ð£ÑéλµÈ  */    
  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);                            //ÅäÖô®¿Ú²ÎÊýº¯Êý
    
    /* ÅäÖÃÏàÓ¦´®¿ÚÖжϠ*/
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    NVIC_Init(&NVIC_InitStructure);    
    
    if( USART_PORT2 == which )
    {
        USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
        USART_ClearFlag(USART2, USART_FLAG_TC);  /* ½â¾öµÚ1¸ö×Ö½ÚÎÞ·¨ÕýÈ··¢ËͳöÈ¥µÄÎÊÌâ */
    }
    else
    {
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
        USART_ClearFlag(USART1, USART_FLAG_TC); /* ½â¾öµÚ1¸ö×Ö½ÚÎÞ·¨ÕýÈ··¢ËͳöÈ¥µÄÎÊÌâ */
    }
        
  /* Ê¹ÄÜ´®¿Ú */
  USART_Cmd(USARTx, ENABLE);    
}
 
 
/* º¯Êý˵Ã÷: Í¨¹ýÖ¸¶¨´®¿Ú·¢ËÍÒ»¸ö×Ö·û 
 * ²ÎÊý˵Ã÷: usartÖ¸¶¨Äĸö´®¿Ú, USART1 »ò USART2
 *           ch ÊÇÒª·¢Ë͵Ä×Ö·û
 * ·µ»ØÖµ:   ÎÞ
 */
__inline void usart_putchar(USART_TypeDef* usart, uint8_t ch)
{
    USART_ClearFlag(usart, USART_FLAG_TC);  /* ½â¾öµÚ1¸ö×Ö½ÚÎÞ·¨ÕýÈ··¢ËͳöÈ¥µÄÎÊÌâ */
  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(which, baudrate, RXIRQ_ENB);  
 
    /* Êä³öÒ»¸ö»Ø³µ, 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);
    }
}
 
 
/* º¯Êý˵Ã÷: Íùµ÷ÊÔ´®¿Ú·¢ËÍ×Ö·û´®
 * ²ÎÊý˵Ã÷: data Ö¸ÏòҪͨ¹ý´®¿Ú·¢Ë͵ÄÊý¾Ý
 *           len  Ö¸ÏòҪͨ¹ý´®¿Ú·¢Ë͵ÄÊý¾Ý³¤¶È
 * ·µ»ØÖµ£º  ÎÞ
 */
void usart_send_data(uint8_t *data, uint32_t len)
{
    uint32_t           i = 0; 
    if( !data || len==0 )
        return ;
    
    for(i=0; i<len; i++)
    {
        usart_putchar(debug_usart, data[i]);
    }
}
 
 
 
/* º¯Êý˵Ã÷: 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; 
}
 
/* ´®¿Ú1 ÖжϽÓÊÕº¯Êý */
 
void USART1_IRQHandler(void)
{
    if( USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) 
    {
        /* ½ÓÊÕÍê³ÉºóµÄÊý¾Ý»¹Ã»À´µÃ¼°´¦Àí, ÔÝÍ£Êý¾ÝµÄ½ÓÊյȴ¦ÀíÍê³ÉºóÔÙ¿ªÊ¼½ÓÊÕ */
        if( g_RxEnd_Flag ) 
            goto OUT;
        
        /* ½ÓÊÕµÄÊý¾Ý³¤¶È³¬¹ý½ÓÊÕbuffer´óСÔò»Øµ½bufferÍ·¿ªÊ¼Ð´Êý¾Ý */
        if( g_RxLen >= RXBUF_SIZE )
                    g_RxLen = 0;
 
        /* Ò»¸öÒ»¸ö×Ö½Ú´Ó´®¿Ú½ÓÊÕ×Ö·û£¬ÊÕµ½µÄ×Ö·û´æ·Åµ½bufferÖУ¬Èç¹ûÊÕµ½\r\n»»ÐÐÔò±êʶÊý¾Ý½ÓÊÕÍê³É */
        g_RxBuf[g_RxLen++] = USART_ReceiveData(USART1);
        if( g_RxLen >= 2 && g_RxBuf [g_RxLen-2]=='\r' && g_RxBuf [g_RxLen-1]=='\n')
        {
            g_RxEnd_Flag = 1;  /* ÉèÖÃÒ»Ö¡½ÓÊÕÍê³É±êʶ */
        }
    }
    
OUT:    
    USART_ClearITPendingBit(USART1, USART_IT_RXNE);  
}
 
/* ´®¿Ú2 ÖжϽÓÊÕº¯Êý */
void USART2_IRQHandler(void)
{
 
}