-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.c
281 lines (251 loc) · 5.86 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
/**************************
* Module: GPIO
* file name: gpio.c
* description: source file for AVR GPIO driver
* author: Mustafa Mahmoud
***************************************/
#include"gpio.h"
#include"avr/io.h" /* to use the I/O ports registers*/
#include"common_macros.h"
/*************************************************************************
*function definitions *
*************************************************************************/
/* description:
* function to determine the direction of the pin wethere input or output
* if the pin number or port number is not correct, the function will do nothing
*/
void GPIO_setUpPinDirection(uint8 port_num, uint8 pin_num, uint8 direction)
{
/* to check if the number of port and number of pin is correct
* if not correct then the function will not handle the request
* if correct then switch on the port to setup the pin direction in each port
* */
if ((port_num >= NUM_OF_PORTS ) | (pin_num>=NUM_OF_PINS) )
{
/* Do nothing */
}
else {
/* to check for the port and pin number and setup the pin direction*/
switch(port_num)
{
case PORTA_ID:
if (direction == PIN_INPUT)
CLEAR_BIT(DDRA,pin_num);
else
SET_BIT(DDRA,pin_num);
break;
case PORTB_ID:
if (direction==PIN_INPUT)
CLEAR_BIT(DDRB,pin_num);
else
SET_BIT(DDRB,pin_num);
break;
case PORTC_ID:
if (direction==PIN_INPUT)
CLEAR_BIT(DDRC,pin_num);
else
SET_BIT(DDRC,pin_num);
break;
case PORTD_ID:
if (direction==PIN_INPUT)
CLEAR_BIT(DDRD,pin_num);
else
SET_BIT(DDRD,pin_num);
break;
}
}
}
/* description:
* function to set pin or clear pin
* if the pin number or port number is not correct, the function will do nothing
*/
void GPIO_writePin(uint8 port_num, uint8 pin_num, uint8 value)
{
/* if the pin number or port number is not correct then the function will do nothing
*/
if ((port_num >= NUM_OF_PORTS ) | (pin_num>=NUM_OF_PINS) )
{
/* Do nothing */
}
else {
/* switch on the port number to set the required pin in the required port */
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:
* function to read the statues of the pin whether it is high or low
* if the number of port of pin number is incorrect then the function return logic low
*/
uint8 GPIO_readPin(uint8 port_num, uint8 pin_num)
{
uint8 statues;
/* if the number of port of pin number is incorrect so return logic low */
if ((port_num >= NUM_OF_PORTS ) | (pin_num>=NUM_OF_PINS) )
{
statues=LOGIC_LOW;
}
else {
/* read the required pin from the required port and return the statues
* of the pin whether 1 or 0 (SET OR CLEARED)
*/
switch(port_num)
{
case PORTA_ID:
if (BIT_IS_SET(PINA,pin_num))
statues=LOGIC_HIGH;
else
statues=LOGIC_LOW;
break;
case PORTB_ID:
if (BIT_IS_SET(PINB,pin_num))
statues=LOGIC_HIGH;
else
statues=LOGIC_LOW;
break;
case PORTC_ID:
if (BIT_IS_SET(PINC,pin_num))
statues=LOGIC_HIGH;
else
statues=LOGIC_LOW;
break;
case PORTD_ID:
if (BIT_IS_SET(PIND,pin_num))
statues=LOGIC_HIGH;
else
statues=LOGIC_LOW;
break;
}
}
return statues;
}
/* description:
* function to determine the direction of the port wethere input or output
* if the pin number or port number is not correct, the function will do nothing
*/
void GPIO_setUpPortDirection(uint8 port_num, uint8 direction)
{
/*if the port number is not correct, do nothing*/
if ((port_num >= NUM_OF_PORTS ) )
{
/*do nothing*/
}
else {
/* switch on the port to get the required port then set the whole port as input or
* output
*/
switch(port_num)
{
case PORTA_ID:
if(direction==PORT_INPUT)
DDRA=0x00;
else
DDRA=0xFF;
break;
case PORTB_ID:
if(direction==PORT_INPUT)
DDRB=0x00;
else
DDRB=0xFF;
break;
case PORTC_ID:
if(direction==PORT_INPUT)
DDRC=0x00;
else
DDRC=0xFF;
break;
case PORTD_ID:
if(direction==PORT_INPUT)
DDRD=0x00;
else
DDRD=0xFF;
break;
}
}
}
/* description:
* function to write on port a required value
* if the pin number or port number is not correct, the function will do nothing
*/
void GPIO_writePort(uint8 port_num, uint8 value)
{
/*if the port number is not correct, the function will do nothing*/
if ((port_num >= NUM_OF_PORTS ))
{
/*do nothing*/
}
else {
/* find the required port then write on the port the required value*/
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:
* function to read the whole port and return it's value
* if the port number or pin number is incorrect the function will return zero
*/
uint8 GPIO_readPort(uint8 port_num)
{
uint8 port_value;
/*if the port number is not correct, the function will do nothing*/
if ((port_num >= NUM_OF_PORTS ))
{
port_value=0;
}
else
{
switch(port_num)
{
case PORTA_ID:
port_value=PINA;
break;
case PORTB_ID:
port_value=PINB;
break;
case PORTC_ID:
port_value=PINC;
break;
case PORTD_ID:
port_value=PIND;
break;
}
}
return port_value;
}