Micropython SPI

  • Hi,

    I've got a M5basic module with fw version and not to mention SPI sensor.
    Code in Visual studio code. When importing SPI I'm getting the error: 'cannot import SPI'

    I'm following the documentation: https://github.com/m5stack/M5Stack_MicroPython

    I investigated and it appears the 'machine' module does not have SPI.
    So my question: How do I make SPI work on M5stack with micropython 1.4.5?
    Secondary question: All documentation seems old or in complete. Where can I find the newest/most complete information?

    ---- Code from documentation

    from machine import SPI, Pin

    spi = SPI(

    spi.write(buf) #NOHEAP
    spi.read(nbytes, *, write=0x00) #write is the byte to ?output on MOSI for each byte read in
    spi.readinto(buf, *, write=0x00) #NOHEAP
    spi.write_readinto(write_buf, read_buf) #NOHEAP; write_buf and read_buf can be the same

    ------Machine module, python terminal: help("machine")

    object <module 'umachine'> is of type module
    name -- umachine
    mem8 -- <8-bit memory>
    mem16 -- <16-bit memory>
    mem32 -- <32-bit memory>
    freq -- <function>
    reset -- <function>
    unique_id -- <function>
    sleep -- <function>
    lightsleep -- <function>
    deepsleep -- <function>
    idle -- <function>
    disable_irq -- <function>
    enable_irq -- <function>
    heap_info -- <function>
    time_pulse_us -- <function>
    nvs_setint -- <function>
    nvs_getint -- <function>
    nvs_setstr -- <function>
    nvs_getstr -- <function>
    nvs_erase -- <function>
    nvs_erase_all -- <function>
    random -- <function>
    Timer -- <class 'Timer'>
    WDT -- <class 'WDT'>
    SLEEP -- 2
    DEEPSLEEP -- 4
    Pin -- <class 'Pin'>
    Signal -- <class 'Signal'>
    TouchPad -- <class 'TouchPad'>
    ADC -- <class 'ADC'>
    DAC -- <class 'DAC'>
    I2C -- <class 'I2C'>
    I2S -- <class 'I2S'>
    PWM -- <class 'PWM'>
    RTC -- <class 'RTC'>
    UART -- <class 'UART'>
    Neopixel -- <class 'Neopixel'>
    reset_cause -- <function>
    HARD_RESET -- 2
    PWRON_RESET -- 1
    WDT_RESET -- 3
    SOFT_RESET -- 5
    wake_reason -- <function>
    PIN_WAKE -- 2
    EXT0_WAKE -- 2
    EXT1_WAKE -- 3
    TIMER_WAKE -- 4
    ULP_WAKE -- 6

  • 1.3.2 does not contain the I2C/SPI etc. in the machine module.

    1.2.3 does include SPI but none of the other code works as uiflow module does not exist.

  • @jwrm22 Want to brick your ESP32 with SPI programming ?

    see http://community.m5stack.com/topic/1840/issues-interfacing-mfrc522-rfid-with-m5stack

    Using GPIO4 CS ? It is used as CS on the micro SD card reader of the M5Stack: You either have to solder a wire to your ESP32:


    or make a micro sd card adapter connecting wires to micro sd card:0_1586716832850_75d899e5-34fd-4672-9285-01744d8e4c14-image.png
    GPIO4 is on your microSD Pin 2 !
    You can get all the SPI signals you wanted in the sd Micro card adapter ! https://github.com/m5stack/M5-Schematic/blob/master/Core/Basic/M5-Core-Schematic(20171206).pdf

    Your copy and paste Micropython code from the M5Stack reference is the basic API reading/writing to the SD (SPI) micro card !


  • While I understand that the SPI is used for the SD card.
    On the back of the m5stack SPI is clearly broken out. No hand soldering required.
    I do not need CS it's just what's used as example.

    There is an implementation in the sited m5stack git source on software SPI on different pins.
    I'm just annoyed the software and documentation are not in sync.
    Do you know where I can find the source used to build for instance?

    I'll see if I can find the code to read/write to the SD card.

  • I do not have enough knowledge of python or git to find the files that write SPI.
    I've found some .c files but don't know what to do with them.

  • Anyone tried flashing m5 stack basic device with raw micropython? Will it brick the device? I doubt that. Raw micropython works just fine with atom and reflashes to uiflow just okay.

  • @ you can find the micropython SPI API in the microphyton reference:
    M5Stack has a repository hooking up a SPI TFT display to the M5Stack on github:

    They are using following Pins for the Hardware SPI bus (vspi, ID channel 2) in order not to have any conflicts with the built in components:
    MOSI: 23
    MISO: 19
    SCK : 18
    CS: 5 (Display CS)
    DC: 26 (display DC)
    TCS: 25 (touch screen CS)

  • @crami25 Ok. SPI is used both for the SD card and TFT.
    The link to the micropython docs is not the same as any of the m5stsck firmware.
    The SPI in the machine module does not exist.

    Can you tell me how I install or add SPI to a micropython build?

  • @thrasher I did it and it works fine. The only downside is that M5Stack doens't give you their micropython libraries to work with... They claim they are open source ( and that was why I got into it) but then they don't share the sources that we need to make the best of the hardware.. I have asked for some in other posts and got no reply

  • As well as any decent documentation. Its a known problem

  • @jwrm22 SPI can be found in machine module

    from machine import SPI

  • @tialm The problem is that the firmware is constantly changing as they work to ix bugs in the firmware. They are working to get it up to the same level as mainstream Micropython but its slow going.
    I'm currently digging through demo and examples (that I'm creating) to find the API's but its slow going due to the rapidly changing firmware.