-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal_gpio.c
301 lines (288 loc) · 9.45 KB
/
hal_gpio.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
/*
* File : hal_gpio.c
* Author : Mohamed Ahmed Abdel Wahab
* LinkedIn : https://www.linkedin.com/in/mohamed-abdel-wahab-162413253/
* Github : https://github.com/moabdelwahab6611
* Created on May 21, 2023, 5:38 PM
*/
/**************************Includes-Section*****************************/
#include "hal_gpio.h"
/***********************************************************************/
/*****************Helper Functions Declarations-Section*****************/
/***********Reference to the Data Direction Control Registers***********/
volatile uint8 *tris_registers[] = {&TRISA, &TRISB, &TRISC, &TRISD, &TRISE};
/**Reference to the Data Latch Register (Read and Write to Data Latch)**/
volatile uint8 *lat_registers[] = {&LATA , &LATB , &LATC , &LATD , &LATE};
/*****************Reference to the Port Status Register*****************/
volatile uint8 *port_registers[] = {&PORTA, &PORTB, &PORTC, &PORTD, &PORTE};
/***********************************************************************/
/*****************Software Interfaces Functions-Section*****************/
/*
* @Brief : To initialize the direction of a specific pin @Ref : direction_t.
* @Param _pin_config : Pointer to the GPIO module configurations @Ref : pin_config_t.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
#if GPIO_PORT_PIN_CONFIGURATIONS==CONFIG_ENABLE
Std_ReturnType gpio_pin_direction_intialize(const pin_config_t *_pin_config)
{
Std_ReturnType ret = E_OK;
if(NULL == _pin_config || _pin_config->pin > PORT_PIN_MAX_NUMBER-1)
{
ret = E_NOT_OK;
}
else
{
switch(_pin_config->direction)
{
case GPIO_DIRECTION_OUTPUT :
CLEAR_BIT(*tris_registers[_pin_config->port], _pin_config->pin);
break;
case GPIO_DIRECTION_INPUT :
SET_BIT(*tris_registers[_pin_config->port], _pin_config->pin);
break;
default : ret = E_NOT_OK;
}
}
return ret;
}
#endif
/*
* @Brief : To get direction status of a pin.
* @Param _pin_config : Pointer to the GPIO module configurations @Ref : pin_config_t.
* @Param direction_status : Pointer to the pin direction status configurations.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
#if GPIO_PORT_PIN_CONFIGURATIONS==CONFIG_ENABLE
Std_ReturnType gpio_pin_get_direction_status(const pin_config_t *_pin_config, direction_t *direction_status)
{
Std_ReturnType ret = E_OK;
if(NULL == _pin_config || NULL == direction_status || _pin_config->pin > PORT_PIN_MAX_NUMBER-1)
{
ret = E_NOT_OK;
}
else
{
*direction_status = READ_BIT(*tris_registers[_pin_config->port], _pin_config->pin);
}
return ret;
}
#endif
/*
* @Brief : To write logic 1 or 0 on a pin.
* @Param _pin_config : Pointer to the GPIO module configurations @Ref : pin_config_t.
* @Param logic
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
#if GPIO_PORT_PIN_CONFIGURATIONS==CONFIG_ENABLE
Std_ReturnType gpio_pin_write_logic(const pin_config_t *_pin_config, logic_t logic)
{
Std_ReturnType ret = E_OK;
if(NULL == _pin_config || _pin_config->pin > PORT_PIN_MAX_NUMBER-1)
{
ret = E_NOT_OK;
}
else
{
switch(logic)
{
case GPIO_LOW :
CLEAR_BIT(*lat_registers[_pin_config->port], _pin_config->pin);
break;
case GPIO_HIGH :
SET_BIT(*lat_registers[_pin_config->port], _pin_config->pin);
break;
default : ret = E_NOT_OK;
}
}
return ret;
}
#endif
/*
* @Brief : To read logic on a pin.
* @Param _pin_config : Pointer to the GPIO module configurations @Ref : pin_config_t.
* @Param logic : Pointer to the pin logic status configurations.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
#if GPIO_PORT_PIN_CONFIGURATIONS==CONFIG_ENABLE
Std_ReturnType gpio_pin_read_logic(const pin_config_t *_pin_config, logic_t *logic)
{
Std_ReturnType ret = E_OK;
if(NULL == _pin_config || NULL == logic || _pin_config->pin > PORT_PIN_MAX_NUMBER-1)
{
ret = E_NOT_OK;
}
else
{
*logic = READ_BIT(*port_registers[_pin_config->port], _pin_config->pin);
}
return ret;
}
#endif
/*
* @Brief : To toggle the pin logic.
* @Param _pin_config : Pointer to the GPIO module configurations @Ref : pin_config_t.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
#if GPIO_PORT_PIN_CONFIGURATIONS==CONFIG_ENABLE
Std_ReturnType gpio_pin_toggle_logic(const pin_config_t *_pin_config)
{
Std_ReturnType ret = E_OK;
if(NULL == _pin_config || _pin_config->pin > PORT_PIN_MAX_NUMBER-1)
{
ret = E_NOT_OK;
}
else
{
TOGGLE_BIT(*lat_registers[_pin_config->port], _pin_config->pin);
}
return ret;
}
#endif
/*
* @Brief : To initialize certain pin.
* @Param _pin_config : Pointer to the GPIO module configurations @Ref : pin_config_t.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
#if GPIO_PORT_PIN_CONFIGURATIONS==CONFIG_ENABLE
Std_ReturnType gpio_pin_intialize(const pin_config_t *_pin_config)
{
Std_ReturnType ret = E_OK;
if(NULL == _pin_config || _pin_config->pin > PORT_PIN_MAX_NUMBER-1)
{
ret = E_NOT_OK;
}
else
{
ret = gpio_pin_direction_intialize(_pin_config);
ret = gpio_pin_write_logic(_pin_config, _pin_config->logic);
}
return ret;
}
#endif
/*
* @Brief : To initialize the direction of the whole port.
* @Param port
* @Param direction
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
#if GPIO_PORT_CONFIGURATIONS==CONFIG_ENABLE
Std_ReturnType gpio_port_direction_intialize(port_index_t port, uint8 direction)
{
Std_ReturnType ret = E_OK;
if(port > PORT_MAX_NUMBER-1)
{
ret = E_NOT_OK;
}
else
{
*tris_registers[port] = direction;
}
return ret;
}
#endif
/*
* @Brief : To get the whole port direction status.
* @Param port
* @Param direction_status : Pointer to get whole port direction status configurations.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
#if GPIO_PORT_CONFIGURATIONS==CONFIG_ENABLE
Std_ReturnType gpio_port_get_direction_status(port_index_t port, uint8 *direction_status)
{
Std_ReturnType ret = E_OK;
if((NULL == direction_status) && (port > PORT_MAX_NUMBER-1))
{
ret = E_NOT_OK;
}
else
{
*direction_status = *tris_registers[port];
}
return ret;
}
#endif
/*
* @Brief : To write logic 1 or 0 on the whole port.
* @Param port
* @Param logic
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
#if GPIO_PORT_CONFIGURATIONS==CONFIG_ENABLE
Std_ReturnType gpio_port_write_logic(port_index_t port, uint8 logic)
{
Std_ReturnType ret = E_OK;
if(port > PORT_MAX_NUMBER-1)
{
ret = E_NOT_OK;
}
else
{
*lat_registers[port] = logic;
}
return ret;
}
#endif
/*
* @Brief : To read the logic on the whole port.
* @Param port
* @Param logic : Pointer to the read whole port logic configurations.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
#if GPIO_PORT_CONFIGURATIONS==CONFIG_ENABLE
Std_ReturnType gpio_port_read_logic(port_index_t port, uint8 *logic)
{
Std_ReturnType ret = E_OK;
if((NULL == logic) && (port > PORT_MAX_NUMBER-1))
{
ret = E_NOT_OK;
}
else
{
*logic = *lat_registers[port];
}
return ret;
}
#endif
/*
* @Brief : To toggle port logic.
* @Param port
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue to perform this action.
*/
#if GPIO_PORT_CONFIGURATIONS==CONFIG_ENABLE
Std_ReturnType gpio_port_toggle_logic(port_index_t port)
{
Std_ReturnType ret = E_OK;
if(port > PORT_MAX_NUMBER-1)
{
ret = E_NOT_OK;
}
else
{
*lat_registers[port] ^= PORTC_MASK;
}
return ret;
}
#endif
/***********************************************************************/