-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio.c
331 lines (320 loc) · 6.79 KB
/
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
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
/******************************************************************************
*
* Module: GPIO
*
* File Name: gpio.c
*
* Description: Source file for the AVR GPIO driver
*
* Author: Mohamed Tarek
*
*******************************************************************************/
#include "gpio.h"
#include "common_macros.h" /* To use the macros like SET_BIT */
#include "avr/io.h" /* To use the IO Ports Registers */
/*
* Description :
* Setup the direction of the required pin input/output.
* If the input port number or pin number are not correct, The function will not handle the request.
*/
void GPIO_setupPinDirection(uint8 port_num, uint8 pin_num, GPIO_PinDirectionType direction)
{
/*
* Check if the input port number is greater than NUM_OF_PINS_PER_PORT value.
* Or if the input pin number is greater than NUM_OF_PINS_PER_PORT value.
* In this case the input is not valid port/pin number
*/
if((pin_num >= NUM_OF_PINS_PER_PORT) || (port_num >= NUM_OF_PORTS))
{
/* Do Nothing */
}
else
{
/* Setup the pin direction as required */
switch(port_num)
{
case PORTA_ID:
if(direction == PIN_OUTPUT)
{
SET_BIT(DDRA,pin_num);
}
else
{
CLEAR_BIT(DDRA,pin_num);
}
break;
case PORTB_ID:
if(direction == PIN_OUTPUT)
{
SET_BIT(DDRB,pin_num);
}
else
{
CLEAR_BIT(DDRB,pin_num);
}
break;
case PORTC_ID:
if(direction == PIN_OUTPUT)
{
SET_BIT(DDRC,pin_num);
}
else
{
CLEAR_BIT(DDRC,pin_num);
}
break;
case PORTD_ID:
if(direction == PIN_OUTPUT)
{
SET_BIT(DDRD,pin_num);
}
else
{
CLEAR_BIT(DDRD,pin_num);
}
break;
}
}
}
/*
* Description :
* Write the value Logic High or Logic Low on the required pin.
* If the input port number or pin number are not correct, The function will not handle the request.
* If the pin is input, this function will enable/disable the internal pull-up resistor.
*/
void GPIO_writePin(uint8 port_num, uint8 pin_num, uint8 value)
{
/*
* Check if the input port number is greater than NUM_OF_PINS_PER_PORT value.
* Or if the input pin number is greater than NUM_OF_PINS_PER_PORT value.
* In this case the input is not valid port/pin number
*/
if((pin_num >= NUM_OF_PINS_PER_PORT) || (port_num >= NUM_OF_PORTS))
{
/* Do Nothing */
}
else
{
/* Write the pin value as required */
switch(port_num)
{
case PORTA_ID:
if(value == LOGIC_HIGH)
{
SET_BIT(PORTA,pin_num);
}
else
{
CLEAR_BIT(PORTA,pin_num);
}
break;
case PORTB_ID:
if(value == LOGIC_HIGH)
{
SET_BIT(PORTB,pin_num);
}
else
{
CLEAR_BIT(PORTB,pin_num);
}
break;
case PORTC_ID:
if(value == LOGIC_HIGH)
{
SET_BIT(PORTC,pin_num);
}
else
{
CLEAR_BIT(PORTC,pin_num);
}
break;
case PORTD_ID:
if(value == LOGIC_HIGH)
{
SET_BIT(PORTD,pin_num);
}
else
{
CLEAR_BIT(PORTD,pin_num);
}
break;
}
}
}
/*
* Description :
* Read and return the value for the required pin, it should be Logic High or Logic Low.
* If the input port number or pin number are not correct, The function will return Logic Low.
*/
uint8 GPIO_readPin(uint8 port_num, uint8 pin_num)
{
uint8 pin_value = LOGIC_LOW;
/*
* Check if the input port number is greater than NUM_OF_PINS_PER_PORT value.
* Or if the input pin number is greater than NUM_OF_PINS_PER_PORT value.
* In this case the input is not valid port/pin number
*/
if((pin_num >= NUM_OF_PINS_PER_PORT) || (port_num >= NUM_OF_PORTS))
{
/* Do Nothing */
}
else
{
/* Read the pin value as required */
switch(port_num)
{
case PORTA_ID:
if(BIT_IS_SET(PINA,pin_num))
{
pin_value = LOGIC_HIGH;
}
else
{
pin_value = LOGIC_LOW;
}
break;
case PORTB_ID:
if(BIT_IS_SET(PINB,pin_num))
{
pin_value = LOGIC_HIGH;
}
else
{
pin_value = LOGIC_LOW;
}
break;
case PORTC_ID:
if(BIT_IS_SET(PINC,pin_num))
{
pin_value = LOGIC_HIGH;
}
else
{
pin_value = LOGIC_LOW;
}
break;
case PORTD_ID:
if(BIT_IS_SET(PIND,pin_num))
{
pin_value = LOGIC_HIGH;
}
else
{
pin_value = LOGIC_LOW;
}
break;
}
}
return pin_value;
}
/*
* Description :
* Setup the direction of the required port all pins input/output.
* If the direction value is PORT_INPUT all pins in this port should be input pins.
* If the direction value is PORT_OUTPUT all pins in this port should be output pins.
* If the input port number is not correct, The function will not handle the request.
*/
void GPIO_setupPortDirection(uint8 port_num, GPIO_PortDirectionType direction)
{
/*
* Check if the input number is greater than NUM_OF_PORTS value.
* In this case the input is not valid port number
*/
if(port_num >= NUM_OF_PORTS)
{
/* Do Nothing */
}
else
{
/* Setup the port direction as required */
switch(port_num)
{
case PORTA_ID:
DDRA = direction;
break;
case PORTB_ID:
DDRB = direction;
break;
case PORTC_ID:
DDRC = direction;
break;
case PORTD_ID:
DDRD = direction;
break;
}
}
}
/*
* Description :
* Write the value on the required port.
* If any pin in the port is output pin the value will be written.
* If any pin in the port is input pin this will activate/deactivate the internal pull-up resistor.
* If the input port number is not correct, The function will not handle the request.
*/
void GPIO_writePort(uint8 port_num, uint8 value)
{
/*
* Check if the input number is greater than NUM_OF_PORTS value.
* In this case the input is not valid port number
*/
if(port_num >= NUM_OF_PORTS)
{
/* Do Nothing */
}
else
{
/* Write the port value as required */
switch(port_num)
{
case PORTA_ID:
PORTA = value;
break;
case PORTB_ID:
PORTB = value;
break;
case PORTC_ID:
PORTC = value;
break;
case PORTD_ID:
PORTD = value;
break;
}
}
}
/*
* Description :
* Read and return the value of the required port.
* If the input port number is not correct, The function will return ZERO value.
*/
uint8 GPIO_readPort(uint8 port_num)
{
uint8 value = LOGIC_LOW;
/*
* Check if the input number is greater than NUM_OF_PORTS value.
* In this case the input is not valid port number
*/
if(port_num >= NUM_OF_PORTS)
{
/* Do Nothing */
}
else
{
/* Read the port value as required */
switch(port_num)
{
case PORTA_ID:
value = PINA;
break;
case PORTB_ID:
value = PINB;
break;
case PORTC_ID:
value = PINC;
break;
case PORTD_ID:
value = PIND;
break;
}
}
return value;
}