Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 613510b

Browse files
committed
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.
1 parent d5336ba commit 613510b

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

drivers/memory/spiflash.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@ STATIC uint8_t buf[SECTOR_SIZE];
4949
void mp_spiflash_init(mp_spiflash_t *self) {
5050
mp_hal_pin_write(self->cs, 1);
5151
mp_hal_pin_output(self->cs);
52-
mp_hal_pin_write(self->spi.sck, 0);
53-
mp_hal_pin_output(self->spi.sck);
54-
mp_hal_pin_output(self->spi.mosi);
55-
mp_hal_pin_input(self->spi.miso);
52+
const mp_machine_spi_p_t *protocol = self->spi->type->protocol;
53+
protocol->init(self->spi, 0, NULL, (mp_map_t*)&mp_const_empty_map);
5654
}
5755

5856
STATIC void mp_spiflash_acquire_bus(mp_spiflash_t *self) {
@@ -66,7 +64,8 @@ STATIC void mp_spiflash_release_bus(mp_spiflash_t *self) {
6664
}
6765

6866
STATIC void mp_spiflash_transfer(mp_spiflash_t *self, size_t len, const uint8_t *src, uint8_t *dest) {
69-
mp_machine_soft_spi_transfer(&self->spi.base, len, src, dest);
67+
const mp_machine_spi_p_t *protocol = self->spi->type->protocol;
68+
protocol->transfer(self->spi, len, src, dest);
7069
}
7170

7271
STATIC int mp_spiflash_wait_sr(mp_spiflash_t *self, uint8_t mask, uint8_t val, uint32_t timeout) {

drivers/memory/spiflash.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030

3131
typedef struct _mp_spiflash_t {
3232
mp_hal_pin_obj_t cs;
33-
// TODO replace with generic SPI object
34-
mp_machine_soft_spi_obj_t spi;
33+
mp_obj_base_t *spi; // object must have protocol pointing to mp_machine_spi_p_t struct
3534
} mp_spiflash_t;
3635

3736
void mp_spiflash_init(mp_spiflash_t *self);

stmhal/storage.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,19 @@ static uint8_t *flash_cache_get_addr_for_read(uint32_t flash_addr) {
176176

177177
static bool flash_is_initialised = false;
178178

179+
STATIC const mp_machine_soft_spi_obj_t spiflash_spi_bus = {
180+
.base = {&mp_machine_soft_spi_type},
181+
.delay_half = MICROPY_PY_MACHINE_SPI_MIN_DELAY,
182+
.polarity = 0,
183+
.phase = 0,
184+
.sck = &MICROPY_HW_SPIFLASH_SCK,
185+
.mosi = &MICROPY_HW_SPIFLASH_MOSI,
186+
.miso = &MICROPY_HW_SPIFLASH_MISO,
187+
};
188+
179189
STATIC const mp_spiflash_t spiflash = {
180190
.cs = &MICROPY_HW_SPIFLASH_CS,
181-
.spi = {
182-
.base = {&mp_machine_soft_spi_type},
183-
.delay_half = MICROPY_PY_MACHINE_SPI_MIN_DELAY,
184-
.polarity = 0,
185-
.phase = 0,
186-
.sck = &MICROPY_HW_SPIFLASH_SCK,
187-
.mosi = &MICROPY_HW_SPIFLASH_MOSI,
188-
.miso = &MICROPY_HW_SPIFLASH_MISO,
189-
},
191+
.spi = (mp_obj_base_t*)&spiflash_spi_bus.base,
190192
};
191193

192194
#endif

0 commit comments

Comments
 (0)