-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstm32f103xx_uart.c
285 lines (244 loc) · 8.83 KB
/
stm32f103xx_uart.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
******************************************************************************
* @file stm32f103xx_uart_driver.c
* @author Vedant A. Rokad
* @processor ARM Cortex-M3
* @controller STM32F103C8T8
* @date 27-Augest-2022
* @brief Device_Driver source file
******************************************************************************
**/
#include "stm32f10x.h"
#include "stm32f103xx_uart.h"
#include <stdint.h>
/****************************************************************************************
* *
* USART_PERIPHERAL_CLOCK *
* *
/***************************************************************************************/
void USART_PeriClockControl(USART_TypeDef *pUSARTx, uint8_t EnorDi)
{
//check whether peripharal is enable or not
if(EnorDi == ENABLE)
{
//enable the usart peripheral
if(pUSARTx == USART1)
RCC->APB2ENR |= (1<<14);
else if(pUSARTx == USART2)
RCC->APB1ENR |= (1<<17);
else if(pUSARTx == USART3)
RCC->APB1ENR |= (1<<18);
}
else
{
//disable the usart peripheral
if(pUSARTx == USART1)
RCC->APB1ENR &= ~(1<<14);
else if(pUSARTx == USART2)
RCC->APB2ENR &= ~(1<<17);
else if(pUSARTx == USART3)
RCC->APB2ENR &= ~(1<<18);
}
}
/****************************************************************************************
* *
* USART_INITIALIZE *
* *
/***************************************************************************************/
void USART_Init(USART_Handle_t *pUSARTHandle)
{
// select the USART mode
// 1.USART_MODE_ONLY_Transmission
if(pUSARTHandle->USART_Config.USART_Mode == USART_MODE_ONLY_TX)
{
pUSARTHandle->pUSARTx->CR1 |= (1<<3);
pUSARTHandle->pUSARTx->CR1 &= ~(1<<2);
}
//2.USART_MODE_ONLY_Receiver
else if(pUSARTHandle->USART_Config.USART_Mode == USART_MODE_ONLY_RX)
{
pUSARTHandle->pUSARTx->CR1 &= ~(1<<3);
pUSARTHandle->pUSARTx->CR1 |= (1<<2);
}
//3.USART_MODE_Transmission_Receiver
else if(pUSARTHandle->USART_Config.USART_Mode == USART_MODE_TXRX)
{
pUSARTHandle->pUSARTx->CR1 |= (1<<3);
pUSARTHandle->pUSARTx->CR1 |= (1<<2);
}
//Set the baudrate
USART_Set_Baudrate(pUSARTHandle->pUSARTx,pUSARTHandle->USART_Config.USART_Baud);
//Configure Number of STOP bit
pUSARTHandle->pUSARTx->CR2 |= (pUSARTHandle->USART_Config.USART_NoOfStopBits << 12);
// USART_WordLength;
if(pUSARTHandle->USART_Config.USART_WordLength == USART_WORDLEN_8BITS)
pUSARTHandle->pUSARTx->CR1 &= ~(1<<12);
else
pUSARTHandle->pUSARTx->CR1 |= (1<<12);
//USART_Parity_Control
if(pUSARTHandle->USART_Config.USART_ParityControl == USART_PARITY_DISABLE)
pUSARTHandle->pUSARTx->CR1 &= ~(1<<10);
else
{
//USART_EVEN_PARITY
if(pUSARTHandle->USART_Config.USART_ParityControl == USART_PARITY_EN_EVEN)
{
pUSARTHandle->pUSARTx->CR1 |= (1<<10);
pUSARTHandle->pUSARTx->CR1 &= ~(1<<9);
}
//USART_ODD_PARITY
else
{
pUSARTHandle->pUSARTx->CR1 |= (1<<10);
pUSARTHandle->pUSARTx->CR1 |= (1<<9);
}
}
//USART_Hardware_Flow_Control
if(pUSARTHandle->USART_Config.USART_HWFlowControl == USART_HW_FLOW_CTRL_NONE)
{
pUSARTHandle->pUSARTx->CR3 &= ~(1<<8);
pUSARTHandle->pUSARTx->CR3 &= ~(1<<9);
}
else if(pUSARTHandle->USART_Config.USART_HWFlowControl == USART_HW_FLOW_CTRL_CTS)
{
pUSARTHandle->pUSARTx->CR3 &= ~(1<<8);
pUSARTHandle->pUSARTx->CR3 |= (1<<9);
}
else if(pUSARTHandle->USART_Config.USART_HWFlowControl == USART_HW_FLOW_CTRL_RTS)
{
pUSARTHandle->pUSARTx->CR3 |= (1<<8);
pUSARTHandle->pUSARTx->CR3 &= ~(1<<9);
}
else if(pUSARTHandle->USART_Config.USART_HWFlowControl == USART_HW_FLOW_CTRL_CTS_RTS)
{
pUSARTHandle->pUSARTx->CR3 |= (1<<8);
pUSARTHandle->pUSARTx->CR3 |= (1<<9);
}
}
/****************************************************************************************
* *
* USART_SEND_DATA *
* *
/***************************************************************************************/
void USART_SendData(USART_Handle_t *pUSARTHandle,uint8_t *pTxBuffer, uint32_t Len)
{
while(Len--)
{
while(! USART_GETFlagStatus(USART_FLAG_TXE,pUSARTHandle->pUSARTx));
uint16_t *pdata;
if(pUSARTHandle->USART_Config.USART_WordLength == USART_WORDLEN_9BITS)
{
pdata = (uint16_t*)pTxBuffer;
pUSARTHandle->pUSARTx->DR = (*pdata & (uint16_t)0x01FF);
if(pUSARTHandle->USART_Config.USART_ParityControl == USART_PARITY_DISABLE)
{
pTxBuffer++;
pTxBuffer++;
}
else{
pTxBuffer++;
}
}
else
{
pUSARTHandle->pUSARTx->DR = (*pTxBuffer & (uint8_t)0xFF);
pTxBuffer++;
}
while(! USART_GETFlagStatus(USART_FLAG_TC , pUSARTHandle->pUSARTx));
}
}
/****************************************************************************************
* *
* USART_GET_FLAG_STATUS *
* *
/***************************************************************************************/
uint8_t USART_GETFlagStatus(uint16_t flagname , USART_TypeDef *pUSARTx)
{
if(pUSARTx->SR & flagname)
{
return FLAG_SET;
}
else
{
return FLAG_RESET;
}
}
/****************************************************************************************
* *
* USART_ClearFlag *
* *
/***************************************************************************************/
void USART_ClearFlag(USART_TypeDef *pUSARTx, uint16_t StatusFlagName)
{
pUSARTx->SR &= ~(StatusFlagName);
}
/****************************************************************************************
* *
* USART_SET_BAUDRATE *
* *
/***************************************************************************************/
void USART_Set_Baudrate(USART_TypeDef *pUSARTx , uint32_t baudrate)
{
uint32_t M_Part , F_Part ;
uint32_t temp;
uint32_t pclk1 = 36000000 ;
uint32_t pclk2 = 72000000 ;
if(pUSARTx == USART1)
{
temp = ((pclk2 * 25) / (4 * 9600) ) ;
}
else
{
temp = ((pclk1 * 25 ) / (4 * baudrate) ) ;
}
M_Part = temp / 100 ;
F_Part = ((temp - (M_Part*100))*16)/100 ;
pUSARTx->BRR |= (M_Part << 4);
pUSARTx->BRR |= (F_Part << 0);
}
/****************************************************************************************
* *
* USART_ENABLE *
* *
/***************************************************************************************/
void USART_Enable(USART_TypeDef *pUSARTx , uint8_t EnorDi)
{
if(EnorDi == ENABLE)
{
pUSARTx->CR1 |= (1 << 13 );
}
else
{
pUSARTx->CR1 &= ~(1 << 13 );
}
}
/****************************************************************************************
* *
* USART_Interrupt_IRQ_Control *
* *
/***************************************************************************************/
void USART_Interrupt_IRQ_Control(USART_TypeDef *pusart,uint16_t Enordi)
{
if(Enordi == ENABLE)
{
if(pusart == USART1)
NVIC_EnableIRQ(37);
else if(pusart == USART1)
NVIC_EnableIRQ(38);
else if(pusart == USART1)
NVIC_EnableIRQ(39);
}
else
{
if(pusart == USART1)
NVIC_DisableIRQ(37);
else if(pusart == USART1)
NVIC_DisableIRQ(38);
else if(pusart == USART1)
NVIC_DisableIRQ(39);
}
}
void USART_IRQPriorityConfig(uint8_t IRQNumber, uint32_t IRQPriority)
{
NVIC_SetPriority(IRQNumber,IRQPriority);
}