-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio.c
94 lines (80 loc) · 1.55 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
#include <stdint.h>
#include <avr/io.h>
#include <stdlib.h>
#include "gpio.h"
struct dpin {
int pin;
char *name;
uint8_t bitno;
volatile uint8_t *port;
volatile uint8_t *ddr;
volatile uint8_t *input;
};
const struct dpin pinmap[] = {
{ 2, "Relay", PD2, &PORTD, &DDRD, &PIND },
{ 9, "Motor ctrl", PB1, &PORTB, &DDRB, &PINB },
{ 13, "LED", PB5, &PORTB, &DDRB, &PINB },
{ 0, NULL, 0, NULL, NULL, NULL }
};
const struct dpin *find_pin(int pin)
{
int i = 0;
while (pinmap[i].name != NULL) {
if (pinmap[i].pin == pin)
return &pinmap[i];
++i;
}
return NULL;
}
int gpio_ddr(int pin, enum gpio_ddr ddr)
{
const struct dpin *p = find_pin(pin);
if (p) {
if (ddr == GPIO_OUTPUT)
*p->ddr |= (1 << p->bitno);
else
*p->ddr &= ~(1 << p->bitno);
return 0;
} else {
return -1;
}
}
int gpio_mode(int pin, enum gpio_mode mode)
{
const struct dpin *p = find_pin(pin);
if (p) {
if (mode == GPIO_HIGH)
*p->port |= (1 << p->bitno);
else
*p->port &= ~(1 << p->bitno);
return 0;
} else {
return -1;
}
}
int gpio_pullup(int pin, enum gpio_pullup pup)
{
const struct dpin *p = find_pin(pin);
if (p) {
if (pup == GPIO_PULLUP)
*p->port |= (1 << p->bitno);
else
*p->port &= ~(1 << p->bitno);
return 0;
} else {
return -1;
}
}
int gpio_read(int pin, enum gpio_pullup *value)
{
const struct dpin *p = find_pin(pin);
if (p) {
if (*p->input & (1 << p->bitno ))
*value = 1;
else
*value = 0;
return 0;
} else {
return -1;
}
}