Skip to content

Commit 20bd29f

Browse files
authored
Merge pull request #36 from maxiwoj/tm/target-flash-status
Tm/target flash status
2 parents 0b9870d + fdd91c3 commit 20bd29f

File tree

7 files changed

+87
-21
lines changed

7 files changed

+87
-21
lines changed

Inc/communication/wakaama_client/objects/object_target.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ typedef struct _target_instance_
2626
uint32_t firmware_version;
2727
char * binary_filename;
2828
uint8_t download_progress;
29-
uint8_t flash_error;
29+
uint16_t flash_error;
3030
TARGET_t *target;
3131
} target_instance_t;
3232

Inc/cortexm/stm32/stm32f4.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,19 @@
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
45+
46+
/*
47+
* flash errors returned to flash_target_task
48+
* 0x0 - 0x1000 - reserved for target (we can use this pool here)
49+
* bit 9 (0x200) - error while flash erasing. [8..0] represent FLASH_SR[8..0]
50+
* bit 10 (0x400) - error while flash writing. [8..0] represent FLASH_SR[8..0]
51+
*/
52+
#define STM32F4_ERASE_ERROR_BIT 0x200
53+
#define STM32F4_FLASH_ERROR_BIT 0x400
54+
#define STM32F4_ERASE_NEVER_END 0x800
55+
#define STM32F4_ERROR_ON_FLASH_WRITE_SETUP 0x801
4456

4557
typedef struct CORTEXM_s CORTEXM_t;
4658

Inc/target.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
#define FLASH_ERROR 2
1010
#define FLASH_COMPLETED 3
1111

12-
#define USB_FS_ERROR 1
12+
// Flash Error status
13+
// 0x0 -- 0x1000 reserved for target
14+
#define USB_FS_ERROR 0x1001
1315

1416
// TODO: define real functions with real arguments
1517
typedef struct TARGET_OPS_s {
16-
int (*flash_target)(void *priv, FIL *file);
18+
int (*flash_target)(void *priv, FIL *file, int *progress);
1719
void (*reset_target)(void *priv);
1820

1921
void (*free_priv)(void *priv);

Src/cortexm/stm32/stm32f4.c

Lines changed: 41 additions & 11 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 */
@@ -69,7 +71,17 @@ static int stm32f4_erase_all_flash(STM32F4_PRIV_t *priv)
6971
if(priv->cortex->ops->check_error(priv->cortex->priv)) {
7072
// TODO: handle error
7173
printf("Error while waiting for erase end\n");
72-
return 1;
74+
return STM32F4_ERASE_NEVER_END;
75+
}
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+
}
7385
}
7486
}
7587

@@ -78,8 +90,11 @@ static int stm32f4_erase_all_flash(STM32F4_PRIV_t *priv)
7890
if ((sr & STM32F4_SR_ERROR_MASK) || !(sr & STM32F4_SR_EOP)) {
7991
// TODO: handle error
8092
printf("Error after erase 0x%x\n", sr);
81-
return 1;
93+
return sr | STM32F4_ERASE_ERROR_BIT;
8294
}
95+
96+
// End of erase, update progress
97+
*progress = progress_end;
8398
return 0;
8499
}
85100

@@ -100,7 +115,7 @@ static int stm32f4_flash_write(STM32F4_PRIV_t *priv, uint32_t dest, const uint32
100115
if(priv->cortex->ops->check_error(priv->cortex->priv)) {
101116
// TODO: handle error
102117
printf("ERROR: Filed to setup write operation\n");
103-
return 1;
118+
return STM32F4_ERROR_ON_FLASH_WRITE_SETUP;
104119
}
105120

106121
/* Execute the stub */
@@ -115,29 +130,37 @@ static int stm32f4_flash_write(STM32F4_PRIV_t *priv, uint32_t dest, const uint32
115130
if (sr & STM32F4_SR_ERROR_MASK) {
116131
// TODO: handle error
117132
printf("ERROR: writing ended with error 0x%x\n", sr);
118-
return 1;
133+
return sr | STM32F4_FLASH_ERROR_BIT;
119134
}
120135

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;
128143
uint32_t addr = 0x8000000; // start of flash memory
129144
uint32_t *data = pvPortMalloc(STM32F4_SIZE_OF_ONE_WRITE/sizeof(uint32_t));
130145
STM32F4_PRIV_t *priv = priv_void;
146+
uint16_t result;
147+
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;
131152

132153
printf("Start flashing STM32F4x\n");
133154

134155
priv->cortex->ops->halt_request(priv->cortex->priv);
135156
stm32f4_flash_unlock(priv);
136-
if(stm32f4_erase_all_flash(priv)) {
157+
result = stm32f4_erase_all_flash(priv, progress, progress_as_float);
158+
if(result) {
137159
vPortFree(data);
138-
return 1;
160+
return result;
139161
}
140162

163+
141164
do {
142165
f_read(file, data, STM32F4_SIZE_OF_ONE_WRITE, &br);
143166
printf("flash 0x%x bytes on 0x%lx\n", br, addr);
@@ -151,20 +174,26 @@ static int stm32f4_program(void *priv_void, FIL *file)
151174
// add modified bytes to bytes that will be written
152175
br++;
153176
br <<= 2;
154-
if(stm32f4_flash_write(priv, addr, data, br)) {
177+
result = stm32f4_flash_write(priv, addr, data, br);
178+
if(result) {
155179
vPortFree(data);
156-
return 1;
180+
return result;
157181
}
158182
// Unaligned read is always smaller then SIZE_OF_ONE_WRITE.
159183
// This is EOF so we have done here.
160184
break;
161185
}
162-
if(stm32f4_flash_write(priv, addr, data, br)) {
186+
result = stm32f4_flash_write(priv, addr, data, br);
187+
if(result) {
163188
vPortFree(data);
164189
return 1;
165190
}
166191
addr += br;
167192

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

@@ -173,6 +202,7 @@ static int stm32f4_program(void *priv_void, FIL *file)
173202
printf("Device flashed\nReset device\n");
174203
priv->cortex->ops->restart(priv->cortex->priv);
175204

205+
*progress = 100;
176206
return 0;
177207
}
178208

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)