gpio.c
7.48 KB
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
/*
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup lpc2387
* @{
*
* @file
* @brief CPU specific low-level GPIO driver implementation for the LPC2387
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
*
* @}
*/
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "irq.h"
#include "periph/gpio.h"
#include "bitfield.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
#define GPIO_NUM_ISR (16)
static BITFIELD(_gpio_config_bitfield, GPIO_NUM_ISR);
static gpio_isr_ctx_t _gpio_states[GPIO_NUM_ISR];
static BITFIELD(_gpio_rising, GPIO_NUM_ISR);
static BITFIELD(_gpio_falling, GPIO_NUM_ISR);
static uint8_t _gpio_isr_map[64]; /* only ports 0+2 can have ISRs */
static void _gpio_configure(gpio_t pin, unsigned rising, unsigned falling);
void gpio_init_ports(void) {
SCS |= 0x1; /* set GPIO ports 0 and 1 to FIO mode (3.7.2) */
memset(&_gpio_isr_map[0], 0xff, 64);
}
static int _isr_map_entry(gpio_t pin) {
unsigned _pin = pin & 31;
unsigned port = pin >> 5;
/* lpc2387 can only interrupt in pins of port 0 and 2 */
if (port && port != 2) {
return -1;
}
if (port) {
_pin += 32;
}
return _pin;
}
int gpio_init(gpio_t pin, gpio_mode_t mode)
{
unsigned _pin = pin & 31;
unsigned port = pin >> 5;
if (mode != GPIO_OUT) {
return -1;
}
FIO_PORT_t *_port = &FIO_PORTS[port];
/* set mask */
_port->MASK = ~(0x1<<_pin);
/* set direction */
_port->DIR = ~0;
gpio_init_mux(pin, 0);
return 0;
}
int gpio_init_mux(unsigned pin, unsigned mux)
{
(void) mux;
unsigned _pin = pin & 31;
PINSEL[pin>>4] &= ~(0x1 << (_pin*2));
return 0;
}
int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
gpio_cb_t cb, void *arg)
{
(void)mode;
DEBUG("gpio_init_int(): pin %u\n", pin);
int isr_map_entry;
/* check if interrupt is possible for this pin */
if ((isr_map_entry = _isr_map_entry(pin)) == -1) {
DEBUG("gpio_init_int(): pin %u cannot be used to generate interrupts.\n", pin);
return -1;
}
/* find free isr state entry */
int _state_index = _gpio_isr_map[isr_map_entry];
if (_state_index == 0xff) {
_state_index = bf_get_unset(_gpio_config_bitfield, GPIO_NUM_ISR);
_gpio_isr_map[isr_map_entry] = _state_index;
DEBUG("gpio_init_int(): pin has state_index=%i isr_map_entry=%i\n", _state_index, isr_map_entry);
}
if (_state_index == 0xff) {
#ifdef DEVELHELP
puts("lpc2387: gpio: warning: no free gpio callback state!");
#endif
return -1;
}
/* store callback */
_gpio_states[_state_index].cb = cb;
_gpio_states[_state_index].arg = arg;
extern void GPIO_IRQHandler(void);
if (flank & GPIO_FALLING) {
bf_set(_gpio_falling, _state_index);
}
else {
bf_unset(_gpio_falling, _state_index);
}
if (flank & GPIO_RISING) {
bf_set(_gpio_rising, _state_index);
}
else {
bf_unset(_gpio_rising, _state_index);
}
_gpio_configure(pin, flank & GPIO_RISING, flank & GPIO_FALLING);
/* activate irq */
INTWAKE |= GPIO0WAKE | GPIO2WAKE; /* allow GPIO to wake up from power down */
install_irq(GPIO_INT, &GPIO_IRQHandler, IRQP_GPIO); /* install irq handler */
return 0;
}
static void _gpio_configure(gpio_t pin, unsigned rising, unsigned falling)
{
unsigned _pin = pin & 31;
unsigned port = pin >> 5;
/* set irq settings */
volatile unsigned long *en_f;
volatile unsigned long *en_r;
volatile unsigned long *en_clr;
if (!port) {
en_f = &IO0_INT_EN_F;
en_r = &IO0_INT_EN_R;
en_clr = &IO0_INT_CLR;
}
else {
en_f = &IO2_INT_EN_F;
en_r = &IO2_INT_EN_R;
en_clr = &IO2_INT_CLR;
}
/* configure irq */
unsigned int bit = 0x1 << _pin;
unsigned state = irq_disable();
*en_clr |= bit; /* clear interrupt */
if (falling) {
*en_f |= bit; /* enable falling edge */
}
else {
*en_f &= ~bit; /* disable falling edge */
}
if (rising) {
*en_r |= bit; /* enable rising edge */
}
else {
*en_r &= ~bit; /* disable rising edge */
}
irq_restore(state);
}
void gpio_irq_enable(gpio_t pin)
{
int isr_map_entry =_isr_map_entry(pin);
int _state_index = _gpio_isr_map[isr_map_entry];
if (_state_index == 0xff) {
DEBUG("gpio_irq_enable(): trying to enable unconfigured pin.\n");
return;
}
_gpio_configure(pin,
bf_isset(_gpio_rising, _state_index),
bf_isset(_gpio_falling, _state_index));
}
void gpio_irq_disable(gpio_t dev)
{
_gpio_configure(dev, 0, 0);
}
int gpio_read(gpio_t pin)
{
unsigned _pin = pin & 31;
unsigned port = pin >> 5;
FIO_PORT_t *_port = &FIO_PORTS[port];
_port->MASK = ~(1<<_pin);
return _port->PIN != 0;
}
void gpio_set(gpio_t pin)
{
unsigned _pin = pin & 31;
unsigned port = pin >> 5;
FIO_PORT_t *_port = &FIO_PORTS[port];
_port->MASK = ~(1<<_pin);
_port->SET = ~0;
}
void gpio_clear(gpio_t pin)
{
unsigned _pin = pin & 31;
unsigned port = pin >> 5;
FIO_PORT_t *_port = &FIO_PORTS[port];
_port->MASK = ~(1<<_pin);
_port->CLR = ~0;
}
void gpio_toggle(gpio_t dev)
{
if (gpio_read(dev)) {
gpio_clear(dev);
}
else {
gpio_set(dev);
}
}
void gpio_write(gpio_t dev, int value)
{
if (value) {
gpio_set(dev);
}
else {
gpio_clear(dev);
}
}
static void test_irq(int port, unsigned long f_mask, unsigned long r_mask)
{
/* Test each bit of rising and falling masks, if set trigger interrupt
* on corresponding device */
unsigned bit = 0x1;
int n = 0;
while (bit) {
if ((r_mask & bit) | (f_mask & bit)) {
int _state_index = _gpio_isr_map[n + (port<<1)];
if (_state_index != 0xff) {
_gpio_states[_state_index].cb(_gpio_states[_state_index].arg);
}
}
bit <<= 1;
n++;
}
}
void GPIO_IRQHandler(void) __attribute__((interrupt("IRQ")));
void GPIO_IRQHandler(void)
{
if (IO_INT_STAT & BIT0) { /* interrupt(s) on PORT0 pending */
unsigned long int_stat_f = IO0_INT_STAT_F; /* save content */
unsigned long int_stat_r = IO0_INT_STAT_R; /* save content */
IO0_INT_CLR = int_stat_f; /* clear flags of fallen pins */
IO0_INT_CLR = int_stat_r; /* clear flags of risen pins */
test_irq(0, int_stat_f, int_stat_r);
}
if (IO_INT_STAT & BIT2) { /* interrupt(s) on PORT2 pending */
unsigned long int_stat_f = IO2_INT_STAT_F; /* save content */
unsigned long int_stat_r = IO2_INT_STAT_R; /* save content */
IO2_INT_CLR = int_stat_f; /* clear flags of fallen pins */
IO2_INT_CLR = int_stat_r; /* clear flags of risen pins */
test_irq(2, int_stat_f, int_stat_r);
}
VICVectAddr = 0; /* Acknowledge Interrupt */
}