drivers/memory/spiflash: Change from hard-coded soft SPI to generic SPI.

The SPI flash driver now supports using an arbitrary SPI object to
communicate with the flash chip, and in particular can use a hardware SPI
peripheral.
crypto-aes
Damien George 5 years ago
parent d5336ba136
commit 613510bce8
  1. 9
      drivers/memory/spiflash.c
  2. 3
      drivers/memory/spiflash.h
  3. 20
      stmhal/storage.c

@ -49,10 +49,8 @@ STATIC uint8_t buf[SECTOR_SIZE];
void mp_spiflash_init(mp_spiflash_t *self) {
mp_hal_pin_write(self->cs, 1);
mp_hal_pin_output(self->cs);
mp_hal_pin_write(self->spi.sck, 0);
mp_hal_pin_output(self->spi.sck);
mp_hal_pin_output(self->spi.mosi);
mp_hal_pin_input(self->spi.miso);
const mp_machine_spi_p_t *protocol = self->spi->type->protocol;
protocol->init(self->spi, 0, NULL, (mp_map_t*)&mp_const_empty_map);
}
STATIC void mp_spiflash_acquire_bus(mp_spiflash_t *self) {
@ -66,7 +64,8 @@ STATIC void mp_spiflash_release_bus(mp_spiflash_t *self) {
}
STATIC void mp_spiflash_transfer(mp_spiflash_t *self, size_t len, const uint8_t *src, uint8_t *dest) {
mp_machine_soft_spi_transfer(&self->spi.base, len, src, dest);
const mp_machine_spi_p_t *protocol = self->spi->type->protocol;
protocol->transfer(self->spi, len, src, dest);
}
STATIC int mp_spiflash_wait_sr(mp_spiflash_t *self, uint8_t mask, uint8_t val, uint32_t timeout) {

@ -30,8 +30,7 @@
typedef struct _mp_spiflash_t {
mp_hal_pin_obj_t cs;
// TODO replace with generic SPI object
mp_machine_soft_spi_obj_t spi;
mp_obj_base_t *spi; // object must have protocol pointing to mp_machine_spi_p_t struct
} mp_spiflash_t;
void mp_spiflash_init(mp_spiflash_t *self);

@ -176,17 +176,19 @@ static uint8_t *flash_cache_get_addr_for_read(uint32_t flash_addr) {
static bool flash_is_initialised = false;
STATIC const mp_machine_soft_spi_obj_t spiflash_spi_bus = {
.base = {&mp_machine_soft_spi_type},
.delay_half = MICROPY_PY_MACHINE_SPI_MIN_DELAY,
.polarity = 0,
.phase = 0,
.sck = &MICROPY_HW_SPIFLASH_SCK,
.mosi = &MICROPY_HW_SPIFLASH_MOSI,
.miso = &MICROPY_HW_SPIFLASH_MISO,
};
STATIC const mp_spiflash_t spiflash = {
.cs = &MICROPY_HW_SPIFLASH_CS,
.spi = {
.base = {&mp_machine_soft_spi_type},
.delay_half = MICROPY_PY_MACHINE_SPI_MIN_DELAY,
.polarity = 0,
.phase = 0,
.sck = &MICROPY_HW_SPIFLASH_SCK,
.mosi = &MICROPY_HW_SPIFLASH_MOSI,
.miso = &MICROPY_HW_SPIFLASH_MISO,
},
.spi = (mp_obj_base_t*)&spiflash_spi_bus.base,
};
#endif

Loading…
Cancel
Save