As we know that SPI has 4 wires to transmit and receive data between a mast and a slave. Please refer the wiki page, SPI Bus for the detail information first.
The information is not enough for us to write a SPI program to drive a master device to transmit/receive data to/from a slave device. I'm interested the below picture of the SPI Bus wiki page because it can help us for writing the program.
We can imagine that there is a ring composed of MOSI, MISO, and two shift registers. A basic communication program is to send a request and to wait for a response. For SPI, we send a request to a slave via MOSI and wait for a response from a slave via MISO.
Read-Id Command of Flash Part
The read-id command of the flash part, W25Q80DV is an example because I consider it is the simplest command for programming.
We want to write a program to get flash part id. The program transmit 9F to the flash part via MOSI and receive EF-40-14 from the flash part via MISO.
MOSI: 9F // Transmit Read ID command (9F) to get ID information
MISO: EF, 40, 14 // Receive ID information.
How do we write the program?
Transmit Data
Because SPI is a ring, when the master transmits a command with 1 byte (e.g., 9F) to the slave (flash part), the master also receives a dummy 1 byte from slave. The command is stored in TX FIFO and the dummy 1 byte is stored in RX FIFO. Therefore when the master transmits the command, it must clear dummy bytes in RX FIFO.
Receive Data
Please remember that SPI is a ring. If the master wants to receive data (e.g., id information with 3 bytes) from the slave (e.g., flash part), the master must send dummy data (e.g., 3 zero bytes) to push the slave to response data (e.g., EF-40-14.)
TX FIFO and RX FIFO
The master usually has 2 FIFOs. TX FIFO is to transmit data to the shift register and RX FIFO is to receive data from the shift register. I'll have another page SPI Host Controller and SPI Device Programming to describe the topic.
-Count
No comments:
Post a Comment