summaryrefslogtreecommitdiffstatshomepage
path: root/ports/stm32/spibdev.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-03-10 00:22:38 +1100
committerDamien George <damien.p.george@gmail.com>2018-03-10 00:59:43 +1100
commit1803e8ef221c7714e4f663c777be2f8733721f3d (patch)
treeef6f70f729add881c9bd3786bd1147db683a2f76 /ports/stm32/spibdev.c
parent1e4caf0b1ec4dcdf2348e9e0a07a12c9f63f3176 (diff)
downloadmicropython-1803e8ef221c7714e4f663c777be2f8733721f3d.tar.gz
micropython-1803e8ef221c7714e4f663c777be2f8733721f3d.zip
stm32/storage: Make spi_bdev interface take a data pointer as first arg.
This allows a board to have multiple instances of the SPI block device.
Diffstat (limited to 'ports/stm32/spibdev.c')
-rw-r--r--ports/stm32/spibdev.c37
1 files changed, 16 insertions, 21 deletions
diff --git a/ports/stm32/spibdev.c b/ports/stm32/spibdev.c
index 88f2cf51b7..74b400a8fc 100644
--- a/ports/stm32/spibdev.c
+++ b/ports/stm32/spibdev.c
@@ -35,8 +35,6 @@
#include "drivers/memory/spiflash.h"
#include "genhdr/pins.h"
-static uint32_t flash_tick_counter_last_write;
-
#if defined(MICROPY_HW_SPIFLASH_MOSI)
// External SPI flash uses standard SPI interface
@@ -50,7 +48,7 @@ STATIC const mp_soft_spi_obj_t soft_spi_bus = {
.miso = &MICROPY_HW_SPIFLASH_MISO,
};
-STATIC const mp_spiflash_config_t spiflash_config = {
+const mp_spiflash_config_t spiflash_config = {
.bus_kind = MP_SPIFLASH_BUS_SPI,
.bus.u_spi.cs = &MICROPY_HW_SPIFLASH_CS,
.bus.u_spi.data = (void*)&soft_spi_bus,
@@ -72,7 +70,7 @@ STATIC const mp_soft_qspi_obj_t soft_qspi_bus = {
.io3 = &MICROPY_HW_SPIFLASH_IO3,
};
-STATIC const mp_spiflash_config_t spiflash_config = {
+const mp_spiflash_config_t spiflash_config = {
.bus_kind = MP_SPIFLASH_BUS_QSPI,
.bus.u_qspi.data = (void*)&soft_qspi_bus,
.bus.u_qspi.proto = &mp_soft_qspi_proto,
@@ -80,32 +78,29 @@ STATIC const mp_spiflash_config_t spiflash_config = {
#endif
-STATIC mp_spiflash_t spiflash;
-
-int32_t spi_bdev_ioctl(uint32_t op, uint32_t arg) {
- (void)arg;
+int32_t spi_bdev_ioctl(spi_bdev_t *bdev, uint32_t op, uint32_t arg) {
switch (op) {
case BDEV_IOCTL_INIT:
- spiflash.config = &spiflash_config;
- mp_spiflash_init(&spiflash);
- flash_tick_counter_last_write = 0;
+ bdev->spiflash.config = (const mp_spiflash_config_t*)arg;
+ mp_spiflash_init(&bdev->spiflash);
+ bdev->flash_tick_counter_last_write = 0;
return 0;
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);
+ if ((bdev->spiflash.flags & 1) && sys_tick_has_passed(bdev->flash_tick_counter_last_write, 1000)) {
+ mp_spiflash_flush(&bdev->spiflash);
led_state(PYB_LED_RED, 0); // indicate a clean cache with LED off
}
return 0;
case BDEV_IOCTL_SYNC:
- if (spiflash.flags & 1) {
+ if (bdev->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);
- mp_spiflash_flush(&spiflash);
+ mp_spiflash_flush(&bdev->spiflash);
led_state(PYB_LED_RED, 0); // indicate a clean cache with LED off
restore_irq_pri(basepri);
}
@@ -114,22 +109,22 @@ int32_t spi_bdev_ioctl(uint32_t op, uint32_t arg) {
return -MP_EINVAL;
}
-int spi_bdev_readblocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
+int spi_bdev_readblocks(spi_bdev_t *bdev, uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
// we must disable USB irqs to prevent MSC contention with SPI flash
uint32_t basepri = raise_irq_pri(IRQ_PRI_OTG_FS);
- mp_spiflash_read(&spiflash, block_num * FLASH_BLOCK_SIZE, num_blocks * FLASH_BLOCK_SIZE, dest);
+ mp_spiflash_read(&bdev->spiflash, block_num * FLASH_BLOCK_SIZE, num_blocks * FLASH_BLOCK_SIZE, dest);
restore_irq_pri(basepri);
return 0;
}
-int spi_bdev_writeblocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
+int spi_bdev_writeblocks(spi_bdev_t *bdev, const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
// we must disable USB irqs to prevent MSC contention with SPI flash
uint32_t basepri = raise_irq_pri(IRQ_PRI_OTG_FS);
- int ret = mp_spiflash_write(&spiflash, block_num * FLASH_BLOCK_SIZE, num_blocks * FLASH_BLOCK_SIZE, src);
- if (spiflash.flags & 1) {
+ int ret = mp_spiflash_write(&bdev->spiflash, block_num * FLASH_BLOCK_SIZE, num_blocks * FLASH_BLOCK_SIZE, src);
+ if (bdev->spiflash.flags & 1) {
led_state(PYB_LED_RED, 1); // indicate a dirty cache with LED on
- flash_tick_counter_last_write = HAL_GetTick();
+ bdev->flash_tick_counter_last_write = HAL_GetTick();
}
restore_irq_pri(basepri);