SPI External Flash

This sample demonstrates how the chip communicates with external flash by SPI. In this example, SPI is configured as a master. Users can write and read external flash with SPI in three different modes: polling, interrupt, and DMA.

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

Connect P5_1 (master CS) to CS of external flash, connect P5_0 (master SCK) to SCK of external flash, connect P5_3 (master MISO) to MISO of external flash, and connect P5_2 (master MOSI) to MOSI of external flash, connect P5_5 (master HOLD) to HOLD of external flash.

The hardware connection of SPI sample code is shown in the figure below.

../../../_images/SPI_Demo_13_1_Hardware_Connection_Diagram.png

SPI Sample Code Hardware Connection Diagram

Configurations

  1. The following macros can be configured to modify pin definitions.

    • #define FLASH_CS P5_1

    • #define FLASH_SCK P5_0

    • #define FLASH_MOSI P5_2

    • #define FLASH_MISO P5_3

    • #define FLASH_HOLD P5_5

  2. The entry function is as follows, call this function in main() to run this sample code. For more details, please refer to the Initialization.

    ext_flash_spi_test_code();
    

Building and Downloading

For building and downloading, please refer to the Building and Downloading.

Experimental Verification

  1. Press the Reset button on the EVB.

  2. Test the page program and read functions of external flash by SPI polling mode. If test success, print the log in Debug Analyzer.

    ext_flash_spi_test_polling_mode: success
    flash_test_task: test_case 0 success
    
  3. Test the page program and read functions of external flash by SPI interrupt mode. If test success, print the log in Debug Analyzer.

    flash_test_task: test_case 1 success
    
  4. Test the page program and read functions of external flash by SPI DMA mode. If test success, print the log in Debug Analyzer.

    flash_test_task: test_case 2 success
    
  5. Test the erase and read functions of external flash by SPI polling mode. If test success, print the log in Debug Analyzer.

    flash_test_task: test_case 3 success
    

Code Overview

Source Code Directory

  • For project directory, please refer to Source Code Directory.

  • Source code directory: sdk\src\sample\io_demo\spi\external_flash\external_flash_test.c .

Initialization of External Flash

Users can refer to the following function to initialize external flash. In this function, it will initialize SPI and reset external flash.

void ext_flash_spi_init(void)

Functional Implementation

Write and Read External Flash by Polling

Call ext_flash_spi_test_polling_mode to write and read external flash by polling mode.

  1. Call ext_flash_spi_erase to erase external flash.

  2. Call ext_flash_spi_page_program to write the sendBuf to external flash by polling mode.

  3. Call ext_flash_spi_read to read external flash by polling mode, the data received in the array recvBuf.

  4. Compare sendBuf and recvBuf to verify that the read and write functions.

Write and Read External Flash by Interrupt

  1. Call ext_flash_spi_erase to erase external flash.

  2. Call ext_flash_spi_page_program_by_interrupt to write the sendBuf to external flash by interrupt mode.

  3. Call ext_flash_spi_read_by_interrupt to read external flash by interrupt mode, the data received in the array recvBuf.

  4. Compare sendBuf and recvBuf to verify that the read and write functions.

Write and Read External Flash by DMA

  1. Call ext_flash_spi_erase to erase external flash.

  2. Call ext_flash_spi_page_program_by_dma to write the sendBuf to external flash by DMA mode.

  3. Call ext_flash_spi_read_by_dma to read external flash by DMA mode, the data received in the array recvBuf.

  4. Compare sendBuf and recvBuf to verify that the read and write functions.

Erase and Read External Flash

  1. Call ext_flash_spi_erase_chip to erase full external flash chip.

  2. Call ext_flash_spi_read to read external flash by polling mode, the data received in the array recvBuf.

  3. Compare 0xFF and recvBuf to verify that the erase and read functions.