Skip to content

Commit e99e6c8

Browse files
committed
stmhal: Move flash storage idle tick handler from TIM3 to SysTick.
Using SysTick to do the counting and dispatch of the flash storage idle handler is more efficient than requiring a dedicated hardware timer. No new counter is needed, just the existing uwTick variable. The processing is not actually done in the SysTick IRQ, it is deferred to the flash IRQ (which runs at lower priority).
1 parent 3cfb02f commit e99e6c8

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

stmhal/stm32_it.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,15 @@ void SysTick_Handler(void) {
269269
// work properly.
270270
SysTick->CTRL;
271271

272-
// Right now we just have the DMA controllers to process during this
273-
// interrupt and we use a custom dispatch handler. If this needs to
272+
// Right now we have the storage and DMA controllers to process during
273+
// this interrupt and we use custom dispatch handlers. If this needs to
274274
// be generalised in the future then a dispatch table can be used as
275275
// follows: ((void(*)(void))(systick_dispatch[uwTick & 0xf]))();
276+
277+
if (STORAGE_IDLE_TICK(uwTick)) {
278+
NVIC->STIR = FLASH_IRQn;
279+
}
280+
276281
if (DMA_IDLE_ENABLED() && DMA_IDLE_TICK(uwTick)) {
277282
dma_idle_handler(uwTick);
278283
}

stmhal/storage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727
#define FLASH_BLOCK_SIZE (512)
2828

29+
#define STORAGE_SYSTICK_MASK (0x1ff) // 512ms
30+
#define STORAGE_IDLE_TICK(tick) (((tick) & STORAGE_SYSTICK_MASK) == 0)
31+
2932
void storage_init(void);
3033
uint32_t storage_get_block_size(void);
3134
uint32_t storage_get_block_count(void);

stmhal/timer.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@ TIM_HandleTypeDef TIM3_Handle;
148148
TIM_HandleTypeDef TIM5_Handle;
149149
TIM_HandleTypeDef TIM6_Handle;
150150

151-
// Used to divide down TIM3 and periodically call the flash storage IRQ
152-
STATIC uint32_t tim3_counter = 0;
153-
154151
#define PYB_TIMER_OBJ_ALL_NUM MP_ARRAY_SIZE(MP_STATE_PORT(pyb_timer_obj_all))
155152

156153
STATIC uint32_t timer_get_source_freq(uint32_t tim_id);
@@ -159,7 +156,6 @@ STATIC mp_obj_t pyb_timer_callback(mp_obj_t self_in, mp_obj_t callback);
159156
STATIC mp_obj_t pyb_timer_channel_callback(mp_obj_t self_in, mp_obj_t callback);
160157

161158
void timer_init0(void) {
162-
tim3_counter = 0;
163159
for (uint i = 0; i < PYB_TIMER_OBJ_ALL_NUM; i++) {
164160
MP_STATE_PORT(pyb_timer_obj_all)[i] = NULL;
165161
}
@@ -256,13 +252,6 @@ TIM_HandleTypeDef *timer_tim6_init(uint freq) {
256252
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
257253
if (htim == &TIM3_Handle) {
258254
USBD_CDC_HAL_TIM_PeriodElapsedCallback();
259-
260-
// Periodically raise a flash IRQ for the flash storage controller
261-
if (tim3_counter++ >= 500 / USBD_CDC_POLLING_INTERVAL) {
262-
tim3_counter = 0;
263-
NVIC->STIR = FLASH_IRQn;
264-
}
265-
266255
} else if (htim == &TIM5_Handle) {
267256
servo_timer_irq_callback();
268257
}

0 commit comments

Comments
 (0)