xmodem.h By Zhi-Hern Loh

A 2nd try

//Xmodem protocol                  

//Xmodem codes
#define SOH 0x01 //start of header(0x02 for 1024 byte packet, 0x01 for 128)
#define EOT 0x04 //end of transmission
#define ACK 0x06 //ack
#define NACK 0x15 //not acknowledge
#define CAN 0x18 //cancel
//memory address of buffer
//this must be above the h/w stack area
//use the info in Codevision to determine the value
#define BUF_START_ADD 0x1200 //0x2000 before
//size of the buffer in bytes
#define BUF_BYTE_SIZE 	0xe000//0xb000 //increase later, now testing

#define PACKET_SIZE 128U//1024U//1024//size of data in a packet

#define DISABLE_XRAM MCUCR &= 0x7F;//SRE off
#define ENABLE_XRAM MCUCR |= 0x80; //SRE on

//before using the following, make sure set TCNT1=0
//#define SEC_COUNTER_ON   TCCR1B = (1<<3)|(0x05)//WGM12=1,CK=1024
#define SEC_COUNTER_ON 	 TCCR1B = 0x05;//CK=1024
#define SEC_TIMER_ISR_ON TIMSK = TIMSK | (1<<4);
#define SEC_TIMER_ISR_OFF TIMSK = TIMSK & 0xef;//bit 4 off

Xmodem protocol documentation
transfer sequence
 1.SOH byte
 2.packet number (1 byte)
 3.1's complement of packet number (1 byte)
 4.the packet (128 bytes)
 5.the checksum (1 byte)     

Reciever must
1.check packet number. 1st block has packet number 1.
if the packet number is wrong, send a CAN byte to cancel

2. add the packet number and the 1's complement to get
0xff, if not, cancel transfer(send CAN).

3.do CRC check, send NAK if failed

4.if nothing fails, add the recieved packet to the buffer

#ifndef begin
#define begin {
#define end }

void Init_Xmodem(void);//intialize Xmodem file transfer
void X_Receive(void);//receiver for Xmodem
void X_Start(void);//start Xmodem transmission
void X_Start_Instant(void);//no wait, instant start of transfer
void X_Write_Packet(void);//write the current packet to the data buffer
void X_Show_Buffer(void);//displays the contents of the data buffer 
void X_Activate_Purge(void);//starts purging buffer for time defined by OCR1A
void X_Poll(void);//polling routine for Xmodem file transfer and mp3 playback

//Global variables
unsigned char Packet_Number;//stores the # of the current 
//packet being recieved. mod 256
unsigned int Byte_Number;//Byte number should be 1 when
//Xmodem transfer of a new packet has just started, and 
//the last value should be 133.
unsigned char EOT_Number;//set to 1 when 1st EOT is sent
//and 2 when 2nd EOT is sent
unsigned int CRCword;//CRC sent with the packet
unsigned int CRC;//calculated crc
unsigned char X_Transmit_State;//State Var for trans.
//State names for X_Transmit_State
#define IDLE 		0
#define SEND_C 		1
#define SEND_ACK	2
#define SEND_NACK	3
#define SEND_CAN	4

unsigned char retry;//# of times 'C' was sent
unsigned char purge;//state variable for receive
//if purge = 1, we want to dump the remainder of the
//current packet, and then send NACK to restart
//the current packet
unsigned char *dataheadptr;
//buffer pointer, points to 1st free address in 
//memory to store the current downloaded packet
//we are using a circular buffer
unsigned char *datatailptr;
//points to the last valid address in the buffer
unsigned char *mp3ptr;
//points to the next data byte to send to decoder

unsigned char buf[PACKET_SIZE];//stores the current packet being recieved

Home | Design | Results | A 2nd try | Schematics | Code | Files | Links | Contact

For problems or questions regarding this web contact Zhi-Hern Loh.
Last updated: 05/02/02.