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.7 2004/08/15 06:30:06 bernie
32 * Make the buffer a local variable, as documented.
34 * Revision 1.6 2004/08/15 05:31:46 bernie
35 * Add an #error to spread some FUD about the quality of this module;
36 * Add a few TODOs from Rasky's review;
37 * Update to the new drv/ser.c API;
38 * Move FlushSerial() to drv/ser.c and generalize.
40 * Revision 1.5 2004/08/12 23:46:21 bernie
41 * Remove extra indentation level in switch statements.
43 * Revision 1.4 2004/08/12 23:35:50 bernie
44 * Replace a handmade loop with memset().
46 * Revision 1.3 2004/08/12 23:34:36 bernie
47 * Replace if/else with continue to reduce indentation level.
49 * Revision 1.2 2004/08/12 23:24:07 bernie
50 * Rename UPDCRC() to UPDCRC16().
52 * Revision 1.1 2004/08/11 19:54:22 bernie
53 * Import XModem protocol into DevLib.
61 #include <drv/buzzer.h>
62 #include <mware/crc.h>
63 #include <mware/kfile.h>
65 #include <string.h> /* for memset() */
69 * \name Protocol control codes
72 #define XM_SOH 0x01 /*!< Start Of Header (128-byte block) */
73 #define XM_STX 0x02 /*!< Start Of Header (1024-byte block) */
74 #define XM_EOT 0x04 /*!< End Of Transmission */
75 #define XM_ACK 0x06 /*!< Acknowledge block */
76 #define XM_NAK 0x15 /*!< Negative Acknowledge */
77 #define XM_C 0x43 /*!< Request CRC-16 transmission */
78 #define XM_CAN 0x18 /*!< CANcel transmission */
81 #define XM_MAXRETRIES 15 /*!< Max retries before giving up */
82 #define XM_MAXCRCRETRIES 7 /*!< Max retries before switching to BCC */
83 #define XM_BUFSIZE 1024 /*!< Size of block buffer */
86 #if (ARCH & ARCH_BOOT)
88 #if (ARCH & ARCH_SLIM)
89 #define CHECK_ABORT KEYPRESSED_STOP
90 #elif (ARCH & ARCH_SARF)
91 #define CHECK_ABORT KEYPRESSED_ESC
95 #if (ARCH & ARCH_SLIM)
96 #define CHECK_ABORT (kbd_getchar() == K_STOP)
97 #elif (ARCH & ARCH_SARF)
98 #define CHECK_ABORT (kbd_getchar() == K_ESC)
100 #endif /* ARCH_BOOT */
104 * Decode serial driver errors and print them on the display.
106 static void print_serial_error(struct Serial *port, int retries)
108 serstatus_t err, status;
110 /* Get serial error code and reset it */
111 status = ser_getstatus(port);
112 ser_setstatus(port, 0);
114 /* Mostra tutti gli errori in sequenza */
115 for (err = 0; status != 0; status >>= 1, err++)
117 /* Se il bit dell'errore e' settato */
120 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)
138 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
140 int blocknr = 0, last_block_done = 0, retries = 0;
148 lcd_printf(0, 2, LCD_FILL, "Starting Transfer...");
151 ser_settimeouts(port, SER_DEFRXTIMEOUT, SER_DEFTXTIMEOUT);
152 ser_setstatus(port, 0);
154 /* Send initial NAK to start transmission */
159 ser_putchar(XM_CAN, port);
160 ser_putchar(XM_CAN, port);
161 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
166 * Discard incoming input until a timeout occurs, then send
167 * a NAK to the transmitter.
174 SerialError(retries);
176 ser_resync(port, 200);
179 if (retries >= XM_MAXRETRIES)
181 ser_putchar(XM_CAN, port);
182 ser_putchar(XM_CAN, port);
183 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
187 /* Transmission start? */
190 if (retries < XM_MAXCRCRETRIES)
192 lcd_printf(0, 2, LCD_FILL, "Request Tx (CRC)");
193 ser_putchar(XM_C, port);
197 /* Give up with CRC and fall back to checksum */
199 lcd_printf(0, 2, LCD_FILL, "Request Tx (BCC)");
200 ser_putchar(XM_NAK, port);
204 ser_putchar(XM_NAK, port);
207 switch (ser_getchar(port))
209 case XM_STX: /* Start of header (1024-byte block) */
213 case XM_SOH: /* Start of header (128-byte block) */
217 /* Get block number */
218 c = ser_getchar(port);
220 /* Check complemented block number */
221 if ((~c & 0xff) != ser_getchar(port))
223 lcd_printf(0, 3, LCD_FILL, "Bad blk (%d)", c);
228 /* Determine which block is being sent */
229 if (c == (blocknr & 0xff))
230 /* Last block repeated */
231 lcd_printf(0, 2, LCD_FILL, "Repeat blk %d", blocknr);
232 else if (c == ((blocknr + 1) & 0xff))
234 lcd_printf(0, 2, LCD_FILL, "Recv blk %d", ++blocknr);
238 lcd_printf(0, 3, LCD_FILL, "Sync lost (%d/%d)", c, blocknr);
243 buf = block_buffer; /* Reset pointer to start of buffer */
246 for (i = 0; i < blocksize; i++)
248 if ((c = ser_getchar(port)) == EOF)
254 /* Store in buffer */
257 /* Calculate block checksum or CRC */
259 crc = UPDCRC16(c, crc);
267 /* Get the checksum byte or the CRC-16 MSB */
268 if ((c = ser_getchar(port)) == EOF)
276 crc = UPDCRC16(c, crc);
279 if ((c = ser_getchar(port)) == EOF)
285 crc = UPDCRC16(c, crc);
289 lcd_printf(0, 3, LCD_FILL, "Bad CRC: %04x", crc);
294 /* Compare the checksum */
295 else if (c != checksum)
297 lcd_printf(0, 3, LCD_FILL, "Bad sum: %04x/%04x", checksum, c);
303 * Avoid flushing the same block twice.
304 * This could happen when the sender does not receive our
305 * acknowledge and resends the same block.
307 if (last_block_done < blocknr)
309 /* Call user function to flush the buffer */
310 if (fd->write(fd, block_buffer, blocksize))
312 /* Acknowledge block and clear error counter */
313 ser_putchar(XM_ACK, port);
315 last_block_done = blocknr;
319 /* User callback failed: abort transfer immediately */
320 retries = XM_MAXRETRIES;
326 case XM_EOT: /* End of transmission */
327 ser_putchar(XM_ACK, port);
328 lcd_printf(0, 2, LCD_FILL, "Transfer completed");
331 case EOF: /* Timeout or serial error */
336 lcd_printf(0, 3, LCD_FILL, "Skipping garbage");
345 * \brief Transmit a file using the XModem protocol.
347 * \param port Serial port to use for transfer
348 * \param fd Source file
350 * \note This function allocates a large amount of stack for
351 * the XModem transfer buffer (1KB).
353 bool xmodem_send(struct Serial *port, KFile *fd)
355 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
357 int blocknr = 1, retries = 0, c, i;
358 bool proceed, usecrc = false;
363 ser_settimeouts(port, SER_DEFRXTIMEOUT, SER_DEFTXTIMEOUT);
364 ser_setstatus(port, 0);
366 lcd_printf(0, 2, LCD_FILL, "Wait remote host");
376 switch (c = ser_getchar(port))
384 lcd_printf(0, 2, LCD_FILL, "Tx start (CRC)");
388 lcd_printf(0, 2, LCD_FILL, "Tx start (BCC)");
390 /* Call user function to read in one block */
391 size = fd->read(fd, block_buffer, XM_BUFSIZE);
394 lcd_printf(0, 2, LCD_FILL, "Resend blk %d", blocknr);
399 /* End of transfer? */
403 /* Call user function to read in one block */
404 size = fd->read(fd, block_buffer, XM_BUFSIZE);
408 lcd_printf(0, 2, LCD_FILL, "Send blk %d", blocknr);
413 SerialError(retries);
414 if (retries <= XM_MAXRETRIES)
416 /* falling through! */
419 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
423 lcd_printf(0, 3, LCD_FILL, "Skipping garbage");
431 ser_putchar(XM_EOT, port);
435 /* Pad block with 0xFF if it's partially full */
436 memset(block_buffer + size, 0xFF, XM_BUFSIZE - size);
438 /* Send block header (STX, blocknr, ~blocknr) */
439 ser_putchar(XM_STX, port);
440 ser_putchar(blocknr & 0xFF, port);
441 ser_putchar(~blocknr & 0xFF, port);
443 /* Send block and compute its CRC/checksum */
446 for (i = 0; i < XM_BUFSIZE; i++)
448 ser_putchar(block_buffer[i], port);
449 crc = UPDCRC16(block_buffer[i], crc);
450 sum += block_buffer[i];
453 /* Send CRC/Checksum */
456 crc = UPDCRC16(0, crc);
457 crc = UPDCRC16(0, crc);
458 ser_putchar(crc >> 8, port);
459 ser_putchar(crc & 0xFF, port);
462 ser_putchar(sum, port);