-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal_usart.h
217 lines (187 loc) · 10.9 KB
/
hal_usart.h
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
/*
* File : hal_usart.h
* 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
*/
#ifndef HAL_USART_H
#define HAL_USART_H
/**************************Includes-Section*****************************/
#include "proc/pic18f4620.h"
#include "../mcal_std_types.h"
#include "../GPIO/hal_gpio.h"
#include "../Interrupt/mcal_internal_interrupt.h"
#include "hal_usart_cfg.h"
/***********************************************************************/
/**********************Macro Declarations-Section***********************/
/* @Brief : Macro to enable USART module. */
#define USART_ENABLE_MODULE 1
/* @Brief : Macro to disable USART module. */
#define USART_DISABLE_MODULE 0
/* @Brief : Macro for USART to work in Synchronous mode. */
#define USART_SYNCHRONOUS_MODE 1
/* @Brief : Macro for USART to work in Asynchronous mode. */
#define USART_ASYNCHRONOUS_MODE 0
/* @Brief : Macro for USART to work in Asynchronous mode High baud generated speed. */
#define USART_ASYNCHRONOUS_MODE_HIGH_SPEED 1
/* @Brief : Macro for USART to work in Asynchronous mode Low baud generated speed. */
#define USART_ASYNCHRONOUS_MODE_LOW_SPEED 0
/* @Brief : Macrosfor selection USART 16-Bit baud rate generator register size. */
#define USART_16BIT_BAUD_GEN 1
/* @Brief : Macro for selection USART 8-Bit baud rate generator register size. */
#define USART_8BIT_BAUD_GEN 0
/* @Brief : Macro to enable USART Transmit in Asynchronous mode. */
#define USART_ENABLE_ASYNCHRONOUS_MODE_TX 1
/* @Brief : Macro to disable USART Transmit in Asynchronous mode. */
#define USART_DISABLE_ASYNCHRONOUS_MODE_TX 0
/* @Brief : Macro to enable USART Transmit interrupt in Asynchronous mode. */
#define USART_ENABLE_ASYNCHRONOUS_MODE_TX_INTERRUPT 1
/* @Brief : Macro to disable USART Transmit interrupt in Asynchronous mode. */
#define USART_DISABLE_ASYNCHRONOUS_MODE_TX_INTERRUPT 0
/* @Brief : Macro to enable USART Transmit in 9-Bit in Asynchronous mode. */
#define USART_ENABLE_ASYNCHRONOUS_MODE_9BIT_TX 1
/* @Brief : Macro to disable USART Transmit in 9-Bit in Asynchronous mode. */
#define USART_DISABLE_ASYNCHRONOUS_MODE_9BIT_TX 0
/* @Brief : Macro to enable USART Receiving in Asynchronous mode. */
#define USART_ENABLE_ASYNCHRONOUS_MODE_RX 1
/* @Brief : Macro to disable USART Receiving in Asynchronous mode. */
#define USART_DISABLE_ASYNCHRONOUS_MODE_RX 0
/* @Brief : Macro to enable USART Receiving interrupt in Asynchronous mode. */
#define USART_ENABLE_ASYNCHRONOUS_MODE_RX_INTERRUPT 1
/* @Brief : Macro to disable USART Receiving interrupt in Asynchronous mode. */
#define USART_DISABLE_ASYNCHRONOUS_MODE_RX_INTERRUPT 0
/* @Brief : Macro to enable USART Receiving in 9-Bit in Asynchronous mode. */
#define USART_ENABLE_ASYNCHRONOUS_MODE_9BIT_RX 1
/* @Brief : Macros to disable USART Receiving in 9-Bit in Asynchronous mode. */
#define USART_DISABLE_ASYNCHRONOUS_MODE_9BIT_RX 0
/* @Brief : Macros to USART for framing error indication configurations. */
#define USART_FRAMING_ERROR_DETECTED 1
#define USART_FRAMING_ERROR_CLEARED 0
/* @Brief : Macros to USART for overrun error indication configurations. */
#define USART_OVERRUN_ERROR_DETECTED 1
#define USART_OVERRUN_ERROR_CLEARED 0
/***********************************************************************/
/******************Macro Function Declarations-Section******************/
/***********************************************************************/
/********************Data Types Declarations-Section********************/
/* @Brief : Baud rate speed configuration for USART. */
typedef enum
{
BAUDRATE_ASYNCHRONOUS_MODE_8BIT_LOW_SPEED, /* @Brief : Low Baud rate speed with 8-Bit Register in Asynchronous mod configuration for USART. */
BAUDRATE_ASYNCHRONOUS_MODE_8BIT_HIGH_SPEED, /* @Brief : High Baud rate speed with 8-Bit Register in Asynchronous mod configuration for USART. */
BAUDRATE_ASYNCHRONOUS_MODE_16BIT_LOW_SPEED, /* @Brief : Low Baud rate speed with 16-Bit Register in Asynchronous mod configuration for USART. */
BAUDRATE_ASYNCHRONOUS_MODE_16BIT_HIGH_SPEED, /* @Brief : High Baud rate speed with 16-Bit Register in Asynchronous mod configuration for USART. */
BAUDRATE_SYNCHRONOUS_MODE_8BIT, /* @Brief : Baud rate speed with 8-Bit Register in Synchronous mod configuration for USART. */
BAUDRATE_SYNCHRONOUS_MODE_16BIT /* @Brief : Baud rate speed with 16-Bit Register in Synchronous mod configuration for USART. */
}baudrate_gen_t;
/* @Brief : Transmit configuration for USART. */
typedef struct
{
uint8 usart_tx_reserved : 5; /* @Brief : USART Transmit Reserved Bits. */
uint8 usart_enable_tx : 1; /* @Brief : USART Enabling or Disabling Transmit configuration. */
uint8 usart_enable_tx_interrupt : 1; /* @Brief : USART Transmit Interrupt configuration. */
uint8 usart_enable_9bit_tx : 1; /* @Brief : USART Enabling or Disabling 9-Bit Transmission configuration. */
interrupt_priority_cfg usart_tx_interrupt_priority; /* @Brief : USART Transmit Interrupt Priority configuration. */
}usart_tx_cfg_t;
/* @Brief : Receiving configuration for USART. */
typedef struct
{
uint8 usart_rx_reserved : 5; /* @Brief : USART Receive Reserved Bits. */
uint8 usart_enable_rx : 1; /* @Brief : USART Enabling or Disabling Receive configuration. */
uint8 usart_enable_rx_interrupt : 1; /* @Brief : USART Receive Interrupt configuration. */
uint8 usart_enable_9bit_rx : 1; /* @Brief : USART Enabling or Disabling 9-Bit Receiving configuration. */
interrupt_priority_cfg usart_rx_interrupt_priority; /* @Brief : USART Receive Interrupt Priority configuration. */
}usart_rx_cfg_t;
/* @Brief : Error status configuration for USART. */
typedef union
{
struct
{
uint8 usart_tx_reserved : 6; /* @Brief : USART Error Status Reserved Bits. */
uint8 usart_ferr : 1; /* @Brief : USART Framing Error configuration. */
uint8 usart_oerr : 1; /* @Brief : USART Overrun Error configuration. */
};
uint8 status;
}usart_error_status_t;
/* @Brief : USART configurations. */
typedef struct
{
uint32 baudrate; /* @Brief : USART Baud Rate configuration. */
baudrate_gen_t baudrate_config; /* @Brief : USART Baud Rate configuration. */
usart_tx_cfg_t usart_tx_cfg; /* @Brief : USART Transmit configuration. */
usart_rx_cfg_t usart_rx_cfg; /* @Brief : USART Receive configuration. */
usart_error_status_t eusart_error_status; /* @Brief : USART Error Status configuration. */
void (*USART_TxDefaultInterruptHandler)(void); /* @Brief : USART Transmit Interrupt Handler. */
void (*USART_RxDefaultInterruptHandler)(void); /* @Brief : USART Receive Interrupt Handler. */
void (*USART_FramingErrorHandler)(void); /* @Brief : USART Framing Error Interrupt Handler. */
void (*USART_OverrunErrorHandler)(void); /* @Brief : USART Overrun Error Interrupt Handler. */
}usart_t;
/***********************************************************************/
/**********Software Interfaces Functions Declarations-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);
/*
* @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);
/*
* @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);
/*
* @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);
/*
* @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);
/*
* @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);
/*
* @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);
/*
* @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);
/***********************************************************************/
#endif /* HAL_USART_H */