Basic SPI Stream
Demonstrates using the SPI stream API to read and write bulk flash.
- SDK directory:
apps/basic/spi_stream
API Features Demonstrated
Platforms
This app works on:
- AMW004 - Zentri Mackerel evaluation board
- AMW106 - Zentri Moray evaluation board
- AVN4343 - Avnet IoT Starter Kit
- NXP-SCCK - NXP Secure Cloud Connectivity Kit
Requirements and Prerequisites
This app requires a ZentriOS evaluation board. See the Platforms list above.
It also requires connection to a SPI bulk flash device. See Hardware, Bulk Flash.
Description
This app demonstrates using the SPI Stream API to read and write a bulk flash device. It was written for the EN25Q80A SPI flash chip, but can be modified to work with other supported SPI bulk flash chips.
Usage Instructions
Before building the app, check that in the spi_stream.c
source, SPI_STREAM_CS_GPIO
is defined to be the ZentriOS device GPIO connected to the bulk flash device chip select. For example, if you are using the AMW106 with a ASG001 Monitor Backpack, define:
#define SPI_STREAM_CS_GPIO ZOS_GPIO_19
Open a ZentriOS serial terminal to the device. See Getting Started, Opening a ZentriOS Terminal.
The ZAP requires no interaction. It runs once and displays output on the terminal similar to the following:
> Starting SPI Stream Example App ...
Configuring SPI Stream with flags: 0x17, clock: 1000000, CS: 19
Opening SPI Stream
[Opened: 0]
> Reading chip ID ...
Chip ID: 0xC22017
Erasing flash ...
Success
Writing flash ...
Success
Reading flash ...
Buffer: 0x20010a50 (64): RX DATA
Address 0 1 2 3 4 5 6 7 8 9 A B C D E F
20010A50 53 50 49 20 53 74 72 65 61 6D 20 74 65 73 74 20 SPI Stream test
20010A60 77 72 69 74 65 20 64 61 74 61 20 2E 2E 2E 2E 2E write data .....
20010A70 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
20010A80 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
Success
ZAP exited. Reboot to run again.
Implementation
zn_app_init
- Calls zn_spi_stream_configure to configure the SPI stream.
- Calls zn_spi_stream_open top open the stream.
- Calls
read_chip_id
- Calls
erase_flash
- Calls
write_flash
- Calls
read_flash
read_chip_id
Reads the chip ID and logs it.
- calls
read_register
to send the "read chip ID" instruction and get the result - logs the result on the terminal
erase_flash
Sends 'erase flash' instruction:
- calls zn_spi_stream_write to send the "erase flash" instruction
- writes
0
with zn_spi_stream_write to de-assert the chip select - calls zn_rtos_delay_milliseconds to wait for a 100 milliseconds
write_flash
Sends 'write' instruction and then writes dummy date to flash:
- calls
enable_write_latch
and if successful:- calls zn_spi_stream_write to send the 'write' instruction and if successful:
- calls zn_spi_stream_write to write the dummy data
- calls zn_spi_stream_write to send the 'write' instruction and if successful:
- writes
0
with zn_spi_stream_write to de-assert the chip select - calls zn_rtos_delay_milliseconds to wait for 1 millisecond
- calls
wait_while_write_in_progress
to wait until either the status register has bit 0 set, or the wait times out
read_register
- writes the write buffer contents to the SPI stream with zn_spi_stream_write
- reads the response from the SPI stream into the read buffer with zn_spi_stream_read
- writes
0
with zn_spi_stream_write to de-assert the chip select - calls zn_rtos_delay_milliseconds to wait for 1 millisecond
Source
See: