-
Notifications
You must be signed in to change notification settings - Fork 3
/
usb.c
executable file
·380 lines (328 loc) · 9.77 KB
/
usb.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//***************************************************************************
//
// File : usb.c
// Copyright : 2013 Paul Chote
// Description : Communication interface with Acquisition PC via USB (uart0)
//
// This file is part of Karaka, which is free software. It is made available
// to you under the terms of version 3 of the GNU General Public License, as
// published by the Free Software Foundation. For more information, see LICENSE.
//
//***************************************************************************
#include <stdbool.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
#include <avr/eeprom.h>
#include <avr/pgmspace.h>
#include <util/atomic.h>
#include "display.h"
#include "gps.h"
#include "main.h"
#include "camera.h"
#include "usb.h"
#define MAX_DATA_LENGTH 200
enum packet_state {HEADERA = 0, HEADERB, TYPE, LENGTH, DATA, CHECKSUM, FOOTERA, FOOTERB};
enum packet_type
{
TIMESTAMP = 'A',
TRIGGER = 'B',
MESSAGE = 'C',
MESSAGE_RAW = 'D',
START_EXPOSURE = 'E',
STOP_EXPOSURE = 'F',
STATUS = 'H',
ENABLE_RELAY = 'R',
};
struct packet_startexposure
{
uint8_t use_monitor;
enum timing_mode mode;
uint16_t exposure;
uint8_t stride;
uint8_t align_first;
};
struct packet_status
{
enum timer_status timer;
enum gps_status gps;
};
struct packet_message
{
uint8_t length;
char str[MAX_DATA_LENGTH-1];
};
struct timer_packet
{
enum packet_state state;
enum packet_type type;
uint8_t length;
uint8_t progress;
uint8_t checksum;
union
{
// Extra byte allows us to always null-terminate strings for display
uint8_t bytes[MAX_DATA_LENGTH+1];
struct packet_startexposure startexp;
} data;
};
const char unknown_packet_fmt[] PROGMEM = "Unknown packet type '%c' - ignoring";
const char long_packet_fmt[] PROGMEM = "Ignoring long packet: %c (length %u)";
const char checksum_failed_fmt[] PROGMEM = "Packet checksum failed. Got 0x%02x, expected 0x%02x";
const char invalid_packet_fmt[] PROGMEM = "Invalid packet end byte. Got 0x%02x, expected 0x%02x";
const char got_packet_fmt[] PROGMEM = "Got packet type '%c'";
static uint8_t input_buffer[256];
static uint8_t input_read = 0;
static volatile uint8_t input_write = 0;
static uint8_t output_buffer[256];
static volatile uint8_t output_read = 0;
static volatile uint8_t output_write = 0;
// Add a byte to the send buffer.
// Will block if the buffer is full
static void queue_byte(uint8_t b)
{
// Don't overwrite data that hasn't been sent yet
while (output_write == (uint8_t)(output_read - 1));
output_buffer[output_write++] = b;
// Enable transmit if necessary
UCSR0B |= _BV(UDRIE0);
}
// Send data from RAM
static void queue_data(uint8_t type, const void *data, uint8_t length)
{
// Header
queue_byte('$');
queue_byte('$');
queue_byte(type);
queue_byte(length);
// Data
uint8_t checksum = 0;
for (uint8_t i = 0; i < length; i++)
{
uint8_t b = ((uint8_t *)data)[i];
queue_byte(b);
checksum ^= b;
}
// Footer
queue_byte(checksum);
queue_byte('\r');
queue_byte('\n');
}
static bool byte_available()
{
return input_write != input_read;
}
static uint8_t read_byte()
{
// Loop until data is available
while (input_read == input_write);
return input_buffer[input_read++];
}
ISR(USART0_UDRE_vect)
{
if (output_write != output_read)
UDR0 = output_buffer[output_read++];
// Ran out of data to send - disable the interrupt
if (output_write == output_read)
UCSR0B &= ~_BV(UDRIE0);
}
ISR(USART0_RX_vect)
{
input_buffer[(uint8_t)(input_write++)] = UDR0;
}
void usb_initialize()
{
#define BAUD 9600
#include <util/setbaud.h>
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A = _BV(U2X0);
#endif
// Enable receive, transmit, data received interrupt
UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0);
input_read = input_write = 0;
output_read = output_write = 0;
}
static void parse_packet(struct timer_packet *p)
{
usb_send_message_fmt_P(got_packet_fmt, p->type);
switch (p->type)
{
case START_EXPOSURE:
{
struct packet_startexposure *data = &p->data.startexp;
timing_mode = data->mode;
// These are only accessed from interrupt context
// when timer_status == ALIGN,EXPOSING,READOUT so
// these is safe to modify with interrupts enabled
exposure_countdown = exposure_total = data->exposure;
trigger_countdown = trigger_stride = data->stride;
// align_boundary is 8-bit, so use a temporary variable
uint16_t temp_boundary = exposure_total;
if (timing_mode == MODE_HIGHRES)
temp_boundary /= 1000;
if (temp_boundary < 1 || data->align_first == 0)
temp_boundary = 1;
else if (temp_boundary > 60)
temp_boundary = 60;
align_boundary = temp_boundary;
camera_start_exposing(data->use_monitor);
// Update display configuration for new sequence
display_update_config();
break;
}
case STOP_EXPOSURE:
// Disable the exposure countdown immediately
STOP_MILLISECOND_TIMER;
millisecond_count = 0;
// These are only accessed from interrupt context
// when timer_status == ALIGN,EXPOSING,READOUT so
// these is safe to modify with interrupts enabled
exposure_total = 0;
exposure_countdown = 0;
camera_stop_exposing();
break;
case ENABLE_RELAY:
eeprom_update_byte(RELAY_EEPROM_OFFSET, RELAY_ENABLED);
eeprom_update_byte(BOOTLOADER_EEPROM_OFFSET, BYPASS_ENABLED);
break;
default:
usb_send_message_fmt_P(unknown_packet_fmt, p->type);
break;
}
}
void usb_tick()
{
static struct timer_packet p = {.state = HEADERA};
while (byte_available())
{
uint8_t b = read_byte();
if (timer_status == TIMER_RELAY)
gps_send_byte(b);
switch (p.state)
{
case HEADERA:
case HEADERB:
if (b == '$')
p.state++;
else
p.state = HEADERA;
break;
case TYPE:
p.type = b;
p.state++;
break;
case LENGTH:
p.length = b;
p.progress = 0;
p.checksum = 0;
if (p.length == 0)
p.state = CHECKSUM;
else if (p.length <= sizeof(p.data))
p.state++;
else
{
usb_send_message_fmt_P(long_packet_fmt, p.type, p.length);
p.state = HEADERA;
}
break;
case DATA:
p.checksum ^= b;
p.data.bytes[p.progress++] = b;
if (p.progress == p.length)
p.state++;
break;
case CHECKSUM:
if (p.checksum == b)
p.state++;
else
{
usb_send_message_fmt_P(checksum_failed_fmt, b, p.checksum);
p.state = HEADERA;
}
break;
case FOOTERA:
if (b == '\r')
p.state++;
else
{
usb_send_message_fmt_P(invalid_packet_fmt, b, '\r');
p.state = HEADERA;
}
break;
case FOOTERB:
if (b == '\n')
parse_packet(&p);
else
usb_send_message_fmt_P(invalid_packet_fmt, b, '\n');
p.state = HEADERA;
break;
}
}
}
void usb_send_message_P(const char *string)
{
struct packet_message msg;
msg.length = strlen_P(string);
if (msg.length > MAX_DATA_LENGTH-1)
msg.length = MAX_DATA_LENGTH-1;
strncpy_P(msg.str, string, msg.length);
queue_data(MESSAGE, &msg, msg.length + 1);
}
void usb_send_message_fmt_P(const char *fmt, ...)
{
va_list args;
struct packet_message msg;
va_start(args, fmt);
int len = vsnprintf_P(msg.str, MAX_DATA_LENGTH, fmt, args);
va_end(args);
if (len > MAX_DATA_LENGTH-1)
len = MAX_DATA_LENGTH-1;
msg.length = (uint8_t)len;
queue_data(MESSAGE, &msg, msg.length + 1);
}
void usb_send_timestamp()
{
// Add exposure progress to timestamp
uint16_t count;
ATOMIC_BLOCK(ATOMIC_FORCEON)
{
count = exposure_countdown;
}
current_timestamp.exposure_progress = exposure_total - count;
queue_data(TIMESTAMP, ¤t_timestamp, sizeof(struct timestamp));
}
void usb_send_trigger()
{
// This is non-atomic, but something is very wrong if this
// doesn't get sent before the next exposure is triggered
queue_data(TRIGGER, (void *)&download_timestamp, sizeof(struct timestamp));
}
void usb_stop_exposure()
{
queue_data(STOP_EXPOSURE, NULL, 0);
}
void usb_send_status(enum timer_status timer, enum gps_status gps)
{
struct packet_status data = {
.timer = timer,
.gps = gps
};
queue_data(STATUS, &data, sizeof(struct packet_status));
}
void usb_send_raw(uint8_t *data, uint8_t length)
{
struct packet_message msg;
msg.length = length;
if (msg.length > MAX_DATA_LENGTH-1)
msg.length = MAX_DATA_LENGTH-1;
memcpy(msg.str, data, msg.length);
queue_data(MESSAGE_RAW, &msg, msg.length + 1);
}
// Send a raw byte without wrapping in a packet
// Used for relay mode
void usb_send_byte(uint8_t b)
{
queue_byte(b);
}