SPI – Serial Peripheral Interface Bus Protocol

This class provides bus protocol of serial peripheral interface (SPI) .

Constructor

machine.SPI

class machine.SPI(port, mode, clk, [group])

Parameter:

  • port - Integer type. Channel selection: [0,1].
  • mode - SPI working mode.
    Clock polarity CPOL: The pin level of clock signal SCLK when SPI is idle (0: low level; 1: high level)
    0 : CPOL=0, CPHA=0
    1 : CPOL=0, CPHA=1
    2: CPOL=1, CPHA=0
    3: CPOL=1, CPHA=1
  • clk - Clock frequency.
    EC600N/EC600S/EC800N/BG95M3/EC600M/EC800M/EG912N:
    0 : 812.5 kHz
    1 : 1.625 MHz
    2 : 3.25 MHz
    3 : 6.5 MHz
    4 : 13 MHz
    5 : 26 MHz
    6:52 MHz
    EC600U/EC200U/EG915U:
    0 : 781.25 kHz
    1 : 1.5625 MHz
    2 : 3.125 MHz
    3 : 5 MHz
    4 : 6.25 MHz
    5 : 10 MHz
    6 : 12.5 MHz
    7 : 20 MHz
    8 : 25 MHz
    9 : 33.33 MHz
    BC25:
    0 : 5 MHz
    X : X MHz (X in [1,39])
    FCM360W/FCM362K:
    0:500kHz
    1:1MHz
    2:5MHz
    3:10MHz
    4:20MHz
  • [gruop] - map SPI to different pins,default value is 0.

BC25 series module does not support SPI working mode of value 1 or 2.

The group parameter is supported only by the EC600M/EC800M/EG810M series. nor flash cannot be mounted if group = 1

52MHz clk is high requirements for hardware.

Example:

>>> from machine import SPI
>>> # Creates a SPI object
>>> spi_obj = SPI(1, 0, 1)

SPI Pin Correspondences:

Module Pin
EC600U port0:
CS: pin4
CLK: pin1
MOSI: pin3
MISO: pin 2
port1:
CS: pin58
CLK: pin61
MOSI: pin59
MISO: pin60
EC200U port0:
CS: pin134
CLK: pin133
MOSI: pin132
MISO: pin131
port1:
CS: pin26
CLK: pin27
MOSI: pin24
MISO: pin25
EC600S/EC600N port0:
CS: pin58
CLK: pin61
MOSI: pin59
MISO: pin60
port1:
CS: pin4
CLK: pin1
MOSI: pin3
MISO: pin2
EC100Y port0:
CS: pin25
CLK: pin26
MOSI: pin27
MISO: pin28
port1:
CS: pin105
CLK: pin104
MOSI: pin107
MISO: pin106
EC800N port0:
CS: pin31
CLK: pin30
MOSI: pin32
MISO: pin33
port1:
CS: pin52
CLK: pin53
MOSI: pin50
MISO: pin51
BC25 port0:
CS: pin6
CLK: pin5
MOSI: pin4
MISO: pin3
BG95 port0:
CS: pin25
CLK: pin26
MOSI: pin27
MISO: pin28
port1:
CS: pin41
CLK: pin40
MOSI: pin64
MISO: pin65
EC600M port0:
CS: pin58
CLK: pin61
MOSI: pin59
MISO: pin60
port1:
CS: pin4
CLK: pin1
MOSI: pin3
MISO: pin2
port2:
CS:pin49
CLK:pin54
MOSI:pin53
MISO:pin52
EG915U port0:
CS: pin25
CLK: pin26
MOSI: pin64
MISO: pin88
port1:
CS:pin5
CLK:pin4
MOSI:pin6
MISO:pin7
EC800M/EG810M port0:
group=0:
CS:pin31
CLK:pin30
MOSI:pin32
MISO:pin33
group=1:
CS:pin52
CLK:pin53
MOSI:pin50
MISO:pin51
port1:
group=0:
CS:pin52
CLK:pin53
MOSI:pin50
MISO:pin51
group=1:(EG810M_EU unsupported)
CS:pin69
CLK:pin68
MOSI:pin85
MISO:pin84
port2:
CS:pin76
CLK:pin77
MOSI:pin78
MISO:pin16
EG912N port0:
CS: pin25
CLK: pin26
MOSI: pin27
MISO: pin28
port1:
CS: pin5
CLK: pin4
MOSI: pin6
MISO: pin7
EG912U port0:(EG912UGLAA unsupported)
CS:pin25
CLK:pin26
MOSI:pin64
MISO:pin88
port1:
CS:pin5
CLK:pin4
MOSI:pin6
MISO:pin7
FCM362K port0:
CS:pin29
CLK:pin30
MOSI:pin37
MISO:pin31
BC32 port0
CS:pin41
CLK:pin42
MOSI:pin43
MISO:pin44
BC92 port0
CS:pin41
CLK:pin42
MOSI:pin43
MISO:pin44
EC200A/UC200A port0:
CS: pin37
CLK: pin40
MOSI: pin38
MISO: pin39
port1:
CS: pin26
CLK: pin27
MOSI: pin25
MISO: pin24
EG915N port0
CS:pin5
CLK:pin4
MOSI:pin6
MISO:pin7

Methods

SPI.read

SPI.read(recv_data, datalen)

This method reads data.

Parameter:

  • recv_data - Bytearray type. An array used to receive data.
  • datalen - Integer type. Length of the data to be read.

Return Value:

0 - Successful execution

-1 - Failed execution

SPI.write

SPI.write(data, datalen)

This method writes data.

Parameter:

  • data - Bytearray type. Data to be written.
  • datalen - Integer type. Length of data to be written.

Return Value:

0 - Successful execution

-1 - Failed execution

SPI.write_read

SPI.write_read(r_data, data, datalen)

This method writes and reads data.

Parameter:

  • r_data - Bytearray type. An array used to receive data.
  • data - Bytearray type. Data to be sent.
  • datalen - Integer type. Length of data to be read.

Return Value:

0 - Successful execution

-1 - Failed execution

SPI.close

SPI.close()

This method close SPI.

Return Value:

0 - Successful execution

-1 - Failed execution

Example:

Please use this function with the peripherals.

import log
from machine import SPI
import utime



spi_obj = SPI(0, 0, 1)

# Sets the log output level 
log.basicConfig(level=log.INFO)
spi_log = log.getLogger("SPI")


if __name__ == '__main__':
    r_data = bytearray(5)  # Creates a buff for receiving data
    data = b"world"  # Tests data

    ret = spi_obj.write_read(r_data, data, 5)  # Writes data and receives data to r_data
    spi_log.info(r_data)