1
1
#!/usr/bin/env python3
2
- from ISPProgrammer import LPC80x , LPC84x , LPC175_6x , MockUart , UartDevice
2
+ from ISPProgrammer import MockUart , UartDevice , NXPChip
3
3
import click
4
+ from timeout_decorator import TimeoutError
4
5
5
- INSTALLED_FAMILIES = (
6
- LPC80x ,
7
- LPC84x ,
8
- LPC175_6x
9
- )
10
6
BAUDRATES = (
11
7
9600 ,
12
8
19200 ,
@@ -16,65 +12,93 @@ BAUDRATES = (
16
12
230400 ,
17
13
460800
18
14
)
19
- DEFAULT_BAUD = BAUDRATES [3 ]# breaks with 115200
20
-
21
- CHIPS = []
22
- for family in INSTALLED_FAMILIES :
23
- CHIPS .extend (family .Family )
24
-
25
15
@click .group ()
26
- def gr1 ():
27
- pass
16
+ @click .option ('--device' , '-d' , default = '/dev/ttyUSB0' , help = 'Serial device' )
17
+ @click .option ('--baud' , '-b' , type = int , default = 9600 , help = 'Baudrate' )
18
+ @click .option ('--crystal_frequency' , '-c' , type = int , default = 12000 , help = "Crystal frequency of chip in khz" )
19
+ @click .option ('--config_file' , '-f' , default = '/etc/lpctools_parts.def' , help = 'Parts definition file' )
20
+ @click .pass_context
21
+ def gr1 (ctx , ** kwargs ):
22
+ ctx .ensure_object (dict )
23
+ ctx .obj .update (kwargs )
28
24
25
+ def ReadChipFile (fname ):
26
+ data = []
27
+ with open (fname , 'r' ) as f :
28
+ for line in f :
29
+ if line .strip ()[0 ] == '#' :
30
+ continue
31
+ elif len (line .strip ()) == 0 :
32
+ continue
33
+ data .append (line .split ("," ))
34
+ return data
29
35
30
- def SetupChip (chipname , device = "/dev/ttyUSB0" ):
31
- chip = None
32
- for ChipFamily in INSTALLED_FAMILIES :
33
- if chipname in ChipFamily .Family :
34
- #iodevice = MockUart(device, baudrate = ChipFamily.MAXBAUDRATE)
35
- iodevice = UartDevice (device , baudrate = ChipFamily .MAXBAUDRATE )
36
- chip = ChipFamily (iodevice )
37
- break
38
- if (chip is None ):
39
- raise UserWarning ("Chip %s unknown" % chipname )
36
+ def GetPartDescriptor (fname , partid ):
37
+ entries = ReadChipFile (fname )
38
+ for entry in entries :
39
+ if partid == int (entry [0 ], 0 ):
40
+ return entry
40
41
42
+ def SetupChip (baudrate , device , crystal_frequency , chip_file ):
43
+ #print(baudrate, device, crystal_frequency, chip_file)
44
+ iodevice = UartDevice (device , baudrate = baudrate )
45
+ chip = NXPChip (iodevice )
41
46
chip .InitConnection ()
42
- print ("Initiated %s" % chipname )
43
- chip .ChangeBaudRate (chip .MAXBAUDRATE )
44
- print ("Setup chip Baudrate set to:" , chip .MAXBAUDRATE )
47
+ while True :
48
+ try :
49
+ part_id = chip .ReadPartID ()
50
+ break
51
+ except TimeoutError :
52
+ pass
53
+ descriptor = GetPartDescriptor (chip_file , part_id )
54
+ if (descriptor is None ):
55
+ raise UserWarning ("Warning chip %s not found in file %s" % (hex (part_id ), chip_file ))
56
+
57
+ print (part_id , descriptor [1 ])
58
+ chip .CrystalFrequency = 12000 #khz == 30MHz
59
+ chip .SectorCount = int (descriptor [4 ])
60
+ chip .RAMSize = int (descriptor [7 ], 0 )
61
+
62
+ ramstart = int (descriptor [6 ], 0 )
63
+ chip .RAMRange = (ramstart , ramstart + chip .RAMSize - 1 )
64
+
65
+ flashstart = int (descriptor [2 ], 0 )
66
+ flashsize = int (descriptor [3 ], 0 )
67
+ chip .FlashRange = (flashstart , flashstart + flashsize - 1 )
68
+
69
+ ram_buffer_offset = int (descriptor [8 ], 0 )
70
+ chip .RAMStartWrite = ramstart + ram_buffer_offset
71
+ chip .kCheckSumLocation = 7 #0x0000001c
45
72
73
+ assert (chip .RAMRange [1 ]- chip .RAMRange [0 ] == chip .RAMSize - 1 )
46
74
return chip
47
75
48
- @click .option ('--device' , '-d' , type = str , default = "/dev/ttyUSB0" , help = 'Serial Device, defaults to /dev/ttyUSB0' )
49
- @click .option ('--chipname' , '-c' , type = click .Choice (CHIPS ), required = True , help = 'Chip Type' )
50
76
@gr1 .command ()
51
- def QueryChip (device , chipname ):
52
- chip = SetupChip (chipname , device )
77
+ @click .pass_context
78
+ def QueryChip (ctx ):
79
+ chip = SetupChip (ctx .obj ['baud' ], ctx .obj ['device' ], ctx .obj ['crystal_frequency' ], ctx .obj ['config_file' ])
53
80
54
- @click .option ('--device' , '-d' , type = str , default = "/dev/ttyUSB0" , help = 'Serial Device, defaults to /dev/ttyUSB0' )
55
- @click .option ('--chipname' , '-c' , type = click .Choice (CHIPS ), required = True , help = 'Chip Type' )
56
81
@gr1 .command ()
57
- def MassErase (device , chipname ):
58
- chip = SetupChip (chipname , device )
82
+ @click .pass_context
83
+ def MassErase (ctx ):
84
+ chip = SetupChip (ctx .obj ['baud' ], ctx .obj ['device' ], ctx .obj ['crystal_frequency' ], ctx .obj ['config_file' ])
59
85
chip .MassErase ()
60
86
print ("Mass Erase Successful" )
61
87
62
- @click .option ('--device' , '-d' , type = str , default = "/dev/ttyUSB0" , help = 'Serial Device, defaults to /dev/ttyUSB0' )
63
- @click .option ('--chipname' , '-c' , type = click .Choice (CHIPS ), required = True , help = 'Chip Type' )
64
88
@click .option ('--imagein' , type = str , required = True , help = 'Location of hex file to program' )
65
89
@gr1 .command ()
66
- def WriteImage (device , chipname , imagein ):
67
- chip = SetupChip (chipname , device )
90
+ @click .pass_context
91
+ def WriteImage (ctx , imagein ):
92
+ chip = SetupChip (ctx .obj ['baud' ], ctx .obj ['device' ], ctx .obj ['crystal_frequency' ], ctx .obj ['config_file' ])
68
93
chip .WriteImage (imagein )
69
94
#chip.Go(0, ThumbMode=False)
70
95
chip .Go (0 , ThumbMode = True )
71
96
72
- @click .option ('--device' , '-d' , type = str , default = "/dev/ttyUSB0" , help = 'Serial Device, defaults to /dev/ttyUSB0' )
73
- @click .option ('--chipname' , '-c' , type = click .Choice (CHIPS ), required = True , help = 'Chip Type' )
74
97
@click .option ('--imageout' , type = str , required = True , help = 'Name of hex file output' )
75
98
@gr1 .command ()
76
- def ReadImage (device , chipname , imageout ):
77
- chip = SetupChip (chipname , device )
99
+ @click .pass_context
100
+ def ReadImage (ctx , imageout ):
101
+ chip = SetupChip (ctx .obj ['baud' ], ctx .obj ['device' ], ctx .obj ['crystal_frequency' ], ctx .obj ['config_file' ])
78
102
chip .ReadImage (imageout )
79
103
80
104
if __name__ == "__main__" :
0 commit comments