SPIの基本

  SPI:高速同期シリアルポート。これは、標準的な4線式同期シリアル双方向バスです。

  名前が示唆するようSPIは、英語のシリアル・ペリフェラル・インターフェースの略でシリアル・ペリフェラル・インターフェースモトローラは最初のプロセッサのそのMC68HCXXファミリに定義されています。SPIインターフェースは、主EEPROM、FLASH、に使用されているリアルタイムクロック、AD変換器、及びデジタル信号プロセッサとデコーダとの間のデジタル信号。SPIは、高速、全二重、同期通信バスであり、PCBレイアウト上のスペースを節約しながら、ICへの保存、チップのピンにのみ4本のワイヤを占有し、利便性、n個この簡単に使用できる機能のうち、今より多くのチップは、この統合通信プロトコルなどAT91RM9200などを、。

  SPIバスシステムが同期してシリアル・ペリフェラル・インターフェース MCUと周辺機器の様々な行うことができ、情報を交換するためにシリアルに通信します。周辺FLASHRAM、ネットワークコントローラ、LCDディスプレイドライバ、A / D変換器等MCUを提供しました。SPIバスシステムに直接それは一般的に4つのラインに使用されるインターフェイス規格の周辺機器のそれぞれの製造業者の多様を指示することができる:MISOからシリアルクロックライン(SCLK)、ホスト入力/出力データ線を、内/スレーブをマスタデータ線MOSIとアクティブロースレーブ選択線SS(いくつかの割り込みラインINTとのSPIインタフェースチップは、何のSPIインターフェースチップの出力マスタ/スレーブ入力データ線MOSIは存在しません)。

  SPI通信原理は、一方向伝送のために(事実3において、このデバイスの主作業モードからその方法は、一般的にマスタと1つまたは複数のスレーブデバイスを有する、単純であり、少なくとも4本のワイヤ月それは)半二重モードです。すべては総SPIデバイスに基づいていますSDI(データ入力)、SDO(あるデータ出力)、SCLK(クロック)、CS(チップセレクト)。

(SPIバスマスター出力/スレーブ入力)でMOSI-SPIバスのマスタアウト/スレーブ。

MISO-SPIバスマスター入力/スレーブ出力(SPIバスマスター入力/スレーブ出力)。

マスタデバイスによって生成SCLK-クロック信号;

SSと呼ばれるこのピンをマスター制御(チップ選択)によって、デバイスからの信号を有効にし、いくつかのICピンCS-。

チップが選択されているか否かを前記CS制御、それだけ言うことであるチップセレクト信号をイネーブル信号(高電位または低電位)に予め定められている、操作は、このチップのために有効です。これが可能同じバスSPI上の複数のデバイスを接続することができます。

  通信のための責任3つのラインMOSI、MISO、SCLK。通信は、データ交換を介して行われ、SPIデータはビット単位で送信されることを意味するシリアル通信プロトコルです。SCKクロックによって提供されるパルス、パルスデータ転送に基づくSDI、SDOは、ここで、クロックラインSCLKである、完全です。データ出力は SDOラインを介してクロック立ち上がりエッジまたは立ち下がりエッジを、即座に立ち上がりまたは立ち下がりエッジで読み出されるとき、データが変化します。データ転送が完了すると、入力は同じ原理を使用します。このように、変化(及び1の下縁に沿って)、少なくとも8倍へのクロック信号は、8ビットデータの送信を完了します。

  通信ポイントツーポイントでは、SPIインターフェースは、簡単かつ効率的に対処する操作と全二重通信を必要としません。システム内の複数のスレーブデバイスが、各デバイスは、イネーブル信号とは別に必要とし、ハードウェアは、I2Cよりも幾分複雑です。

 

  

  SPI通信有4种不同的模式,不同的从设备可能在出厂时就配置为某种模式,这是不能改变的;但通信双方必须是工作在同一模式下,所以需要对主设备的SPI模式进行配置。通过CPOL(时钟极性)和CPHA(时钟相位)来设置主设备的通信模式,具体如下: Mode0:CPOL=0,CPHA=0 Mode1:CPOL=0,CPHA=1 Mode2:CPOL=1,CPHA=0 Mode3:CPOL=1,CPHA=1

  时钟极性CPOL用来配置SCLK的电平处于哪种状态时是空闲态或者有效态,时钟相位CPHA用来配置数据采样是在第几个SCLK脉冲边沿: CPOL=0,表示当SCLK=0时处于空闲态,所以有效状态就是SCLK处于高电平时 CPOL=1,表示当SCLK=1时处于空闲态,所以有效状态就是SCLK处于低电平时 CPHA=0,表示数据采样是在第1个边沿,数据发送在第2个边沿 CPHA=1,表示数据采样是在第2个边沿,数据发送在第1个边沿

例如: CPOL=0,CPHA=0:此时空闲态时,SCLK处于低电平,数据采样是在第1个边沿,也就是SCLK由低电平到高电平的跳变,所以数据采样是在上升沿,数据发送是在下降沿。

CPOL=0,CPHA=1:此时空闲态时,SCLK处于低电平,数据发送是在第1个边沿,也就是SCLK由低电平到高电平的跳变,所以数据采样是在下降沿,数据发送是在上升沿。

CPOL=1,CPHA=0:此时空闲态时,SCLK处于高电平,数据采集是在第1个边沿,也就是SCLK由高电平到低电平的跳变,所以数据采集是在下降沿,数据发送是在上升沿。

CPOL=1,CPHA=1:此时空闲态时,SCLK处于高电平,数据发送是在第1个边沿,也就是SCLK由高电平到低电平的跳变,所以数据采集是在上升沿,数据发送是在下降沿。

  

SPI库函数

SPISettings  配置SPI总线端口

Description 描述

The SPISettings object is used to configure the SPI port for your SPI device. All 3 parameters are combined to a single SPISettings object, which is given to SPI.beginTransaction().

When all of your settings are constants, SPISettings should be used directly in SPI.beginTransaction(). See the syntax section below. For constants, this syntax results in smaller and faster code.

If any of your settings are variables, you may create a SPISettings object to hold the 3 settings. Then you can give the object name to SPI.beginTransaction(). Creating a named SPISettings object may be more efficient when your settings are not constants, especially if the maximum speed is a variable computed or configured, rather than a number you type directly into your sketch.

Syntax  语法

SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0)) Note:Best if all 3 settings are constants

SPISettings mySettting(speedMaximum, dataOrder, dataMode) Note:Best when any setting is a variable''

Parameters  参数

speedMaximum: The maximum speed of communication. For a SPI chip rated up to 20 MHz, use 20000000.

dataOrder: MSBFIRST or LSBFIRST

dataMode : SPI_MODE0, SPI_MODE1, SPI_MODE2, or SPI_MODE3  四种数据传送模式

Returns  None 返回

 

begin()  初始化SPI总线的参数

Description

Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.

Syntax  SPI.begin()

Parameters  None

Returns  None

 

end()  关闭SPI总线

Description

Disables the SPI bus (leaving pin modes unchanged).

Syntax  SPI.end()

Parameters  None

Returns  None

 

beginTransaction()  开始使用SPI总线

Description

Initializes the SPI bus using the defined SPISettings.

Syntax

SPI.beginTransaction(mySettings);

Parameters

mySettings: the chosen settings according to SPISettings.

Returns None.

 

endTransaction()  停用SPI总线

Description

Stop using the SPI bus. Normally this is called after de-asserting the chip select, to allow other libraries to use the SPI bus.

Syntax

SPI.endTransaction()

Parameters None.

Returns None.

 

setBitOrder()  设置传输数据时的先后顺序

Description 

This function should not be used in new projects. Use SPISettings with SPI.beginTransaction() to configure SPI parameters.

Sets the order of the bits shifted out of and into the SPI bus, either LSBFIRST (least-significant bit first) or MSBFIRST (most-significant bit first).

Syntax

SPI.setBitOrder(order)

Parameters

order: either LSBFIRST or MSBFIRST

Returns None

 

setClockDivider()  设置时钟分频器

Description

This function should not be used in new projects. Use SPISettings with SPI.beginTransaction() to configure SPI parameters.

Sets the SPI clock divider relative to the system clock. On AVR based boards, the dividers available are 2, 4, 8, 16, 32, 64 or 128. The default setting is SPI_CLOCK_DIV4, which sets the SPI clock to one-quarter the frequency of the system clock (4 Mhz for the boards at 16 MHz).

Arduino DueOn the Due, the system clock can be divided by values from 1 to 255. The default value is 21, which sets the clock to 4 MHz like other Arduino boards.

Syntax

SPI.setClockDivider(divider)

Parameters

Returns  None

 

setDataMode() 设置数据模式,即时钟的极性和相位

Description

This function should not be used in new projects. Use SPISettings with SPI.beginTransaction() to configure SPI parameters.

Sets the SPI data mode: that is, clock polarity and phase. See the Wikipedia article on SPI for details.

Syntax

SPI.setDataMode(mode)

Parameters

Returns  None

 

transfer(), transfer16()  数据传送

Description

SPI transfer is based on a simultaneous send and receive: the received data is returned in receivedVal (or receivedVal16). In case of buffer transfers the received data is stored in the buffer in-place (the old data is replaced with the data received).

Syntax

receivedVal = SPI.transfer(val) receivedVal16 = SPI.transfer16(val16) SPI.transfer(buffer, size)

Parameters

val: the byte to send out over the bus val16: the two bytes variable to send out over the bus buffer: the array of data to be transferred

Returns   the received data

 

usingInterrupt()   在中断中做SPI处理

Description

If your program will perform SPI transactions within an interrupt, call this function to register the interrupt number or name with the SPI library. This allows SPI.beginTransaction() to prevent usage conflicts. Note that the interrupt specified in the call to usingInterrupt() will be disabled on a call to beginTransaction() and re-enabled in endTransaction().

Syntax

SPI.usingInterrupt(interruptNumber)

Parameters

interruptNumber: the associated interrupt number.

Returns  None.