summaryrefslogtreecommitdiffhomepage
path: root/digital/dev2/src/common/gpio.c
blob: 5733b86f52cb46d9507265383e7140ed3aa02540 (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
/* 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[5];
    /* 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)
{
    Endpoint_SelectEndpoint (GPIO_TX_EPNUM);
    while (!Endpoint_ReadWriteAllowed ())
	;
    Endpoint_Write_Byte (c);
    if (!Endpoint_ReadWriteAllowed ())
	Endpoint_ClearCurrentBank ();
    Endpoint_SelectEndpoint (GPIO_RX_EPNUM);
    ctx.data_sent = 1;
}

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 },
	};
	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)
      {
	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:
	    gpio_bin_send (PIND);
	    break;
	  }
	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;
	  }
      }
}