@@ -34,7 +34,7 @@ static stlink_t *connected_stlink = NULL;
3434static void cleanup (int32_t signum ) {
3535 (void )signum ;
3636
37- if (connected_stlink ) { // switch back to mass storage mode before closing
37+ if (connected_stlink ) { // switch back to mass storage mode before closing
3838 stlink_run (connected_stlink , RUN_NORMAL );
3939 stlink_exit_debug_mode (connected_stlink );
4040 stlink_close (connected_stlink );
@@ -90,11 +90,11 @@ int32_t main(int32_t ac, char** av) {
9090 o .connect = CONNECT_NORMAL ;
9191
9292 getopt_ret = flash_get_opts (& o , ac - 1 , av + 1 );
93- if (getopt_ret == -1 ) {
93+ if (getopt_ret == -1 ) {
9494 printf ("invalid command line\n" );
9595 usage ();
9696 return (-1 );
97- } else if (getopt_ret == 1 ) {
97+ } else if (getopt_ret == 1 ) {
9898 usage ();
9999 return 0 ;
100100 }
@@ -104,15 +104,15 @@ int32_t main(int32_t ac, char** av) {
104104
105105 sl = stlink_open_usb (o .log_level , o .connect , (char * )o .serial , o .freq );
106106
107- if (sl == NULL ) { return (-1 ); }
107+ if (sl == NULL ) { return (-1 ); }
108108
109- if (sl -> flash_type == STM32_FLASH_TYPE_UNKNOWN ) {
109+ if (sl -> flash_type == STM32_FLASH_TYPE_UNKNOWN ) {
110110 printf ("Failed to connect to target\n" );
111111 fprintf (stderr , "Failed to parse flash type or unrecognized flash type\n" );
112112 goto on_error ;
113113 }
114114
115- if ( o .flash_size != 0u && o .flash_size != sl -> flash_size ) {
115+ if ( o .flash_size != 0u && o .flash_size != sl -> flash_size ) {
116116 sl -> flash_size = o .flash_size ;
117117 printf ("Forcing flash size: --flash=0x%08X\n" , sl -> flash_size );
118118 }
@@ -127,98 +127,98 @@ int32_t main(int32_t ac, char** av) {
127127 signal (SIGSEGV , & cleanup );
128128
129129 // core must be halted to use RAM based flashloaders
130- if (stlink_force_debug (sl )) {
130+ if (stlink_force_debug (sl )) {
131131 printf ("Failed to halt the core\n" );
132132 goto on_error ;
133133 }
134134
135- if (stlink_status (sl )) {
135+ if (stlink_status (sl )) {
136136 printf ("Failed to get Core's status\n" );
137137 goto on_error ;
138138 }
139139
140- if (o .cmd == FLASH_CMD_WRITE ) {
140+ if (o .cmd == FLASH_CMD_WRITE ) {
141141 uint32_t size = 0 ;
142142
143- if (erase_type == MASS_ERASE ) {
143+ if (erase_type == MASS_ERASE ) {
144144 err = stlink_erase_flash_mass (sl );
145- if (err == -1 ) {
145+ if (err == -1 ) {
146146 printf ("stlink_erase_flash_mass() == -1\n" );
147147 goto on_error ;
148148 }
149149 }
150150
151151 // write
152- if (o .format == FLASH_FORMAT_IHEX ) {
152+ if (o .format == FLASH_FORMAT_IHEX ) {
153153 err = stlink_parse_ihex (o .filename , stlink_get_erased_pattern (sl ), & mem , & size , & o .addr );
154154
155- if (err == -1 ) {
155+ if (err == -1 ) {
156156 printf ("Cannot parse %s as Intel-HEX file\n" , o .filename );
157157 goto on_error ;
158158 }
159159 }
160- if ((o .addr >= sl -> flash_base ) && (o .addr < sl -> flash_base + sl -> flash_size )) {
161- if (o .format == FLASH_FORMAT_IHEX ) {
160+ if ((o .addr >= sl -> flash_base ) && (o .addr < sl -> flash_base + sl -> flash_size )) {
161+ if (o .format == FLASH_FORMAT_IHEX ) {
162162 err = stlink_mwrite_flash (sl , mem , size , o .addr , erase_type );
163163 } else {
164164 err = stlink_fwrite_flash (sl , o .filename , o .addr , erase_type );
165165 }
166166
167- if (err == -1 ) {
167+ if (err == -1 ) {
168168 printf ("stlink_fwrite_flash() == -1\n" );
169169 goto on_error ;
170170 }
171- } else if ((o .addr >= sl -> sram_base ) && (o .addr < sl -> sram_base + sl -> sram_size )) {
172- if (o .format == FLASH_FORMAT_IHEX ) {
171+ } else if ((o .addr >= sl -> sram_base ) && (o .addr < sl -> sram_base + sl -> sram_size )) {
172+ if (o .format == FLASH_FORMAT_IHEX ) {
173173 err = stlink_mwrite_sram (sl , mem , size , o .addr );
174174 } else {
175175 err = stlink_fwrite_sram (sl , o .filename , o .addr );
176176 }
177177
178- if (err == -1 ) {
178+ if (err == -1 ) {
179179 printf ("stlink_fwrite_sram() == -1\n" );
180180 goto on_error ;
181181 }
182- } else if ((o .addr >= sl -> option_base ) && (o .addr < sl -> option_base + sl -> option_size )) {
182+ } else if ((o .addr >= sl -> option_base ) && (o .addr < sl -> option_base + sl -> option_size )) {
183183 err = stlink_fwrite_option_bytes (sl , o .filename , o .addr );
184184
185- if (err == -1 ) {
185+ if (err == -1 ) {
186186 printf ("stlink_fwrite_option_bytes() == -1\n" );
187187 goto on_error ;
188188 }
189- } else if (o .area == FLASH_OPTION_BYTES ) {
190- if (o .val == 0 ) {
189+ } else if (o .area == FLASH_OPTION_BYTES ) {
190+ if (o .val == 0 ) {
191191 printf ("attempting to set option byte to 0, abort.\n" );
192192 goto on_error ;
193193 }
194194
195195 err = stlink_write_option_bytes32 (sl , o .val );
196196
197- if (err == -1 ) {
197+ if (err == -1 ) {
198198 printf ("stlink_write_option_bytes32() == -1\n" );
199199 goto on_error ;
200200 }
201- } else if (o .area == FLASH_OPTCR ) {
201+ } else if (o .area == FLASH_OPTCR ) {
202202 DLOG ("@@@@ Write %d (%0#10x) to option control register\n" , o .val , o .val );
203203
204204 err = stlink_write_option_control_register32 (sl , o .val );
205- } else if (o .area == FLASH_OPTCR1 ) {
205+ } else if (o .area == FLASH_OPTCR1 ) {
206206 DLOG ("@@@@ Write %d (%0#10x) to option control register 1\n" , o .val , o .val );
207207
208208 err = stlink_write_option_control_register1_32 (sl , o .val );
209- } else if (o .area == FLASH_OPTION_BYTES_BOOT_ADD ) {
209+ } else if (o .area == FLASH_OPTION_BYTES_BOOT_ADD ) {
210210 DLOG ("@@@@ Write %d (%0#10x) to option bytes boot address\n" , o .val , o .val );
211211
212212 err = stlink_write_option_bytes_boot_add32 (sl , o .val );
213- } else if (o .area == FLASH_OTP ) {
213+ } else if (o .area == FLASH_OTP ) {
214214 if (sl -> otp_base == 0 ) {
215215 err = -1 ;
216216 printf ("OTP Write NOT implemented\n" );
217217 goto on_error ;
218218 }
219219 err = stlink_fwrite_flash (sl , o .filename , o .addr , NO_ERASE );
220220
221- if (err == -1 ) {
221+ if (err == -1 ) {
222222 printf ("stlink_fwrite_flash() == -1\n" );
223223 goto on_error ;
224224 }
@@ -228,35 +228,35 @@ int32_t main(int32_t ac, char** av) {
228228 goto on_error ;
229229 }
230230
231- } else if (o .cmd == FLASH_CMD_ERASE ) {
231+ } else if (o .cmd == FLASH_CMD_ERASE ) {
232232
233233 // erase
234- if ((erase_type == MASS_ERASE ) || (o .size == 0 || o .addr == 0 )) {
234+ if ((erase_type == MASS_ERASE ) || (o .size == 0 || o .addr == 0 )) {
235235 err = stlink_erase_flash_mass (sl );
236- if (err == -1 ) {
236+ if (err == -1 ) {
237237 printf ("stlink_erase_flash_mass() == -1\n" );
238238 goto on_error ;
239239 }
240240 printf ("Mass erase completed successfully.\n" );
241241 } else {
242242 err = stlink_erase_flash_section (sl , o .addr , o .size , false);
243- if (err == -1 ) {
243+ if (err == -1 ) {
244244 printf ("stlink_erase_flash_section() == -1\n" );
245245 goto on_error ;
246246 }
247247 printf ("Section erase completed successfully.\n" );
248248 }
249249
250250 // reset after erase
251- if (stlink_reset (sl , RESET_AUTO )) {
251+ if (stlink_reset (sl , RESET_AUTO )) {
252252 printf ("Failed to reset device\n" );
253253 goto on_error ;
254254 }
255255
256- } else if (o .cmd == CMD_RESET ) {
256+ } else if (o .cmd == CMD_RESET ) {
257257
258258 // reset
259- if (stlink_reset (sl , RESET_AUTO )) {
259+ if (stlink_reset (sl , RESET_AUTO )) {
260260 printf ("Failed to reset device\n" );
261261 goto on_error ;
262262 } else {
@@ -266,20 +266,20 @@ int32_t main(int32_t ac, char** av) {
266266 } else {
267267
268268 // read
269- if ((o .area == FLASH_MAIN_MEMORY ) || (o .area == FLASH_SYSTEM_MEMORY )) {
270- if ((o .size == 0 ) && (o .addr >= sl -> flash_base ) && (o .addr < sl -> flash_base + sl -> flash_size )) {
269+ if ((o .area == FLASH_MAIN_MEMORY ) || (o .area == FLASH_SYSTEM_MEMORY )) {
270+ if ((o .size == 0 ) && (o .addr >= sl -> flash_base ) && (o .addr < sl -> flash_base + sl -> flash_size )) {
271271 o .size = sl -> flash_size ;
272272 }
273- else if ((o .size == 0 ) && (o .addr >= sl -> sram_base ) && (o .addr < sl -> sram_base + sl -> sram_size )) {
273+ else if ((o .size == 0 ) && (o .addr >= sl -> sram_base ) && (o .addr < sl -> sram_base + sl -> sram_size )) {
274274 o .size = sl -> sram_size ;
275275 }
276276 err = stlink_fread (sl , o .filename , o .format == FLASH_FORMAT_IHEX , o .addr , o .size );
277277
278- if (err == -1 ) {
278+ if (err == -1 ) {
279279 printf ("could not read main memory (%d)\n" , err );
280280 goto on_error ;
281281 }
282- } else if (o .area == FLASH_OPTION_BYTES ) {
282+ } else if (o .area == FLASH_OPTION_BYTES ) {
283283 uint32_t remaining_option_length = sl -> option_size / 4 ;
284284 DLOG ("@@@@ Read %u (%#x) option bytes from %#10x\n" ,
285285 remaining_option_length ,
@@ -288,66 +288,66 @@ int32_t main(int32_t ac, char** av) {
288288
289289 uint32_t option_byte = 0 ;
290290 err = stlink_read_option_bytes32 (sl , & option_byte );
291- if (err == -1 ) {
291+ if (err == -1 ) {
292292 printf ("could not read option bytes (%d)\n" , err );
293293 goto on_error ;
294- } else if (NULL != o .filename ) {
294+ } else if (NULL != o .filename ) {
295295 int32_t fd = open (o .filename , O_RDWR | O_TRUNC | O_CREAT | O_BINARY , 00700 );
296- if (fd == -1 ) {
296+ if (fd == -1 ) {
297297 fprintf (stderr , "open(%s) == -1\n" , o .filename );
298298 goto on_error ;
299299 }
300300 err = (uint32_t ) write (fd , & option_byte , 4 );
301- if (err == -1 ) {
301+ if (err == -1 ) {
302302 printf ("could not write buffer to file (%d)\n" , err );
303303 goto on_error ;
304304 }
305305 close (fd );
306306 } else {
307307 printf ("%08x\n" , option_byte );
308308 }
309- } else if (o .area == FLASH_OPTION_BYTES_BOOT_ADD ) {
309+ } else if (o .area == FLASH_OPTION_BYTES_BOOT_ADD ) {
310310 uint32_t option_byte = 0 ;
311311 err = stlink_read_option_bytes_boot_add32 (sl , & option_byte );
312- if (err == -1 ) {
312+ if (err == -1 ) {
313313 printf ("could not read option bytes boot address (%d)\n" , err );
314314 goto on_error ;
315315 } else {
316316 printf ("%08x\n" ,option_byte );
317317 }
318- } else if (o .area == FLASH_OPTCR ) {
318+ } else if (o .area == FLASH_OPTCR ) {
319319 uint32_t option_byte = 0 ;
320320 err = stlink_read_option_control_register32 (sl , & option_byte );
321- if (err == -1 ) {
321+ if (err == -1 ) {
322322 printf ("could not read option control register (%d)\n" , err );
323323 goto on_error ;
324324 } else {
325325 printf ("%08x\n" ,option_byte );
326326 }
327- } else if (o .area == FLASH_OPTCR1 ) {
327+ } else if (o .area == FLASH_OPTCR1 ) {
328328 uint32_t option_byte = 0 ;
329329 err = stlink_read_option_control_register1_32 (sl , & option_byte );
330- if (err == -1 ) {
330+ if (err == -1 ) {
331331 printf ("could not read option control register (%d)\n" , err );
332332 goto on_error ;
333333 } else {
334334 printf ("%08x\n" ,option_byte );
335335 }
336- } else if (o .area == FLASH_OTP ) {
336+ } else if (o .area == FLASH_OTP ) {
337337 if (sl -> otp_base == 0 ) {
338338 err = -1 ;
339339 printf ("OTP Read NOT implemented\n" );
340340 goto on_error ;
341341 }
342342 err = stlink_fread (sl , o .filename , 0 , sl -> otp_base , sl -> otp_size );
343- if (err == -1 ) {
343+ if (err == -1 ) {
344344 printf ("could not read OTP area (%d)\n" , err );
345345 goto on_error ;
346346 }
347347 }
348348 }
349349
350- if (o .reset ) stlink_reset (sl , RESET_AUTO );
350+ if (o .reset ) stlink_reset (sl , RESET_AUTO );
351351
352352 stlink_run (sl , RUN_NORMAL );
353353
0 commit comments