4 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 1999, 2001 Bernardo Innocenti <bernie@develer.com>
6 * This file is part of DevLib - See devlib/README for information.
8 * \brief X-Modem serial transmission protocol (implementation)
10 * Suppots the CRC-16 and 1K-blocks variants of the standard.
11 * \see ymodem.txt for the protocol description.
14 * \author Bernardo Innocenti <bernie@develer.com>
19 * Revision 1.3 2004/08/12 23:34:36 bernie
20 * Replace if/else with continue to reduce indentation level.
22 * Revision 1.2 2004/08/12 23:24:07 bernie
23 * Rename UPDCRC() to UPDCRC16().
25 * Revision 1.1 2004/08/11 19:54:22 bernie
26 * Import XModem protocol into DevLib.
34 #include <drv/buzzer.h>
35 #include <mware/crc.h>
36 #include <mware/kfile.h>
40 * \name Protocol control codes
43 #define XM_SOH 0x01 /*!< Start Of Header (128-byte block) */
44 #define XM_STX 0x02 /*!< Start Of Header (1024-byte block) */
45 #define XM_EOT 0x04 /*!< End Of Transmission */
46 #define XM_ACK 0x06 /*!< Acknowledge block */
47 #define XM_NAK 0x15 /*!< Negative Acknowledge */
48 #define XM_C 0x43 /*!< Request CRC-16 transmission */
49 #define XM_CAN 0x18 /*!< CANcel transmission */
52 #define XM_MAXRETRIES 15 /*!< Max retries before giving up */
53 #define XM_MAXCRCRETRIES 7 /*!< Max retries before switching to BCC */
54 #define XM_BUFSIZE 1024 /*!< Size of block buffer */
57 #if (ARCH & ARCH_BOOT)
59 #if (ARCH & ARCH_SLIM)
60 #define CHECK_ABORT KEYPRESSED_STOP
61 #elif (ARCH & ARCH_SARF)
62 #define CHECK_ABORT KEYPRESSED_ESC
66 #if (ARCH & ARCH_SLIM)
67 #define CHECK_ABORT (kbd_getchar() == K_STOP)
68 #elif (ARCH & ARCH_SARF)
69 #define CHECK_ABORT (kbd_getchar() == K_ESC)
71 #endif /* ARCH_BOOT */
74 /*! Buffer to hold a block of data */
75 static char block_buffer[XM_BUFSIZE];
79 * Decode serial driver errors and print
80 * them on the display.
82 static void SerialError(int retries)
84 serstatus_t err, status;
86 /* Get serial error code and reset it */
87 status = ser_getstatus();
90 /* Mostra tutti gli errori in sequenza */
91 for (err = 0; status != 0; status >>= 1, err++)
93 /* Se il bit dell'errore e' settato */
96 lcd_printf(0, 3, LCD_FILL, "%s %d", serial_errors[err], retries);
105 * Reset previous serial errors and flush the receive buffer
106 * (set a short timeout to speed up purge)
108 static void FlushSerial(void)
111 ser_settimeouts(200, SER_DEFTXTIMEOUT);
112 while (ser_getchar() != EOF) {}
113 ser_settimeouts(SER_DEFRXTIMEOUT, SER_DEFTXTIMEOUT);
118 bool xmodem_recv(KFile *fd)
121 int blocknr = 0, last_block_done = 0, retries = 0;
129 lcd_printf(0, 2, LCD_FILL, "Starting Transfer...");
132 ser_settimeouts(SER_DEFRXTIMEOUT, SER_DEFTXTIMEOUT);
135 /* Send initial NAK to start transmission */
142 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
147 * Discard incoming input until a timeout occurs, then send
148 * a NAK to the transmitter.
155 SerialError(retries);
160 if (retries >= XM_MAXRETRIES)
164 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
168 /* Transmission start? */
171 if (retries < XM_MAXCRCRETRIES)
173 lcd_printf(0, 2, LCD_FILL, "Request Tx (CRC)");
178 /* Give up with CRC and fall back to checksum */
180 lcd_printf(0, 2, LCD_FILL, "Request Tx (BCC)");
188 switch (ser_getchar())
190 case XM_STX: /* Start of header (1024-byte block) */
194 case XM_SOH: /* Start of header (128-byte block) */
198 /* Get block number */
201 /* Check complemented block number */
202 if ((~c & 0xff) != ser_getchar())
204 lcd_printf(0, 3, LCD_FILL, "Bad blk (%d)", c);
209 /* Determine which block is being sent */
210 if (c == (blocknr & 0xff))
211 /* Last block repeated */
212 lcd_printf(0, 2, LCD_FILL, "Repeat blk %d", blocknr);
213 else if (c == ((blocknr + 1) & 0xff))
215 lcd_printf(0, 2, LCD_FILL, "Recv blk %d", ++blocknr);
219 lcd_printf(0, 3, LCD_FILL, "Sync lost (%d/%d)", c, blocknr);
224 buf = block_buffer; /* Reset pointer to start of buffer */
227 for (i = 0; i < blocksize; i++)
229 if ((c = ser_getchar()) == EOF)
235 /* Store in buffer */
238 /* Calculate block checksum or CRC */
240 crc = UPDCRC16(c, crc);
248 /* Get the checksum byte or the CRC-16 MSB */
249 if ((c = ser_getchar()) == EOF)
257 crc = UPDCRC16(c, crc);
260 if ((c = ser_getchar()) == EOF)
266 crc = UPDCRC16(c, crc);
270 lcd_printf(0, 3, LCD_FILL, "Bad CRC: %04x", crc);
275 /* Compare the checksum */
276 else if (c != checksum)
278 lcd_printf(0, 3, LCD_FILL, "Bad sum: %04x/%04x", checksum, c);
284 * Avoid flushing the same block twice.
285 * This could happen when the sender does not receive our
286 * acknowledge and resends the same block.
288 if (last_block_done < blocknr)
290 /* Call user function to flush the buffer */
291 if (fd->write(fd, block_buffer, blocksize))
293 /* Acknowledge block and clear error counter */
296 last_block_done = blocknr;
300 /* User callback failed: abort transfer immediately */
301 retries = XM_MAXRETRIES;
307 case XM_EOT: /* End of transmission */
309 lcd_printf(0, 2, LCD_FILL, "Transfer completed");
312 case EOF: /* Timeout or serial error */
317 lcd_printf(0, 3, LCD_FILL, "Skipping garbage");
325 bool xmodem_send(KFile *fd)
328 int blocknr = 1, retries = 0, c, i;
329 bool proceed, usecrc = false;
334 ser_settimeouts(SER_DEFRXTIMEOUT, SER_DEFTXTIMEOUT);
337 lcd_printf(0, 2, LCD_FILL, "Wait remote host");
347 switch (c = ser_getchar())
355 lcd_printf(0, 2, LCD_FILL, "Tx start (CRC)");
359 lcd_printf(0, 2, LCD_FILL, "Tx start (BCC)");
361 /* Call user function to read in one block */
362 size = fd->read(fd, block_buffer, XM_BUFSIZE);
365 lcd_printf(0, 2, LCD_FILL, "Resend blk %d", blocknr);
370 /* End of transfer? */
374 /* Call user function to read in one block */
375 size = fd->read(fd, block_buffer, XM_BUFSIZE);
379 lcd_printf(0, 2, LCD_FILL, "Send blk %d", blocknr);
384 SerialError(retries);
385 if (retries <= XM_MAXRETRIES)
387 /* falling through! */
390 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
394 lcd_printf(0, 3, LCD_FILL, "Skipping garbage");
406 /* Pad block with 0xFF if it's partially full */
407 if (size < XM_BUFSIZE)
408 for (i = size; i < XM_BUFSIZE; i++)
409 block_buffer[i] = (char)0xFF;
411 /* Send block header (STX, blocknr, ~blocknr) */
413 ser_putchar(blocknr & 0xFF);
414 ser_putchar(~blocknr & 0xFF);
416 /* Send block and compute its CRC/checksum */
419 for (i = 0; i < XM_BUFSIZE; i++)
421 ser_putchar(block_buffer[i]);
422 crc = UPDCRC16(block_buffer[i], crc);
423 sum += block_buffer[i];
426 /* Send CRC/Checksum */
429 crc = UPDCRC16(0, crc);
430 crc = UPDCRC16(0, crc);
431 ser_putchar(crc >> 8);
432 ser_putchar(crc & 0xFF);