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.
SPI Sample Code Hardware Connection Diagram
Configurations
-
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
-
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
Press the Reset button on the EVB.
-
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
-
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
-
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
-
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.candsdk\src\sample\io_demo\spi\external_flash\external_flash.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.
Call
ext_flash_spi_eraseto erase external flash.Call
ext_flash_spi_page_programto write thesendBufto external flash by polling mode.Call
ext_flash_spi_readto read external flash by polling mode, the data received in the arrayrecvBuf.Compare
sendBufandrecvBufto verify that the read and write functions.
Write and Read External Flash by Interrupt
Call
ext_flash_spi_eraseto erase external flash.Call
ext_flash_spi_page_program_by_interruptto write thesendBufto external flash by interrupt mode.Call
ext_flash_spi_read_by_interruptto read external flash by interrupt mode, the data received in the arrayrecvBuf.Compare
sendBufandrecvBufto verify that the read and write functions.
Write and Read External Flash by DMA
Call
ext_flash_spi_eraseto erase external flash.Call
ext_flash_spi_page_program_by_dmato write thesendBufto external flash by DMA mode.Call
ext_flash_spi_read_by_dmato read external flash by DMA mode, the data received in the arrayrecvBuf.Compare
sendBufandrecvBufto verify that the read and write functions.
Erase and Read External Flash
Call
ext_flash_spi_erase_chipto erase full external flash chip.Call
ext_flash_spi_readto read external flash by polling mode, the data received in the arrayrecvBuf.Compare
0xFFandrecvBufto verify that the erase and read functions.