1 #error This module has not been revised for the API changes in several DevLib modules
5 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
6 * Copyright 1999, 2001 Bernardo Innocenti <bernie@develer.com>
7 * This file is part of DevLib - See README.devlib for information.
9 * \brief X-Modem serial transmission protocol (implementation)
11 * Suppots the CRC-16 and 1K-blocks variants of the standard.
12 * \see ymodem.txt for the protocol description.
14 * \todo Decouple this code from the LCD, buzzer and timer drivers
15 * introducing user hooks or macros like CHECK_ABORT.
17 * \todo Break xmodem_send() and xmodem_recv() in smaller functions.
19 * \todo Add CONFIG_* vars to exclude either the receiver or the sender,
20 * to reduce the footprint for applications that don't need both.
22 * \todo Maybe convert drv/ser.c to the KFile interface for symmetry and
26 * \author Bernardo Innocenti <bernie@develer.com>
31 *#* Revision 1.9 2005/11/04 16:20:02 bernie
32 *#* Fix reference to README.devlib in header.
34 *#* Revision 1.8 2004/08/25 14:12:09 rasky
35 *#* Aggiornato il comment block dei log RCS
37 *#* Revision 1.7 2004/08/15 06:30:06 bernie
38 *#* Make the buffer a local variable, as documented.
40 *#* Revision 1.6 2004/08/15 05:31:46 bernie
41 *#* Add an #error to spread some FUD about the quality of this module;
42 *#* Add a few TODOs from Rasky's review;
43 *#* Update to the new drv/ser.c API;
44 *#* Move FlushSerial() to drv/ser.c and generalize.
46 *#* Revision 1.5 2004/08/12 23:46:21 bernie
47 *#* Remove extra indentation level in switch statements.
49 *#* Revision 1.4 2004/08/12 23:35:50 bernie
50 *#* Replace a handmade loop with memset().
52 *#* Revision 1.3 2004/08/12 23:34:36 bernie
53 *#* Replace if/else with continue to reduce indentation level.
55 *#* Revision 1.2 2004/08/12 23:24:07 bernie
56 *#* Rename UPDCRC() to UPDCRC16().
58 *#* Revision 1.1 2004/08/11 19:54:22 bernie
59 *#* Import XModem protocol into DevLib.
67 #include <drv/buzzer.h>
68 #include <mware/crc.h>
69 #include <mware/kfile.h>
71 #include <string.h> /* for memset() */
75 * \name Protocol control codes
78 #define XM_SOH 0x01 /*!< Start Of Header (128-byte block) */
79 #define XM_STX 0x02 /*!< Start Of Header (1024-byte block) */
80 #define XM_EOT 0x04 /*!< End Of Transmission */
81 #define XM_ACK 0x06 /*!< Acknowledge block */
82 #define XM_NAK 0x15 /*!< Negative Acknowledge */
83 #define XM_C 0x43 /*!< Request CRC-16 transmission */
84 #define XM_CAN 0x18 /*!< CANcel transmission */
87 #define XM_MAXRETRIES 15 /*!< Max retries before giving up */
88 #define XM_MAXCRCRETRIES 7 /*!< Max retries before switching to BCC */
89 #define XM_BUFSIZE 1024 /*!< Size of block buffer */
92 #if (ARCH & ARCH_BOOT)
94 #if (ARCH & ARCH_SLIM)
95 #define CHECK_ABORT KEYPRESSED_STOP
96 #elif (ARCH & ARCH_SARF)
97 #define CHECK_ABORT KEYPRESSED_ESC
101 #if (ARCH & ARCH_SLIM)
102 #define CHECK_ABORT (kbd_getchar() == K_STOP)
103 #elif (ARCH & ARCH_SARF)
104 #define CHECK_ABORT (kbd_getchar() == K_ESC)
106 #endif /* ARCH_BOOT */
110 * Decode serial driver errors and print them on the display.
112 static void print_serial_error(struct Serial *port, int retries)
114 serstatus_t err, status;
116 /* Get serial error code and reset it */
117 status = ser_getstatus(port);
118 ser_setstatus(port, 0);
120 /* Mostra tutti gli errori in sequenza */
121 for (err = 0; status != 0; status >>= 1, err++)
123 /* Se il bit dell'errore e' settato */
126 lcd_printf(0, 3, LCD_FILL, "%s %d", serial_errors[err], retries);
135 * \brief Receive a file using the XModem protocol.
137 * \param port Serial port to use for transfer
138 * \param fd Destination file
140 * \note This function allocates a large amount of stack (>1KB).
142 bool xmodem_recv(struct Serial *port, KFile *fd)
144 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
146 int blocknr = 0, last_block_done = 0, retries = 0;
154 lcd_printf(0, 2, LCD_FILL, "Starting Transfer...");
157 ser_settimeouts(port, SER_DEFRXTIMEOUT, SER_DEFTXTIMEOUT);
158 ser_setstatus(port, 0);
160 /* Send initial NAK to start transmission */
165 ser_putchar(XM_CAN, port);
166 ser_putchar(XM_CAN, port);
167 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
172 * Discard incoming input until a timeout occurs, then send
173 * a NAK to the transmitter.
180 SerialError(retries);
182 ser_resync(port, 200);
185 if (retries >= XM_MAXRETRIES)
187 ser_putchar(XM_CAN, port);
188 ser_putchar(XM_CAN, port);
189 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
193 /* Transmission start? */
196 if (retries < XM_MAXCRCRETRIES)
198 lcd_printf(0, 2, LCD_FILL, "Request Tx (CRC)");
199 ser_putchar(XM_C, port);
203 /* Give up with CRC and fall back to checksum */
205 lcd_printf(0, 2, LCD_FILL, "Request Tx (BCC)");
206 ser_putchar(XM_NAK, port);
210 ser_putchar(XM_NAK, port);
213 switch (ser_getchar(port))
215 case XM_STX: /* Start of header (1024-byte block) */
219 case XM_SOH: /* Start of header (128-byte block) */
223 /* Get block number */
224 c = ser_getchar(port);
226 /* Check complemented block number */
227 if ((~c & 0xff) != ser_getchar(port))
229 lcd_printf(0, 3, LCD_FILL, "Bad blk (%d)", c);
234 /* Determine which block is being sent */
235 if (c == (blocknr & 0xff))
236 /* Last block repeated */
237 lcd_printf(0, 2, LCD_FILL, "Repeat blk %d", blocknr);
238 else if (c == ((blocknr + 1) & 0xff))
240 lcd_printf(0, 2, LCD_FILL, "Recv blk %d", ++blocknr);
244 lcd_printf(0, 3, LCD_FILL, "Sync lost (%d/%d)", c, blocknr);
249 buf = block_buffer; /* Reset pointer to start of buffer */
252 for (i = 0; i < blocksize; i++)
254 if ((c = ser_getchar(port)) == EOF)
260 /* Store in buffer */
263 /* Calculate block checksum or CRC */
265 crc = UPDCRC16(c, crc);
273 /* Get the checksum byte or the CRC-16 MSB */
274 if ((c = ser_getchar(port)) == EOF)
282 crc = UPDCRC16(c, crc);
285 if ((c = ser_getchar(port)) == EOF)
291 crc = UPDCRC16(c, crc);
295 lcd_printf(0, 3, LCD_FILL, "Bad CRC: %04x", crc);
300 /* Compare the checksum */
301 else if (c != checksum)
303 lcd_printf(0, 3, LCD_FILL, "Bad sum: %04x/%04x", checksum, c);
309 * Avoid flushing the same block twice.
310 * This could happen when the sender does not receive our
311 * acknowledge and resends the same block.
313 if (last_block_done < blocknr)
315 /* Call user function to flush the buffer */
316 if (fd->write(fd, block_buffer, blocksize))
318 /* Acknowledge block and clear error counter */
319 ser_putchar(XM_ACK, port);
321 last_block_done = blocknr;
325 /* User callback failed: abort transfer immediately */
326 retries = XM_MAXRETRIES;
332 case XM_EOT: /* End of transmission */
333 ser_putchar(XM_ACK, port);
334 lcd_printf(0, 2, LCD_FILL, "Transfer completed");
337 case EOF: /* Timeout or serial error */
342 lcd_printf(0, 3, LCD_FILL, "Skipping garbage");
351 * \brief Transmit a file using the XModem protocol.
353 * \param port Serial port to use for transfer
354 * \param fd Source file
356 * \note This function allocates a large amount of stack for
357 * the XModem transfer buffer (1KB).
359 bool xmodem_send(struct Serial *port, KFile *fd)
361 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
363 int blocknr = 1, retries = 0, c, i;
364 bool proceed, usecrc = false;
369 ser_settimeouts(port, SER_DEFRXTIMEOUT, SER_DEFTXTIMEOUT);
370 ser_setstatus(port, 0);
372 lcd_printf(0, 2, LCD_FILL, "Wait remote host");
382 switch (c = ser_getchar(port))
390 lcd_printf(0, 2, LCD_FILL, "Tx start (CRC)");
394 lcd_printf(0, 2, LCD_FILL, "Tx start (BCC)");
396 /* Call user function to read in one block */
397 size = fd->read(fd, block_buffer, XM_BUFSIZE);
400 lcd_printf(0, 2, LCD_FILL, "Resend blk %d", blocknr);
405 /* End of transfer? */
409 /* Call user function to read in one block */
410 size = fd->read(fd, block_buffer, XM_BUFSIZE);
414 lcd_printf(0, 2, LCD_FILL, "Send blk %d", blocknr);
419 SerialError(retries);
420 if (retries <= XM_MAXRETRIES)
422 /* falling through! */
425 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
429 lcd_printf(0, 3, LCD_FILL, "Skipping garbage");
437 ser_putchar(XM_EOT, port);
441 /* Pad block with 0xFF if it's partially full */
442 memset(block_buffer + size, 0xFF, XM_BUFSIZE - size);
444 /* Send block header (STX, blocknr, ~blocknr) */
445 ser_putchar(XM_STX, port);
446 ser_putchar(blocknr & 0xFF, port);
447 ser_putchar(~blocknr & 0xFF, port);
449 /* Send block and compute its CRC/checksum */
452 for (i = 0; i < XM_BUFSIZE; i++)
454 ser_putchar(block_buffer[i], port);
455 crc = UPDCRC16(block_buffer[i], crc);
456 sum += block_buffer[i];
459 /* Send CRC/Checksum */
462 crc = UPDCRC16(0, crc);
463 crc = UPDCRC16(0, crc);
464 ser_putchar(crc >> 8, port);
465 ser_putchar(crc & 0xFF, port);
468 ser_putchar(sum, port);