hpm5100_spi_flash_rw.ino

HPMicro HPM5100: 400 MHz RISC-V MCU with Integrated Analog Peripherals · File : hpm5100_spi_flash_rw.ino · Language : C · Use case : Écrire et lire des données sur une mémoire Flash externe via l'interface SPI.

Demonstrates sending low-level commands (Write Enable, Page Program) to an external SPI Flash.

The code handles manual Chip Select (CS) management and byte-by-byte transactions. The SDK's board_init_spi_flash() function typically configures the SPI pins and clock. Check the timings of the specific Flash used (tWR, tPP).

hpm5100_spi_flash_rw.ino

#include "board.h"
#include "hpm_spi_drv.h"
#include "hpm_clock_drv.h"

#define SPI_PORT HPM_SPI0
#define FLASH_CMD_WRITE_ENABLE 0x06
#define FLASH_CMD_PAGE_PROGRAM 0x02
#define FLASH_CMD_READ 0x03

uint8_t spi_transfer_byte(SPI_Type *ptr, uint8_t tx_data)
{
    uint8_t rx_data;
    while (!spi_get_tx_fifo_status(ptr, spi_tx_fifo_not_full));
    spi_write_data(ptr, tx_data);
    while (!spi_get_rx_fifo_status(ptr, spi_rx_fifo_not_empty));
    rx_data = spi_read_data(ptr);
    return rx_data;
}

void spi_write_flash(uint32_t addr, uint8_t *data, uint32_t len)
{
    // Simplified: Assumes CS handling and Flash initialization done in board_init
    spi_select_slave(SPI_PORT, 0, true);
    spi_transfer_byte(SPI_PORT, FLASH_CMD_WRITE_ENABLE);
    spi_select_slave(SPI_PORT, 0, false);

    spi_select_slave(SPI_PORT, 0, true);
    spi_transfer_byte(SPI_PORT, FLASH_CMD_PAGE_PROGRAM);
    spi_transfer_byte(SPI_PORT, (addr >> 16) & 0xFF);
    spi_transfer_byte(SPI_PORT, (addr >> 8) & 0xFF);
    spi_transfer_byte(SPI_PORT, addr & 0xFF);
    for (uint32_t i = 0; i < len; i++) {
        spi_transfer_byte(SPI_PORT, data[i]);
    }
    spi_select_slave(SPI_PORT, 0, false);
}

void main(void)
{
    uint8_t write_data[] = "HPM5100";
    uint8_t read_data[10];
    board_init();
    // board_init_spi_flash() assumed from board package
    
    spi_write_flash(0x000000, write_data, sizeof(write_data));
    // Read logic similar to write
    while (1) {
        board_delay_ms(1000);
    }
}

← Back to the board