Documente Academic
Documente Profesional
Documente Cultură
This application note describes the SPI communications system on the Desktop NanoBoard NB2DSK01. It covers multiplexed access to the SPI bus and the various SPI-based resources attached to the bus. Interfacing to the SPI bus from an FPGA design is also covered in detail.
After JTAG, the second most important communications system on Altium's Desktop NanoBoard NB2DSK01 involves communications with devices over the Serial Peripheral Interface (SPI) bus. This document takes a closer look at how the SPI bus protocol is used on the NB2DSK01 to facilitate communications with various SPI-compatible resources in the system. For detailed information on the Desktop NanoBoard NB2DSK01, refer to the document TR0143 Technical Reference Manual for Altium's Desktop NanoBoard NB2DSK01. For more information on specifically using an SPI Flash memory device, refer to the document AP0162 Utilizing the SPI Flash Memory on the Desktop NanoBoard NB2DSK01.
SPI Background
Before discussing SPI communications in terms of the NB2DSK01, it is worth taking a quick look at the SPI bus itself, and the constituent signals involved. The SPI bus is a full-duplex, synchronous serial data link, that provides an efficient, low-cost communications solution. Like I2C, SPI provides good support for communications with low-speed peripheral devices, though is capable of higher data speeds than I2C. At the simplest level, SPI communications consists of a single bus master connected to a single bus slave (Figure 1). The bus itself involves four signals: SCLK Serial Clock, generated by the master. DIN Serial Data Input. This is the data written to the slave SPI-compatible device. Data is shifted into the device on the rising edge of the serial clock. DOUT Serial Data Output. This is the data read from the slave SPI-compatible device. Data is shifted out of the device on the falling edge of the serial clock. CS - Chip Select for the slave SPI-compatible device. When the master activates this signal (active Low), the slave device is enabled for communications.
Master SPI Device
SCLK DIN
DOUT CS
CS1
CS2
Note: DIN and DOUT refer to direction from the slave device perspective. These signals are often seen in literature as MOSI and MISO respectively. For each additional slave device added to the communications system, the SCLK, DOUT and DIN lines are shared. The bus master simply needs to generate a separate device select line for each slave (Figure 2). By activating a single select line and keeping all others inactive, the master is able to communicate with the required slave over the shared SPI bus.
AP0163 (v2.0) May 15, 2008
CS3
Providing the required SPI bus arbitration between the masters, and access to the SPI devices, is the NB2DSK01's SPI Controller. The Controller, which is part of the NanoBoard firmware, plays the role of multiplexer/router determining which master has access to the SPI bus and which SPI slave device is selected for communications. Figure 3 illustrates the SPI communications system in-place for the NB2DSK01.
Altium Designer
NB2DSK01 Motherboard
Flash Memory (Embedded) Flash Memory (Embedded/ FPGA Boot)
FLASH2_CS_N
FLASH1_CS_N
Board Clock
CLKGEN_CS_N
RTC
RTC_CS_N
TFT_SPI_CS_N
SPI Controller T S K 3 0 0 0 A
Peripheral Board A
SPI Device 1 SPI Device 2 EXTSPI_CSA_N0 EXTSPI_CSA_N1
Device Selection
Daughter Board
F P G A
Peripheral Board C
Mode SPI FLASH2_CS_N FLASH1_CS_N CLKGEN_CS_N RTC_CS_N TFT_SPI_CS_N EXTSPI_CSA_N[1..0] EXTSPI_CSB_N[1..0] EXTSPI_CSC_N[1..0] SPI Device 1 SPI Device 2 EXTSPI_CSC_N0 EXTSPI_CSC_N1
DAU_SPI_DOUT DAU_SPI_DIN
DOUT DIN SPI Address Register & Bus Routing SCLK SEL ADDR[4..0] To SPI Bus and Device Multiplexers
During operation, the daughter board FPGA design communicates with the NB2DSK01's SPI Controller to establish a path between the design and a specific motherboard/peripheral board SPI device. Communications with the target device is a twostage process: Firstly, the address of the target SPI device must be written to an address register within the SPI Controller and bus ownership must be requested. Secondly, once ownership of the SPI bus is granted, the SPI lines from the daughter board FPGA must be routed to the external SPI bus itself, and the select line for the required slave device made active.
In terms of communication with the SPI Controller, the FPGA design has two modes of access either accessing and writing to the Controller itself, or communicating directly with a selected target device over the routed SPI bus. The following sections look more closely at how the daughter board FPGA can switch between these two modes, obtain ownership of the SPI bus, select a device for communications, and release the bus after it has finished with the slave device.
Mode of Access
As mentioned previously, the design running in the daughter board FPGA can either access the SPI Controller itself, or the SPI bus directly. These two modes of access are determined by the level of the DAU_SPI_MODE signal: DAU_SPI_MODE = High communicate with the SPI Controller and, more specifically, an 8-bit SPI device address register therein. DAU_SPI_MODE = Low communicate directly with a slave device over the SPI bus.
Ownership will only be granted provided the external SPI bus is currently not being used. This allows any current transmission on the bus to terminate naturally without any errors. If multiple requests for the bus are present, the following order of ownership hierarchy is adhered to: Altium Designer highest priority Daughter Board Firmware (TSK3000A)
spi_send_wait_receive(base,0x80 | device); if (spi_send_wait_receive(base,0x80 | device) == 0) { spi_mode_lo spi_cs_lo return true; } else { return false; } } In the embedded software, the call to this routine could be in the form of a while loop, whereby the routine would be repeated (request bit set, target address loaded) until it returns True, meaning that Altium Designer or the firmware have released the SPI bus. For example: . . . while (!nanoboard_spi_open(Base_SPI, Device_Address)); . . . (base); (base);
DAU_SPI_CLK
DAU_SPI_DOUT
DAU_SPI_DIN
DAU_SPI_MODE
DAU_SPI_SEL
Figure 5. Failed request to own the bus. The DAU_SPI_DIN line is still High after writing to the SPI address register, meaning the bus is not available.
DAU_SPI_CLK
DAU_SPI_DOUT
DAU_SPI_DIN
DAU_SPI_MODE
DAU_SPI_SEL
Figure 6. Successful request to own the bus. The DAU_SPI_DIN line is Low after writing to the SPI address register, meaning the bus is available. The DAU_SPI_MODE and DAU_SPI_SEL lines are then taken Low, to enable communications with the addressed device.
For devices that can be accessed from a daughter board FPGA design, the unique device addresses and associated chip select signals are summarized in Table 1.
Table 1. SPI device addresses and chip select signals.
Target Device Flash Memory (Embedded/FPGA Boot) Flash Memory (Embedded) Board Clock Touch Screen Digitizer Real Time Clock (RTC) Peripheral Board A (Device 1) Peripheral Board A (Device 2) Peripheral Board B (Device 1) Peripheral Board B (Device 2) Peripheral Board C (Device 1) Peripheral Board C (Device 2)
00010 00100 00101 00110 00111 01000 01001 01010 01011 01100
0x02 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C
FLASH2_CS_N CLKGEN_CS_N TFT_SPI_CS_N RTC_CS_N EXTSPI_CSA_N0 EXTSPI_CSA_N1 EXTSPI_CSB_N0 EXTSPI_CSB_N1 EXTSPI_CSC_N0 EXTSPI_CSC_N1
Component Symbol
Placed from... FPGA NB2DSK01 Port-Plugin integrated library (FPGA NB2DSK01 PortPlugin.IntLib), located in the \Library\Fpga folder of the installation.
TOUCH_SCREEN_DIGITIZER
AUDIO_CODEC_CTRL
FPGA PB01 Port-Plugin integrated library (FPGA PB01 Port-Plugin.IntLib), located in the \Library\Fpga folder of the installation.
Figure 7 illustrates an example of interfacing to the NB2DSK01 motherboard's SPI bus from a schematic-based design that uses a 32-bit processor in this case a TSK3000A. In this example, the design is using one of the SPI Flash memory devices as embedded memory, therefore the SERIALFMEMORY port component has been used to make this use of the SPI bus clearer. A configurable Wishbone Interconnect device (WB_INTERCON) is used to simplify connection and also handle the address mapping taking the 24-bit address line from the processor and mapping it to the 2-bit address line used to drive the intermediate SPI Controller device (SPI_W).
Component Symbol
Description Place this component to include a generic interface to the NB2DSK01 motherboard's SPI bus within your FPGA design.
Example Code
Example software routines used for opening and closing the NanoBoard SPI channel can be found in the following files: llpi_nb_spi.h llpi_nb_spi.c
Example software routines used in communicating with the SPI_W Controller can be found in the following files: llpi_wb_spi.h llpi_wb_spi.c
These files can be found in the \System\Tasking\dsf\llpi\include and \System\Tasking\dsf\llpi\src folders of the Altium Designer installation respectively.
Revision History
Date 27-Nov-2007 15-May-2008 Version No. 1.0 2.0 Revision Initial release Updated for Altium Designer Summer 08.
Software, hardware, documentation and related materials: Copyright 2008 Altium Limited. All Rights Reserved. The material provided with this notice is subject to various forms of national and international intellectual property protection, including but not limited to copyright protection. You have been granted a non-exclusive license to use such material for the purposes stated in the end-user license agreement governing its use. In no event shall you reverse engineer, decompile, duplicate, distribute, create derivative works from or in any way exploit the material licensed to you except as expressly permitted by the governing agreement. Failure to abide by such restrictions may result in severe civil and criminal penalties, including but not limited to fines and imprisonment. Provided, however, that you are permitted to make one archival copy of said materials for back up purposes only, which archival copy may be accessed and used only in the event that the original copy of the materials is inoperable. Altium, Altium Designer, Board Insight, DXP, Innovation Station, LiveDesign, NanoBoard, NanoTalk, OpenBus, P-CAD, SimCode, Situs, TASKING, and Topological Autorouting and their respective logos are trademarks or registered trademarks of Altium Limited or its subsidiaries. All other registered or unregistered trademarks referenced herein are the property of their respective owners and no trademark rights to the same are claimed. v8.0 31/3/08.