-
Notifications
You must be signed in to change notification settings - Fork 4
/
c328.c
304 lines (233 loc) · 6.13 KB
/
c328.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
/* hadie - High Altitude Balloon flight software */
/*============================================================*/
/* Copyright (C)2010 Philip Heron <phil@sanslogic.co.uk> */
/* */
/* This program is distributed under the terms of the GNU */
/* General Public License, version 2. You may use, modify, */
/* and redistribute it under the terms of this license. A */
/* copy should be included with this source. */
/* Interface to the C328 UART camera */
#include "config.h"
#include <stdint.h>
#include <string.h>
#include <avr/io.h>
#include "c328.h"
/* >10ms timeout at 300 hz */
#define CMD_TIMEOUT (20)
/* Wait longer for the camera to take the image and return DATA response */
#define PIC_TIMEOUT (200)
#define RXREADY (UCSR0A & (1 << RXC0))
/* Receive buffer */
#define RXBUF_LEN (256)
uint8_t rxbuf[RXBUF_LEN];
uint16_t rxbuf_len = 0;
/* Expected package size */
static uint16_t pkg_len = 64; /* Default is 64 according to datasheet */
/* Timeout counter */
volatile static uint8_t timeout_clk = 0;
inline void c3_tick(void)
{
if(timeout_clk) timeout_clk--;
}
static void tx_byte(uint8_t b)
{
/* Wait for empty transmit buffer */
while(!(UCSR0A & (1 << UDRE0)));
/* Put data into buffer, sends the data */
UDR0 = b;
}
static uint8_t c3_rx(uint8_t timeout)
{
rxbuf_len = 0;
timeout_clk = timeout;
while(timeout_clk)
{
if(!RXREADY) continue;
rxbuf[rxbuf_len++] = UDR0;
if(rxbuf_len == 6) break;
}
if(rxbuf_len != 6) return(0); /* Timeout or incomplete response */
if(rxbuf[0] != 0xAA) return(0); /* All responses should begin 0xAA */
/* Return the received command ID */
return(rxbuf[1]);
}
static void c3_tx(uint8_t cmd, uint8_t a1, uint8_t a2, uint8_t a3, uint8_t a4)
{
tx_byte(0xAA);
tx_byte(cmd);
tx_byte(a1);
tx_byte(a2);
tx_byte(a3);
tx_byte(a4);
}
static char c3_cmd(uint8_t cmd, uint8_t a1, uint8_t a2, uint8_t a3, uint8_t a4)
{
uint8_t r;
c3_tx(cmd, a1, a2, a3, a4);
r = c3_rx(CMD_TIMEOUT);
/* Did we get an ACK for this command? */
if(r != CMD_ACK || rxbuf[2] != cmd) return(-1);
return(0);
}
void c3_init(void)
{
/* Do UART initialisation, port 0 @ 14.4k baud for 7.3728 MHz clock */
UBRR0H = 0;
UBRR0L = 31;
/* Enable TX & RX */
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
/* 8-bit, no parity and 1 stop bit */
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}
char c3_sync(void)
{
char i;
/* Send the SYNC command until the camera responds, up to 60 */
for(i = 0; i < 60; i++)
{
/* Send the sync and wait for an ACK */
if(c3_cmd(CMD_SYNC, 0, 0, 0, 0) != 0) continue;
/* ACK should be followed by a SYNC */
if(c3_rx(CMD_TIMEOUT) != CMD_SYNC) continue;
/* ACK the SYNC and return success code */
c3_tx(CMD_ACK, CMD_SYNC, 0, 0, 0);
return(0);
}
/* If we got here, the camera failed to sync. Panic */
return(-1);
}
char c3_setup(uint8_t ct, uint8_t rr, uint8_t jr)
{
return(c3_cmd(CMD_INIT, 0, ct, rr, jr));
}
char c3_set_package_size(uint16_t s)
{
char r;
if(s > RXBUF_LEN) return(-1);
r = c3_cmd(CMD_SET_PKG_SIZE, 0x08, s & 0xFF, s >> 8, 0);
if(r == 0) pkg_len = s;
return(r);
}
char c3_snapshot(uint8_t st, uint16_t skip_frame)
{
return(c3_cmd(CMD_SNAPSHOT, st, skip_frame & 0xFF, skip_frame >> 8, 0));
}
char c3_get_picture(uint8_t pt, uint16_t *length)
{
/* Send the command */
if(c3_cmd(CMD_GET_PICTURE, pt, 0, 0, 0) != 0) return(-1);
/* The camera should now send a DATA message */
if(c3_rx(PIC_TIMEOUT) != CMD_DATA) return(-1);
/* Get the file size from the DATA args */
*length = rxbuf[3] + (rxbuf[4] << 8);
return(0);
}
char c3_get_package(uint16_t id, uint8_t **dst, uint16_t *length)
{
uint8_t checksum;
volatile uint16_t s;
/* s is volatile to work around an apparent bug in avr-gcc --
* discovered by ms7821 in #highaltitude */
rxbuf_len = 0;
checksum = 0;
s = pkg_len;
/* Get the package by sending an ACK */
c3_tx(CMD_ACK, 0, 0, id & 0xFF, id >> 8);
/* The camera should immediatly start returning data */
timeout_clk = CMD_TIMEOUT;
while(timeout_clk && rxbuf_len < s)
{
if(!RXREADY) continue;
/* Read the byte and update checksum */
rxbuf[rxbuf_len] = UDR0;
checksum += rxbuf[rxbuf_len++];
if(rxbuf_len == 4)
{
/* Get the actual length of the package */
s = rxbuf[2] + (rxbuf[3] << 8) + 6;
if(s > pkg_len) return(-1);
}
timeout_clk = CMD_TIMEOUT;
}
/* Test for timeout or incomplete package */
if(rxbuf_len != s) return(-2);
/* Fix and test checksum */
checksum -= rxbuf[rxbuf_len - 2];
if(checksum != rxbuf[rxbuf_len - 2]) return(-3);
/* All done */
*dst = rxbuf;
*length = rxbuf_len;
return(0);
}
char c3_finish_picture(void)
{
c3_tx(CMD_ACK, 0, 0, 0xF0, 0xF0);
return(0);
}
/****************** Simple interface ******************/
static uint16_t image_len;
static uint16_t image_read;
static uint8_t *package;
static uint16_t package_len;
static uint16_t package_id;
char c3_open(uint8_t jr)
{
/* Open, setup and take the image */
if(c3_sync() != 0) return(-1);
if(c3_setup(CT_JPEG, 0, jr) != 0) return(-2);
if(c3_set_package_size(RXBUF_LEN) != 0) return(-3);
if(c3_snapshot(ST_JPEG, 0) != 0) return(-4);
if(c3_get_picture(PT_SNAPSHOT, &image_len) != 0) return(-5);
image_read = 0;
package = NULL;
package_len = 0;
package_id = 0;
return(0);
}
char c3_close(void)
{
c3_finish_picture();
return(0);
}
uint16_t c3_read(uint8_t *ptr, uint16_t length)
{
uint16_t r; /* Number of bytes left to read */
/* Don't read past the end of the image */
r = image_len - image_read;
if(length > r) length = r;
r = length;
while(r)
{
if(package_len == 0)
{
char i = c3_get_package(package_id++, &package, &package_len);
if(i != 0) return(length - r);
/* Skip the package headers and checksum */
package += 4;
package_len -= 6;
}
else
{
uint16_t b = r;
if(b > package_len) b = package_len;
if(ptr)
{
memcpy(ptr, package, b);
ptr += b;
}
package += b;
package_len -= b;
r -= b;
image_read += b;
}
}
return(length);
}
uint16_t c3_filesize(void)
{
return(image_len);
}
char c3_eof(void)
{
return(image_read >= image_len);
}