Skip to content

Commit fa2f370

Browse files
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # .settings/language.settings.xml
1 parent ccf673d commit fa2f370

File tree

3 files changed

+54
-41
lines changed

3 files changed

+54
-41
lines changed

include/sys/Trace.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class Trace : public var::String {
3737
inline void critical() MCU_ALWAYS_INLINE { stratify_trace_event(POSIX_TRACE_CRITICAL, c_str(), size()); }
3838
inline void fatal() MCU_ALWAYS_INLINE { stratify_trace_event(POSIX_TRACE_FATAL, c_str(), size()); }
3939
#else
40-
Trace& operator=(const char * a){ return *this; }
40+
Trace& operator=(const char * a){ a = 0; return *this; }
4141
char * cdata(){ return m_cdata; }
42-
void assign(const char * str){}
42+
void assign(const char * str){ str = 0; } //suppress warnings
4343
inline void message(){}
4444
inline void warning(){}
4545
inline void error(){}

src/isp/LpcIsp.cpp

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,17 @@ int LpcIsp::copy_names(char * device, char * pio0, char * pio1){
5050
int LpcIsp::program(const char * filename, int crystal, const char * dev, bool (*progress)(void*,int,int), void * context){
5151
int ret;
5252
FILE * f;
53-
u8 * image_buffer;
53+
u8 image_buffer[LPCPHY_RAM_BUFFER_SIZE];
54+
const int image_page_size = LPCPHY_RAM_BUFFER_SIZE;
5455
u32 size;
5556
u32 bytes_read;
5657
u32 start_address;
5758
u8 failed;
59+
u32 bytes_written = 0;
5860

5961
device = dev;
6062

6163
if( strncmp(dev, "lpc8", 4) == 0 ){
62-
printf("LPC8xx mode\n");
6364
m_phy.set_max_speed(LpcPhy::MAX_SPEED_38400);
6465
m_phy.set_uuencode(false);
6566

@@ -79,7 +80,6 @@ int LpcIsp::program(const char * filename, int crystal, const char * dev, bool (
7980
}
8081

8182
sys::Timer::wait_msec(10);
82-
printf("Erase device\n");
8383
if ( erase_dev() ){
8484
m_trace.assign("Erase device");
8585
m_trace.error();
@@ -98,8 +98,6 @@ int LpcIsp::program(const char * filename, int crystal, const char * dev, bool (
9898
sprintf(m_trace.cdata(), "Image size:%d", (int)size);
9999
m_trace.message();
100100
} else {
101-
printf("Failed to open %s\n", filename);
102-
perror("Failed to open");
103101
m_trace.assign("Didn't open file");
104102
m_trace.error();
105103
isplib_error("Could not open file %s\n", filename);
@@ -114,31 +112,23 @@ int LpcIsp::program(const char * filename, int crystal, const char * dev, bool (
114112
}
115113

116114
isplib_debug(DEBUG_LEVEL, "File size is %d\n", (int)size);
117-
printf("malloc buffer\n");
118-
image_buffer = (u8*)malloc( size );
119-
if (!image_buffer){
120-
isplib_error("Failed to allocate memory\n");
121-
m_trace.assign("Malloc error");
122-
m_trace.error();
123-
return -3;
124-
}
125-
memset(image_buffer, 0xFF, size);
115+
memset(image_buffer, 0xFF, image_page_size);
126116

127117
f = fopen(filename, "rb");
128118

129119
if ( f != NULL ){
130-
if ( (bytes_read = fread(image_buffer, 1, size, f)) != size ){
120+
if ( (bytes_read = fread(image_buffer, 1, image_page_size, f)) != image_page_size ){
131121
m_trace.assign("Failed to read file");
132122
m_trace.error();
133123
isplib_error("Could not read file %s (%d of %d bytes read)\n", filename, bytes_read, size);
134124
fclose(f);
135125
return -1;
136126
}
137-
fclose(f);
138127
} else {
139128
m_trace.assign("Failed to open file");
140129
m_trace.error();
141130
isplib_error("Could not open file %s to read image\n", filename);
131+
fclose(f);
142132
return -1;
143133
}
144134

@@ -152,30 +142,57 @@ int LpcIsp::program(const char * filename, int crystal, const char * dev, bool (
152142
m_trace.assign("failed to set checksum");
153143
m_trace.error();
154144
isplib_error("Device %s is not supported\n", dev);
145+
fclose(f);
155146
return -1;
156147
}
157148

158149
m_phy.set_ram_buffer( lpc_device_get_ram_start(dev) + 0x300 );
159150

160151
//Write the program memory
161152
failed = 0;
162-
if ( !write_progmem(image_buffer, start_address, size, progress, context) ){
163-
m_trace.assign("failed to write image");
164-
m_trace.error();
165-
failed = 1;
166-
isplib_error("Failed to write program memory\n");
167-
return -1;
168-
}
153+
bytes_written = 0;
154+
do {
169155

156+
if( bytes_written > 0 ){
157+
bytes_read = fread(image_buffer, 1, image_page_size, f);
158+
}
159+
160+
if( bytes_read > 0 ){
161+
162+
if ( !write_progmem(image_buffer, start_address + bytes_written, bytes_read, 0, 0) ){
163+
m_trace.assign("failed to write image");
164+
m_trace.error();
165+
failed = 1;
166+
isplib_error("Failed to write program memory\n");
167+
fclose(f);
168+
return -1;
169+
}
170+
171+
bytes_written += bytes_read;
172+
} else {
173+
break;
174+
}
175+
176+
if ( progress ){
177+
if ( progress(context, bytes_written, size) ){
178+
m_trace.assign("Aborted");
179+
m_trace.warning();
180+
return 0; //abort requested
181+
}
182+
}
183+
184+
} while( bytes_written < size );
170185
prog_shutdown();
171186

172-
if ( !failed ){
187+
fclose(f);
188+
189+
if ( !failed && (bytes_written == size) ){
173190
isplib_debug(DEBUG_LEVEL, "Device Successfully Programmed\n");
174191
} else {
175192
isplib_error("Device Failed to program correctly\n");
193+
return -1;
176194
}
177195

178-
free(image_buffer);
179196

180197
return 0;
181198

@@ -273,24 +290,27 @@ u32 LpcIsp::read_progmem(void * data, u32 addr, u32 size, bool (*progress)(void*
273290
}
274291

275292
u32 LpcIsp::write_progmem(void * data, u32 addr, u32 size, bool (*progress)(void*,int, int), void * context){
276-
int buffer_size;
277293
int bytes_written;
278294
int page_size;
279295
int j;
280296
int sector;
281297
//char err;
282298

283299
//First read the buffer size
284-
buffer_size = LPCPHY_RAM_BUFFER_SIZE;
285300
bytes_written = 0;
286301
do {
287302

288-
if ( (int)(size-bytes_written) > buffer_size ) page_size = buffer_size;
289-
else page_size = size-bytes_written;
303+
if ( (int)(size-bytes_written) > LPCPHY_RAM_BUFFER_SIZE ){
304+
page_size = LPCPHY_RAM_BUFFER_SIZE;
305+
} else {
306+
page_size = size-bytes_written;
307+
}
290308

291309
//Check to see if data is already all 0xFF
292310
for(j=0; j < page_size; j++){
293-
if ( ((unsigned char*)data)[bytes_written+j] != 0xFF ) break;
311+
if ( ((unsigned char*)data)[bytes_written+j] != 0xFF ){
312+
break;
313+
}
294314
}
295315

296316
if ( j < page_size ){ //only write if data has non 0xFF values
@@ -300,20 +320,13 @@ u32 LpcIsp::write_progmem(void * data, u32 addr, u32 size, bool (*progress)(void
300320
&((char*)data)[bytes_written], page_size,
301321
sector ) != page_size ){
302322
isplib_error("writing data at address 0x%04X\n", (u32)(addr + bytes_written));
303-
sprintf(m_trace.cdata(), "failed to write 0x%04X", (u32)(addr + bytes_written));
323+
sprintf(m_trace.cdata(), "failed to write 0x%04lX", (u32)(addr + bytes_written));
304324
m_trace.error();
305325
return 0;
306326
}
307327
}
308328
//delay_ms(2);
309329
bytes_written+=page_size;
310-
if ( progress ){
311-
if ( progress(context, bytes_written, size) ){
312-
m_trace.assign("Aborted");
313-
m_trace.warning();
314-
return 0; //abort requested
315-
}
316-
}
317330
} while( bytes_written < (int)size );
318331

319332
return bytes_written;

src/isp/lpc_devices.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typedef struct {
2121
} lpc_device_t;
2222

2323

24-
#define TOTAL_DEVICES 3
24+
#define TOTAL_DEVICES 4
2525

2626
static lpc_device_t devices[] = {
2727
{

0 commit comments

Comments
 (0)