Snippet code for read manufacturer and device ID of W25X32 SPI NOR Flash on NuttX

This simple snippet of code demonstrate how to read the manufacturer and the device ID of a W25X32 SPI Nor flash using NuttX RTOS :

int spi_nor_flash(void)
        uint8_t tx[6], rx[6];

        struct spi_dev_s *spi = up_spiinitialize(0);

        SPI_SETBITS(spi, 8);

        tx[0] = 0x90;
        tx[1] = 0;
        tx[2] = 0;
        tx[3] = 0;
        tx[4] = 0;
        tx[5] = 0;

        kl_gpiowrite(PIN_SPI0_CS, false);
        SPI_EXCHANGE(spi, &tx, &rx, 6);
        kl_gpiowrite(PIN_SPI0_CS, true);

        printf("Manuf. = 0x%02X | Device = 0x%02X\n", rx[4], rx[5]);

        return 0;

This is output I got in the serial (DEBUG_SPI enable):

spi_setfrequency: Frequency 400000->428571
spi_exchange: txbuffer=1ffff9dc rxbuffer=1ffff9e4 nwords=6
Received = 0xFF 0xFF 0xFF 0xFF 0xEF 0x15

Where 0xEF is the Manufacturer ID and 0x15 is the Device ID returned.

Note: You need to define the pins PIN_SPI0_SCK, PIN_SPI0_MISO, PIN_SPI0_MOSI and PIN_SPI0_CS in the file ‘configs/freedom-kl25z/include/board.h’.