Fireduino Buy

Dual-Core Cortex-M3 processor, integrated high-quality audio Codec and WiFi module, have IOT expansion performance, perfectly compatible with the Arduino interface and Arduino IDE programming, and supports FireBlock graphics programming.

Spi

Update time:2018-04-13 Views:6529

Serial Peripheral Interface(SPI)

SPI is a serial peripheral interface (Serial Peripheral Interface), is a high-speed, full-duplex, synchronous communication bus on the main chip pins occupy four main lines。

MOSI - Master data output and Slave data input;

MISO - Master data input and Slave data output;

SCLK - Clock signals generated by the master device;

SS - Enable signal from the device, controlled by the master device

Wherein MOSI / MISO / SCLK three lines to achieve full-duplex communication, simple and efficient, because the address of the device by a chip-select pin SS to specify when connecting multiple SPI devices require multiple chip select signals.

SCLK provides clock pulse,SDI, SDO is based on the complete data transmission pulse.Data output by the SDO line,when data clock low-going edge/rising edge the change,in the next low-going edge/rising edge is read.Complete a data transfer,input and use the same principle.So,change in at least eight times the clock (low-going edge and rising edge for a time),can complete 8 bits of data transmission.

SS line for an enable line, especially when connected to a plurality of peripheral devices, you can control the SS line of different peripheral devices to communicate.

FireDuino 02125425.jpg


Write a program about SPI is important to note issues:

  • Byte transfer from the most significant bit (MSB) Transfer, or from the least significant bit (LSB) Transfer it? This is SPI.setBitOrder () function set.

  • When the data is not transmitted, the clock line is high or low?

  • Trigger is rising or falling edge trigger it? This is set by the SPI.setDataMode () function.

  • SPI running speed is more fast? This is set by the SPI.setClockDivider () function

Fireduino SPI API

SPISettings(speed, dataOrder, dataMode)

   speed:CLK speed

   dataOrder: MSBFIRST or LSBFIRST 

   dataMode : SPI_MODE0, SPI_MODE1, SPI_MODE2, or SPI_MODE3

begin()

   Description 
       Initialize SPI bus with parameter.

   Syntax
       SPI.begin()
       SPI.begin(10)

   Parameter 
       1.None
       2.Chip Select

   Returns  
      None

beginTransaction()

   Description 
       Use the defined SBSettings initialize SPI bus.

   Syntax
       SPISettings  mySettings = SPISettings(14000000, MSBFIRST, SPI_MODE0); 
       SPI.beginTransaction(mySettings);
   or
   SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0))

   Parameter 
       mySettings:
           SPISettings Type define parameters

   Returns  
       None

endTransaction()

   Description 
       Stop using the SPI bus.

   Syntax
       SPI.endTransaction()

   Parameter 
      None


   Returns
       None

transfer(...)

transfer16(...)

   Description 
       For the transmission of data on the SPI bus, including sending and receiving.

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

   Parameter 
       val:send byte
       val16:send half word
       buffer:send buffer

   Returns 
       None

end()

   Description 
       Stop using the SPI bus (holding pin mode does not change).

   Syntax
       SPI.end()

   Parameter  
       None

   Returns 
       None

setBitOrder(...)

   Description 
       When setting serial data transmission, the first Transfer high or low, there LSBFIRST (LSB first) and MSBFIRST (most significant bit first) two types of options.

   Syntax
       SPI.setBitOrder(order)

   Parameter 
       order:LSBFIRST or MSBFIRST

   Returns 
       None

setDataMode(...)

   Description 
       Setting SPI data modes: clock polarity and clock phase. 
       Clock polarity: idle indicates clock signal is high or low; 
       Clock Phase: determine if data is sampled on the rising edge of SCK or falling edge of SCK sampling. 
       Contains four data mode (see parameter), sampling, data should be ready, and then sampled.

   Syntax
       SPI.setDataMode(mode)

   Parameter  
       mode: 
           SPI_MODE0(Sampled on the rising edge, falling edge set, when SCK is idle 0),
           SPI_MODE1(Set rising, falling edge sampling, SCK is idle 0),
           SPI_MODE2(Sampling falling, rising set, when SCK idle 1),
           SPI_MODE3(Falling set, the rising edge of the sampling, SCK idle 1)。
   Syntax
       None

setClockDivide(...)

   Description 
       Setting the clock SPI serial communication. Communication clock is obtained from the system clock frequency divider values are 2,4,8,16,32,64 or 128. The default setting is SPI_CLOCK_DIV4, set the SPI serial communication clock a quarter of the system clock, namely 400,000.

   Parameter  
       SPI.setClockDivider(divider)

   Parameter 
       divider: 
           SPI_CLOCK_DIV2
           SPI_CLOCK_DIV4
           SPI_CLOCK_DIV8
           SPI_CLOCK_DIV16
           SPI_CLOCK_DIV32
           SPI_CLOCK_DIV64
           SPI_CLOCK_DIV128
   Returns 
       None

SPI Example

#include <Arduino.h>#include "SPI.h"#define SPI_CSconst int chipSelectPin = 10;const int chipResetPin = 9;void setup() {
	Serial.begin(115200);#ifdef SPI_CS
	Serial.print("\r\nSPI_CS  YES...\r\n");
	SPI.begin(10);#else
	Serial.print("\r\nSPI_CS  NO...\r\n");
	SPI.begin();
	pinMode(chipSelectPin, OUTPUT);
	digitalWrite(chipSelectPin, LOW);#endif
	SPI.setBitOrder(MSBFIRST);
	SPI.setClockDivider(2);
	SPI.setDataMode(SPI_MODE0); 
	pinMode(chipResetPin, OUTPUT);
	digitalWrite(chipResetPin, LOW);	
	delay(10);
	digitalWrite(chipResetPin, HIGH);	
	delay(10);}void loop() {unsigned char version;#ifdef SPI_CS
	SPI.transfer(0x00,SPI_CONTINUE);
	SPI.transfer(0x39,SPI_CONTINUE);
	SPI.transfer(0x00,SPI_CONTINUE);
	version = SPI.transfer(0x00);#else
	digitalWrite(chipSelectPin, LOW);
	SPI.transfer(0x00);
	SPI.transfer(0x39);
	SPI.transfer(0x00);
	version = SPI.transfer(10,0x00);
	digitalWrite(chipSelectPin, HIGH);#endif 
	Serial.print("version:");
	Serial.println(version);
	delay(1000);}