aboutsummaryrefslogtreecommitdiffstats
path: root/fw/usb.c
blob: ae95de749503db846aa92074dc876d396f2fa102 (plain) (blame)
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <xc.h>

#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>

#include <usb.h>
#include <usb_device.h>
#include <usb_device_cdc.h>

#include "types.h"
#include "eeprom.h"

#include "usb.h"

/***** Constants *****/

// Safety counter
#define BAILOUT 100u

// Line coding
// See struct USB_CDC_LINE_CODING
enum {
	DATA_RATE = 9600, // bps
	CHAR_FORMAT = NUM_STOP_BITS_1,
	PARITY_TYPE = PARITY_NONE,
	DATA_BITS = 8,
};

/***** Types *****/

// A state is a function that executes the state's action and returns the next state.
typedef struct State {
	struct State* (*next)(void);
} State;

// Input buffer
typedef struct {
	U8 data[CDC_DATA_OUT_EP_SIZE];
	U8 len, head;
} RxQueue;

/***** Function Declarations *****/

// States
static State *idleState(void);
static State *echoState(void);
static State *writeEepromState(void);
static State *readEepromState(void);

/***** Global Variables *****/

static U8 txBuf[CDC_DATA_IN_EP_SIZE];
static RxQueue rxBuf = {.len = 0u, .head = 0u};

/***** Function Definitions *****/

// Rx a char from USB.
// Returns FAIL if no data.
static Status
getchar(char *c) {
	if (rxBuf.len <= 0u) {
		rxBuf.len = getsUSBUSART(rxBuf.data, sizeof(rxBuf.data));
		rxBuf.head = 0u;
	}

	if (rxBuf.len > 0u) {
		*c = rxBuf.data[rxBuf.head];
		rxBuf.head++;
		rxBuf.len--;
		return OK;
	} else {
		return FAIL;
	}
}

// Attempt to Rx the next char up to retries+1 times.
static Status
getcharBlock(char *c, U8 retries) {
	Status status;

	do {
		status = getchar(c);
		if (status == OK) {
			return OK;
		}

		USBDeviceTasks();
	} while (--retries + 1u);

	return FAIL;
}

void
usbTask(void) {
	static State state = {idleState};

	USBDeviceTasks();

	if (USBGetDeviceState() < CONFIGURED_STATE) {
		return;
	}
	if (USBIsDeviceSuspended()) {
		return;
	}

	if (USBUSARTIsTxTrfReady()) {
		// Execute action and transition to next state
		state = *state.next();
	}

	CDCTxService();
}

static void
nack(void) {
	putsUSBUSART("nack\n");
}

// Read (the start of) a command from USB.
static State *
idleState(void) {
	static State state;
	char opcode, junk;
	Status status;

	state.next = idleState;

	// Read opcode
	status = getchar(&opcode);
	if (status != OK) {
		// No data
		return &state;
	}

	// Skip space
	if (getchar(&junk) != OK) {
		// Incomplete command
		nack();
		return &state;
	}

	// State transition
	switch (opcode) {
	case 'e': state.next = echoState; break;
	case 'w': state.next = writeEepromState; break;
	case 'r': state.next = readEepromState; break;
	default: // invalid command
		rxBuf.len = 0u; // discard input
		nack();
		break;
	}

	return &state;
}

// Handle "e" echo command.
static State *
echoState(void) {
	static State state;
	static U8 i = 0u;
	Status status;

	state.next = echoState;

	while (i < sizeof(txBuf)) {
		status = getchar((char *) &txBuf[i]);
		if (status == OK && txBuf[i++] == '\n') {
			// End of command
			state.next = idleState;
			break;
		} else if (status != OK) {
			// Wait to receive more data and continue on next call
			return &state;
		}
	}

	if (i > 0u) {
		putUSBUSART(txBuf, i);
		i = 0u;
	}

	return &state;
}

// Parse a 2-digit hex number and consume the space after it.
static Status
parseU8(U8 *n) {
	U8 i;
	char c;
	Status status;

	*n = 0u;
	for (i = 0u; i < 2u; i++) {
		*n <<= 4u;
		status = getcharBlock(&c, BAILOUT);
		if (status != OK) {
			return FAIL;
		}
		if (isdigit(c)) {
			*n += c - '0';
		} else if (c >= 'A' && c <= 'F') {
			*n += 10u + (c - 'A');
		} else if (c >= 'a' && c <= 'f') {
			*n += 10u + (c - 'a');
		} else {
			return FAIL;
		}
	}

	// Skip space
	status = getcharBlock(&c, BAILOUT);
	if (status != OK || !isspace(c)) {
		return FAIL;
	}

	return OK;
}

// Handle "w" write eeprom command.
static State *
writeEepromState(void) {
	static State state;
	U16 addr;
	U8 size;
	U8 i;
	char c;
	Status status;

	state.next = idleState;

	// Read <addrHi/Lo> and <size>
	if (parseU8(&addr.hi) != OK) {
		nack();
		return &state;
	}
	if (parseU8(&addr.lo) != OK) {
		nack();
		return &state;
	}
	if (parseU8(&size) != OK) {
		nack();
		return &state;
	}

	eepromWriteEnable();

	// Read <bytes> into buffer
	for (i = 0u; i < size; i++) {
		// Reuse txBuf to save memory

		// Receive byte
		status = getcharBlock(&c, BAILOUT);
		if (status != OK) {
			nack();
			eepromWriteDisable();
			return &state;
		}

		// Check for overflow
		if (i > 0u && (i%sizeof(txBuf)) == 0u) {
			eepromWrite(addr, txBuf, sizeof(txBuf));
			addU16(&addr, sizeof(txBuf));
		}

		txBuf[i%sizeof(txBuf)] = c;
	}

	// Flush buffer to eeprom
	if (i > 0u) {
		eepromWrite(addr, txBuf, i%sizeof(txBuf));
	}

	eepromWriteDisable();

	// Consume '\n'
	status = getcharBlock(&c, BAILOUT);
	if (status != OK || c != '\n') {
		nack();
	}

	putsUSBUSART("ok\n");

	return &state;
}

// Handle "r" read eeprom command.
static State *
readEepromState(void) {
	static State state;

	// TODO

	state.next = idleState;
	return &state;
}

static void
configure(void) {
	line_coding.dwDTERate = DATA_RATE;
	line_coding.bCharFormat = CHAR_FORMAT;
	line_coding.bParityType = PARITY_TYPE;
	line_coding.bDataBits = DATA_BITS;
}

bool
USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, uint16_t size) {
	switch ((int)event) {
	case EVENT_TRANSFER:
		break;

	case EVENT_SOF:
		break;

	case EVENT_SUSPEND:
		break;

	case EVENT_RESUME:
		break;

	case EVENT_CONFIGURED:
		CDCInitEP();
		configure();
		break;

	case EVENT_SET_DESCRIPTOR:
		break;

	case EVENT_EP0_REQUEST:
		USBCheckCDCRequest();
		break;

	case EVENT_BUS_ERROR:
		break;

	case EVENT_TRANSFER_TERMINATED:
		break;

	default:
		break;
	}

	return true;
}