External SPI Flash Example

Does anyone have any example code for adding an external SPI flash to the RSL10? I have seen plenty of discussions on using the internal flash but nothing related to adding additional flash memory, specifically over SPI.

@danielrparsons

Given each Flash module might have slightly different read/write/erase operations, I don’t think this sample would be very general.

Each Flash module should have a datasheet outlining each of the SPI interactions in detail, so implementing the functionality using the SPI CMSIS Driver should not be too difficult.

Thank you for using our community forum!

You’d need to do something like the following.

  • Configure a SPI interface:
		uint32_t pin_cfg  = (RTE_SPI0_DIO_DRIVE | RTE_SPI0_DIO_LPF | RTE_SPI0_DIO_PULL);

		ret = NVMLIB_Interface_Configure(&device, (void*) SPI0,
		                                     SPI_DEFAULT_CFG, 3840U, pin_cfg,
		                                     RTE_SPI0_SCLK_PIN, RTE_SPI0_SSEL_PIN,
		                                     RTE_SPI0_MOSI_PIN, RTE_SPI0_MISO_PIN);
  • Build a library of routines that implement the NVM commands you want using SPI_driver calls such as:

typedef struct _STORAGE_DEVICE_DESCRIPTOR {
    STORAGE_DEVICE_CHARACTRISTIC    *characteristics;       /** Pointer to the device characteristics        */
    HARDWARE_RESOURCES              *hw;                    /** Pointer to the SPI driver instance           */
    uint32_t   *cmd_buffer;            /** Pointer to the scratchpad buffer             */
} STORAGE_DEVICE_DESCRIPTOR;

uint32_t NVMLIB_SPI_StatusRegister_Read(STORAGE_DEVICE_DESCRIPTOR *desc)
{
    uint32_t reg;
    uint32_t data_in[2];

    desc->cmd_buffer[0] = SPI_STORAGE_CMD_RDSR;

    SPI_CS_Assert(desc->hw);
    SPI_Transfer(desc->hw, desc->cmd_buffer, &data_in[0], 2);
    SPI_CS_Deassert(desc->hw);
    _NVMLIB_Wait(1);

    reg = (data_in[0]>>8)&0x000000FFU;

    return reg;
}
 
static inline void _NVMLIB_Wait(const unsigned wait_ms)
{
        Sys_Delay((3*wait_ms)<<11);
}

The actual commands supported, the number of bytes per register, etc. all vary with different memories. Some memories will support dual or quad spi, so the SPI configuration would differ for them. Hopefully this gives you an idea of where to start.

1 Like