STM32L151 NB-IoT and FreeRTOS Project
guowenxue
2018-11-14 1ff641b9d4351c644b33902a7ddae962f88dfa47
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
/****************************************************************************
*   Copyright: (C)2018 Î人ÁèÔÆÎïÍøÖÇ¿ÆÊµÑéÊÒ www.iot-yun.com
*      Author: GuoWenxue<guowenxue@gmail.com> QQ: 281143292
* Description: STM32L151C8T6 CubeMX ¿ª·¢°å printfºÍ´®¿Ú³õʼ»¯º¯Êý
*              Î´Ê¹ÓÃÖжÏģʽ´¦Àí·¢ËͺͽÓÊÕ
*   ChangeLog:
*        °æ±¾ºÅ     ÈÕÆÚ       ×÷Õß      ËµÃ÷
*        V1.0.0  2018.11.05  GuoWenxue   ·¢²¼¸Ã°æ±¾
****************************************************************************/
 
#include <stdio.h>
 
#include "stm32_usart.h"
 
static USART_TypeDef* debug_usart=USART1; /* Ä¬ÈÏʹÓô®¿Ú1×÷Ϊµ÷ÊÔ´®¿Ú */
 
/* ¿ª·¢°åÉϵĸ÷¸ö´®¿ÚËùʹÓõĴ®¿Ú ¹Ü½Å¡¢Ê±ÖÓ¶¨Òå */
stm32_usart_pins_t   usart_pins[USART_MAX] =
{
/* USARTx  GPIOx   Tx_Pin      Rx_Pin      Tx_Pin_source     Rx_Pin_Source     GPIO_AF_USARTx    USART_Clock          TxRx_GPIO_Clock */
    {USART1, GPIOA,  GPIO_Pin_9, GPIO_Pin_10,  GPIO_PinSource9, GPIO_PinSource10, GPIO_AF_USART1, RCC_APB2Periph_USART1, RCC_AHBPeriph_GPIOA},
    {USART2, GPIOA,  GPIO_Pin_2,  GPIO_Pin_3,  GPIO_PinSource2,  GPIO_PinSource3, GPIO_AF_USART2, RCC_APB1Periph_USART2, RCC_AHBPeriph_GPIOA},
    {USART3, GPIOB, GPIO_Pin_10, GPIO_Pin_11, GPIO_PinSource10, GPIO_PinSource11, GPIO_AF_USART3, RCC_APB1Periph_USART3, RCC_AHBPeriph_GPIOB},
};
 
/* º¯Êý˵Ã÷:  ÅäÖô®¿ÚGPIO¿ÚºÍʱÖÓ£»
 * ²ÎÊý˵Ã÷:  whichÖ¸¶¨ÒªÅäÖõĴ®¿Ú,ÆäÖµÓ¦¸ÃΪ USART_PORT1¡¢USART_PORT2¡¢USART_PORT3£¬¶¨ÒåÔÚstm32_usart.hÖУ»
 * ·µ»ØÖµ:    ÎÞ
 */
void init_usart_gpio(uint8_t which)
{
    GPIO_InitTypeDef   GPIO_InitStructure;    
    
    if( which >= USART_MAX )
        return ;    
    
    /* Enable USART GPIO and USART clock */
  RCC_AHBPeriphClockCmd(usart_pins[which].gpio_clk, ENABLE);
    
    if( USART_PORT1 == which ) 
        RCC_APB2PeriphClockCmd(usart_pins[which].usart_clk, ENABLE);
    else
        RCC_APB1PeriphClockCmd(usart_pins[which].usart_clk, ENABLE);
    
  /* Connect PXx to USARTx_Tx, PXx to USARTx_Rx  */
    GPIO_PinAFConfig(usart_pins[which].group, usart_pins[which].txpin_src, usart_pins[which].gpio_af);
    GPIO_PinAFConfig(usart_pins[which].group, usart_pins[which].rxpin_src, usart_pins[which].gpio_af);
  
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    
  /* Configure USART Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = usart_pins[which].txpin;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;    
  GPIO_Init(usart_pins[which].group, &GPIO_InitStructure);
 
  /* Configure USART1 Rx as input floating */    
  GPIO_InitStructure.GPIO_Pin = usart_pins[which].rxpin;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;    
  GPIO_Init(usart_pins[which].group, &GPIO_InitStructure);    
 
}
 
 
/* º¯Êý˵Ã÷:  ÅäÖô®¿ÚµÄ²¨ÌØÂÊ¡¢Êý¾Ýλ¡¢ÆæÅ¼Ð£Ñéλ¡¢Í£Ö¹Î»¡¢Á÷¿ØµÈ£»
 * ²ÎÊý˵Ã÷:  whichÖ¸¶¨ÒªÅäÖõĴ®¿Ú,ÆäÖµÓ¦¸ÃΪ USART_PORT1¡¢USART_PORT2¡¢USART_PORT3£¬¶¨ÒåÔÚstm32_usart.hÖУ»
 *            baudrate: ²¨ÌØÂÊ,ÆäֵΪ: 115200,9600,4800,2400,1200....
 *            rxirq: ÊÇ·ñʹÄܽÓÊÕÖжϣ» 1->ʹÄÜ   0->²»Ê¹ÄÜ
 *            txirq: ÊÇ·ñʹÄÜ·¢ËÍÖжϣ» 1->ʹÄÜ   0->²»Ê¹ÄÜ
 * ·µ»ØÖµ:    ÎÞ
 */
void stm32_init_usart(uint8_t which, uint32_t baudrate, uint8_t txirq, uint8_t rxirq)
{
    USART_InitTypeDef USART_InitStructure;
    
    if( which >= USART_MAX )
        return ;
    
    init_usart_gpio(which);
    
  /* 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(usart_pins[which].USARTx, &USART_InitStructure);        //ÅäÖô®¿Ú²ÎÊýº¯Êý
 
    
#if 0  /* Not implement yet */
    if( rxirq )   //ʹÄܽÓÊÕÖжÏ
        USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);         
    
    if( txirq )  //ʹÄÜ·¢ËÍ»º³å¿ÕÖжϠ      
        USART_ITConfig(USARTx, USART_IT_TXE, ENABLE);                        
#endif    
    
  /* Enable the USARTx */
  USART_Cmd(usart_pins[which].USARTx, ENABLE);        
}
 
 
 
 
/* º¯Êý˵Ã÷: ³õʼ»¯µ÷ÊÔ´®¿Ú,²¢Ö¸¶¨ÏàÓ¦µÄ´®¿ÚΪprintfº¯ÊýµÄÊä³ö´®¿Ú
 * ²ÎÊý˵Ã÷: whichÖ¸¶¨ÒªÅäÖõĴ®¿Ú,ÆäÖµÓ¦¸ÃΪ USART_PORT1¡¢USART_PORT2¡¢USART_PORT3£¬¶¨ÒåÔÚstm32_usart.hÖУ»
 *           baudrate: ²¨ÌØÂÊ,ÆäֵΪ: 115200,9600,4800,2400,1200....
 * ·µ»ØÖµ£º  ÎÞ
 */
void stm32_init_printf(uint8_t which, uint32_t baudrate)
{
    if( which >= USART_MAX )
        return ;
    
    debug_usart = usart_pins[which].USARTx;
    
    /* ³õʼ»¯ USART1µÄGPIO¿Ú */ 
    stm32_init_usart(which, baudrate, TXIRQ_DISABLE, RXIRQ_DISABLE);
 
    /* Êä³öÒ»¸ö»Ø³µ, secureCRTÈí¼þ²Ù×÷´®¿ÚµÄʱºòÐèÒª */
  USART_PUTCHR(debug_usart, '\n');
}
 
 
/* º¯Êý˵Ã÷: printfº¯ÊýʵÏֵĹ³×Óº¯Êý
 * ²ÎÊý˵Ã÷: ch: Òª·¢Ë͵Ä×Ö·û     FILE *f: Î´Ê¹ÓÃ
 * ·µ»ØÖµ£º  ·¢Ë͵Ä×Ö·û
 */
int fputc(int ch, FILE *f) 
    /* windowsÏ»»ÐзûÊÇ\r\n,Èç¹ûÅöµ½\nÔòÔÚÇ°ÃæÌí¼Ó\r */
    if('\n' == ch)
    {
       USART_PUTCHR(debug_usart, '\r');
    }
        
  USART_PUTCHR(debug_usart, ch);
    
  return ch; 
}