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
/*******************************************************************************
* COPYRIGHT (C) 2021 CMS Technologies Ltd. *
* *
********************************************************************************
* FileName : uart_demo.c *
* Author : *
* Version : 1.0 *
* Date : 2021.08.13 *
* Description : *
* Function List : *
********************************************************************************/
#include "UART_DEMO.h"
unsigned char UART0_RX_BUF[UART_MAX_RECV_LEN];
unsigned short UART0_RX_STA = 0;
unsigned char UART0_TX_BUF[UART_MAX_RECV_LEN];
/******************************************************************************
* Function Name: Uart0_Init
* @brief UART0 init demo
* @param bound
* @return init status
******************************************************************************/
int8_t Uart0_Init(uint32_t bound)//主要查看寄存器
{
int8_t ret;
GPIO_InitTypeDef GPIO_InitStruct = {0};
UART_InitTypeDef UART_InitStructure = {0};
GPIO_PinAFConfig(GPIO_PORT1, GPIO_Pin_1, GPIO_P11, GROUP_AF_RXD0);
GPIO_PinAFConfig(GPIO_PORT1, GPIO_Pin_2, GPIO_P12, GROUP_AF_TXD0);
/*TX GPIO CONFIG*/
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Level = GPIO_Level_HIGH;
GPIO_InitStruct.GPIO_Ctrl = GPIO_Control_DIG;
GPIO_Init(GPIO_PORT1, &GPIO_InitStruct);
/*RX GPIO CONFIG*/
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1 ;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_Ctrl = GPIO_Control_DIG;
GPIO_Init(GPIO_PORT1, &GPIO_InitStruct);
/*USART CONFIG*/
UART_InitStructure.UART_BaudRate = bound;
UART_InitStructure.UART_WordLength = UART_WordLength_8b;
UART_InitStructure.UART_StopBits = UART_StopBits_1;//Ò»¸öֹͣλ
UART_InitStructure.UART_Parity = UART_Parity_No;//ÎÞÆæżУÑéλ
UART_InitStructure.phase = UART_Phase_Normal;
UART_InitStructure.bitorder = UART_Bit_LSB;
UART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx; //ÊÕ·¢Ä£Ê½
ret = UART_Init(UART0, &UART_InitStructure); //³õʼ»¯´®¿Ú
if (ret)
{
SCI_ERROR_LOG(ret);
return ret;
}
ISR_Register(ST0_IRQn, uart0_interrupt_send); //´®¿Ú0·¢ËÍÖжϷþÎñ·¾¶×¢²á
ISR_Register(SR0_IRQn, uart0_interrupt_receive); //´®¿Ú0½ÓÊÕÖжϷþÎñ·¾¶×¢²á
return SCI_SUCCESS;
}
/******************************************************************************
* Function Name: Uart0_Send
* @brief UART0 Send data
* @param None
* @return None
*******************************************************************************/
void Uart0_Send(uint8_t ch)
{
UART_SendByte(UART0, ch);
}
/*****************************************************************************
* Function Name: Uart0_Receive
* @brief UART0 receive data
* @param None
* @return rx data
*****************************************************************************/
char Uart0_Receive(void)
{
return UART_ReceiveByte(UART0);
}
/*****************************************************************************
* Function Name: Uart0_IntSend
* @brief UART0 Send data by interrupt
* @param None
* @return None
*****************************************************************************/
void Uart0_IntSend(uint8_t *tx_buf, uint32_t tx_num)
{
pData.data = tx_buf;
pData.len = tx_num;
INTC_SetPendingIRQ(ST0_IRQn);
}
/****************************************************************************
* Function Name: uart0_interrupt_send
* @brief UART0 Send interrupt service routine
* @param None
* @return None
*****************************************************************************/
void uart0_interrupt_send(void *msg)
{
ATE_FRAME_t *pFrame = (ATE_FRAME_t *)msg;
INTC_ClearPendingIRQ(ST0_IRQn);
if ((pFrame->len > 0U) && pFrame->data)
{
UART0_TX = *pFrame->data;
pFrame->data++;
pFrame->len --;
}
else //send finished
{
}
}
/*****************************************************************************
* Function Name: uart_callback_error
* @brief
* @param None
* @return None
******************************************************************************/
void uart_callback_error(uint8_t err_type)
{
//user edit here when appear error
}
/*****************************************************************************
* Function Name: uart0_interrupt_receive
* @brief UART0 Receive interrupt service routine
* @param None
* @return None
*****************************************************************************/
void uart0_interrupt_receive(void)
{
volatile uint8_t rx_data;
volatile uint8_t err_type;
INTC_ClearPendingIRQ(SR0_IRQn);
err_type = UART_GetErrStaus(UART0, UART_FLAG_FEF | UART_FLAG_PEF | UART_FLAG_OVF);
if (err_type)
{
UART_ClearFlag(UART0,UART_FLAG_FEF | UART_FLAG_PEF | UART_FLAG_OVF);
uart_callback_error(err_type);
}
rx_data = UART0_RX;
UART_Put((uint16_t)rx_data);
// if ((UART0_RX_STA & 0x8000U) == 0) //½ÓÊÕδÍê³É
// {
// if (UART0_RX_STA & 0x4000U) //½ÓÊÕµ½0x0d
// {
// if (rx_data != 0x0a)
// {
// UART0_RX_BUF[UART0_RX_STA & 0x3fff] = rx_data;
// UART0_RX_STA++;
// }
// else
// {
// UART0_RX_STA |= 0x8000;
// UART0_RX_BUF[UART0_RX_STA & 0x3fff] = rx_data;
// UART0_RX_STA ++;
// }
// }
// else //»¹Î´½ÓÊÕµ½0x0d
// {
// if (rx_data == 0x0d)
// {
// UART0_RX_STA |= 0x4000;
// UART0_RX_BUF[UART0_RX_STA & 0x3fff] = rx_data;
// UART0_RX_STA ++;
// }
// else
// {
// UART0_RX_BUF[UART0_RX_STA & 0x3fff] = rx_data;
// UART0_RX_STA ++;
// }
// }
// }
// else if ((UART0_RX_STA & 0x8000U) == 1) // receive finished
// {
// }
}