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.8 2004/08/25 14:12:09 rasky
32 *#* Aggiornato il comment block dei log RCS
34 *#* Revision 1.7 2004/08/15 06:30:06 bernie
35 *#* Make the buffer a local variable, as documented.
37 *#* Revision 1.6 2004/08/15 05:31:46 bernie
38 *#* Add an #error to spread some FUD about the quality of this module;
39 *#* Add a few TODOs from Rasky's review;
40 *#* Update to the new drv/ser.c API;
41 *#* Move FlushSerial() to drv/ser.c and generalize.
43 *#* Revision 1.5 2004/08/12 23:46:21 bernie
44 *#* Remove extra indentation level in switch statements.
46 *#* Revision 1.4 2004/08/12 23:35:50 bernie
47 *#* Replace a handmade loop with memset().
49 *#* Revision 1.3 2004/08/12 23:34:36 bernie
50 *#* Replace if/else with continue to reduce indentation level.
52 *#* Revision 1.2 2004/08/12 23:24:07 bernie
53 *#* Rename UPDCRC() to UPDCRC16().
55 *#* Revision 1.1 2004/08/11 19:54:22 bernie
56 *#* Import XModem protocol into DevLib.
64 #include <drv/buzzer.h>
65 #include <mware/crc.h>
66 #include <mware/kfile.h>
68 #include <string.h> /* for memset() */
72 * \name Protocol control codes
75 #define XM_SOH 0x01 /*!< Start Of Header (128-byte block) */
76 #define XM_STX 0x02 /*!< Start Of Header (1024-byte block) */
77 #define XM_EOT 0x04 /*!< End Of Transmission */
78 #define XM_ACK 0x06 /*!< Acknowledge block */
79 #define XM_NAK 0x15 /*!< Negative Acknowledge */
80 #define XM_C 0x43 /*!< Request CRC-16 transmission */
81 #define XM_CAN 0x18 /*!< CANcel transmission */
84 #define XM_MAXRETRIES 15 /*!< Max retries before giving up */
85 #define XM_MAXCRCRETRIES 7 /*!< Max retries before switching to BCC */
86 #define XM_BUFSIZE 1024 /*!< Size of block buffer */
89 #if (ARCH & ARCH_BOOT)
91 #if (ARCH & ARCH_SLIM)
92 #define CHECK_ABORT KEYPRESSED_STOP
93 #elif (ARCH & ARCH_SARF)
94 #define CHECK_ABORT KEYPRESSED_ESC
98 #if (ARCH & ARCH_SLIM)
99 #define CHECK_ABORT (kbd_getchar() == K_STOP)
100 #elif (ARCH & ARCH_SARF)
101 #define CHECK_ABORT (kbd_getchar() == K_ESC)
103 #endif /* ARCH_BOOT */
107 * Decode serial driver errors and print them on the display.
109 static void print_serial_error(struct Serial *port, int retries)
111 serstatus_t err, status;
113 /* Get serial error code and reset it */
114 status = ser_getstatus(port);
115 ser_setstatus(port, 0);
117 /* Mostra tutti gli errori in sequenza */
118 for (err = 0; status != 0; status >>= 1, err++)
120 /* Se il bit dell'errore e' settato */
123 lcd_printf(0, 3, LCD_FILL, "%s %d", serial_errors[err], retries);
132 * \brief Receive a file using the XModem protocol.
134 * \param port Serial port to use for transfer
135 * \param fd Destination file
137 * \note This function allocates a large amount of stack (>1KB).
139 bool xmodem_recv(struct Serial *port, KFile *fd)
141 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
143 int blocknr = 0, last_block_done = 0, retries = 0;
151 lcd_printf(0, 2, LCD_FILL, "Starting Transfer...");
154 ser_settimeouts(port, SER_DEFRXTIMEOUT, SER_DEFTXTIMEOUT);
155 ser_setstatus(port, 0);
157 /* Send initial NAK to start transmission */
162 ser_putchar(XM_CAN, port);
163 ser_putchar(XM_CAN, port);
164 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
169 * Discard incoming input until a timeout occurs, then send
170 * a NAK to the transmitter.
177 SerialError(retries);
179 ser_resync(port, 200);
182 if (retries >= XM_MAXRETRIES)
184 ser_putchar(XM_CAN, port);
185 ser_putchar(XM_CAN, port);
186 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
190 /* Transmission start? */
193 if (retries < XM_MAXCRCRETRIES)
195 lcd_printf(0, 2, LCD_FILL, "Request Tx (CRC)");
196 ser_putchar(XM_C, port);
200 /* Give up with CRC and fall back to checksum */
202 lcd_printf(0, 2, LCD_FILL, "Request Tx (BCC)");
203 ser_putchar(XM_NAK, port);
207 ser_putchar(XM_NAK, port);
210 switch (ser_getchar(port))
212 case XM_STX: /* Start of header (1024-byte block) */
216 case XM_SOH: /* Start of header (128-byte block) */
220 /* Get block number */
221 c = ser_getchar(port);
223 /* Check complemented block number */
224 if ((~c & 0xff) != ser_getchar(port))
226 lcd_printf(0, 3, LCD_FILL, "Bad blk (%d)", c);
231 /* Determine which block is being sent */
232 if (c == (blocknr & 0xff))
233 /* Last block repeated */
234 lcd_printf(0, 2, LCD_FILL, "Repeat blk %d", blocknr);
235 else if (c == ((blocknr + 1) & 0xff))
237 lcd_printf(0, 2, LCD_FILL, "Recv blk %d", ++blocknr);
241 lcd_printf(0, 3, LCD_FILL, "Sync lost (%d/%d)", c, blocknr);
246 buf = block_buffer; /* Reset pointer to start of buffer */
249 for (i = 0; i < blocksize; i++)
251 if ((c = ser_getchar(port)) == EOF)
257 /* Store in buffer */
260 /* Calculate block checksum or CRC */
262 crc = UPDCRC16(c, crc);
270 /* Get the checksum byte or the CRC-16 MSB */
271 if ((c = ser_getchar(port)) == EOF)
279 crc = UPDCRC16(c, crc);
282 if ((c = ser_getchar(port)) == EOF)
288 crc = UPDCRC16(c, crc);
292 lcd_printf(0, 3, LCD_FILL, "Bad CRC: %04x", crc);
297 /* Compare the checksum */
298 else if (c != checksum)
300 lcd_printf(0, 3, LCD_FILL, "Bad sum: %04x/%04x", checksum, c);
306 * Avoid flushing the same block twice.
307 * This could happen when the sender does not receive our
308 * acknowledge and resends the same block.
310 if (last_block_done < blocknr)
312 /* Call user function to flush the buffer */
313 if (fd->write(fd, block_buffer, blocksize))
315 /* Acknowledge block and clear error counter */
316 ser_putchar(XM_ACK, port);
318 last_block_done = blocknr;
322 /* User callback failed: abort transfer immediately */
323 retries = XM_MAXRETRIES;
329 case XM_EOT: /* End of transmission */
330 ser_putchar(XM_ACK, port);
331 lcd_printf(0, 2, LCD_FILL, "Transfer completed");
334 case EOF: /* Timeout or serial error */
339 lcd_printf(0, 3, LCD_FILL, "Skipping garbage");
348 * \brief Transmit a file using the XModem protocol.
350 * \param port Serial port to use for transfer
351 * \param fd Source file
353 * \note This function allocates a large amount of stack for
354 * the XModem transfer buffer (1KB).
356 bool xmodem_send(struct Serial *port, KFile *fd)
358 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
360 int blocknr = 1, retries = 0, c, i;
361 bool proceed, usecrc = false;
366 ser_settimeouts(port, SER_DEFRXTIMEOUT, SER_DEFTXTIMEOUT);
367 ser_setstatus(port, 0);
369 lcd_printf(0, 2, LCD_FILL, "Wait remote host");
379 switch (c = ser_getchar(port))
387 lcd_printf(0, 2, LCD_FILL, "Tx start (CRC)");
391 lcd_printf(0, 2, LCD_FILL, "Tx start (BCC)");
393 /* Call user function to read in one block */
394 size = fd->read(fd, block_buffer, XM_BUFSIZE);
397 lcd_printf(0, 2, LCD_FILL, "Resend blk %d", blocknr);
402 /* End of transfer? */
406 /* Call user function to read in one block */
407 size = fd->read(fd, block_buffer, XM_BUFSIZE);
411 lcd_printf(0, 2, LCD_FILL, "Send blk %d", blocknr);
416 SerialError(retries);
417 if (retries <= XM_MAXRETRIES)
419 /* falling through! */
422 lcd_printf(0, 2, LCD_FILL, "Transfer aborted");
426 lcd_printf(0, 3, LCD_FILL, "Skipping garbage");
434 ser_putchar(XM_EOT, port);
438 /* Pad block with 0xFF if it's partially full */
439 memset(block_buffer + size, 0xFF, XM_BUFSIZE - size);
441 /* Send block header (STX, blocknr, ~blocknr) */
442 ser_putchar(XM_STX, port);
443 ser_putchar(blocknr & 0xFF, port);
444 ser_putchar(~blocknr & 0xFF, port);
446 /* Send block and compute its CRC/checksum */
449 for (i = 0; i < XM_BUFSIZE; i++)
451 ser_putchar(block_buffer[i], port);
452 crc = UPDCRC16(block_buffer[i], crc);
453 sum += block_buffer[i];
456 /* Send CRC/Checksum */
459 crc = UPDCRC16(0, crc);
460 crc = UPDCRC16(0, crc);
461 ser_putchar(crc >> 8, port);
462 ser_putchar(crc & 0xFF, port);
465 ser_putchar(sum, port);