@@ -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
0 commit comments