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 devlib/README 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.6 2004/08/15 05:31:46 bernie
32 * Add an #error to spread some FUD about the quality of this module;
33 * Add a few TODOs from Rasky's review;
34 * Update to the new drv/ser.c API;
35 * Move FlushSerial() to drv/ser.c and generalize.
37 * Revision 1.5 2004/08/12 23:46:21 bernie
38 * Remove extra indentation level in switch statements.
40 * Revision 1.4 2004/08/12 23:35:50 bernie
41 * Replace a handmade loop with memset().
43 * Revision 1.3 2004/08/12 23:34:36 bernie
44 * Replace if/else with continue to reduce indentation level.
46 * Revision 1.2 2004/08/12 23:24:07 bernie
47 * Rename UPDCRC() to UPDCRC16().
49 * Revision 1.1 2004/08/11 19:54:22 bernie
50 * Import XModem protocol into DevLib.
58 #include <drv/buzzer.h>
59 #include <mware/crc.h>
60 #include <mware/kfile.h>
62 #include <string.h> /* for memset() */
66 * \name Protocol control codes
69 #define XM_SOH 0x01 /*!< Start Of Header (128-byte block) */
70 #define XM_STX 0x02 /*!< Start Of Header (1024-byte block) */
71 #define XM_EOT 0x04 /*!< End Of Transmission */
72 #define XM_ACK 0x06 /*!< Acknowledge block */
73 #define XM_NAK 0x15 /*!< Negative Acknowledge */
74 #define XM_C 0x43 /*!< Request CRC-16 transmission */
75 #define XM_CAN 0x18 /*!< CANcel transmission */
78 #define XM_MAXRETRIES 15 /*!< Max retries before giving up */
79 #define XM_MAXCRCRETRIES 7 /*!< Max retries before switching to BCC */
80 #define XM_BUFSIZE 1024 /*!< Size of block buffer */
83 #if (ARCH & ARCH_BOOT)
85 #if (ARCH & ARCH_SLIM)
86 #define CHECK_ABORT KEYPRESSED_STOP
87 #elif (ARCH & ARCH_SARF)
88 #define CHECK_ABORT KEYPRESSED_ESC
92 #if (ARCH & ARCH_SLIM)
93 #define CHECK_ABORT (kbd_getchar() == K_STOP)
94 #elif (ARCH & ARCH_SARF)
95 #define CHECK_ABORT (kbd_getchar() == K_ESC)
97 #endif /* ARCH_BOOT */
100 /*! Buffer to hold a block of data */
101 static char block_buffer[XM_BUFSIZE];
105 * Decode serial driver errors and print them on the display.
107 static void print_serial_error(struct Serial *port, int retries)
109 serstatus_t err, status;
111 /* Get serial error code and reset it */
112 status = ser_getstatus(port);
113 ser_setstatus(port, 0);
115 /* Mostra tutti gli errori in sequenza */
116 for (err = 0; status != 0; status >>= 1, err++)
118 /* Se il bit dell'errore e' settato */
121 lcd_printf(0, 3, LCD_FILL, "%s %d", serial_errors[err], retries);
129 * \brief Receive a file using the XModem protocol.
131 * \param port Serial port to use for transfer
132 * \param fd Destination file
134 * \note This function allocates a large amount of stack (>1KB).
136 bool xmodem_recv(struct Serial *port, KFile *fd)
139 int blocknr = 0, last_block_done = 0, retries = 0;
147 lcd_printf(0, 2, LCD_FILL, "Starting Transfer...");
150 ser_settimeouts(port, SER_DEFRXTIMEOUT, SER_DEFTXTIMEOUT);
151 ser_setstatus(port, 0);
153 /* Send initial NAK to start transmission */
158 ser_putchar(XM_CAN, port);
159 ser_putchar(XM_CAN, port);
160 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
165 * Discard incoming input until a timeout occurs, then send
166 * a NAK to the transmitter.
173 SerialError(retries);
175 ser_resync(port, 200);
178 if (retries >= XM_MAXRETRIES)
180 ser_putchar(XM_CAN, port);
181 ser_putchar(XM_CAN, port);
182 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
186 /* Transmission start? */
189 if (retries < XM_MAXCRCRETRIES)
191 lcd_printf(0, 2, LCD_FILL, "Request Tx (CRC)");
192 ser_putchar(XM_C, port);
196 /* Give up with CRC and fall back to checksum */
198 lcd_printf(0, 2, LCD_FILL, "Request Tx (BCC)");
199 ser_putchar(XM_NAK, port);
203 ser_putchar(XM_NAK, port);
206 switch (ser_getchar(port))
208 case XM_STX: /* Start of header (1024-byte block) */
212 case XM_SOH: /* Start of header (128-byte block) */
216 /* Get block number */
217 c = ser_getchar(port);
219 /* Check complemented block number */
220 if ((~c & 0xff) != ser_getchar(port))
222 lcd_printf(0, 3, LCD_FILL, "Bad blk (%d)", c);
227 /* Determine which block is being sent */
228 if (c == (blocknr & 0xff))
229 /* Last block repeated */
230 lcd_printf(0, 2, LCD_FILL, "Repeat blk %d", blocknr);
231 else if (c == ((blocknr + 1) & 0xff))
233 lcd_printf(0, 2, LCD_FILL, "Recv blk %d", ++blocknr);
237 lcd_printf(0, 3, LCD_FILL, "Sync lost (%d/%d)", c, blocknr);
242 buf = block_buffer; /* Reset pointer to start of buffer */
245 for (i = 0; i < blocksize; i++)
247 if ((c = ser_getchar(port)) == EOF)
253 /* Store in buffer */
256 /* Calculate block checksum or CRC */
258 crc = UPDCRC16(c, crc);
266 /* Get the checksum byte or the CRC-16 MSB */
267 if ((c = ser_getchar(port)) == EOF)
275 crc = UPDCRC16(c, crc);
278 if ((c = ser_getchar(port)) == EOF)
284 crc = UPDCRC16(c, crc);
288 lcd_printf(0, 3, LCD_FILL, "Bad CRC: %04x", crc);
293 /* Compare the checksum */
294 else if (c != checksum)
296 lcd_printf(0, 3, LCD_FILL, "Bad sum: %04x/%04x", checksum, c);
302 * Avoid flushing the same block twice.
303 * This could happen when the sender does not receive our
304 * acknowledge and resends the same block.
306 if (last_block_done < blocknr)
308 /* Call user function to flush the buffer */
309 if (fd->write(fd, block_buffer, blocksize))
311 /* Acknowledge block and clear error counter */
312 ser_putchar(XM_ACK, port);
314 last_block_done = blocknr;
318 /* User callback failed: abort transfer immediately */
319 retries = XM_MAXRETRIES;
325 case XM_EOT: /* End of transmission */
326 ser_putchar(XM_ACK, port);
327 lcd_printf(0, 2, LCD_FILL, "Transfer completed");
330 case EOF: /* Timeout or serial error */
335 lcd_printf(0, 3, LCD_FILL, "Skipping garbage");
344 * \brief Transmit a file using the XModem protocol.
346 * \param port Serial port to use for transfer
347 * \param fd Source file
349 * \note This function allocates a large amount of stack for
350 * the XModem transfer buffer (1KB).
352 bool xmodem_send(struct Serial *port, KFile *fd)
355 int blocknr = 1, retries = 0, c, i;
356 bool proceed, usecrc = false;
361 ser_settimeouts(port, SER_DEFRXTIMEOUT, SER_DEFTXTIMEOUT);
362 ser_setstatus(port, 0);
364 lcd_printf(0, 2, LCD_FILL, "Wait remote host");
374 switch (c = ser_getchar(port))
382 lcd_printf(0, 2, LCD_FILL, "Tx start (CRC)");
386 lcd_printf(0, 2, LCD_FILL, "Tx start (BCC)");
388 /* Call user function to read in one block */
389 size = fd->read(fd, block_buffer, XM_BUFSIZE);
392 lcd_printf(0, 2, LCD_FILL, "Resend blk %d", blocknr);
397 /* End of transfer? */
401 /* Call user function to read in one block */
402 size = fd->read(fd, block_buffer, XM_BUFSIZE);
406 lcd_printf(0, 2, LCD_FILL, "Send blk %d", blocknr);
411 SerialError(retries);
412 if (retries <= XM_MAXRETRIES)
414 /* falling through! */
417 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
421 lcd_printf(0, 3, LCD_FILL, "Skipping garbage");
429 ser_putchar(XM_EOT, port);
433 /* Pad block with 0xFF if it's partially full */
434 memset(block_buffer + size, 0xFF, XM_BUFSIZE - size);
436 /* Send block header (STX, blocknr, ~blocknr) */
437 ser_putchar(XM_STX, port);
438 ser_putchar(blocknr & 0xFF, port);
439 ser_putchar(~blocknr & 0xFF, port);
441 /* Send block and compute its CRC/checksum */
444 for (i = 0; i < XM_BUFSIZE; i++)
446 ser_putchar(block_buffer[i], port);
447 crc = UPDCRC16(block_buffer[i], crc);
448 sum += block_buffer[i];
451 /* Send CRC/Checksum */
454 crc = UPDCRC16(0, crc);
455 crc = UPDCRC16(0, crc);
456 ser_putchar(crc >> 8, port);
457 ser_putchar(crc & 0xFF, port);
460 ser_putchar(sum, port);