XBee Firmware Library  1.6.0
Files | Data Structures | Typedefs | Enumerations | Enumerator | Functions | Variables
XMODEM code used by the firmware update process

Files

file  xbee_xmodem.c
 
file  xmodem.h
 
file  xmodem_crc16.c
 
file  xmodem_crc16.h
 

Data Structures

struct  xbee_xmodem_state_t
 

Typedefs

typedef int(* xbee_xmodem_read_fn) (void FAR *context, void FAR *buffer, int16_t bytes)
 Function to assign to file.read or stream.read member of an xbee_xmodem_state_t object. More...
 
typedef struct xbee_xmodem_state_t xbee_xmodem_state_t
 
typedef int(* xbee_xmodem_write_fn) (void FAR *context, const void FAR *buffer, int16_t bytes)
 Function to assign to stream.write member of an xbee_xmodem_state_t object. More...
 

Enumerations

enum  xbee_xmodem_state {
  XBEE_XMODEM_STATE_FLUSH, XBEE_XMODEM_STATE_START, XBEE_XMODEM_STATE_SEND, XBEE_XMODEM_STATE_RESEND,
  XBEE_XMODEM_STATE_SENDING, XBEE_XMODEM_STATE_WAIT_ACK, XBEE_XMODEM_STATE_EOF, XBEE_XMODEM_STATE_FINAL_ACK,
  XBEE_XMODEM_STATE_SUCCESS, XBEE_XMODEM_STATE_FAILURE
}
 

Functions

_xmodem_debug int _assemble_packet (xbee_xmodem_state_t *xbxm, uint16_t block_size)
 
_xmodem_debug uint16_t _block_size (xbee_xmodem_state_t *xbxm)
 
int _xbee_xmodem_getchar (xbee_xmodem_state_t *xbxm)
 
int _xbee_xmodem_putchar (xbee_xmodem_state_t *xbxm, uint8_t ch)
 
int _xbee_xmodem_ser_read (void FAR *context, void FAR *buffer, int16_t bytes)
 
int _xbee_xmodem_ser_write (void FAR *context, const void FAR *buffer, int16_t bytes)
 
XBEE_BEGIN_DECLS uint16_t crc16_calc (const void FAR *data, uint16_t length, uint16_t current)
 Calculate CRC-16 of a data buffer using polynomial (0x1021) without reflection. More...
 
int xbee_xmodem_set_source (xbee_xmodem_state_t *xbxm, void FAR *buffer, xbee_xmodem_read_fn read, const void FAR *context)
 Configure the data source for the Xmodem send. More...
 
int xbee_xmodem_set_stream (xbee_xmodem_state_t *xbxm, xbee_xmodem_read_fn read, xbee_xmodem_write_fn write, const void FAR *context)
 Configure the stream used to communicate with the target. More...
 
int xbee_xmodem_tx_init (xbee_xmodem_state_t *xbxm, uint16_t flags)
 Initialize state structure for use with xbee_xmodem_tx_tick() to send a file via Xmodem. More...
 
int xbee_xmodem_tx_tick (xbee_xmodem_state_t *xbxm)
 Function to drive the Xmodem send state machine. Call until it returns a non-zero result. More...
 
int xbee_xmodem_use_serport (xbee_xmodem_state_t *xbxm, xbee_serial_t *serport)
 Used for xmodem transfers over a simple serial port. More...
 

Variables

char FAR * buffer
 buffer we can use More...
 
void FAR *   context
 context for file.read() More...
 
void FAR *   context
 context for stream.read & .write More...
 
const FAR uint16_t crc16_table [256]
 
struct {
   void FAR *   context
 context for file.read() More...
 
   xbee_xmodem_read_fn   read
 source of bytes to send More...
 
file
 function and context to read source of sent data More...
 
uint16_t flags
 flags for tracking state of transfer More...
 
int offset
 offset into packet being sent More...
 
uint16_t packet_num
 current packet number; starts at 1 and low byte used in block headers More...
 
xbee_xmodem_read_fn   read
 source of bytes to send More...
 
xbee_xmodem_read_fn   read
 read response bytes from target More...
 
enum xbee_xmodem_state state
 current state of transfer More...
 
struct {
   void FAR *   context
 context for stream.read & .write More...
 
   xbee_xmodem_read_fn   read
 read response bytes from target More...
 
   xbee_xmodem_write_fn   write
 send blocks to target More...
 
stream
 functions and context to communicate with target device More...
 
uint16_t timer
 timer value used to hold low word of xbee_millisecond_timer() More...
 
uint_fast8_t tries
 

of tries left before giving up

More...
 
xbee_xmodem_write_fn   write
 send blocks to target More...
 

Detailed Description

Typedef Documentation

typedef int(* xbee_xmodem_read_fn) (void FAR *context, void FAR *buffer, int16_t bytes)

Function to assign to file.read or stream.read member of an xbee_xmodem_state_t object.

Used to read data from the Xmodem receiver (ACK/NAK bytes) or the firmware source (e.g., file or embedded array).

Parameters
[in]contexteither file.context or stream.context from the xbee_xmodem_state_t object
[in,out]bufferbuffer to store read data
[in]bytesmaximum number of bytes to write to buffer
Return values
>0number of bytes read
-ENODATAno more bytes to read
-EINVALNULL pointer or negative byte count passed to function
0no more bytes to read

Structure used to track the state of an Xmodem send.

typedef int(* xbee_xmodem_write_fn) (void FAR *context, const void FAR *buffer, int16_t bytes)

Function to assign to stream.write member of an xbee_xmodem_state_t object.

Used to write data to the Xmodem receiver (blocks of data).

Parameters
[in]contextstream.context
[in]bufferbytes to send to the receiver
[in]bytesnumber of bytes to send
Return values
>=0number of bytes sent
-EINVALNULL pointer or negative byte count passed to function
<0irrecoverable error

Enumeration Type Documentation

Values for state member of xbee_xmodem_state_t

Enumerator
XBEE_XMODEM_STATE_FLUSH 

flush receive buffer and wait

XBEE_XMODEM_STATE_START 

waiting for NAK or CRC char

XBEE_XMODEM_STATE_SEND 

start of another packet

XBEE_XMODEM_STATE_RESEND 

resend last packet

XBEE_XMODEM_STATE_SENDING 

sending bytes of packet

XBEE_XMODEM_STATE_WAIT_ACK 

waiting for ACK of current packet

XBEE_XMODEM_STATE_EOF 

reached end of file, close connection

XBEE_XMODEM_STATE_FINAL_ACK 

waiting for final ACK from receiver

XBEE_XMODEM_STATE_SUCCESS 

completed transfer was successful

XBEE_XMODEM_STATE_FAILURE 

transfer failed

Function Documentation

_xmodem_debug int _assemble_packet ( xbee_xmodem_state_t xbxm,
uint16_t  block_size 
)
_xmodem_debug uint16_t _block_size ( xbee_xmodem_state_t xbxm)
_xmodem_debug int _xbee_xmodem_getchar ( xbee_xmodem_state_t xbxm)
_xmodem_debug int _xbee_xmodem_putchar ( xbee_xmodem_state_t xbxm,
uint8_t  ch 
)
_xmodem_debug int _xbee_xmodem_ser_read ( void FAR *  context,
void FAR *  buffer,
int16_t  bytes 
)
_xmodem_debug int _xbee_xmodem_ser_write ( void FAR *  context,
const void FAR *  buffer,
int16_t  bytes 
)
uint16_t crc16_calc ( const void FAR *  data,
uint16_t  length,
uint16_t  current 
)

Calculate CRC-16 of a data buffer using polynomial (0x1021) without reflection.

Parameters
[in]datapointer to data to CRC
[in]lengthnumber of bytes to CRC
[in]currentWhen calculating a CRC-16 over a stream or a large range of bytes, it is necessary to call crc16_calc multiple times.
  • Use an initial value of 0x0000 to generate an "XMODEM" CRC.
  • Use an initial value of 0xFFFF to generate a "FALSE CCITT" CRC.
Returns
CRC-16, without reflection, for length bytes of data, using current as the starting CRC and 0x1021 as a polynomial.
See also
http://regregex.bbcmicro.net/crc-catalogue.htm#appendix.a
_xmodem_debug int xbee_xmodem_set_source ( xbee_xmodem_state_t xbxm,
void FAR *  buffer,
xbee_xmodem_read_fn  read,
const void FAR *  context 
)

Configure the data source for the Xmodem send.

Parameters
[out]xbxmstate structure to configure
[in]bufferbuffer for use by xbee_xmodem_tx_tick – must be at least 5 bytes larger than the block size passed to xbee_xmodem_tx_init()
[in]readfunction used to read bytes to send to the target
[in]contextcontext passed to read function
Return values
0successfully configured data source
-EINVALinvalid parameter passed in
_xmodem_debug int xbee_xmodem_set_stream ( xbee_xmodem_state_t xbxm,
xbee_xmodem_read_fn  read,
xbee_xmodem_write_fn  write,
const void FAR *  context 
)

Configure the stream used to communicate with the target.

Associates function pointers and a context that are used to send data to and receive data from the target (device receiving data via Xmodem).

Parameters
[out]xbxmstate structure to configure
[in]readfunction used to read bytes from the target
[in]writefunction used to send bytes to the target
[in]contextcontext passed to read and write functions
Return values
0successfully configured communication path to target
-EINVALinvalid parameter passed in
See also
xbee_ota_init, xbee_xmodem_use_serport
_xmodem_debug int xbee_xmodem_tx_init ( xbee_xmodem_state_t xbxm,
uint16_t  flags 
)

Initialize state structure for use with xbee_xmodem_tx_tick() to send a file via Xmodem.

Parameters
[out]xbxmstate structure to initialize
[in]flagsone of the following macros, indicating the block size to use for the transfer
  • XBEE_XMODEM_FLAG_NONE
  • XBEE_XMODEM_FLAG_64
  • XBEE_XMODEM_FLAG_128
  • XBEE_XMODEM_FLAG_1024
Return values
-EINVALinvalid parameter passed in
0initialized state, can pass it to xbee_xmodem_tx_tick
See also
xbee_ota_init
_xmodem_debug int xbee_xmodem_tx_tick ( xbee_xmodem_state_t xbxm)

Function to drive the Xmodem send state machine. Call until it returns a non-zero result.

Return values
-EINVALinvalid parameter passed in
-ETIMEDOUTconnection timed out waiting for data from target
<0error on send, transfer aborted
0transfer in progress, call function again
1transfer completed successfully
_xmodem_debug int xbee_xmodem_use_serport ( xbee_xmodem_state_t xbxm,
xbee_serial_t serport 
)

Used for xmodem transfers over a simple serial port.

Associates the serial port with the xbee_xmodem_state_t and sets stream.read and stream.write function pointers to helper functions that read/write a serial port.

Must be called before xbee_xmodem_tx_tick() and either before or after xbee_xmodem_tx_init().

Parameters
[in,out]xbxmstate object to configure for serial read/write
[in]serportport to use for transfer
Return values
0successfully associated serport with xbxm
-EINVALNULL parameter passed in
<0error assigning serport to xbxm
See also
xbee_ota_init, xbee_xmodem_set_stream

Variable Documentation

char FAR* buffer

buffer we can use

void FAR* context

context for file.read()

context for stream.read & .write

void FAR* context

context for file.read()

void FAR* context

context for stream.read & .write

const FAR uint16_t crc16_table[256]

Table for XMODEM and "False CCITT" CRC, calculated with standard 0x1021 polynomial.

struct { ... } file

function and context to read source of sent data

uint16_t flags

flags for tracking state of transfer

int offset

offset into packet being sent

uint16_t packet_num

current packet number; starts at 1 and low byte used in block headers

source of bytes to send

read response bytes from target

source of bytes to send

read response bytes from target

enum xbee_xmodem_state state

current state of transfer

struct { ... } stream

functions and context to communicate with target device

uint16_t timer

timer value used to hold low word of xbee_millisecond_timer()

uint_fast8_t tries

of tries left before giving up

send blocks to target

send blocks to target