2019-11-01 23:39:34 +00:00
|
|
|
#!/usr/bin/python3
|
2017-11-30 22:03:01 +00:00
|
|
|
|
|
|
|
## This quick and dirty tool converts a textfile of frequencies and
|
|
|
|
## names into a GoodWatch codeplug, for storage in information flash
|
|
|
|
## at 0x1800. Use cc430-bsl.py to write the codeplug to flash.
|
|
|
|
|
2019-11-01 23:39:34 +00:00
|
|
|
import sys, argparse, codecs;
|
2017-11-30 22:03:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def freqbytes(freq):
|
|
|
|
"""Converts a frequency to three FREQ bytes. Assumes 26MHz xtal."""
|
|
|
|
freqMult = (0x10000 / 1000000.0) / 26.0;
|
|
|
|
num=int(freq*1e6*freqMult);
|
|
|
|
FREQ2=(num>>16) & 0xFF;
|
|
|
|
FREQ1=(num>> 8) & 0xFF;
|
|
|
|
FREQ0= num & 0xFF
|
|
|
|
return FREQ2, FREQ1, FREQ0;
|
|
|
|
|
2018-09-03 23:06:33 +00:00
|
|
|
def ihexcrc(hexline):
|
|
|
|
"""Calculates an Intel Hex CRC of a line."""
|
|
|
|
#%print("Calculating CRC of %s."%hexline);
|
|
|
|
CRC=0;
|
2019-11-01 23:39:34 +00:00
|
|
|
for b in bytes(hexline):
|
|
|
|
CRC=CRC+int(b);
|
2018-09-03 23:06:33 +00:00
|
|
|
CRC=CRC^0xFF;
|
|
|
|
CRC=CRC+1;
|
|
|
|
return CRC&0xFF;
|
|
|
|
|
2017-11-30 22:29:38 +00:00
|
|
|
codeplugadr=0x1800;
|
2017-11-30 22:03:01 +00:00
|
|
|
def handleline(line):
|
|
|
|
"""Handles one line of the file."""
|
2017-11-30 22:29:38 +00:00
|
|
|
global codeplugadr;
|
2017-11-30 22:03:01 +00:00
|
|
|
if len(line)==0: #Empty line.
|
|
|
|
return;
|
|
|
|
elif line[0]=='#': #Comment
|
|
|
|
return;
|
|
|
|
else:
|
|
|
|
# This is a real line. First word ought to be the frequency,
|
|
|
|
# and the second word ought to be the name (8 characters or
|
|
|
|
# less). Further words would be flags, but we ignore them for
|
|
|
|
# now.
|
2017-11-30 22:29:38 +00:00
|
|
|
LEN=12;
|
2017-11-30 22:03:01 +00:00
|
|
|
words=line.split();
|
|
|
|
freq=float(words[0]);
|
|
|
|
name=words[1];
|
2019-11-01 23:39:34 +00:00
|
|
|
namehex=codecs.encode(bytes(name, 'utf-8'), 'hex').decode('utf-8');
|
2017-11-30 22:03:01 +00:00
|
|
|
assert(len(name)<=8);
|
2017-11-30 22:29:38 +00:00
|
|
|
while len(namehex)<16:
|
|
|
|
namehex='20'+namehex;
|
|
|
|
FLAGS=0;
|
2017-11-30 22:03:01 +00:00
|
|
|
(FREQ2, FREQ1, FREQ0) = freqbytes(freq);
|
2018-09-03 23:06:33 +00:00
|
|
|
|
2017-12-03 03:49:33 +00:00
|
|
|
|
2018-09-03 23:06:33 +00:00
|
|
|
line="%02x%04x00%02x%02x%02x%02x%s" %(
|
|
|
|
LEN, codeplugadr, FLAGS, FREQ2,FREQ1,FREQ0, namehex
|
2017-12-03 03:49:33 +00:00
|
|
|
);
|
2019-11-01 23:39:34 +00:00
|
|
|
CRC=ihexcrc(codecs.decode(bytes(line, 'utf-8'), 'hex'));
|
2018-09-03 23:06:33 +00:00
|
|
|
|
2017-12-03 03:49:33 +00:00
|
|
|
codeplugadr=codeplugadr+12;
|
2018-09-03 23:06:33 +00:00
|
|
|
return ":%s%02x\n" % (line, CRC);
|
2017-11-30 22:03:01 +00:00
|
|
|
|
|
|
|
def convertcodeplug(infile, outfile):
|
|
|
|
"""Converts a codeplug textfile into an intel hex file for flashing."""
|
|
|
|
i=open(infile,'r');
|
2017-11-30 22:50:42 +00:00
|
|
|
o=open(outfile,'w');
|
2017-11-30 22:03:01 +00:00
|
|
|
for line in i:
|
2017-11-30 22:50:42 +00:00
|
|
|
ihl=handleline(line.strip());
|
|
|
|
if(ihl!=None):
|
|
|
|
o.write(ihl);
|
|
|
|
o.write(":00000001FF\n");
|
|
|
|
i.close();
|
|
|
|
o.close();
|
2017-11-30 22:03:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__=='__main__':
|
|
|
|
parser = argparse.ArgumentParser(description='GoodWatch Codeplug Compiler')
|
|
|
|
parser.add_argument('-i','--input', help='Input Textfile');
|
|
|
|
parser.add_argument('-o','--output', help='Output Intel Hex File');
|
|
|
|
|
|
|
|
args=parser.parse_args();
|
|
|
|
|
|
|
|
if args.input!=None and args.output!=None:
|
|
|
|
convertcodeplug(args.input, args.output);
|