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