-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal_usart.c
443 lines (409 loc) · 15.9 KB
/
hal_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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* File : hal_usart.c
* Author : Mohamed Ahmed Abdel Wahab
* LinkedIn : https://www.linkedin.com/in/mohamed-abdel-wahab-162413253/
* Github : https://github.com/moabdelwahab6611
* Created on June 13, 2023, 9:22 PM
*/
/**************************Includes-Section*****************************/
#include "hal_usart.h"
/***********************************************************************/
/*****************Helper Functions Declarations-Section*****************/
/*
* @Brief : Callback pointer to function.
*/
#if USART_TX_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
static void (*USART_TxDefaultInterruptHandler)(void) = NULL; /* @Brief : USART Transmit Interrupt Handler. */
#endif
/*
* @Brief : Callback pointer to function.
*/
#if USART_RX_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
static void (*USART_RxDefaultInterruptHandler)(void) = NULL; /* @Brief : USART Receive Interrupt Handler. */
static void (*USART_FramingErrorHandler)(void) = NULL; /* @Brief : USART Framing Error Interrupt Handler. */
static void (*USART_OverrunErrorHandler)(void) = NULL; /* @Brief : USART Overrun Error Interrupt Handler. */
#endif
/*
* @Brief : To calculate baud rate.
* @Param _usart : Pointer to the USART module configurations.
*/
static void USART_Baud_Rate_Calculation(const usart_t *_usart);
/*
* @Brief : To initialize USART Transmit pin in asynchronous mode.
* @Param _usart : Pointer to the USART module configurations.
*/
static void USART_ASYNCRONOUS_TX_Init(const usart_t *_usart);
/*
* @Brief : To initialize USART Receive pin in asynchronous mode.
* @Param _usart : Pointer to the USART module configurations.
*/
static void USART_ASYNCRONOUS_RX_Init(const usart_t *_usart);
/***********************************************************************/
/*****************Software Interfaces Functions-Section*****************/
/*
* @Brief : Initialization of USART communication peripheral.
* @Param _usart : Pointer to the USART module configurations.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
Std_ReturnType USART_Asynchronous_Initialize(const usart_t *_usart)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _usart)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Disable USART. */
RCSTAbits.SPEN = USART_DISABLE_MODULE;
/* @Brief : TX and RX pins configuration. */
TRISCbits.RC7 = 1; /* RX : Input Configuration if needed */
TRISCbits.RC6 = 1; /* TX : Input Configuration if needed */
/* @Brief : Baud rate calculation. */
USART_Baud_Rate_Calculation(_usart);
/* @Brief : To initialize USART Transmit pin in asynchronous mode. */
USART_ASYNCRONOUS_TX_Init(_usart);
/* @Brief : To initialize USART Receive pin in asynchronous mode. */
USART_ASYNCRONOUS_RX_Init(_usart);
/* @Brief : Enable USART. */
RCSTAbits.SPEN = USART_ENABLE_MODULE;
ret = E_OK;
}
return ret;
}
/*
* @Brief : De-initialization of USART communication peripheral.
* @Param _usart : Pointer to the USART module configurations.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
Std_ReturnType USART_Asynchronous_DeInitialize(const usart_t *_usart)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _usart)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Disable USART. */
RCSTAbits.SPEN = USART_DISABLE_MODULE;
ret = E_OK;
}
return ret;
}
/*
* @Brief : To read byte from a Receive data register.
* @Param _data : Pointer to the _data stored in a receive data register.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
Std_ReturnType USART_Asynchronous_ReadByte_Blocking(uint8 *_data)
{
Std_ReturnType ret = E_NOT_OK;
while(!PIR1bits.RCIF);
*_data = RCREG;
return ret;
}
/*
* @Brief : To read byte from a Receive data register.
* @Param _data : Pointer to the _data stored in a receive data register.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
Std_ReturnType USART_Asynchronous_ReadByte_NonBlocking(uint8 *_data)
{
Std_ReturnType ret = E_NOT_OK;
if(1 == PIR1bits.RCIF)
{
*_data = RCREG;
ret = E_OK;
}
else
{
ret = E_NOT_OK;
}
return ret;
}
/*
* @Brief : To restart Receiver in USART asynchronous mode.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
Std_ReturnType USART_Asynchronous_Restart_RX(void)
{
Std_ReturnType ret = E_OK;
/* @Brief : To disable Receiver in USART asynchronous mode.*/
RCSTAbits.CREN = 0;
/* @Brief : To enable Receiver in USART asynchronous mode.*/
RCSTAbits.CREN = 1;
return ret;
}
/*
* @Brief : To write byte in a Transmit data register.
* @Param _data
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
Std_ReturnType USART_Asynchronous_WriteByte_Blocking(uint8 _data)
{
Std_ReturnType ret = E_OK;
while(!TXSTAbits.TRMT);
#if USART_TX_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
/* @Brief : Enable the interrupt. */
USART_TX_InterruptEnable();
#endif
TXREG = _data;
return ret;
}
/*
* @Brief : To write byte in a Transmit data register.
* @Param _data
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
Std_ReturnType USART_Asynchronous_WriteByte_NonBlocking(uint8 _data)
{
Std_ReturnType ret = E_NOT_OK;
uint16 char_counter = ZERO_INT;
if(1 == TXSTAbits.TRMT)
{
#if USART_TX_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
/* @Brief : Enable the interrupt. */
USART_TX_InterruptEnable();
#endif
TXREG = _data;
ret = E_OK;
}
else
{
ret = E_NOT_OK;
}
return ret;
}
/*
* @Brief : To write a string.
* @Param _data : Pointer to the _data stored in a receive data register.
* @Param str_length
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
Std_ReturnType USART_Asynchronous_WriteString_Blocking(uint8 *_data, uint16 str_length)
{
Std_ReturnType ret = E_NOT_OK;
uint16 char_counter = ZERO_INT;
for(char_counter = ZERO_INT; char_counter < str_length; char_counter++)
{
ret = USART_Asynchronous_WriteByte_Blocking(_data[char_counter]);
}
return ret;
}
/*
* @Brief : To calculate baud rate.
* @Param _usart : Pointer to the USART module configurations.
*/
static void USART_Baud_Rate_Calculation(const usart_t *_usart)
{
float Baud_Rate_Temp_Value = 0;
switch(_usart->baudrate_config)
{
case BAUDRATE_ASYNCHRONOUS_MODE_8BIT_LOW_SPEED :
TXSTAbits.SYNC = USART_ASYNCHRONOUS_MODE;
TXSTAbits.BRGH = USART_ASYNCHRONOUS_MODE_LOW_SPEED;
BAUDCONbits.BRG16 = USART_8BIT_BAUD_GEN;
Baud_Rate_Temp_Value = ((_XTAL_FREQ / (float)_usart->baudrate) / 64) - 1;
break;
case BAUDRATE_ASYNCHRONOUS_MODE_8BIT_HIGH_SPEED :
TXSTAbits.SYNC = USART_ASYNCHRONOUS_MODE;
TXSTAbits.BRGH = USART_ASYNCHRONOUS_MODE_HIGH_SPEED;
BAUDCONbits.BRG16 = USART_8BIT_BAUD_GEN;
Baud_Rate_Temp_Value = ((_XTAL_FREQ / (float)_usart->baudrate) / 16) - 1;
break;
case BAUDRATE_ASYNCHRONOUS_MODE_16BIT_LOW_SPEED :
TXSTAbits.SYNC = USART_ASYNCHRONOUS_MODE;
TXSTAbits.BRGH = USART_ASYNCHRONOUS_MODE_LOW_SPEED;
BAUDCONbits.BRG16 = USART_16BIT_BAUD_GEN;
Baud_Rate_Temp_Value = ((_XTAL_FREQ / (float)_usart->baudrate) / 16) - 1;
break;
case BAUDRATE_ASYNCHRONOUS_MODE_16BIT_HIGH_SPEED :
TXSTAbits.SYNC = USART_ASYNCHRONOUS_MODE;
TXSTAbits.BRGH = USART_ASYNCHRONOUS_MODE_HIGH_SPEED;
BAUDCONbits.BRG16 = USART_16BIT_BAUD_GEN;
Baud_Rate_Temp_Value = ((_XTAL_FREQ / (float)_usart->baudrate) / 4) - 1;
break;
case BAUDRATE_SYNCHRONOUS_MODE_8BIT :
TXSTAbits.SYNC = USART_SYNCHRONOUS_MODE;
BAUDCONbits.BRG16 = USART_8BIT_BAUD_GEN;
Baud_Rate_Temp_Value = ((_XTAL_FREQ / (float)_usart->baudrate) / 4) - 1;
break;
case BAUDRATE_SYNCHRONOUS_MODE_16BIT :
TXSTAbits.SYNC = USART_SYNCHRONOUS_MODE;
BAUDCONbits.BRG16 = USART_16BIT_BAUD_GEN;
Baud_Rate_Temp_Value = ((_XTAL_FREQ / (float)_usart->baudrate) / 4) - 1;
break;
default : ;
}
SPBRG = (uint8)((uint32)Baud_Rate_Temp_Value);
SPBRGH = (uint8)(((uint32)Baud_Rate_Temp_Value) >> 8);
}
/*
* @Brief : To initialize USART Transmit pin in asynchronous mode.
* @Param _usart : Pointer to the USART module configurations.
*/
static void USART_ASYNCRONOUS_TX_Init(const usart_t *_usart)
{
if(USART_ENABLE_ASYNCHRONOUS_MODE_TX == _usart->usart_tx_cfg.usart_enable_tx)
{
/* @Brief : USART Transmit enable or disable configuration. */
TXSTAbits.TXEN = USART_ENABLE_ASYNCHRONOUS_MODE_TX;
/* @Brief : USART Transmit interrupt configuration. */
USART_TxDefaultInterruptHandler = _usart->USART_TxDefaultInterruptHandler;
/* @Brief : USART Transmit interrupt configuration. */
if(USART_ENABLE_ASYNCHRONOUS_MODE_TX_INTERRUPT == _usart->usart_tx_cfg.usart_enable_tx_interrupt)
{
PIE1bits.TXIE = USART_ENABLE_ASYNCHRONOUS_MODE_TX_INTERRUPT;
/* @Brief : USART Interrupt configuration. */
#if USART_TX_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
USART_TX_InterruptEnable();
/* @Brief : USART Priority configuration. */
#if INTERRUPT_PRIORITY_LEVELS_ENABLE==INTERRUPT_FEATURE_ENABLE
INTERRUPT_PriorityLevelsEnable();
if(INTERRUPT_HIGH_PRIORITY == _usart->usart_tx_cfg.usart_tx_interrupt_priority)
{
INTERRUPT_GlobalInterruptHighEnable();
USART_TX_HighPrioritySet();
}
else if(INTERRUPT_LOW_PRIORITY == _usart->usart_tx_cfg.usart_tx_interrupt_priority)
{
INTERRUPT_GlobalInterruptLowEnable();
USART_TX_LowPrioritySet();
}
else{/*****Nothing*****/}
#else
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
#endif
#endif
}
else if(USART_DISABLE_ASYNCHRONOUS_MODE_TX_INTERRUPT == _usart->usart_tx_cfg.usart_enable_tx_interrupt)
{
PIE1bits.TXIE = USART_DISABLE_ASYNCHRONOUS_MODE_TX_INTERRUPT;
}
else{/*****Nothing*****/}
/* @Brief : USART 9-Bit Transmit configuration. */
if(USART_ENABLE_ASYNCHRONOUS_MODE_9BIT_TX == _usart->usart_tx_cfg.usart_enable_9bit_tx)
{
TXSTAbits.TX9 = USART_ENABLE_ASYNCHRONOUS_MODE_9BIT_TX;
}
else if(USART_DISABLE_ASYNCHRONOUS_MODE_9BIT_TX == _usart->usart_tx_cfg.usart_enable_9bit_tx)
{
TXSTAbits.TX9 = USART_DISABLE_ASYNCHRONOUS_MODE_9BIT_TX;
}
else{/*****Nothing*****/}
}
else{/*****Nothing*****/}
}
/*
* @Brief : To initialize USART Receive pin in asynchronous mode.
* @Param _usart : Pointer to the USART module configurations.
*/
static void USART_ASYNCRONOUS_RX_Init(const usart_t *_usart)
{
if(USART_ENABLE_ASYNCHRONOUS_MODE_RX == _usart->usart_rx_cfg.usart_enable_rx)
{
/* @Brief : USART Receive enable or disable configuration. */
RCSTAbits.CREN = USART_ENABLE_ASYNCHRONOUS_MODE_RX;
/* @Brief : USART Receive interrupt configuration. */
USART_RxDefaultInterruptHandler = _usart->USART_RxDefaultInterruptHandler;
/* @Brief : USART Receive error status configuration. */
USART_FramingErrorHandler = _usart->USART_FramingErrorHandler;
USART_OverrunErrorHandler = _usart->USART_OverrunErrorHandler;
/* @Brief : USART Receive interrupt configuration. */
if(USART_ENABLE_ASYNCHRONOUS_MODE_RX_INTERRUPT == _usart->usart_rx_cfg.usart_enable_rx_interrupt)
{
PIE1bits.RCIE = USART_ENABLE_ASYNCHRONOUS_MODE_RX_INTERRUPT;
/* @Brief : USART Interrupt configuration. */
#if USART_RX_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
USART_RX_InterruptEnable();
/* @Brief : USART Priority configuration. */
#if INTERRUPT_PRIORITY_LEVELS_ENABLE==INTERRUPT_FEATURE_ENABLE
INTERRUPT_PriorityLevelsEnable();
if(INTERRUPT_HIGH_PRIORITY == _usart->usart_rx_cfg.usart_rx_interrupt_priority)
{
INTERRUPT_GlobalInterruptHighEnable();
USART_RX_HighPrioritySet();
}
else if(INTERRUPT_LOW_PRIORITY == _usart->usart_rx_cfg.usart_rx_interrupt_priority)
{
INTERRUPT_GlobalInterruptLowEnable();
USART_RX_LowPrioritySet();
}
else{/*****Nothing*****/}
#else
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
#endif
#endif
}
else if(USART_DISABLE_ASYNCHRONOUS_MODE_RX_INTERRUPT == _usart->usart_rx_cfg.usart_enable_rx_interrupt)
{
PIE1bits.RCIE = USART_DISABLE_ASYNCHRONOUS_MODE_RX_INTERRUPT;
}
else{/*****Nothing*****/}
/* @Brief : USART 9-Bit Receive configuration. */
if(USART_ENABLE_ASYNCHRONOUS_MODE_9BIT_RX == _usart->usart_rx_cfg.usart_enable_9bit_rx)
{
RCSTAbits.RX9 = USART_ENABLE_ASYNCHRONOUS_MODE_9BIT_RX;
}
else if(USART_DISABLE_ASYNCHRONOUS_MODE_9BIT_RX == _usart->usart_rx_cfg.usart_enable_9bit_rx)
{
RCSTAbits.RX9 = USART_DISABLE_ASYNCHRONOUS_MODE_9BIT_RX;
}
else{/*****Nothing*****/}
}
else{/*****Nothing*****/}
}
/*
* @Brief : Callback pointer to function to USART Transmit interrupt service routine.
*/
void USART_TX_ISR(void)
{
/* @Brief : Disable the interrupt. */
USART_TX_InterruptDisable();
if(USART_TxDefaultInterruptHandler)
{
USART_TxDefaultInterruptHandler();
}
else{/*****Nothing*****/}
}
/*
* @Brief : Callback pointer to function to USART Receive interrupt service routine.
*/
void USART_RX_ISR(void)
{
if(USART_RxDefaultInterruptHandler)
{
USART_RxDefaultInterruptHandler();
}
else{/*****Nothing*****/}
if(USART_FramingErrorHandler)
{
USART_FramingErrorHandler();
}
else{/*****Nothing*****/}
if(USART_OverrunErrorHandler)
{
USART_OverrunErrorHandler();
}
else{/*****Nothing*****/}
}
/***********************************************************************/