@@ -20,14 +20,14 @@ lundin@mlu.mine.nu
20
20
#include <fcntl.h>
21
21
22
22
#include "hexfile.h"
23
+ #include "serialport.h"
23
24
24
25
#define FLASHBASE 0x08000000
25
26
#define RAMBASE 0x20000000
26
27
27
28
int baudrate = 38400 ;
28
- char * port = "/dev/ttyUSB0" ;
29
- int portfd = -1 ;
30
- int filefd = -1 ;
29
+ char * portname = "/dev/ttyUSB0" ;
30
+ serport_t port = SERIAL_NOT_OPEN ;
31
31
int verbose = 0 ;
32
32
33
33
struct
@@ -45,23 +45,23 @@ target =
45
45
46
46
void opentargetport ()
47
47
{
48
- if (portfd < 0 )
48
+ if (port != SERIAL_NOT_OPEN )
49
49
{
50
- portfd = open ( port ,O_NONBLOCK | O_RDWR );
51
- if (portfd < 0 )
50
+ port = openSerialPort ( portname ,O_NONBLOCK | O_RDWR );
51
+ if (port == SERIAL_NOT_OPEN )
52
52
{
53
- fprintf (stderr ,"Could not open port [%s] \n" ,port );
53
+ fprintf (stderr ,"Error: Could not open port [%s] \n" ,portname );
54
54
_exit (-1 );
55
55
}
56
56
}
57
57
// Set serial port parameters E81, raw
58
- setPortConfig (portfd , baudrate );
58
+ setPortConfig (port , baudrate );
59
59
}
60
60
61
61
void closeport ()
<
8000
/code>
62
62
{
63
- if (portfd > 0 ) close ( portfd );
64
- portfd = -1 ;
63
+ if (port != SERIAL_NOT_OPEN ) closeSerialPort ( port );
64
+ port = SERIAL_NOT_OPEN ;
65
65
}
66
66
67
67
/***********************************************************************
@@ -77,7 +77,7 @@ int bootloader_receive_byte()
77
77
unsigned char buf [2 ];
78
78
while (errorcount ++ < 100 )
79
79
{
80
- result = read (portfd ,buf ,1 );
80
+ result = read (port ,buf ,1 );
81
81
if (result < 0 )
82
82
{
83
83
if (errno != 11 ) break ;
@@ -88,7 +88,7 @@ int bootloader_receive_byte()
88
88
}
89
89
if (result < 0 )
90
90
{
91
- printf ("Error %i:%s\n" ,errno ,strerror (errno ));
91
+ printf ("Device is not responding %i:%s\n" ,e
8000
rrno ,strerror (errno ));
92
92
}
93
93
return buf [0 ];
94
94
}
@@ -100,18 +100,18 @@ int bootloader_wait_ack()
100
100
{
101
101
if (ack == 0x1F )
102
102
{
103
- printf ("NACK\n" );
103
+ printf ("protocol error: NACK\n" );
104
104
return 0 ;
105
105
}
106
106
else
107
107
{
108
- printf ("Read ack: 0x%02x\n" ,ack );
108
+ printf ("protocol error: Wait ack got 0x%02x\n" ,ack );
109
109
return 0 ;
110
110
}
111
111
}
112
112
else
113
113
{
114
- if (verbose > 3 ) printf ("ACK\n" );
114
+ if (verbose > 4 ) printf ("ACK\n" );
115
115
return 1 ;
116
116
}
117
117
}
@@ -121,7 +121,7 @@ int bootloader_send_command(unsigned char cmd)
121
121
unsigned char buf [2 ];
122
122
buf [0 ]= cmd ;
123
123
buf [1 ]= ~cmd ;
124
- write (portfd ,buf ,2 );
124
+ write (port ,buf ,2 );
125
125
return bootloader_wait_ack ();
126
126
}
127
127
@@ -136,7 +136,7 @@ int bootloader_send_address(unsigned int addr)
136
136
addr = addr >>8 ;
137
137
buf [4 ] = buf [4 ]^buf [k ];
138
138
}
139
- write (portfd ,buf ,5 );
139
+ write (port ,buf ,5 );
140
140
return bootloader_wait_ack ();
141
141
}
142
142
@@ -145,9 +145,9 @@ void bootloader_connect()
145
145
int ack = 0 ;
146
146
int count = 0 ;
147
147
unsigned char buf [2 ]= {0x7F ,0x7F };
148
- while (!ack & count ++ < 2 )
148
+ while (!ack & ( count ++ < 2 ) )
149
149
{
150
- write (portfd ,buf ,1 );
150
+ write (port ,buf ,1 );
151
151
ack = bootloader_wait_ack ();
152
152
}
153
153
}
@@ -188,7 +188,6 @@ void bootloader_get_commands()
188
188
189
189
void bootloader_go (unsigned int addr )
190
190
{
191
- int k ;
192
191
printf ("Bootloader go\n" );
193
192
}
194
193
@@ -237,13 +236,13 @@ int bootloader_write_memory(int addr,int len,char * outbuffer)
237
236
if (! ack ) return -1 ;
238
237
n = thisblock - 1 ;
239
238
check = n ;
240
- write (portfd ,& n ,1 );
239
+ write (port ,& n ,1 );
241
240
for (k = 0 ; k < thisblock ; k ++ )
242
241
{
243
- write (portfd ,& outbuffer [k + count ],1 );
242
+ write (port ,& outbuffer [k + count ],1 );
244
243
check = check ^outbuffer [k + count ];
245
244
}
246
- write (portfd ,& check ,1 );
245
+ write (port ,& check ,1 );
247
246
ack = bootloader_wait_ack ();
248
247
if (! ack ) return -1 ;
249
248
count += thisblock ;
@@ -253,7 +252,6 @@ int bootloader_write_memory(int addr,int len,char * outbuffer)
253
252
254
253
void bootloader_erase_memory ()
255
254
{
256
- int k ;
257
255
int ack ;
258
256
if (verbose > 0 ) printf ("Bootloader erase memory\n" );
259
257
ack = bootloader_send_command (0x43 );
@@ -265,7 +263,6 @@ void bootloader_erase_memory()
265
263
266
264
void bootloader_write_unprotect ()
267
265
{
268
- int k ;
269
266
printf ("Bootloader write unprotect\n" );
270
267
}
271
268
@@ -287,7 +284,7 @@ void memop(char * cmdstr)
287
284
intelln_t * hexdata ;
288
285
289
286
int k = 0 ;
290
- int result ;
287
+ int result = 0 ;
291
288
int len = strlen (cmdstr );
292
289
293
290
memtype = cmdstr ;
@@ -332,13 +329,14 @@ void memop(char * cmdstr)
332
329
len = 0 ;
333
330
if (result < 0 )
334
331
{
335
- fprintf (stderr ,"Error in memory write\n" );
336
- return ;
332
+ fprintf (stderr ,"Memory write failed \n" );
333
+ break ;
337
334
}
338
335
}
339
336
curhexline = curhexline -> next ;
340
337
}
341
- if (verbose >=0 ) printf ("Memory write finished without error\n" );
338
+ if (result == 0 )
339
+ if (verbose >=0 ) printf ("Memory write finished without error\n" );
342
340
}
343
341
344
342
// Read data from target
@@ -357,7 +355,6 @@ int main(int argc, char * argv[])
357
355
int k ;
358
356
int eraseflag = 0 ;
359
357
int lastopt = 0 ;
360
- unsigned char data [16000 ];
361
358
362
359
for (k = 0 ; k < argc ;k ++ )
363
360
{
@@ -371,11 +368,11 @@ int main(int argc, char * argv[])
371
368
{
372
369
if (argv [k ][2 ])
373
370
{
374
- port = argv [k ]+ 2 ;
371
+ portname = argv [k ]+ 2 ;
375
372
}
376
373
else
377
374
{
378
- port = argv [++ k ];
375
+ portname = argv [++ k ];
379
376
}
380
377
lastopt = k ;
381
378
closeport ();
@@ -410,7 +407,7 @@ int main(int argc, char * argv[])
410
407
}
411
408
}
412
409
413
- printf ("Using port %s with baudrate %i \n" ,port ,baudrate );
410
+ printf ("Using port %s with baudrate %i \n" ,portname ,baudrate );
414
411
opentargetport ();
415
412
bootloader_connect ();
416
413
bootloader_get_commands ();
0 commit comments