-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathexample(xmodem).c
429 lines (337 loc) · 9.56 KB
/
example(xmodem).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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*!
* \brief
*
* \author Jan Oleksiewicz <jnk0le@hotmail.com>
* \license SPDX-License-Identifier: MIT
*/
#include <avr/io.h>
#include <util/delay.h>
#include <util/crc16.h>
#include "usart.h"
#define SETUP_RETRIES_DELAY 1000 /* in ms */
#define SETUP_CRC_RETRIES_TO_FALLBACK 15
#define SETUP_CKSUM_RETRIES_TO_GIVE_UP 2
#define PACKET_RETRY_DELAY 1000 /* in ms / timeouts if nothing is received or packet is too short */
#define PACKET_MAX_TIMEOUT_RETRANSMITS 15 /* 0-255 */
#define SOH 0x01
#define EOT 0x04
#define ACK 0x06
#define NACK 0x15
#define CAN 0x18
//#define SUB 0x1a
#define bad_packet 0x00
#define good_packet 0x01
#define duplicate 0x02
typedef enum {
ready_for_transmission = 0,
awaiting_for_command,
preparing_transmission,
preparing_timeout,
transmission_in_progres,
//purge ?
transmission_completed,
transmission_max_retransmits,
transmission_aborted
} ProgramStatus;
struct XmodemPacket {
uint8_t buff[133];
uint8_t position;
uint8_t last_packet; // last successfully received packet
uint8_t retrycount; // timeout retries (no packets or too small)
uint16_t starttime; // uint16_t gives 65 sec margin
uint8_t fallback;
//uint16_t crc;
//ProgramStatus transmission_status;
} xmdm;
uint8_t HandleIncomingData(uint8_t dat);
uint8_t validate_packet(uint8_t *bufptr, uint8_t *packet_number, uint8_t fallback);
uint16_t calcrc(uint8_t *bufptr, uint8_t size);
uint8_t calchecksum(uint8_t *bufptr, uint8_t size);
void MoveData(uint8_t *bufptr, uint8_t BytesToMove);
void HexDump16(uint8_t *bufptr, uint16_t Length);
uint32_t milis;
//inline uint32_t getMilis(void) __attribute__((always_inline));
uint32_t getMilis(void) { ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { return milis; } return 0; }
void timer1_init(void) // using timer 1 for milis is so wastefull, but still more usable than wasting it exclusively
{
TCCR1A = 0;
TCCR1B = (1 << CS10) | (1 << WGM12) | (1 << WGM13); //CTC mode, overflow on ICR1
TIMSK1 = (1 << ICIE1); // CAPT vector is the overflow now
ICR1 = F_CPU / 1000; // 1 millisecond accurate up to crystal ppm
}
ISR(TIMER1_CAPT_vect)
{
milis++;
}
//just an example buffer to put downloaded content somewhere, it could be just spi flash
uint8_t file[1024];
uint16_t fileposition;
int main(void)
{
ProgramStatus transmission_status = ready_for_transmission;
char cmd;
uart_init(BAUD_CALC(115200));
timer1_init();
sei();
while (1)
{
switch (transmission_status)
{
case ready_for_transmission:
uart_puts_P("Awaiting for command, type 'h' for help...\r\n");
uart_putc('>');
transmission_status = awaiting_for_command;
break;
case awaiting_for_command:
cmd = uart_getc();
if (cmd) // if received character
{
uart_puts("\r\n");
switch (cmd)
{
case 'd':
uart_puts_P("File transfer can be terminated by double ^X sequence before waiting for receiver timeout\r\n\n");
uart_puts_P("Waiting for transmission ...\r\n");
xmdm.last_packet = 0;
xmdm.retrycount = 0;
xmdm.fallback = 0;
xmdm.starttime = getMilis();
transmission_status = preparing_transmission;
break;
case 't':
case 's':
uart_puts_P("unsupported feature\r\n");
transmission_status = ready_for_transmission;
break;
case 'x':
uart_puts_P("hexdump of sent file : \r\n");
HexDump16(file, 1024);
transmission_status = ready_for_transmission;
break;
default:
uart_puts_P("Usage:\r\n");
uart_puts_P("d\t download file from pc\r\n");
uart_puts_P("t\t send file to pc [not supported by now]\r\n");
uart_puts_P("s\t look for any readable strings in received file [not supported by now]\r\n");
uart_puts_P("x\t show hexdump of stored file\r\n");
uart_putc('>');
}
}
break;
case preparing_transmission:
cmd = uart_getc();
if (cmd == SOH) // ignore everything before SOH
{
xmdm.retrycount = 0;
xmdm.starttime = getMilis();
transmission_status = HandleIncomingData(cmd);
break;
}
else if (cmd == CAN) //
transmission_status = transmission_aborted;
if ((uint16_t)getMilis() - xmdm.starttime > SETUP_RETRIES_DELAY)
{
if (xmdm.retrycount < SETUP_CRC_RETRIES_TO_FALLBACK + SETUP_CKSUM_RETRIES_TO_GIVE_UP)
{
if (xmdm.fallback)
uart_putc(NACK);
else
uart_putc('C');
xmdm.starttime = getMilis();
xmdm.retrycount++;
}
else
{
transmission_status = preparing_timeout;
}
if (xmdm.retrycount >= SETUP_CRC_RETRIES_TO_FALLBACK)
{
xmdm.fallback = 1; // will be used in other parts
}
}
break;
case preparing_timeout:
uart_putc(CAN);
uart_putc(CAN); // CAN in case if host started right now
uart_puts_P("\r\nfailed to establish transmission with host\r\n");
transmission_status = ready_for_transmission;
break;
case transmission_in_progres:
//uint8_t incoming_data;
//while (BUFFER_EMPTY != uart_LoadData(&incoming_data))
// transmission_status = HandleIncomingData(incoming_data);
//while (1) // read all data from buffer
//{
//int16_t tmp = uart_getData();
//if (tmp >= 0)
//transmission_status = HandleIncomingData((uint8_t)tmp);
//else // negative value - buffer empty
//break;
//}
while (uart_AvailableBytes())
transmission_status = HandleIncomingData(uart_getc());
if ((uint16_t)getMilis() - xmdm.starttime > PACKET_RETRY_DELAY)
{
if (xmdm.retrycount > PACKET_MAX_TIMEOUT_RETRANSMITS)
{
transmission_status = transmission_aborted;
}
else // no need to purge in this case
{
uart_putc(NACK);
xmdm.position = 0;
xmdm.retrycount++;
}
}
break;
//case purge: ??
//break;
case transmission_max_retransmits:
uart_putc(CAN);
uart_putc(CAN);
uart_puts_P("Timeout or short packets retransmits limit hit ... Aborting\r\n");
transmission_status = ready_for_transmission;
break;
case transmission_completed:
uart_puts_P("Transmission completed, thanks for your attention\r\n");
transmission_status = ready_for_transmission;
break;
case transmission_aborted:
uart_puts_P("\r\n^X, Aborting ... \r\n");
transmission_status = ready_for_transmission;
}
//AnotherRealTimeTaskDuringTransmission(); // why not ?
}
}
uint8_t HandleIncomingData(uint8_t dat)
{
if (dat == EOT && xmdm.position == 0)
{
uart_putc(ACK);
uart_putc(ACK);
return transmission_completed;
}
if (xmdm.position == 1 && dat == CAN && xmdm.buff[0] == CAN) // double check - should arrive after timeout / whole packet
return transmission_aborted;
xmdm.buff[xmdm.position++] = dat;
if (xmdm.position == (133 - xmdm.fallback)) // 133 or 132 bytes
{
xmdm.position = 0;
uint8_t packet_status = validate_packet(xmdm.buff, &xmdm.last_packet, xmdm.fallback);
switch (packet_status)
{
case good_packet:
uart_putc(ACK);
xmdm.starttime = getMilis();
MoveData(&xmdm.buff[3], 128);
// insert a data handler here, for example, write buffer to a flash device
return transmission_in_progres;
case duplicate:
uart_putc(ACK); // just ack this
xmdm.starttime = getMilis();
//count duplicates
break;
case bad_packet:
uart_putc(NACK);
xmdm.starttime = getMilis();
//count badpacket
return transmission_in_progres;
//default:
}
}
return transmission_in_progres;
}
uint8_t validate_packet(uint8_t *bufptr, uint8_t *packet_number, uint8_t fallback)
{
if (bufptr[0] != SOH) // valid start
return bad_packet;
if ((bufptr[1] + bufptr[2]) != 0xff) // sanity check before duplicating
return bad_packet;
if (bufptr[1] == *packet_number)
return duplicate;
if (bufptr[1] != ((*packet_number + 1) & 0xff)) // check if it is the next packet
return bad_packet;
if (fallback)
{
uint8_t cksum = calchecksum(&bufptr[3], 128);
if (bufptr[131] != cksum)
return bad_packet;
}
else
{
uint16_t crc = calcrc(&bufptr[3], 128); // compute CRC and validate it
if ((bufptr[131] != (uint8_t)(crc >> 8)) || (bufptr[132] != (uint8_t)(crc)))
return bad_packet;
}
*packet_number = *packet_number + 1;
return good_packet;
}
uint16_t calcrc(uint8_t *bufptr, uint8_t size)
{
uint16_t crc = 0;
while (size--)
{
crc = _crc_xmodem_update(crc, *bufptr++); // util/crc16.h
//crc = small_crc_update(crc, *bufptr++);
}
return crc;
}
uint8_t calchecksum(uint8_t *bufptr, uint8_t size)
{
uint8_t cksum = 0;
while (size--) cksum += *bufptr++;
return cksum;
}
void MoveData(uint8_t *bufptr, uint8_t BytesToMove) // insert a data handler here, for example, write buffer to a flash device
{
for (uint8_t i = 0; i < BytesToMove; i++)
{
file[fileposition++] = bufptr[i];
fileposition &= 1023; // this is an example, so just overwrite our file if sent file is >1K
}
}
void HexDump16(uint8_t *bufptr, uint16_t Length)
{
uint16_t i;
char buff[17];
buff[16] = 0;
// Process every byte in the data.
for (i = 0; i < Length; i++)
{
// Multiple of 16 means new line (with line offset).
if ((i % 16) == 0)
{
// Just don't print ASCII for the zeroth line.
if (i != 0)
{
uart_puts(" ");
uart_puts(buff);
uart_puts("\r\n");
}
// Output the offset.
uart_puts(" ");
uart_puthex(i >> 8);
uart_puthex(i);
uart_putc(' ');
}
// Now the hex code for the specific character.
uart_putc(' ');
uart_puthex(bufptr[i]);
// And store a printable ASCII character for later.
if ((bufptr[i] < 0x20) || (bufptr[i] > 0x7e))
buff[i % 16] = '.';
else
buff[i % 16] = bufptr[i];
}
// Pad out last line if not exactly 16 characters.
while ((i % 16) != 0)
{
buff[i % 16] = 0;
uart_puts(" ");
i++;
}
// And print the final ASCII bit.
uart_puts(" ");
uart_puts(buff);
uart_puts("\r\n");
}