Software (bitbang) SPI Setup Error

According to the OpenMV library, there is a software (bitbang) SPI available with ID = -1
https://docs.openmv.io/library/machine.SPI.html

When using ID = 2 for the physical pins of the SPI bus (SCK, MISO, MOSI) = (P3, P2, P1, P0), the SPI works fine.
spi = SPI(2)
spi.init(SPI.MASTER, prescaler = 64, polarity=0, phase=0, bits=8, firstbit=SPI.MSB )

However when I change to ID = -1, this IDE error appears “ValueError: SPI(-1) doesn’t exist”
spi = SPI(-1)
spi.init(SPI.MASTER, prescaler = 64, polarity=0, phase=0, bits=8, firstbit=SPI.MSB )

Alternatively is there a way to change the hardware SPI to different pins. I’m having trouble squeezing in to use all of these modules - SPI, UART, I2C, and servo modules.

Hmm,

I’m not sure why this isn’t working.

Ibrahim?

Please post the script.

The following script works with spi = SPI(2). When spi=SPI(-1), the IDE gives the error “ValueError: SPI(-1) doesn’t exist”

SPI Test

import sensor, image, time
import pyb
from pyb import Pin, SPI

data_length = 2
data_buf = bytearray(data_length)

data_buf[0] = 0b00000000
data_buf[1] = 0b01010101

#spi = SPI(2)
spi = SPI(-1)
spi.init(SPI.MASTER, prescaler = 64, polarity=0, phase=0, bits=8, firstbit=SPI.MSB )

while(True):

spi.send(data_buf)
pyb.delay(10)

You need to import machine and then use SPI. It’s a different module.

Thanks. This test code worked. What are the possible baudrates for the sofware SPI? Also, what is the bus frequency for the hardware SPI?

SPI Test

import pyb
import machine
from machine import SPI, Pin

spi = SPI(-1, baudrate=int(16000000/32), polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=Pin(‘P2’), mosi=Pin(‘P0’), miso=Pin(‘P1’))

data_length = 2
data_buf = bytearray(data_length)

data_buf[0] = 0b01110000
data_buf[1] = 0b01010101

while(True):

spi.write(data_buf)
pyb.delay(10)

Just set a baudrate and the code will try to match it. The internal SPI bus has a 160MHz clock. Max is 80,40,20,10,5,etc.

Software is based on nanosecond delays, since it’s not banged there’s no rate per say. You can put in a large number and it will try to go fast.

I’m using the software SPI with an LED driver that only needs SCK and MOSI. How do I disable the MISO pin in the software SPI to not tie up a pin that is not being used? Tried miso=None but it returned an error.

import machine
from machine import SPI, Pin
spi = SPI(-1, baudrate=int(16000000/32), polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=Pin(‘P2’), mosi=Pin(‘P0’), miso=None)

It might not be that flexible.

Our Arduino Interface Library is ready finally. You can use one to expand I/O now.