summaryrefslogtreecommitdiff
path: root/digital/dev2/src/common/gpio.c
blob: 5b3f94e6d6bb36e8a9eafb354dc5a60dbe4b38b6 (plain)
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
/* gpio.c */
/* dev2 - Multi-purpose development board using USB and Ethernet. {{{
 *
 * Copyright (C) 2013 Nicolas Schodet
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"
#include "io.h"

#include "gpio.h"
#include "gpio_proto.h"

#include "descriptors.h"
#include "modules/usb/usb.h"
#include "modules/utils/utils.h"

/* Context. */
struct gpio_t
{
    /* Whether binary mode is activated. */
    uint8_t bin;
    /* Currently handled operation, or 0 if none. */
    uint8_t op;
    /* Arguments being received. */
    uint8_t args[11];
    /* Number of arguments to receive. */
    uint8_t args_nb;
    /* Number of received arguments. */
    uint8_t args_index;
    /* Data was sent on TX endpoint, need ZLP or last packet finalisation. */
    uint8_t data_sent;
};

static struct gpio_t ctx;

void
gpio_init (void)
{
    PORTD = 0;
    DDRD = 0;
    ctx.bin = 0;
}

void
gpio_uninit (void)
{
    gpio_init ();
}

static void
gpio_bin_send (uint8_t *c, uint8_t n)
{
    Endpoint_SelectEndpoint (GPIO_TX_EPNUM);
    while (!Endpoint_ReadWriteAllowed ())
	;
    while (n--)
	Endpoint_Write_Byte (*c++);
    if (!Endpoint_ReadWriteAllowed ())
	Endpoint_ClearCurrentBank ();
    Endpoint_SelectEndpoint (GPIO_RX_EPNUM);
    ctx.data_sent = 1;
}

static void
gpio_bin_serial_out (void)
{
    uint8_t bits = ctx.args[2];
    uint8_t *p = &ctx.args[3];
    uint8_t b;
    uint8_t i;
    for (i = 0; i < bits; i++)
      {
	/* Read more data. */
	if (i % 8 == 0)
	    b = *p++;
	/* Set data. */
	if (b & 1)
	    PORTD |= ctx.args[0];
	else
	    PORTD &= ~ctx.args[0];
	/* Clock cycle. */
	PORTD ^= ctx.args[1];
	PORTD ^= ctx.args[1];
	/* Next. */
	b >>= 1;
      }
}

static void
gpio_bin_serial_in (void)
{
    uint8_t bits = ctx.args[2];
    uint8_t *p = &ctx.args[3];
    uint8_t b = 0;
    uint8_t bp = 1;
    uint8_t i;
    for (i = 0; i < bits; i++)
      {
	/* Read data. */
	if (PIND & ctx.args[0])
	    b |= bp;
	/* Clock cycle. */
	PORTD ^= ctx.args[1];
	PORTD ^= ctx.args[1];
	/* Next. */
	bp <<= 1;
	/* Store data. */
	if (bp == 0)
	  {
	    *p++ = b;
	    b = 0;
	    bp = 1;
	  }
      }
    /* Store last data. */
    if (bp != 1)
	*p++ = b;
    gpio_bin_send (&ctx.args[3], (bits + 7) / 8);
}

static void
gpio_bin_accept (uint8_t c)
{
    if (!ctx.op)
      {
	uint8_t op_args[][2] = {
	      { GPIO_OP_RESET_SYNC, 0 },
	      { GPIO_OP_SETUP, 5 },
	      { GPIO_OP_DIR, 1 },
	      { GPIO_OP_DIR_OUT, 1 },
	      { GPIO_OP_DIR_IN, 1 },
	      { GPIO_OP_OUT, 1 },
	      { GPIO_OP_OUT_SET, 1 },
	      { GPIO_OP_OUT_RESET, 1 },
	      { GPIO_OP_OUT_TOGGLE, 1 },
	      { GPIO_OP_OUT_CHANGE, 2 },
	      { GPIO_OP_IN, 0 },
	      { GPIO_OP_SERIAL_OUT, 3 },
	      { GPIO_OP_SERIAL_IN, 3 },
	};
	uint8_t i;
	ctx.op = 0;
	ctx.args_nb = 0;
	ctx.args_index = 0;
	for (i = 0; i < UTILS_COUNT (op_args); i++)
	  {
	    if (op_args[i][0] == c)
	      {
		ctx.op = c;
		ctx.args_nb = op_args[i][1];
		break;
	      }
	  }
      }
    else
      {
	ctx.args[ctx.args_index++] = c;
      }
    if (ctx.op && ctx.args_index == ctx.args_nb)
      {
	uint8_t done = 1;
	switch (ctx.op)
	  {
	  case GPIO_OP_RESET_SYNC:
	    PORTD = 0;
	    DDRD = 0;
	    break;
	  case GPIO_OP_SETUP:
	    /* Ignore arguments for the moment. */
	    break;
	  case GPIO_OP_DIR:
	    DDRD = ctx.args[0];
	    /* Also disable pull up. */
	    PORTD &= ctx.args[0];
	    break;
	  case GPIO_OP_DIR_OUT:
	    DDRD |= ctx.args[0];
	    break;
	  case GPIO_OP_DIR_IN:
	    DDRD &= ~ctx.args[0];
	    /* Also disable pull up. */
	    PORTD &= ~ctx.args[0];
	    break;
	  case GPIO_OP_OUT:
	    PORTD = ctx.args[0];
	    break;
	  case GPIO_OP_OUT_SET:
	    PORTD |= ctx.args[0];
	    break;
	  case GPIO_OP_OUT_RESET:
	    PORTD &= ~ctx.args[0];
	    break;
	  case GPIO_OP_OUT_TOGGLE:
	    PORTD ^= ctx.args[0];
	    break;
	  case GPIO_OP_OUT_CHANGE:
	    PORTD = (PORTD & ~ctx.args[0]) | ctx.args[1];
	    break;
	  case GPIO_OP_IN:
	      {
		uint8_t in = PIND;
		gpio_bin_send (&in, 1);
	      }
	    break;
	  case GPIO_OP_SERIAL_OUT:
	    if (ctx.args_nb == 3)
	      {
		if (ctx.args[2] <= 64)
		  {
		    ctx.args_nb += (ctx.args[2] + 7) / 8;
		    done = 0;
		  }
	      }
	    else
		gpio_bin_serial_out ();
	    break;
	  case GPIO_OP_SERIAL_IN:
	    gpio_bin_serial_in ();
	    break;
	  }
	if (done)
	    ctx.op = 0;
      }
}

void
gpio_task (void)
{
    if (!ctx.bin)
      {
	Endpoint_SelectEndpoint (GPIO_RX_EPNUM);
	/* If data is available from USB: */
	if (Endpoint_ReadWriteAllowed ())
	  {
	    /* Read as much as possible, and clear endpoint. */
	    do {
		uint8_t c = Endpoint_Read_Byte ();
		if (c == GPIO_OP_RESET_SYNC)
		  {
		    ctx.bin = 1;
		    gpio_task ();
		    return;
		  }
	    } while (Endpoint_ReadWriteAllowed ());
	    Endpoint_ClearCurrentBank ();
	    /* Now, print current GPIO state if possible. */
	    Endpoint_SelectEndpoint (GPIO_TX_EPNUM);
	    if (Endpoint_ReadWriteAllowed ())
	      {
		uint8_t i, pin;
		pin = PIND;
		for (i = 0; i < 8; i++)
		  {
		    Endpoint_Write_Byte (pin & 0x80 ? '1' : '0');
		    pin <<= 1;
		  }
		Endpoint_Write_Byte ('\r');
		Endpoint_ClearCurrentBank ();
	      }
	  }
      }
    else
      {
	Endpoint_SelectEndpoint (GPIO_RX_EPNUM);
	if (Endpoint_ReadWriteAllowed ())
	  {
	    do {
		gpio_bin_accept (Endpoint_Read_Byte ());
	    } while (Endpoint_ReadWriteAllowed ());
	    Endpoint_ClearCurrentBank ();
	  }
	if (ctx.data_sent)
	  {
	    Endpoint_SelectEndpoint (GPIO_TX_EPNUM);
	    Endpoint_ClearCurrentBank ();
	    ctx.data_sent = 0;
	  }
      }
}