forked from bigtreetech/BIGTREETECH-TouchScreenFirmware
-
Notifications
You must be signed in to change notification settings - Fork 15
/
usart.c
111 lines (99 loc) · 3.69 KB
/
usart.c
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
#include "usart.h"
#include "GPIO_Init.h"
static USART_TypeDef *usart[_USART_CNT] = {
USART1, //TX--PA9 RX--PA10
USART2, //TX--PA2 RX--PA3
USART3, //TX--PB10 RX--PB11
UART4, //TX--PC10 RX--PC11
UART5}; //TX--PC12 RX--PD2
static const uint16_t uart_tx[_USART_CNT] = {PA9, PA2, PB10, PC10, PC12}; //TX
static const uint16_t uart_rx[_USART_CNT] = {PA10, PA3, PB11, PC11, PD2}; //RX
void USART_GPIO_Init(uint8_t port)
{
GPIO_InitSet(uart_tx[port], MGPIO_MODE_AF_PP, 0);
GPIO_InitSet(uart_rx[port], MGPIO_MODE_IPU, 0);
}
void USART_GPIO_DeInit(uint8_t port)
{
//set tx/rx to input
GPIO_InitSet(uart_tx[port], MGPIO_MODE_IPN, 0);
GPIO_InitSet(uart_rx[port], MGPIO_MODE_IPN, 0);
}
void USART_Protocol_Init(uint8_t port,uint32_t baud)
{
USART_InitTypeDef USART_InitStructure;
switch(port)
{
case _USART1: RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); break;
case _USART2: RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); break;
case _USART3: RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); break;
case _UART4: RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); break;
case _UART5: RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); break;
}
USART_InitStructure.USART_BaudRate = baud;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(usart[port],&USART_InitStructure);
USART_Cmd(usart[port],ENABLE);
}
void USART_IRQ_Init(uint8_t port, uint16_t usart_it)
{
uint32_t IRQ_Channel[_USART_CNT] = {USART1_IRQn, USART2_IRQn, USART3_IRQn, UART4_IRQn, UART5_IRQn};
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = IRQ_Channel[port];
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(usart[port], usart_it, ENABLE);
USART_ClearITPendingBit(usart[port], usart_it);
}
void USART_Config(uint8_t port, uint32_t baud, uint16_t usart_it)
{
USART_Protocol_Init(port, baud);
USART_IRQ_Init(port, usart_it);
USART_GPIO_Init(port); //所有初始化完成后,再使能IO, 否则上电后会自动发送一个 0xFF
}
void USART_DeConfig(uint8_t port)
{
USART_GPIO_DeInit(port);
switch(port)
{
case _USART1:
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
break;
case _USART2:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, DISABLE);
break;
case _USART3:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, DISABLE);
break;
case _UART4:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, DISABLE);
break;
case _UART5:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, DISABLE);
break;
}
}
void USART_Write(uint8_t port, uint8_t d)
{
while((usart[port]->SR & USART_FLAG_TC) == (uint16_t)RESET);
usart[port]->DR = ((u16)d & (uint16_t)0x01FF);
}
void USART_Puts(uint8_t port, uint8_t *str)
{
while (*str)
{
while((usart[port]->SR & USART_FLAG_TC) == (uint16_t)RESET);
usart[port]->DR = ((u16)*str++ & (uint16_t)0x01FF);
}
}