Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Anchor11429751142975 SPI BSP Anchor11429761142976Among the most common—and simplest—serial interfaces supported by built-in CPU peripherals is Serial Peripheral Interface (SPI). Four hardware signals connect a defined master (or host) to each slave (or device): a slave select, a clock, a slave input and a slave output. Three of these, all except the slave select, may be shared among all slaves, though hosts often have several SPI controllers to simplify integration and allow simultaneous access to multiple slaves. Serial flash, serial EEPROM and SD/MMC cards are among the many devices which use SPI.anchor

Panel

...

Signal Descriptionanchor
HTML Table
summary
classPlain_Table
Table Row (tr)
Table Cell (td)
Anchor
11429791142979
Table Cell (td)
Anchor
11429811142981
Table Row (tr)
Table Cell (td)
Anchor
11429831142983
titleSPI signals


SignalDescription
SSEL (CS)
Table Cell (td)
1142985
1142985
Slave select
Table Row (tr) Table Cell (td) Anchor11429871142987
SCLK
td
Anchor11429891142989
Clock
Table Row (tr) Table Cell (td) Anchor11429911142991
SO (MISO)
Table Cell (td) Anchor11429931142993
Slave output (master input)
Table Row (tr) Table Cell (td) Anchor11429951142995
SI (MOSI)
Table Cell (td) Anchor11429971142997
Slave input (master output)

...

...



Anchor11430001143000No specification exists for SPI, a condition which invites technological divergence. So though the simplicity of the interface limits variations between implementations, the required transfer unit length, shift direction, clock frequency and clock polarity and phase do vary from device to device. Take as an example Figure C-3 the figure below which gives the bit form of a basic command/response exchange on a typical serial flash. The command and response both divide into 8-bit chunks, the transfer unit for the device. Within these units, the data is transferred from most significant bit (MSB) to least significant bit (LSB), which is the slave’s shift direction. Though not evident from the diagram—the horizontal axis being labeled in clocks rather than time—the slave cannot operate at a frequency higher than 20-MHz. Finally, the clock signal prior to slave select activation is low (clock polarity or CPOL is 0), and data is latched on the rising clock edge (clock phase or CPHA is 0). Together, those are the aspects of SPI communication that may need to be configured: Anchor11430041143004

  • Transfer unit length. A transfer unit is the underlying unit of commands, responses and data. The most common value is eight bits, though slaves commonly require (and masters commonly support) between 8 and 16 bits.

...

  • Shift direction. Either the MSB or LSB of each transfer unit can be the first transmitted on the data line.

...

  • Clock frequency. Limits are usually imposed upon the frequency of the clock signal. Of all variable SPI communication parameters, only this one is explicitly set by the device driver.

...

  • Clock polarity and phase (CPOL and CPHA). SPI communication takes place in any of four modes, depending on the clock phase and clock polarity settings:

...

    • If CPOL = 0, the clock is low when inactive.

...

    • If CPOL = 1, the clock is high when inactive.

...

    • If CPHA = 0, data is “read” on the leading edge of the clock and “changed” on the following edge.

...

    • If CPHA = 1, data is “changed” on the leading edge of the clock and “read” on the leading edge.

...

...

  • The most commonly-supported settings are {CPOL, CPHA} = {0, 0} and {1, 1}.

...

  • Slave select polarity. The “active” level of the slave select may be electrically high or low. Low is ubiquitous, high rare.

...

  • Panel
    titleExample SPI transaction

...

  • Image Added


11430201143020A BSP is required that abstracts a CPU’s SPI peripheral. The port includes one code file named according to the following rubric:anchor

11430211143021FS_DEV_<dev_name>_BSP.C or FS_DEV_<dev_name>_SPI_BSP.c Anchor

11430221143022This file is generally placed with other BSP files in a directory named according to the following rubric: Anchor11430231143023

\Micrium\Software\EvalBoards\<manufacturer>\<board_name>

Anchor11430241143024\<compiler>\BSP\ Anchor11430251143025

Several example ports are included in the µC/FS distribution in files named according to the following rubric: Anchor11430261143026

\Micrium\Software\uC-FS\Examples\BSP\Dev\NAND\<manufacturer>\<cpu_name>

...

\Micrium\Software\uC-FS\Examples\BSP\Dev\NOR\<manufacturer>\<cpu_name>

Anchor11430281143028\Micrium\Software\uC-FS\Examples\BSP\Dev\SD\SPI\<manufacturer>\<cpu_name> Anchor11430291143029

Check all of these directories for ports for a CPU if porting any SPI device; the CPU may be been used with a different type of device, but the port should support another with none or few modifications. Each port must implement the functions to be placed into a FS_DEV_SPI_API structure:anchor

Code Block

...

const
 {
    FSDev_BSP_SPI_Open,

    FSDev_BSP_SPI_Close,

    FSDev_BSP_SPI_Lock,

    FSDev_BSP_SPI_Unlock,

    FSDev_BSP_SPI_Rd,

    FSDev_BSP_SPI_Wr,

    FSDev_BSP_SPI_ChipSelEn,

    FSDev_BSP_SPI_ChipSelDis,

    FSDev_BSP_SPI_SetClkFreq
};
HTML Table
summary
classCode_Listing
Table Row (tr)
Table Cell (td)
rowspan11
Anchor
11430321143032
languagecpp
const  FS_DEV_SPI_API  FSDev_####_BSP_SPI =
{
Anchor
11430331143033
Anchor
11430341143034
Anchor
11430351143035
Anchor
11430361143036
Anchor
11430371143037
Anchor
11430381143038
Anchor
11430391143039
Anchor
11430401143040
Anchor
11430411143041
Anchor
11430421143042
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)

...


};

The functions which must be implemented are listed and described in Table C-4 the table below. SPI is no more than a physical interconnect. The protocol of command-response interchange the master follows to control a slave is specified on a per-slave basis. Control of the chip select (SSEL) is separated from the reading and writing of data to the slave because multiple bus transactions (e.g., a read then a write then another read) are often performed without breaking slave selection. Indeed, some slaves require bus transactions (or “empty” clocks) AFTER the select has been disabled.anchor

Panel

...

Function Description Open()
HTML Table
summary
classPlain_Table
Table Row (tr)
Table Cell (td)
Anchor
11430701143070
Table Cell (td)
Anchor
11430721143072
Table Row (tr)
Table Cell (td)
Anchor
11430741143074
Table Cell (td)
Anchor
11430761143076
titleSPI port functions


FunctionDescription
Open()Open (initialize) hardware for SPI.
Table Row (tr)
Table Cell (td)
Anchor
11430781143078
Close()
Table Cell (td) Anchor11430801143080
Close (uninitialize) hardware for SPI.
Table Row (tr)td
Anchor11430821143082
Lock()
Table Cell (td) Anchor11430841143084
Acquire SPI bus lock.
Table Row (tr) Table Cell (td) Anchor1143086
1143086
Unlock()
Table Cell (td) Anchor11430881143088
Release SPI bus lock.
Table Row (tr) Table Cell (td) Anchor11430901143090
Rd()
Table Cell (td)anchor1143092
1143092
Read from SPI bus.
Table Row (tr) Table Cell (td) Anchor11430941143094
Wr()
Table Cell (td) Anchor11430961143096
Write to SPI bus.
Table Row (tr) Table Cell (td) Anchor11430981143098
ChipSelEn()
Table Cell (td) Anchor11431001143100
Enable device chip select.
Table Row (tr) Table Cell (td)anchor1143102
1143102
ChipSelDis()
Table Cell (td) Anchor11431041143104
Disable device chip select
Table Row (tr) Table Cell (td) Anchor11431061143106
SetClkFreq()
Table Cell (td) Anchor11431081143108
Set SPI clock frequency

...



The first argument of each of these port functions is the device unit number, an identifier unique to each driver/device type—after all, it is the number in the device name. For example, “sd:0:” and “nor:0:” both have unit number 1. If two SPI devices are located on the same SPI bus, either of two approaches can resolve unit number conflicts: Anchor11431131143113

  • Unique unit numbers. All devices on the same bus can use the same SPI BSP if and only if each device has a unique unit number. For example, the SD/MMC card “sd:0:” and serial NOR “nor:1:” require only one BSP.

...

  • Unique SPI BSPs. Devices of different types (e.g., a SD/MMC card and a serial NOR) can have the same unit number if and only if each device uses a separate BSP. For example, the SD/MMC card “sd:0:” and serial “nor:0:” require separate BSPs.