stm32/storage: Merge all misc block-dev funcs into a single ioctl func.

It makes it cleaner, and simpler to support multiple different block
devices.  It also allows to easily extend a given block device with new
ioctl operations.
This commit is contained in:
Damien George 2018-03-09 22:22:29 +11:00
parent a739b35a96
commit 1e4caf0b1e
4 changed files with 77 additions and 64 deletions

View File

@ -28,6 +28,7 @@
#include <string.h>
#include "py/obj.h"
#include "py/mperrno.h"
#include "systick.h"
#include "led.h"
#include "flash.h"
@ -122,23 +123,34 @@ static uint32_t flash_cache_sector_start;
static uint32_t flash_cache_sector_size;
static uint32_t flash_tick_counter_last_write;
void flash_bdev_init(void) {
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;
uint32_t flash_bdev_num_blocks(void) {
case BDEV_IOCTL_NUM_BLOCKS:
return FLASH_MEM_SEG1_NUM_BLOCKS + FLASH_MEM_SEG2_NUM_BLOCKS;
}
void flash_bdev_flush(void) {
case BDEV_IOCTL_IRQ_HANDLER:
flash_bdev_irq_handler();
return 0;
case BDEV_IOCTL_SYNC:
if (flash_flags & FLASH_FLAG_DIRTY) {
flash_flags |= FLASH_FLAG_FORCE_WRITE;
while (flash_flags & FLASH_FLAG_DIRTY) {
NVIC->STIR = FLASH_IRQn;
}
}
return 0;
}
return -MP_EINVAL;
}
static uint8_t *flash_cache_get_addr_for_write(uint32_t flash_addr) {
@ -149,7 +161,7 @@ static uint8_t *flash_cache_get_addr_for_write(uint32_t flash_addr) {
flash_sector_size = FLASH_SECTOR_SIZE_MAX;
}
if (flash_cache_sector_id != flash_sector_id) {
flash_bdev_flush();
flash_bdev_ioctl(BDEV_IOCTL_SYNC, 0);
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;
@ -186,7 +198,7 @@ static uint32_t convert_block_to_flash_addr(uint32_t block) {
return -1;
}
void flash_bdev_irq_handler(void) {
static void flash_bdev_irq_handler(void) {
if (!(flash_flags & FLASH_FLAG_DIRTY)) {
return;
}

View File

@ -25,6 +25,7 @@
*/
#include "py/obj.h"
#include "py/mperrno.h"
#include "systick.h"
#include "led.h"
#include "storage.h"
@ -81,20 +82,26 @@ STATIC const mp_spiflash_config_t spiflash_config = {
STATIC mp_spiflash_t spiflash;
void spi_bdev_init(void) {
int32_t spi_bdev_ioctl(uint32_t op, uint32_t arg) {
(void)arg;
switch (op) {
case BDEV_IOCTL_INIT:
spiflash.config = &spiflash_config;
mp_spiflash_init(&spiflash);
flash_tick_counter_last_write = 0;
}
return 0;
void spi_bdev_irq_handler(void) {
case BDEV_IOCTL_NUM_BLOCKS:
return MICROPY_HW_SPIFLASH_SIZE_BITS / 8 / FLASH_BLOCK_SIZE;
case BDEV_IOCTL_IRQ_HANDLER:
if ((spiflash.flags & 1) && sys_tick_has_passed(flash_tick_counter_last_write, 1000)) {
mp_spiflash_flush(&spiflash);
led_state(PYB_LED_RED, 0); // indicate a clean cache with LED off
}
}
return 0;
void spi_bdev_flush(void) {
case BDEV_IOCTL_SYNC:
if (spiflash.flags & 1) {
// we must disable USB irqs to prevent MSC contention with SPI flash
uint32_t basepri = raise_irq_pri(IRQ_PRI_OTG_FS);
@ -102,6 +109,9 @@ void spi_bdev_flush(void) {
led_state(PYB_LED_RED, 0); // indicate a clean cache with LED off
restore_irq_pri(basepri);
}
return 0;
}
return -MP_EINVAL;
}
int spi_bdev_readblocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {

View File

@ -37,20 +37,14 @@
#if defined(MICROPY_HW_SPIFLASH_SIZE_BITS)
// Use external SPI flash as the storage medium
#define BDEV_NUM_BLOCKS (MICROPY_HW_SPIFLASH_SIZE_BITS / 8 / FLASH_BLOCK_SIZE)
#define BDEV_INIT spi_bdev_init
#define BDEV_IRQ_HANDLER spi_bdev_irq_handler
#define BDEV_FLUSH spi_bdev_flush
#define BDEV_IOCTL spi_bdev_ioctl
#define BDEV_READBLOCKS spi_bdev_readblocks
#define BDEV_WRITEBLOCKS spi_bdev_writeblocks
#else
// Use internal flash as the storage medium
#define BDEV_NUM_BLOCKS flash_bdev_num_blocks()
#define BDEV_INIT flash_bdev_init
#define BDEV_IRQ_HANDLER flash_bdev_irq_handler
#define BDEV_FLUSH flash_bdev_flush
#define BDEV_IOCTL flash_bdev_ioctl
#define BDEV_READBLOCK flash_bdev_readblock
#define BDEV_WRITEBLOCK flash_bdev_writeblock
@ -64,15 +58,13 @@ void storage_init(void) {
if (!storage_is_initialised) {
storage_is_initialised = true;
BDEV_INIT();
BDEV_IOCTL(BDEV_IOCTL_INIT, 0);
#if defined(BDEV_IRQ_HANDLER)
// Enable the flash IRQ, which is used to also call our storage IRQ handler
// It needs to go at a higher priority than all those components that rely on
// the flash storage (eg higher than USB MSC).
HAL_NVIC_SetPriority(FLASH_IRQn, IRQ_PRI_FLASH, IRQ_SUBPRI_FLASH);
HAL_NVIC_EnableIRQ(FLASH_IRQn);
#endif
}
}
@ -81,19 +73,15 @@ uint32_t storage_get_block_size(void) {
}
uint32_t storage_get_block_count(void) {
return FLASH_PART1_START_BLOCK + BDEV_NUM_BLOCKS;
return FLASH_PART1_START_BLOCK + BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0);
}
void storage_irq_handler(void) {
#if defined(BDEV_IRQ_HANDLER)
BDEV_IRQ_HANDLER();
#endif
BDEV_IOCTL(BDEV_IOCTL_IRQ_HANDLER, 0);
}
void storage_flush(void) {
#if defined(BDEV_FLUSH)
BDEV_FLUSH();
#endif
BDEV_IOCTL(BDEV_IOCTL_SYNC, 0);
}
static void build_partition(uint8_t *buf, int boot, int type, uint32_t start_block, uint32_t num_blocks) {
@ -141,7 +129,7 @@ bool storage_read_block(uint8_t *dest, uint32_t block) {
dest[i] = 0;
}
build_partition(dest + 446, 0, 0x01 /* FAT12 */, FLASH_PART1_START_BLOCK, BDEV_NUM_BLOCKS);
build_partition(dest + 446, 0, 0x01 /* FAT12 */, FLASH_PART1_START_BLOCK, BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0));
build_partition(dest + 462, 0, 0, 0, 0);
build_partition(dest + 478, 0, 0, 0, 0);
build_partition(dest + 494, 0, 0, 0, 0);
@ -152,7 +140,7 @@ bool storage_read_block(uint8_t *dest, uint32_t block) {
return true;
#if defined(BDEV_READBLOCK)
} else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + BDEV_NUM_BLOCKS) {
} else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) {
return BDEV_READBLOCK(dest, block - FLASH_PART1_START_BLOCK);
#endif
} else {
@ -166,7 +154,7 @@ bool storage_write_block(const uint8_t *src, uint32_t block) {
// can't write MBR, but pretend we did
return true;
#if defined(BDEV_WRITEBLOCK)
} else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + BDEV_NUM_BLOCKS) {
} else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) {
return BDEV_WRITEBLOCK(src, block - FLASH_PART1_START_BLOCK);
#endif
} else {
@ -176,7 +164,7 @@ bool storage_write_block(const uint8_t *src, uint32_t block) {
mp_uint_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
#if defined(BDEV_READBLOCKS)
if (FLASH_PART1_START_BLOCK <= block_num && block_num + num_blocks <= FLASH_PART1_START_BLOCK + BDEV_NUM_BLOCKS) {
if (FLASH_PART1_START_BLOCK <= block_num && block_num + num_blocks <= FLASH_PART1_START_BLOCK + BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) {
return BDEV_READBLOCKS(dest, block_num - FLASH_PART1_START_BLOCK, num_blocks);
}
#endif
@ -191,7 +179,7 @@ mp_uint_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_bl
mp_uint_t storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
#if defined(BDEV_WRITEBLOCKS)
if (FLASH_PART1_START_BLOCK <= block_num && block_num + num_blocks <= FLASH_PART1_START_BLOCK + BDEV_NUM_BLOCKS) {
if (FLASH_PART1_START_BLOCK <= block_num && block_num + num_blocks <= FLASH_PART1_START_BLOCK + BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) {
return BDEV_WRITEBLOCKS(src, block_num - FLASH_PART1_START_BLOCK, num_blocks);
}
#endif

View File

@ -31,6 +31,14 @@
#define STORAGE_SYSTICK_MASK (0x1ff) // 512ms
#define STORAGE_IDLE_TICK(tick) (((tick) & STORAGE_SYSTICK_MASK) == 2)
// Try to match Python-level VFS block protocol where possible for these constants
enum {
BDEV_IOCTL_INIT = 1,
BDEV_IOCTL_SYNC = 3,
BDEV_IOCTL_NUM_BLOCKS = 4,
BDEV_IOCTL_IRQ_HANDLER = 6,
};
void storage_init(void);
uint32_t storage_get_block_size(void);
uint32_t storage_get_block_count(void);
@ -43,16 +51,11 @@ bool storage_write_block(const uint8_t *src, uint32_t block);
mp_uint_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks);
mp_uint_t storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks);
uint32_t flash_bdev_num_blocks(void);
void flash_bdev_init(void);
void flash_bdev_irq_handler(void);
void flash_bdev_flush(void);
int32_t flash_bdev_ioctl(uint32_t op, uint32_t arg);
bool flash_bdev_readblock(uint8_t *dest, uint32_t block);
bool flash_bdev_writeblock(const uint8_t *src, uint32_t block);
void spi_bdev_init(void);
void spi_bdev_irq_handler(void);
void spi_bdev_flush(void);
int32_t spi_bdev_ioctl(uint32_t op, uint32_t arg);
int spi_bdev_readblocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks);
int spi_bdev_writeblocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks);