Skip to content

Commit fdd91c3

Browse files
committed
Add progress to flash command
This patch adds progress to flash command. It will be used in future by Wakaama. For STM32F4xx flash erase progress is counted approximately. When JTAG speed will change or block of data that is writen by one operation there is need to change STM32F4_ERASE_TIME_IN_WRITES define. It may differ on different devices from that family. Additionaly commands 'r' and 'b' are added to UART interface to allow fast check flashing command of red.bin and blue.bin file on first detected target.
1 parent 5a1c928 commit fdd91c3

File tree

6 files changed

+60
-11
lines changed

6 files changed

+60
-11
lines changed

Inc/cortexm/stm32/stm32f4.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040

4141
#define STM32F4_DBGMCU_IDCODE 0xE0042000
4242

43-
#define STM32F4_SIZE_OF_ONE_WRITE 0x1000
43+
#define STM32F4_SIZE_OF_ONE_WRITE 0x1000
44+
#define STM32F4_ERASE_TIME_IN_WRITES 10
4445

4546
/*
4647
* flash errors returned to flash_target_task

Inc/target.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
// TODO: define real functions with real arguments
1717
typedef struct TARGET_OPS_s {
18-
int (*flash_target)(void *priv, FIL *file);
18+
int (*flash_target)(void *priv, FIL *file, int *progress);
1919
void (*reset_target)(void *priv);
2020

2121
void (*free_priv)(void *priv);

Src/cortexm/stm32/stm32f4.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ static void stm32f4_flash_unlock(STM32F4_PRIV_t *priv)
5555
}
5656
}
5757

58-
static int stm32f4_erase_all_flash(STM32F4_PRIV_t *priv)
58+
static int stm32f4_erase_all_flash(STM32F4_PRIV_t *priv, int *progress, int progress_end)
5959
{
6060
uint16_t sr;
61+
int time = 0;
62+
int progress_step = progress_end/10;
6163

6264
printf("Erase Flash\n");
6365
/* Flash mass erase start instruction */
@@ -71,6 +73,16 @@ static int stm32f4_erase_all_flash(STM32F4_PRIV_t *priv)
7173
printf("Error while waiting for erase end\n");
7274
return STM32F4_ERASE_NEVER_END;
7375
}
76+
osDelay(1);
77+
time++;
78+
// after each 100 loops add one to progress, we asume that whole erase will take 1000 loops
79+
if(time > 100) {
80+
time = 0;
81+
if(*progress < progress_end - progress_step) {
82+
*progress += progress_step;
83+
printf("Flash progress %d\n", *progress);
84+
}
85+
}
7486
}
7587

7688
/* Check for error */
@@ -80,6 +92,9 @@ static int stm32f4_erase_all_flash(STM32F4_PRIV_t *priv)
8092
printf("Error after erase 0x%x\n", sr);
8193
return sr | STM32F4_ERASE_ERROR_BIT;
8294
}
95+
96+
// End of erase, update progress
97+
*progress = progress_end;
8398
return 0;
8499
}
85100

@@ -121,7 +136,7 @@ static int stm32f4_flash_write(STM32F4_PRIV_t *priv, uint32_t dest, const uint32
121136
return 0;
122137
}
123138

124-
static int stm32f4_program(void *priv_void, FIL *file)
139+
static int stm32f4_program(void *priv_void, FIL *file, int *progress)
125140
{
126141
UINT br;
127142
uint8_t unaligned;
@@ -130,16 +145,22 @@ static int stm32f4_program(void *priv_void, FIL *file)
130145
STM32F4_PRIV_t *priv = priv_void;
131146
uint16_t result;
132147

148+
// these variables are only needed to show progress
149+
int number_of_writes = (f_size(file) + STM32F4_SIZE_OF_ONE_WRITE - 1)/STM32F4_SIZE_OF_ONE_WRITE + STM32F4_ERASE_TIME_IN_WRITES;
150+
float progress_as_float = 100 * STM32F4_ERASE_TIME_IN_WRITES/number_of_writes;
151+
float progress_on_one_write = 100.0/number_of_writes;
152+
133153
printf("Start flashing STM32F4x\n");
134154

135155
priv->cortex->ops->halt_request(priv->cortex->priv);
136156
stm32f4_flash_unlock(priv);
137-
result = stm32f4_erase_all_flash(priv);
157+
result = stm32f4_erase_all_flash(priv, progress, progress_as_float);
138158
if(result) {
139159
vPortFree(data);
140160
return result;
141161
}
142162

163+
143164
do {
144165
f_read(file, data, STM32F4_SIZE_OF_ONE_WRITE, &br);
145166
printf("flash 0x%x bytes on 0x%lx\n", br, addr);
@@ -169,6 +190,10 @@ static int stm32f4_program(void *priv_void, FIL *file)
169190
}
170191
addr += br;
171192

193+
progress_as_float += progress_on_one_write;
194+
*progress = (int)progress_as_float;
195+
printf("Flash progress %d\n", *progress);
196+
172197
// EOF is when readed less bytes than requested
173198
} while(br == STM32F4_SIZE_OF_ONE_WRITE);
174199

@@ -177,6 +202,7 @@ static int stm32f4_program(void *priv_void, FIL *file)
177202
printf("Device flashed\nReset device\n");
178203
priv->cortex->ops->restart(priv->cortex->priv);
179204

205+
*progress = 100;
180206
return 0;
181207
}
182208

Src/fatfs.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ FRESULT scan_files (
8787
printf("Scan path \"%s\"\n", path);
8888
FRESULT res;
8989
DIR dir;
90-
int i;
90+
//int i;
9191
static FILINFO fno;
9292

9393
res = f_opendir(&dir, path); /* Open the directory */
@@ -98,8 +98,8 @@ FRESULT scan_files (
9898
if (fno.fattrib & AM_DIR) { /* It is a directory */
9999
/*i = strlen(path);
100100
sprintf(&path[i], "/%s", fno.fname);
101-
res = scan_files(path); /* Enter the directory */
102-
/*if (res != FR_OK) break;
101+
res = scan_files(path); // Enter the directory
102+
if (res != FR_OK) break;
103103
path[i] = 0;*/
104104
} else { /* It is a file. */
105105
printf("%s%s\n\r", path, fno.fname);
@@ -131,7 +131,7 @@ void usb_ls() {
131131
int usb_open_file(const char *filename, FIL *fp, BYTE mode) {
132132
FRESULT result = f_mount(&USBHFatFS, "", 1);
133133
if (result == FR_OK) {
134-
char *path = pvPortMalloc(sizeof filename + 4);
134+
char *path = pvPortMalloc(strlen(filename) + 4);
135135
sprintf(path, "0:/%s", filename);
136136
result = f_open(fp, path, mode);
137137
vPortFree(path);
@@ -158,7 +158,7 @@ int usb_write(const void *bytes, const char *filename, size_t size) {
158158
result = f_mount(&USBHFatFS, "", 1);
159159
if (result == FR_OK) {
160160
FIL fp;
161-
char *path = pvPortMalloc(sizeof filename + 4);
161+
char *path = pvPortMalloc(strlen(filename) + 4);
162162
sprintf(path, "0:/%s", filename);
163163
result = f_open(&fp, path, FA_WRITE | FA_OPEN_APPEND);
164164
vPortFree(path);
@@ -176,6 +176,7 @@ int usb_write(const void *bytes, const char *filename, size_t size) {
176176
CHECK_FRESULT(result, "mount failed", -1);
177177
}
178178

179+
return 0;
179180
}
180181
/* USER CODE END Application */
181182

Src/main.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "wakaama.h"
5959
#include "jtag/jtag_scan.h"
6060
#include "binary_download.h"
61+
#include "target.h"
6162
/* USER CODE END Includes */
6263

6364
/* Private variables ---------------------------------------------------------*/
@@ -363,6 +364,9 @@ void StartDefaultTask(void const * argument)
363364
HAL_GPIO_WritePin(USB_GPIO_OUT_GPIO_Port, USB_GPIO_OUT_Pin, GPIO_PIN_SET);
364365
BlinkBlue();
365366
char usrInput[2];
367+
FIL file;
368+
int result;
369+
int progress;
366370
/* Infinite loop */
367371
debug_init(&huart3);
368372
for(;;)
@@ -385,6 +389,22 @@ void StartDefaultTask(void const * argument)
385389
usb_write("asd", "temp", 3);
386390
}
387391
break;
392+
case 'r':
393+
progress = 0;
394+
if (usb_open_file("red.bin", &file, FA_READ) != 0) {
395+
break;
396+
}
397+
target_list.next->ops->flash_target(target_list.next->priv, &file, &progress);
398+
usb_close_file(&file);
399+
break;
400+
case 'b':
401+
progress = 0;
402+
if (usb_open_file("blue.bin", &file, FA_READ) != 0) {
403+
break;
404+
}
405+
target_list.next->ops->flash_target(target_list.next->priv, &file, &progress);
406+
usb_close_file(&file);
407+
break;
388408
}
389409
}
390410
/* USER CODE END 5 */

Src/target.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void flash_target_task(void *object) {
2222

2323
FIL file;
2424
int result;
25+
int progress;
2526

2627
result = usb_open_file(target_object->binary_filename, &file, FA_READ);
2728
if (result != 0) {
@@ -30,7 +31,7 @@ void flash_target_task(void *object) {
3031
vTaskDelete(NULL);
3132
}
3233

33-
result = target_object->target->ops->flash_target(target_object->target->priv, &file);
34+
result = target_object->target->ops->flash_target(target_object->target->priv, &file, &progress);
3435
if (result != 0) {
3536
target_object->flash_error = result;
3637
target_object->flash_state = FLASH_ERROR;

0 commit comments

Comments
 (0)