4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2004, 2005, 2006, 2007 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 1999, 2001 Bernardo Innocenti <bernie@develer.com>
33 * \brief X-Modem serial transmission protocol (implementation)
35 * Supports the CRC-16 and 1K-blocks variants of the standard.
36 * \see ymodem.txt for the protocol description.
38 * \todo Break xmodem_send() and xmodem_recv() in smaller functions.
40 * \todo Maybe convert drv/ser.c to the KFile interface for symmetry and
44 * \author Bernardo Innocenti <bernie@develer.com>
45 * \author Francesco Sacchi <batt@develer.com>
51 #include <appconfig.h>
52 #include <string.h> /* for memset() */
54 #include <algos/crc.h>
55 #include <cfg/debug.h>
61 * \name Protocol control codes
64 #define XM_SOH 0x01 /**< Start Of Header (128-byte block) */
65 #define XM_STX 0x02 /**< Start Of Header (1024-byte block) */
66 #define XM_EOT 0x04 /**< End Of Transmission */
67 #define XM_ACK 0x06 /**< Acknowledge block */
68 #define XM_NAK 0x15 /**< Negative Acknowledge */
69 #define XM_C 0x43 /**< Request CRC-16 transmission */
70 #define XM_CAN 0x18 /**< CANcel transmission */
73 #define XM_MAXRETRIES 15 /**< Max retries before giving up */
74 #define XM_MAXCRCRETRIES 7 /**< Max retries before switching to BCC */
76 #if CONFIG_XMODEM_1KCRC == 1
77 #define XM_BUFSIZE 1024 /**< 1024 bytes of block buffer */
79 #define XM_BUFSIZE 128 /**< 128 bytes of block buffer */
83 #if CONFIG_XMODEM_RECV
85 * \brief Receive a file using the XModem protocol.
87 * \param port Serial port to use for transfer
88 * \param fd Destination file
90 * \note This function allocates a large amount of stack (\see XM_BUFSIZE).
92 bool xmodem_recv(struct Serial *port, KFile *fd)
94 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
96 int blocknr = 0, last_block_done = 0, retries = 0;
104 XMODEM_PROGRESS("Starting Transfer...\n");
106 ser_setstatus(port, 0);
108 /* Send initial NAK to start transmission */
111 if (XMODEM_CHECK_ABORT)
113 ser_putchar(XM_CAN, port);
114 ser_putchar(XM_CAN, port);
115 XMODEM_PROGRESS("Transfer aborted\n");
120 * Discard incoming input until a timeout occurs, then send
121 * a NAK to the transmitter.
127 if (ser_getstatus(port))
128 XMODEM_PROGRESS("Retries %d\n", retries);
130 ser_resync(port, 200);
133 if (retries >= XM_MAXRETRIES)
135 ser_putchar(XM_CAN, port);
136 ser_putchar(XM_CAN, port);
137 XMODEM_PROGRESS("Transfer aborted\n");
141 /* Transmission start? */
144 if (retries < XM_MAXCRCRETRIES)
146 XMODEM_PROGRESS("Request Tx (CRC)\n");
147 ser_putchar(XM_C, port);
151 /* Give up with CRC and fall back to checksum */
153 XMODEM_PROGRESS("Request Tx (BCC)\n");
154 ser_putchar(XM_NAK, port);
158 ser_putchar(XM_NAK, port);
161 switch (ser_getchar(port))
163 #if XM_BUFSIZE >= 1024
164 case XM_STX: /* Start of header (1024-byte block) */
169 case XM_SOH: /* Start of header (128-byte block) */
171 /* Needed to avoid warning if XM_BUFSIZE < 1024 */
174 /* Get block number */
175 c = ser_getchar(port);
177 /* Check complemented block number */
178 if ((~c & 0xff) != ser_getchar(port))
180 XMODEM_PROGRESS("Bad blk (%d)\n", c);
185 /* Determine which block is being sent */
186 if (c == (blocknr & 0xff))
187 /* Last block repeated */
188 XMODEM_PROGRESS("Repeat blk %d\n", blocknr);
189 else if (c == ((blocknr + 1) & 0xff))
191 XMODEM_PROGRESS("Recv blk %d\n", ++blocknr);
195 XMODEM_PROGRESS("Sync lost (%d/%d)\n", c, blocknr);
200 buf = block_buffer; /* Reset pointer to start of buffer */
203 for (i = 0; i < blocksize; i++)
205 if ((c = ser_getchar(port)) == EOF)
211 /* Store in buffer */
214 /* Calculate block checksum or CRC */
216 crc = UPDCRC16(c, crc);
224 /* Get the checksum byte or the CRC-16 MSB */
225 if ((c = ser_getchar(port)) == EOF)
233 crc = UPDCRC16(c, crc);
236 if ((c = ser_getchar(port)) == EOF)
242 crc = UPDCRC16(c, crc);
246 XMODEM_PROGRESS("Bad CRC: %04x\n", crc);
251 /* Compare the checksum */
252 else if (c != checksum)
254 XMODEM_PROGRESS("Bad sum: %04x/%04x\n", checksum, c);
260 * Avoid flushing the same block twice.
261 * This could happen when the sender does not receive our
262 * acknowledge and resends the same block.
264 if (last_block_done < blocknr)
266 /* Call user function to flush the buffer */
267 if (fd->write(fd, block_buffer, blocksize))
269 /* Acknowledge block and clear error counter */
270 ser_putchar(XM_ACK, port);
272 last_block_done = blocknr;
276 /* User callback failed: abort transfer immediately */
277 retries = XM_MAXRETRIES;
283 case XM_EOT: /* End of transmission */
284 ser_putchar(XM_ACK, port);
285 XMODEM_PROGRESS("Transfer completed\n");
288 case EOF: /* Timeout or serial error */
293 XMODEM_PROGRESS("Skipping garbage\n");
302 #if CONFIG_XMODEM_SEND
304 * \brief Transmit some data using the XModem protocol.
306 * \param port Serial port to use for transfer
307 * \param fd Source file
309 * \note This function allocates a large amount of stack for
310 * the XModem transfer buffer (\see XM_BUFSIZE).
312 bool xmodem_send(struct Serial *port, KFile *fd)
314 char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
316 int blocknr = 1, retries = 0, c, i;
317 bool proceed, usecrc = false;
322 * Reading a block can be very slow, so we read the first block early
323 * to avoid receiving double XM_C char.
324 * This could happen if we check for XM_C and then read the block, giving
325 * the receiving device time to send another XM_C char misinterpretating
328 size = fd->read(fd, block_buffer, XM_BUFSIZE);
330 ser_setstatus(port, 0);
332 XMODEM_PROGRESS("Wait remote host\n");
339 if (XMODEM_CHECK_ABORT)
342 switch (c = ser_getchar(port))
345 XMODEM_PROGRESS("Resend blk %d\n", blocknr);
352 XMODEM_PROGRESS("Tx start (CRC)\n");
356 XMODEM_PROGRESS("Tx start (BCC)\n");
362 /* End of transfer? */
366 /* Call user function to read in one block */
367 size = fd->read(fd, block_buffer, XM_BUFSIZE);
368 XMODEM_PROGRESS("Send blk %d\n", blocknr);
375 ser_setstatus(port, 0);
377 XMODEM_PROGRESS("Retries %d\n", retries);
378 if (retries <= XM_MAXRETRIES)
380 /* falling through! */
383 XMODEM_PROGRESS("Transfer aborted\n");
387 XMODEM_PROGRESS("Skipping garbage\n");
395 ser_putchar(XM_EOT, port);
399 /* Pad block with 0xFF if it's partially full */
400 memset(block_buffer + size, 0xFF, XM_BUFSIZE - size);
402 /* Send block header (STX, blocknr, ~blocknr) */
403 #if XM_BUFSIZE == 128
404 ser_putchar(XM_SOH, port);
406 ser_putchar(XM_STX, port);
408 ser_putchar(blocknr & 0xFF, port);
409 ser_putchar(~blocknr & 0xFF, port);
411 /* Send block and compute its CRC/checksum */
414 for (i = 0; i < XM_BUFSIZE; i++)
416 ser_putchar(block_buffer[i], port);
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, port);
427 ser_putchar(crc & 0xFF, port);
430 ser_putchar(sum, port);