2018-02-13 11:21:46 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013-2018 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/obj.h"
|
2018-03-09 11:22:29 +00:00
|
|
|
#include "py/mperrno.h"
|
2018-11-21 15:39:46 +00:00
|
|
|
#include "irq.h"
|
2018-02-13 11:21:46 +00:00
|
|
|
#include "led.h"
|
|
|
|
#include "flash.h"
|
|
|
|
#include "storage.h"
|
|
|
|
|
2018-03-09 13:50:27 +00:00
|
|
|
#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
|
2018-02-13 11:21:46 +00:00
|
|
|
|
2022-03-13 18:03:38 +00:00
|
|
|
// The linker script specifies flash storage and RAM cache locations.
|
2021-11-29 02:01:51 +00:00
|
|
|
extern uint8_t _micropy_hw_internal_flash_storage_start;
|
|
|
|
extern uint8_t _micropy_hw_internal_flash_storage_end;
|
|
|
|
extern uint8_t _micropy_hw_internal_flash_storage2_start;
|
|
|
|
extern uint8_t _micropy_hw_internal_flash_storage2_end;
|
|
|
|
extern uint8_t _micropy_hw_internal_flash_storage_ram_cache_start[];
|
|
|
|
extern uint8_t _micropy_hw_internal_flash_storage_ram_cache_end[];
|
|
|
|
|
|
|
|
#define CACHE_MEM_START_ADDR \
|
|
|
|
((uintptr_t)&_micropy_hw_internal_flash_storage_ram_cache_start[0])
|
|
|
|
#define FLASH_SECTOR_SIZE_MAX \
|
|
|
|
(&_micropy_hw_internal_flash_storage_ram_cache_end[0] - &_micropy_hw_internal_flash_storage_ram_cache_start[0])
|
|
|
|
#define FLASH_MEM_SEG1_START_ADDR \
|
|
|
|
((long)&_micropy_hw_internal_flash_storage_start)
|
|
|
|
#define FLASH_MEM_SEG1_NUM_BLOCKS \
|
|
|
|
((&_micropy_hw_internal_flash_storage_end - &_micropy_hw_internal_flash_storage_start) / 512)
|
|
|
|
|
|
|
|
#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE_SEGMENT2
|
|
|
|
#define FLASH_MEM_SEG2_START_ADDR \
|
|
|
|
((long)&_micropy_hw_internal_flash_storage2_start)
|
|
|
|
#define FLASH_MEM_SEG2_NUM_BLOCKS \
|
|
|
|
((&_micropy_hw_internal_flash_storage2_end - &_micropy_hw_internal_flash_storage2_start) / 512)
|
|
|
|
#endif
|
|
|
|
|
2018-02-13 11:21:46 +00:00
|
|
|
#if !defined(FLASH_MEM_SEG2_START_ADDR)
|
|
|
|
#define FLASH_MEM_SEG2_NUM_BLOCKS (0) // no second segment
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define FLASH_FLAG_DIRTY (1)
|
|
|
|
#define FLASH_FLAG_FORCE_WRITE (2)
|
|
|
|
#define FLASH_FLAG_ERASED (4)
|
|
|
|
static __IO uint8_t flash_flags = 0;
|
|
|
|
static uint32_t flash_cache_sector_id;
|
|
|
|
static uint32_t flash_cache_sector_start;
|
|
|
|
static uint32_t flash_cache_sector_size;
|
|
|
|
static uint32_t flash_tick_counter_last_write;
|
|
|
|
|
2018-03-09 11:22:29 +00:00
|
|
|
static void flash_bdev_irq_handler(void);
|
|
|
|
|
|
|
|
int32_t flash_bdev_ioctl(uint32_t op, uint32_t arg) {
|
|
|
|
(void)arg;
|
|
|
|
switch (op) {
|
|
|
|
case BDEV_IOCTL_INIT:
|
|
|
|
flash_flags = 0;
|
|
|
|
flash_cache_sector_id = 0;
|
|
|
|
flash_tick_counter_last_write = 0;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case BDEV_IOCTL_NUM_BLOCKS:
|
2021-08-26 02:38:29 +01:00
|
|
|
// Units are FLASH_BLOCK_SIZE
|
2018-03-09 11:22:29 +00:00
|
|
|
return FLASH_MEM_SEG1_NUM_BLOCKS + FLASH_MEM_SEG2_NUM_BLOCKS;
|
|
|
|
|
|
|
|
case BDEV_IOCTL_IRQ_HANDLER:
|
|
|
|
flash_bdev_irq_handler();
|
|
|
|
return 0;
|
|
|
|
|
2018-09-12 06:46:04 +01:00
|
|
|
case BDEV_IOCTL_SYNC: {
|
|
|
|
uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access
|
2018-03-09 11:22:29 +00:00
|
|
|
if (flash_flags & FLASH_FLAG_DIRTY) {
|
|
|
|
flash_flags |= FLASH_FLAG_FORCE_WRITE;
|
|
|
|
while (flash_flags & FLASH_FLAG_DIRTY) {
|
2018-09-12 06:46:04 +01:00
|
|
|
flash_bdev_irq_handler();
|
2018-03-09 11:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 06:46:04 +01:00
|
|
|
restore_irq_pri(basepri);
|
2018-03-09 11:22:29 +00:00
|
|
|
return 0;
|
2018-09-12 06:46:04 +01:00
|
|
|
}
|
2018-02-13 11:21:46 +00:00
|
|
|
}
|
2018-03-09 11:22:29 +00:00
|
|
|
return -MP_EINVAL;
|
2018-02-13 11:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t *flash_cache_get_addr_for_write(uint32_t flash_addr) {
|
|
|
|
uint32_t flash_sector_start;
|
|
|
|
uint32_t flash_sector_size;
|
2020-06-30 07:33:32 +01:00
|
|
|
int32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size);
|
2018-02-13 11:21:46 +00:00
|
|
|
if (flash_sector_size > FLASH_SECTOR_SIZE_MAX) {
|
|
|
|
flash_sector_size = FLASH_SECTOR_SIZE_MAX;
|
|
|
|
}
|
|
|
|
if (flash_cache_sector_id != flash_sector_id) {
|
2018-03-09 11:22:29 +00:00
|
|
|
flash_bdev_ioctl(BDEV_IOCTL_SYNC, 0);
|
2018-02-13 11:21:46 +00:00
|
|
|
memcpy((void *)CACHE_MEM_START_ADDR, (const void *)flash_sector_start, flash_sector_size);
|
|
|
|
flash_cache_sector_id = flash_sector_id;
|
|
|
|
flash_cache_sector_start = flash_sector_start;
|
|
|
|
flash_cache_sector_size = flash_sector_size;
|
|
|
|
}
|
|
|
|
flash_flags |= FLASH_FLAG_DIRTY;
|
|
|
|
led_state(PYB_LED_RED, 1); // indicate a dirty cache with LED on
|
|
|
|
flash_tick_counter_last_write = HAL_GetTick();
|
|
|
|
return (uint8_t *)CACHE_MEM_START_ADDR + flash_addr - flash_sector_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t *flash_cache_get_addr_for_read(uint32_t flash_addr) {
|
|
|
|
uint32_t flash_sector_start;
|
|
|
|
uint32_t flash_sector_size;
|
2020-06-30 07:33:32 +01:00
|
|
|
int32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size);
|
2018-02-13 11:21:46 +00:00
|
|
|
if (flash_cache_sector_id == flash_sector_id) {
|
|
|
|
// in cache, copy from there
|
|
|
|
return (uint8_t *)CACHE_MEM_START_ADDR + flash_addr - flash_sector_start;
|
|
|
|
}
|
|
|
|
// not in cache, copy straight from flash
|
|
|
|
return (uint8_t *)flash_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t convert_block_to_flash_addr(uint32_t block) {
|
|
|
|
if (block < FLASH_MEM_SEG1_NUM_BLOCKS) {
|
|
|
|
return FLASH_MEM_SEG1_START_ADDR + block * FLASH_BLOCK_SIZE;
|
|
|
|
}
|
2021-11-29 02:01:51 +00:00
|
|
|
#ifdef FLASH_MEM_SEG2_START_ADDR
|
2018-02-13 11:21:46 +00:00
|
|
|
if (block < FLASH_MEM_SEG1_NUM_BLOCKS + FLASH_MEM_SEG2_NUM_BLOCKS) {
|
|
|
|
return FLASH_MEM_SEG2_START_ADDR + (block - FLASH_MEM_SEG1_NUM_BLOCKS) * FLASH_BLOCK_SIZE;
|
|
|
|
}
|
2021-11-29 02:01:51 +00:00
|
|
|
#endif
|
2018-02-13 11:21:46 +00:00
|
|
|
// can add more flash segments here if needed, following above pattern
|
|
|
|
|
|
|
|
// bad block
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-09 11:22:29 +00:00
|
|
|
static void flash_bdev_irq_handler(void) {
|
2018-02-13 11:21:46 +00:00
|
|
|
if (!(flash_flags & FLASH_FLAG_DIRTY)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This code uses interrupts to erase the flash
|
|
|
|
/*
|
|
|
|
if (flash_erase_state == 0) {
|
2018-05-02 04:11:56 +01:00
|
|
|
flash_erase_it(flash_cache_sector_start, flash_cache_sector_size / 4);
|
2018-02-13 11:21:46 +00:00
|
|
|
flash_erase_state = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flash_erase_state == 1) {
|
|
|
|
// wait for erase
|
|
|
|
// TODO add timeout
|
|
|
|
#define flash_erase_done() (__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) == RESET)
|
|
|
|
if (!flash_erase_done()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
flash_erase_state = 2;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This code erases the flash directly, waiting for it to finish
|
|
|
|
if (!(flash_flags & FLASH_FLAG_ERASED)) {
|
2018-05-02 04:11:56 +01:00
|
|
|
flash_erase(flash_cache_sector_start, flash_cache_sector_size / 4);
|
2018-02-13 11:21:46 +00:00
|
|
|
flash_flags |= FLASH_FLAG_ERASED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not a forced write, wait at least 5 seconds after last write to flush
|
|
|
|
// On file close and flash unmount we get a forced write, so we can afford to wait a while
|
2018-03-27 10:38:57 +01:00
|
|
|
if ((flash_flags & FLASH_FLAG_FORCE_WRITE) || HAL_GetTick() - flash_tick_counter_last_write >= 5000) {
|
2018-02-13 11:21:46 +00:00
|
|
|
// sync the cache RAM buffer by writing it to the flash page
|
|
|
|
flash_write(flash_cache_sector_start, (const uint32_t *)CACHE_MEM_START_ADDR, flash_cache_sector_size / 4);
|
|
|
|
// clear the flash flags now that we have a clean cache
|
|
|
|
flash_flags = 0;
|
|
|
|
// indicate a clean cache with LED off
|
|
|
|
led_state(PYB_LED_RED, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool flash_bdev_readblock(uint8_t *dest, uint32_t block) {
|
|
|
|
// non-MBR block, get data from flash memory, possibly via cache
|
|
|
|
uint32_t flash_addr = convert_block_to_flash_addr(block);
|
|
|
|
if (flash_addr == -1) {
|
|
|
|
// bad block number
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint8_t *src = flash_cache_get_addr_for_read(flash_addr);
|
|
|
|
memcpy(dest, src, FLASH_BLOCK_SIZE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool flash_bdev_writeblock(const uint8_t *src, uint32_t block) {
|
|
|
|
// non-MBR block, copy to cache
|
|
|
|
uint32_t flash_addr = convert_block_to_flash_addr(block);
|
|
|
|
if (flash_addr == -1) {
|
|
|
|
// bad block number
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-12 06:58:42 +01:00
|
|
|
uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access
|
2018-02-13 11:21:46 +00:00
|
|
|
uint8_t *dest = flash_cache_get_addr_for_write(flash_addr);
|
|
|
|
memcpy(dest, src, FLASH_BLOCK_SIZE);
|
2018-09-12 06:58:42 +01:00
|
|
|
restore_irq_pri(basepri);
|
2018-02-13 11:21:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-13 06:31:35 +00:00
|
|
|
int flash_bdev_readblocks_ext(uint8_t *dest, uint32_t block, uint32_t offset, uint32_t len) {
|
|
|
|
// Get data from flash memory, possibly via cache
|
|
|
|
while (len) {
|
|
|
|
uint32_t l = MIN(len, FLASH_BLOCK_SIZE - offset);
|
|
|
|
uint32_t flash_addr = convert_block_to_flash_addr(block);
|
|
|
|
if (flash_addr == -1) {
|
|
|
|
// bad block number
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint8_t *src = flash_cache_get_addr_for_read(flash_addr + offset);
|
|
|
|
memcpy(dest, src, l);
|
|
|
|
dest += l;
|
|
|
|
block += 1;
|
|
|
|
offset = 0;
|
|
|
|
len -= l;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int flash_bdev_writeblocks_ext(const uint8_t *src, uint32_t block, uint32_t offset, uint32_t len) {
|
|
|
|
// Copy to cache
|
|
|
|
while (len) {
|
|
|
|
uint32_t l = MIN(len, FLASH_BLOCK_SIZE - offset);
|
|
|
|
uint32_t flash_addr = convert_block_to_flash_addr(block);
|
|
|
|
if (flash_addr == -1) {
|
|
|
|
// bad block number
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access
|
|
|
|
uint8_t *dest = flash_cache_get_addr_for_write(flash_addr + offset);
|
|
|
|
memcpy(dest, src, l);
|
|
|
|
restore_irq_pri(basepri);
|
|
|
|
src += l;
|
|
|
|
block += 1;
|
|
|
|
offset = 0;
|
|
|
|
len -= l;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-09 13:50:27 +00:00
|
|
|
#endif // MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
|