Skip to content

Xmodem Loader

February 11, 2017

I have to say I am *very* tired of this thing.  It’s simple bullheadedness that keeps me poking at it.  As of close of day today I am consistently transferring blocks of any size and acknowledging them but I am pretty consistently missing the EOT at the end so my loader hangs. It’s not quite as simple as that because I replaced the failing readch() with one that would time out but it still hangs. I did have a glorious 10 minutes when everything seemed to be working but then it went to sh*t again. Anyway, the biggest change I made today was to eliminate the nointerrupts()/interrupts() statements from the avr code. The rest is just poking around trying stuff.

//ssxload super simple xmodem loader
//loads a single 32 byte block with checksum from a custom host program
//17-02-08 timeout on initial receives
//17-02-09 no timeouts, single start ack, no error checking
//17-02-09 header read moved into main
#include <nstdlib.h>
#include <olduino.h>
#include <cpu1802spd4port7.h>
#include "xloader.h"
#define blocksize 64
unsigned char target[1024];
unsigned char blkno, hdr[256],bseq[256],iseq[256],csum[256];
int readblk(unsigned char * where){//read a block
	unsigned int cnt=blocksize;
	bseq[blkno]=readch();
	iseq[blkno]=readch();
	while(cnt>0){
		*where++=readch();
		cnt--;
	}
	csum[blkno]=readch(); //read the checksum
	return 0;
}


void main(){
	int ch='?',eot=0;
	unsigned char *t=target;
	unsigned int i;
	unsigned char thishdr;
	for(i=0;i<1024;i++){target[i]=0;}
	blkno=0;
	asm(" seq\n nop\n req\n");
	putch(NAK); //send NAK to start
	thishdr=readch(); //get the header character
	while(thishdr!=EOT){
		hdr[blkno]=thishdr;
		readblk(t);
		blkno++;
		t+=blocksize;
		putch(ACK);
		asm(" seq\n nop\n req\n seq\n nop\n req\n");
		thishdr=getchOrTo(1000);
		asm(" seq\n nop\n req\n");
	}
	putch(ACK);
//and we're done - god willing

	delay(5000);//let python clear out
	printf("terminating header was %d\n",thishdr);
	dump(target,128);
	printf("number of blocks %d\n",blkno);
	printf("bb hh:bn/in cs\n");
	for (i=0;i<blkno;i++){
		printf("%cx %cx:%cx/%cx %cx\n",i,hdr[i],bseq[i],iseq[i],csum[i]);
	}
	while(1);
}
#include <nstdlib.c>
#include <olduino.c>
from __future__ import print_function
import sys
import logging
logging.basicConfig()
import serial
try:
    from cStringIO import StringIO
except:
    from StringIO import StringIO
from time import sleep
import os
if len(sys.argv)>1:
    filename=sys.argv[1]
else:
    filename="test32.txt"
fileSize=os.path.getsize(filename)
print ("File Size is",fileSize)

def xmodem_send(serial, file):
	blocksize=64
	t=0
	#	t, anim ='|/-\\'
	while 1:
	    if serial.read(1) != 'N':
		t = t + 1
		print ('.')
		if t == 3 : return False
	    else:
		break

	p = 1
	s = file.read(blocksize)
	while s:
	    s = s + '\xFF'*(blocksize - len(s))
	    chk = 0
	    for c in s:
		chk+=ord(c)
		#print (c,ord(c),chk,chk%256)
	    while 1:
		serial.write('S') #SOH)
		serial.write(chr(p))
		serial.write(chr(255 - p))
		serial.write(s)
		serial.write(chr(chk%256))
		serial.flush()
		print ('checksum is ',format(chk%256, '02X'),end=' ')
		answer = serial.read(1)
		if  answer == 'N': continue
		if  answer == 'K': break
		return False
	    s = file.read(blocksize)
	    p = (p + 1)%256
	    print ('.')
	serial.write('T')
	serial.flush()
	return True

#Main program starts here - define the serial port, set RTS off, then open it
#open the file to be loaded
stream = open(filename,'rb')

port = serial.Serial(parity=serial.PARITY_NONE,
                     bytesize=serial.EIGHTBITS,
                     stopbits=serial.STOPBITS_ONE,timeout=10,xonxoff=0,
                     rtscts=0,dsrdtr=0,baudrate=57600)
port.rts=False
port.port='COM3'
port.open()
port.rts=True
sleep(0.001)
port.rts=False
port.flushInput()
#transfer the file
result=xmodem_send(port, stream)

stream.close()
port.close()

if result:
    print ("\ntransfer successful")
    sleep(.25)
else:
    print ("\ntransfer unsuccessful")
    #x=raw_input("press enter to continue...");

I always want the last word so that wordpress doesn’t eat my code!
I always want the last word so that wordpress doesn’t eat my code!

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: