Piconomic Logo www.piconomic.co.za

xmodem.h : XMODEM-CRC receive module
[/xmodem_bootloader]

Receive a file via the XMODEM-CRC protocol. More...

Typedefs

typedef void(* xmodem_on_rx_data_t )(u8_t *data, u8_t bytes_received)
 Definition for a pointer to a function that will be called once a frame has been received.

Functions

bool_t xmodem_rx_file (xmodem_on_rx_data_t on_rx_data)
 Blocking function that receives a file transferred with the XMODEM-CRC protocol.

Detailed Description

Files: xmodem.h & xmodem.c

XMODEM is a simple file transfer protocol. The file is broken up into a series of 128-byte data packets that are sent to the receiver. The packet is prefixed with a 3 byte header containing a SOH character, a "block number" from 0-255, and the "inverse" block number (255 minus the block number). A 16-bit CRC is appended to the data.

A single character response is sent by the receiver to control the flow of packets:

A callback function must be specified to handle the received data.

See also:
http://en.wikipedia.org/wiki/XMODEM

Function Documentation

bool_t xmodem_rx_file ( xmodem_on_rx_data_t  on_rx_data  ) 
Parameters:
on_rx_data Pointer to a function that will be called with each correctly received packet.
Return values:
TRUE File succesfully received
FALSE Timed out while trying to receive a file

Definition at line 213 of file xmodem.c.

References tmr_poll_has_exipred(), TMR_POLL_MS_TO_START_VAL, tmr_poll_start(), uart_poll_rx_byte(), and uart_poll_tx_byte().

00214 {
00215     u8_t   retry_count    = XMODEM_MAX_RETRIES_START;
00216     bool_t first_ack_sent = FALSE;
00217     u8_t   data;
00218 
00219     // Reset packet number
00220     xmodem_packet_number = 1;
00221 
00222     // Repeat until transfer is finished or error count is exceeded
00223     while(retry_count--)
00224     {
00225         if(!first_ack_sent)
00226         {
00227             // Send initial start character to start transfer (with CRC checking)
00228             uart_poll_tx_byte(XMODEM_C);
00229         }
00230 
00231         // Receive packet
00232         switch(xmodem_rx_packet())
00233         {
00234         case XMODEM_NO_ERROR:
00235             // Pass received data on to handler
00236             (*on_rx_data)(xmodem_rx_buffer+3,XMODEM_DATA_SIZE);
00237             // Acknowledge packet
00238             uart_poll_tx_byte(XMODEM_ACK);
00239             // Set flag to indicate that first packet has been correctly received
00240             first_ack_sent = TRUE;
00241             // Next packet
00242             xmodem_packet_number++;
00243             // Reset retry count
00244             retry_count = XMODEM_MAX_RETRIES;
00245             break;
00246 
00247         case XMODEM_RECEIVED_EOT:           
00248             // Acknowledge EOT and make sure sender has received ACK
00249             XMODEM_EOT_STATE:
00250             while(--retry_count)
00251             {
00252                 uart_poll_tx_byte(XMODEM_ACK);
00253                 tmr_poll_start(TMR_POLL_MS_TO_START_VAL(XMODEM_TIMEOUT_MS));
00254                 while(!tmr_poll_has_exipred())
00255                 {
00256                     if(uart_poll_rx_byte(&data))
00257                         if(data == XMODEM_EOT)
00258                         {
00259                             // Unfortunate, but neccesary use of the "goto" keyword
00260                             goto XMODEM_EOT_STATE;
00261                         }
00262                 }
00263                 // File successfully transferred
00264                 return TRUE;
00265             }
00266             // File not successfully transferred
00267             return FALSE;
00268 
00269         case XMODEM_ERR_DUPLICATE_PACKET_NUMBER:
00270             // Acknowledge packet
00271             uart_poll_tx_byte(XMODEM_ACK);
00272             break;
00273 
00274         case XMODEM_ERR_RECEIVED_NOTHING:
00275             // Fall through...
00276         case XMODEM_ERR_RECEIVE_TIMEOUT:
00277             // Fall through...
00278         case XMODEM_ERR_INCORRECT_HEADER:
00279             // Fall through...
00280         case XMODEM_ERR_INCORRECT_PACKET_NUMBER:
00281             // Fall through...
00282         case XMODEM_ERR_INCORRECT_CRC:
00283             // Fall through...
00284         default:
00285             if(first_ack_sent)
00286             {
00287                 // Send NAK
00288                 uart_poll_tx_byte(XMODEM_NAK);
00289             }
00290             break;
00291         }        
00292     }
00293 
00294     // File not successfully transferred
00295     return FALSE;
00296 }

Generated on Fri Aug 13 16:50:37 2010 for Piconomic Firmware Library by doxygen 1.6.3