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.2 2004/08/12 23:24:07 bernie
20 * Rename UPDCRC() to UPDCRC16().
22 * Revision 1.1 2004/08/11 19:54:22 bernie
23 * Import XModem protocol into DevLib.
31 #include <drv/buzzer.h>
32 #include <mware/crc.h>
33 #include <mware/kfile.h>
37 * \name Protocol control codes
40 #define XM_SOH 0x01 /*!< Start Of Header (128-byte block) */
41 #define XM_STX 0x02 /*!< Start Of Header (1024-byte block) */
42 #define XM_EOT 0x04 /*!< End Of Transmission */
43 #define XM_ACK 0x06 /*!< Acknowledge block */
44 #define XM_NAK 0x15 /*!< Negative Acknowledge */
45 #define XM_C 0x43 /*!< Request CRC-16 transmission */
46 #define XM_CAN 0x18 /*!< CANcel transmission */
49 #define XM_MAXRETRIES 15 /*!< Max retries before giving up */
50 #define XM_MAXCRCRETRIES 7 /*!< Max retries before switching to BCC */
51 #define XM_BUFSIZE 1024 /*!< Size of block buffer */
54 #if (ARCH & ARCH_BOOT)
56 #if (ARCH & ARCH_SLIM)
57 #define CHECK_ABORT KEYPRESSED_STOP
58 #elif (ARCH & ARCH_SARF)
59 #define CHECK_ABORT KEYPRESSED_ESC
63 #if (ARCH & ARCH_SLIM)
64 #define CHECK_ABORT (kbd_getchar() == K_STOP)
65 #elif (ARCH & ARCH_SARF)
66 #define CHECK_ABORT (kbd_getchar() == K_ESC)
68 #endif /* ARCH_BOOT */
71 /*! Buffer to hold a block of data */
72 static char block_buffer[XM_BUFSIZE];
76 * Decode serial driver errors and print
77 * them on the display.
79 static void SerialError(int retries)
81 serstatus_t err, status;
83 /* Get serial error code and reset it */
84 status = ser_getstatus();
87 /* Mostra tutti gli errori in sequenza */
88 for (err = 0; status != 0; status >>= 1, err++)
90 /* Se il bit dell'errore e' settato */
93 lcd_printf(0, 3, LCD_FILL, "%s %d", serial_errors[err], retries);
102 * Reset previous serial errors and flush the receive buffer
103 * (set a short timeout to speed up purge)
105 static void FlushSerial(void)
108 ser_settimeouts(200, SER_DEFTXTIMEOUT);
109 while (ser_getchar() != EOF) {}
110 ser_settimeouts(SER_DEFRXTIMEOUT, SER_DEFTXTIMEOUT);
115 bool xmodem_recv(KFile *fd)
118 int blocknr = 0, last_block_done = 0, retries = 0;
126 lcd_printf(0, 2, LCD_FILL, "Starting Transfer...");
129 ser_settimeouts(SER_DEFRXTIMEOUT, SER_DEFTXTIMEOUT);
132 /* Send initial NAK to start transmission */
139 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
144 * Discard incoming input until a timeout occurs, then send
145 * a NAK to the transmitter.
152 SerialError(retries);
157 if (retries >= XM_MAXRETRIES)
161 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
165 /* Transmission start? */
168 if (retries < XM_MAXCRCRETRIES)
170 lcd_printf(0, 2, LCD_FILL, "Request Tx (CRC)");
175 /* Give up with CRC and fall back to checksum */
177 lcd_printf(0, 2, LCD_FILL, "Request Tx (BCC)");
185 switch (ser_getchar())
187 case XM_STX: /* Start of header (1024-byte block) */
191 case XM_SOH: /* Start of header (128-byte block) */
195 /* Get block number */
198 /* Check complemented block number */
199 if ((~c & 0xff) != ser_getchar())
201 lcd_printf(0, 3, LCD_FILL, "Bad blk (%d)", c);
206 /* Determine which block is being sent */
207 if (c == (blocknr & 0xff))
208 /* Last block repeated */
209 lcd_printf(0, 2, LCD_FILL, "Repeat blk %d", blocknr);
210 else if (c == ((blocknr + 1) & 0xff))
212 lcd_printf(0, 2, LCD_FILL, "Recv blk %d", ++blocknr);
216 lcd_printf(0, 3, LCD_FILL, "Sync lost (%d/%d)", c, blocknr);
221 buf = block_buffer; /* Reset pointer to start of buffer */
224 for (i = 0; i < blocksize; i++)
226 if ((c = ser_getchar()) == EOF)
232 /* Store in buffer */
235 /* Calculate block checksum or CRC */
237 crc = UPDCRC16(c, crc);
245 /* Get the checksum byte or the CRC-16 MSB */
246 if ((c = ser_getchar()) == EOF)
254 crc = UPDCRC16(c, crc);
257 if ((c = ser_getchar()) == EOF)
263 crc = UPDCRC16(c, crc);
267 lcd_printf(0, 3, LCD_FILL, "Bad CRC: %04x", crc);
272 /* Compare the checksum */
273 else if (c != checksum)
275 lcd_printf(0, 3, LCD_FILL, "Bad sum: %04x/%04x", checksum, c);
281 * Avoid flushing the same block twice.
282 * This could happen when the sender does not receive our
283 * acknowledge and resends the same block.
285 if (last_block_done < blocknr)
287 /* Call user function to flush the buffer */
288 if (fd->write(fd, block_buffer, blocksize))
290 /* Acknowledge block and clear error counter */
293 last_block_done = blocknr;
297 /* User callback failed: abort transfer immediately */
298 retries = XM_MAXRETRIES;
304 case XM_EOT: /* End of transmission */
306 lcd_printf(0, 2, LCD_FILL, "Transfer completed");
309 case EOF: /* Timeout or serial error */
314 lcd_printf(0, 3, LCD_FILL, "Skipping garbage");
322 bool xmodem_send(KFile *fd)
325 int blocknr = 1, retries = 0, c, i;
326 bool proceed, usecrc = false;
331 ser_settimeouts(SER_DEFRXTIMEOUT, SER_DEFTXTIMEOUT);
334 lcd_printf(0, 2, LCD_FILL, "Wait remote host");
344 switch (c = ser_getchar())
352 lcd_printf(0, 2, LCD_FILL, "Tx start (CRC)");
356 lcd_printf(0, 2, LCD_FILL, "Tx start (BCC)");
358 /* Call user function to read in one block */
359 size = fd->read(fd, block_buffer, XM_BUFSIZE);
362 lcd_printf(0, 2, LCD_FILL, "Resend blk %d", blocknr);
367 /* End of transfer? */
371 /* Call user function to read in one block */
372 size = fd->read(fd, block_buffer, XM_BUFSIZE);
376 lcd_printf(0, 2, LCD_FILL, "Send blk %d", blocknr);
381 SerialError(retries);
382 if (retries <= XM_MAXRETRIES)
384 /* falling through! */
387 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
391 lcd_printf(0, 3, LCD_FILL, "Skipping garbage");
401 /* Pad block with 0xFF if it's partially full */
402 if (size < XM_BUFSIZE)
403 for (i = size; i < XM_BUFSIZE; i++)
404 block_buffer[i] = (char)0xFF;
406 /* Send block header (STX, blocknr, ~blocknr) */
408 ser_putchar(blocknr & 0xFF);
409 ser_putchar(~blocknr & 0xFF);
411 /* Send block and compute its CRC/checksum */
414 for (i = 0; i < XM_BUFSIZE; i++)
416 ser_putchar(block_buffer[i]);
417 crc = UPDCRC16(block_buffer[i], crc);
418 sum += block_buffer[i];
421 /* Send CRC/Checksum */
424 crc = UPDCRC16(0, crc);
425 crc = UPDCRC16(0, crc);
426 ser_putchar(crc >> 8);
427 ser_putchar(crc & 0xFF);