Skip to content

Commit 5a1c928

Browse files
committed
Return error from stm32f4_program
Now error code from stm32f4_program function has meaning and could be send back to user.
1 parent 0b9870d commit 5a1c928

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@
4242

4343
#define STM32F4_SIZE_OF_ONE_WRITE 0x1000
4444

45+
/*
46+
* flash errors returned to flash_target_task
47+
* 0x0 - 0x1000 - reserved for target (we can use this pool here)
48+
* bit 9 (0x200) - error while flash erasing. [8..0] represent FLASH_SR[8..0]
49+
* bit 10 (0x400) - error while flash writing. [8..0] represent FLASH_SR[8..0]
50+
*/
51+
#define STM32F4_ERASE_ERROR_BIT 0x200
52+
#define STM32F4_FLASH_ERROR_BIT 0x400
53+
#define STM32F4_ERASE_NEVER_END 0x800
54+
#define STM32F4_ERROR_ON_FLASH_WRITE_SETUP 0x801
55+
4556
typedef struct CORTEXM_s CORTEXM_t;
4657

4758
typedef struct STM32F4_PRIV_s {

Inc/target.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
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 {

Src/cortexm/stm32/stm32f4.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static int stm32f4_erase_all_flash(STM32F4_PRIV_t *priv)
6969
if(priv->cortex->ops->check_error(priv->cortex->priv)) {
7070
// TODO: handle error
7171
printf("Error while waiting for erase end\n");
72-
return 1;
72+
return STM32F4_ERASE_NEVER_END;
7373
}
7474
}
7575

@@ -78,7 +78,7 @@ static int stm32f4_erase_all_flash(STM32F4_PRIV_t *priv)
7878
if ((sr & STM32F4_SR_ERROR_MASK) || !(sr & STM32F4_SR_EOP)) {
7979
// TODO: handle error
8080
printf("Error after erase 0x%x\n", sr);
81-
return 1;
81+
return sr | STM32F4_ERASE_ERROR_BIT;
8282
}
8383
return 0;
8484
}
@@ -100,7 +100,7 @@ static int stm32f4_flash_write(STM32F4_PRIV_t *priv, uint32_t dest, const uint32
100100
if(priv->cortex->ops->check_error(priv->cortex->priv)) {
101101
// TODO: handle error
102102
printf("ERROR: Filed to setup write operation\n");
103-
return 1;
103+
return STM32F4_ERROR_ON_FLASH_WRITE_SETUP;
104104
}
105105

106106
/* Execute the stub */
@@ -115,7 +115,7 @@ static int stm32f4_flash_write(STM32F4_PRIV_t *priv, uint32_t dest, const uint32
115115
if (sr & STM32F4_SR_ERROR_MASK) {
116116
// TODO: handle error
117117
printf("ERROR: writing ended with error 0x%x\n", sr);
118-
return 1;
118+
return sr | STM32F4_FLASH_ERROR_BIT;
119119
}
120120

121121
return 0;
@@ -128,14 +128,16 @@ static int stm32f4_program(void *priv_void, FIL *file)
128128
uint32_t addr = 0x8000000; // start of flash memory
129129
uint32_t *data = pvPortMalloc(STM32F4_SIZE_OF_ONE_WRITE/sizeof(uint32_t));
130130
STM32F4_PRIV_t *priv = priv_void;
131+
uint16_t result;
131132

132133
printf("Start flashing STM32F4x\n");
133134

134135
priv->cortex->ops->halt_request(priv->cortex->priv);
135136
stm32f4_flash_unlock(priv);
136-
if(stm32f4_erase_all_flash(priv)) {
137+
result = stm32f4_erase_all_flash(priv);
138+
if(result) {
137139
vPortFree(data);
138-
return 1;
140+
return result;
139141
}
140142

141143
do {
@@ -151,15 +153,17 @@ static int stm32f4_program(void *priv_void, FIL *file)
151153
// add modified bytes to bytes that will be written
152154
br++;
153155
br <<= 2;
154-
if(stm32f4_flash_write(priv, addr, data, br)) {
156+
result = stm32f4_flash_write(priv, addr, data, br);
157+
if(result) {
155158
vPortFree(data);
156-
return 1;
159+
return result;
157160
}
158161
// Unaligned read is always smaller then SIZE_OF_ONE_WRITE.
159162
// This is EOF so we have done here.
160163
break;
161164
}
162-
if(stm32f4_flash_write(priv, addr, data, br)) {
165+
result = stm32f4_flash_write(priv, addr, data, br);
166+
if(result) {
163167
vPortFree(data);
164168
return 1;
165169
}

0 commit comments

Comments
 (0)