• sales

    +86-0755-88291180

【MicroPython】machine.SPI Function

  • machine.SPI(id,baudrate=1000000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=None, mosi=None, miso=None)
    • SPI object constructor, used to initialize the corresponding channel and pin
    • id: use SPI channel, can be 0 or 1
    • baudrate: SPI communication rate, which is the frequency on the SCK pin
    • polarity: Clock polarity, if it is 0, SCK outputs a low level when the bus is idle, otherwise it outputs a high level.
    • phase: the clock phase, if it is 0, the data is captured on the first clock edge, otherwise, the data is captured on the second clock edge.
    • bits: the number of data bits per transfer
    • firstbit: transmit high or low bits first
    • sck: SCK pin, should be a Pin object
    • mosi: MOSI pin, should be a Pin object
    • miso: MISO pin, should be a Pin object
    • sck, mosi and miso are all pins used by SPI and should be Pin objects
  • SPI.init()
    • init function, used to restart SPI
  • SPI.deinit()
    • deinit function, used to close the SPI
  • SPI.read(nbytes,write=0x00)
    • read function, used to read data from the device and return
    • nbytes: number of bytes read
    • write: When reading data, MOSI outputs data.
  • SPI.readinto(buf,write=0x00)
    • The readinto function is used to read data from the device and store it in the specified character array.
    • buf: character array, used to store received data
    • write: When reading data, MOSI outputs data.
  • SPI.write(buf)
    • write function, which writes a character array to the slave device.
    • buf: character array, used to store transmission data
  • SPI.write_readinto(write_buf, read_buf)
    • write_readinto function for sending and receiving data at the same time
    • write_buf: character array, used to store transmission data
    • read_buf: character array, used to store received data
  • PS: The length of the character array for transmitting and receiving data here is required to be consistent.

Demo Address

This article is only for RP2040 MicroPython firmware, and the source code shall prevail. This article is written according to the official source code at the time of writing, which is used to provide convenience for beginners and is for reference only. Those who are capable are recommended to refer to MicroPython.

TAG: Industrial USB TO 4CH RS232/485/422/TTL Serial UART Converter FT4232HL Raspberry Pi Starlight Camera IR-CUT IMX462 (A) Sensor 2MP 90 Degree Pi5 Case Raspberry Pi Zero 2W Dual Ethernet Expansion Board USB Hub Expansion RJ45 Milk V Duo IO Board Raspberry Pi 5 PoE MINI HAT(G) Power over RJ45 Ethernet 802.3af/at Moudle for Pi5 DeepSeek AI Voice Chat ESP32 C6 Development Board 2.06-inch AMOLED Display 410×502 2.06inch TouchScreen Programmable Watch Raspberry Pi 5 Camera SpotPear Milk-V Duo S Usage of USB Type A interface PC Monitor LCD ESP32 S3 Development Board 1.91 inch AMOLED Display Screen For Arduino LVGL With SD-Port/ QMI8658C 6-Axis Sensor Electronic EYE 0.71 inch Round LCD Display Screen For Arduino Raspberry Pi ESP32 Pico STM32 Raspberry Pi display Raspberry Pi Pico 2 RP2350 With 0.96 inch LCD Display 0.96inch Screen 160×80 Onboard RP2350A Modbus-Protocol-Specification User Guide Ranging Sensor Raspberry Pi CM5 4G/5G Dual Ethernet Port ETH Gigabit/2.5G Expansion Board with RJ45 IO Base and GPIO Industrial USB to RS485 Converter Milk V Duo