diff options
author | Damien George <damien.p.george@gmail.com> | 2014-04-16 23:08:36 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-04-16 23:08:36 +0100 |
commit | 6d983539bcff2a540dd136a7e84e1db2b334528f (patch) | |
tree | a5ba1e873fe2ee63a6bc7b8603ce3ff9ce7c2e81 /stmhal/timer.c | |
parent | f6be480bda92486c3185ceaf8f25f461dfb2aa72 (diff) | |
download | micropython-6d983539bcff2a540dd136a7e84e1db2b334528f.tar.gz micropython-6d983539bcff2a540dd136a7e84e1db2b334528f.zip |
stmhal: Improve flash storage cache management.
Internal flash used for the filesystem is now written (from the cache)
only after a 5s delay, or when a file is closed, or when the drive is
unmounted from the host. This delay means that multiple writes can
accumulate in the cache, and leads to less writes to the flash, making
it last longer.
It's implemented by a high-priority interrupt that takes care of flash
erase and write, and flushing the cache.
This is still only an interim solution for the flash filesystem. It
eventually needs to be replaced with something that uses less RAM for
the cache, something that can use more of the flash, and something that
does proper wear levelling.
Diffstat (limited to 'stmhal/timer.c')
-rw-r--r-- | stmhal/timer.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/stmhal/timer.c b/stmhal/timer.c index 5ea605039d..26524fbdd9 100644 --- a/stmhal/timer.c +++ b/stmhal/timer.c @@ -19,6 +19,7 @@ // the interrupts to be dispatched, so they are all collected here. // // TIM3: +// - flash storage controller, to flush the cache // - USB CDC interface, interval, to check for new data // - LED 4, PWM to set the LED intensity // @@ -29,14 +30,17 @@ TIM_HandleTypeDef TIM3_Handle; TIM_HandleTypeDef TIM5_Handle; TIM_HandleTypeDef TIM6_Handle; +// Used to divide down TIM3 and periodically call the flash storage IRQ +static uint32_t tim3_counter = 0; + // TIM3 is set-up for the USB CDC interface void timer_tim3_init(void) { // set up the timer for USBD CDC __TIM3_CLK_ENABLE(); TIM3_Handle.Instance = TIM3; - TIM3_Handle.Init.Period = (USBD_CDC_POLLING_INTERVAL*1000) - 1; - TIM3_Handle.Init.Prescaler = 84-1; + TIM3_Handle.Init.Period = (USBD_CDC_POLLING_INTERVAL*1000) - 1; // TIM3 fires every USBD_CDC_POLLING_INTERVAL ms + TIM3_Handle.Init.Prescaler = 84-1; // for System clock at 168MHz, TIM3 runs at 1MHz TIM3_Handle.Init.ClockDivision = 0; TIM3_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_Base_Init(&TIM3_Handle); @@ -105,6 +109,13 @@ void timer_tim6_init(uint freq) { void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if (htim == &TIM3_Handle) { USBD_CDC_HAL_TIM_PeriodElapsedCallback(); + + // Periodically raise a flash IRQ for the flash storage controller + if (tim3_counter++ >= 500 / USBD_CDC_POLLING_INTERVAL) { + tim3_counter = 0; + NVIC->STIR = FLASH_IRQn; + } + } else if (htim == &TIM5_Handle) { servo_timer_irq_callback(); } |