Skip to content

Commit

Permalink
Add support for onboard SPI flash
Browse files Browse the repository at this point in the history
  • Loading branch information
mcauser committed Nov 23, 2019
1 parent 9069d3b commit fc1baf2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
24 changes: 24 additions & 0 deletions bdev.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "storage.h"

// External SPI flash uses standard SPI interface

STATIC const mp_soft_spi_obj_t soft_spi_bus = {
.delay_half = MICROPY_HW_SOFTSPI_MIN_DELAY,
.polarity = 0,
.phase = 0,
.sck = MICROPY_HW_SPIFLASH_SCK,
.mosi = MICROPY_HW_SPIFLASH_MOSI,
.miso = MICROPY_HW_SPIFLASH_MISO,
};

STATIC mp_spiflash_cache_t spi_bdev_cache;

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,
.bus.u_spi.proto = &mp_soft_spi_proto,
.cache = &spi_bdev_cache,
};

spi_bdev_t spi_bdev;
7 changes: 7 additions & 0 deletions board_init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "py/mphal.h"

void Black_F407ZG_board_early_init(void) {
// set SPI flash CS pin high
mp_hal_pin_output(pin_B14);
mp_hal_pin_write(pin_B14, 1);
}
30 changes: 30 additions & 0 deletions mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#define MICROPY_HW_BOARD_NAME "MCUDEV STM32F407ZG"
#define MICROPY_HW_MCU_NAME "STM32F407ZG"
#define MICROPY_HW_FLASH_FS_LABEL "BlackF407ZG"

// 1 = use internal flash (1024 KByte)
// 0 = use onboard SPI flash (2 MByte) Winbond W25Q16
#define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (1)

#define MICROPY_HW_HAS_SWITCH (1) // has 2 buttons KEY0=PE4, KEY1=PE3
#define MICROPY_HW_HAS_FLASH (1)
Expand Down Expand Up @@ -127,6 +132,31 @@
#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_low(pin))
#define MICROPY_HW_LED_OFF(pin) (mp_hal_pin_high(pin))

// If using onboard SPI flash
#if !MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE

// Winbond W25Q16 SPI Flash = 16 Mbit (2 MByte)
#define MICROPY_HW_SPIFLASH_SIZE_BITS (16 * 1024 * 1024)
#define MICROPY_HW_SPIFLASH_CS (pin_B14)
#define MICROPY_HW_SPIFLASH_SCK (pin_B3)
#define MICROPY_HW_SPIFLASH_MISO (pin_B4)
#define MICROPY_HW_SPIFLASH_MOSI (pin_B5)

#define MICROPY_BOARD_EARLY_INIT Black_F407ZG_board_early_init
void Black_F407ZG_board_early_init(void);

extern const struct _mp_spiflash_config_t spiflash_config;
extern struct _spi_bdev_t spi_bdev;
#define MICROPY_HW_BDEV_IOCTL(op, arg) ( \
(op) == BDEV_IOCTL_NUM_BLOCKS ? (MICROPY_HW_SPIFLASH_SIZE_BITS / 8 / FLASH_BLOCK_SIZE) : \
(op) == BDEV_IOCTL_INIT ? spi_bdev_ioctl(&spi_bdev, (op), (uint32_t)&spiflash_config) : \
spi_bdev_ioctl(&spi_bdev, (op), (arg)) \
)
#define MICROPY_HW_BDEV_READBLOCKS(dest, bl, n) spi_bdev_readblocks(&spi_bdev, (dest), (bl), (n))
#define MICROPY_HW_BDEV_WRITEBLOCKS(src, bl, n) spi_bdev_writeblocks(&spi_bdev, (src), (bl), (n))

#endif

// SD card detect switch
// #define MICROPY_HW_SDCARD_DETECT_PIN (pin_A8) // nope
// #define MICROPY_HW_SDCARD_DETECT_PULL (GPIO_PULLUP)
Expand Down

0 comments on commit fc1baf2

Please sign in to comment.