SPI flash support, with xiao nrf52 and portenta-h7

This commit is contained in:
cpq committed 2026-12-03 11:56:57 +00:00
1 parent 57e30ab2e9
commit f02ea9859e
17 files changed
+442 -17

No files matched your search

+61
View File
@@ -15,6 +15,12 @@
#define IMU_PWR PIN(1, 8) // LSM6DS3TR-C IMU power, active high (Sense only)
#define IMU_SCL PIN(0, 27) // IMU I2C bus
#define IMU_SDA PIN(0, 7)
#define FLASH_CS PIN(0, 25) // P25Q16H 2 MB flash, used over plain SPI
#define FLASH_SCK PIN(0, 21) // The board wires it for QSPI: IO0..IO3
#define FLASH_MOSI PIN(0, 20) // IO0
#define FLASH_MISO PIN(0, 24) // IO1
#define FLASH_WP PIN(0, 22) // IO2, active low
#define FLASH_HOLD PIN(0, 23) // IO3, active low
uint64_t hal_get_tick(void);
size_t hal_ram_used(void);
@@ -49,6 +55,61 @@ static inline void hal_gpio_output(uint16_t pin) {
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos);
}
// SPIM2 to the on-board flash, 8 MHz, mode 0. SPIM0 is not used because it
// shares its registers with TWIM0, the IMU's I2C
static inline void hal_spi_init(void) {
hal_gpio_write(FLASH_CS, true);
hal_gpio_write(FLASH_WP, true);
hal_gpio_write(FLASH_HOLD, true);
hal_gpio_write(FLASH_SCK, false);
hal_gpio_output(FLASH_CS);
hal_gpio_output(FLASH_WP);
hal_gpio_output(FLASH_HOLD);
hal_gpio_output(FLASH_SCK); // Its input buffer must stay connected
hal_gpio_output(FLASH_MOSI);
hal_gpio_bank(FLASH_MISO)->PIN_CNF[PINNO(FLASH_MISO)] =
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos);
NRF_SPIM2->PSEL.SCK = FLASH_SCK; // PIN() matches the PSEL port/pin format
NRF_SPIM2->PSEL.MOSI = FLASH_MOSI;
NRF_SPIM2->PSEL.MISO = FLASH_MISO;
NRF_SPIM2->FREQUENCY = SPIM_FREQUENCY_FREQUENCY_M8;
NRF_SPIM2->CONFIG = 0; // MSB first, mode 0
NRF_SPIM2->ORC = 0xff; // Sent when there is nothing to write
NRF_SPIM2->ENABLE = SPIM_ENABLE_ENABLE_Enabled << SPIM_ENABLE_ENABLE_Pos;
}
// struct mg_spi callbacks. spi is unused, there is one bus
static inline void hal_spi_begin(void *spi) {
(void) spi;
hal_gpio_write(FLASH_CS, false);
}
static inline void hal_spi_end(void *spi) {
(void) spi;
hal_gpio_write(FLASH_CS, true);
}
// Sends write and receives into read, len bytes. Either can be NULL. Buffers
// must be in RAM, for EasyDMA
static inline void hal_spi_txn(void *spi, uint8_t *write, uint8_t *read,
size_t len) {
(void) spi;
while (len > 0) {
size_t n = len > 0xffff ? 0xffff : len; // MAXCNT is 16 bits
NRF_SPIM2->TXD.PTR = (uint32_t) write;
NRF_SPIM2->TXD.MAXCNT = write == NULL ? 0 : n;
NRF_SPIM2->RXD.PTR = (uint32_t) read;
NRF_SPIM2->RXD.MAXCNT = read == NULL ? 0 : n;
NRF_SPIM2->EVENTS_END = 0;
NRF_SPIM2->TASKS_START = 1;
while (!NRF_SPIM2->EVENTS_END) (void) 0;
if (write != NULL) write += n;
if (read != NULL) read += n;
len -= n;
}
}
// Vectors above the bootloader, 64 MHz crystal clock, 1 ms SysTick
static inline void hal_system_init(void) {
extern uint32_t __isr_vector[];