I use Pico Ws with the Pimoroni Display Pack 2 to create meter displays for projects. This display is hard wired to SPI0, which uses these default SPIO GPIO lines:
miso = 16, cs = 17, clk = 18, mosi = 1917 as the display’s chip select (CS).
I have added an Adafruit PiCowbell Adalogger for Pico board, which has a battery backed RTC and MicroSD for data logging, The SD card socket on this board is hard wired to SPI0 port, where the GPIO lines are: miso = 16, cs = 17, clk = 18, mosi = 19, ie the same as the Display Pack 2.0 and so when an SD card is inserted, reading or writing to it the display will not work as the CS line is the same for both devices. When reading or writing to either the Display or SD card, CS (GPIO17) goes low for both devices and the miso/mosi data lines clash.
I have carefully cut the CS PCB track to the SD Card and connected it to a spare GPIO port, eg GPIO22 but the software does not work when trying to use this new CS line - I have shown the test below, which works fine when using GPIO17 for the CS (but then I have to unplug to the display!).
The idea of SPI is to allow multiple devices to be wired to an SPI port using common clk, miso, mosi io, with independant CS lines to each device. I can only get it to work using GPIO17 for CS and so I must be doing something wrong.
from machine import Pin, SPI
import gc
import time # for time.sleep(secs)
import sdcard
import uos # FAT only SD cards - check if there a method for FAT32 cards?
SPIO_MISO = 16
SPIO_CLK = 18
SPIO_MOSI = 19
# SPIO_SD_CS = 17 # conn 22 < this will work
SPIO_SD_CS = 22 # conn 29 <<<< THIS DOES NOT WORK
# Assign chip select (CS) pin (and start it high) << tried but it makes no difference
# machine.Pin(SPIO_SD_CS, machine.Pin.OUT)
# SD_CS = Pin(SPIO_SD_CS, Pin.OUT)
# SD_CS.value(1)
# create spi0
sd_spi = SPI(0,
sck=Pin(SPIO_CLK, Pin.OUT),
mosi=Pin(SPIO_MOSI, Pin.OUT),
miso=Pin(SPIO_MISO, Pin.OUT)
)
SD_MOUNTPOINT = "/sd" # create sub-directory pathnames, eg DATA_DIR = SD_MOUNTPOINT + "/data"
sd = None
last_exception = None
# First one can fail at power up - not needed once power has settled
for _ in range(4):
try:
sd = sdcard.SDCard(sd_spi, Pin(SPIO_SD_CS)) # <<< Pass the CS line
uos.mount(sd, SD_MOUNTPOINT)
print("SD mounted FAT")
break
except OSError as ex:
last_exception = ex
time.sleep(1)
if sd is None:
print("No SD card or wrong format")
raise last_exception
gc.collect()
# SD card mounted, create a file and write something to it
TESTFILENAME = "testfile.txt"
with open("/sd/" + TESTFILENAME, "w") as file:
file.write("Hello, SD Card!\r\n")
file.write("We wrote to file '" + TESTFILENAME + "' on the SD card and will now read it back\r\n")
# Open the file we just created and read from it
with open("/sd/" + TESTFILENAME, "r") as file:
data = file.read()
print(data)
If anyone has successfully used a SD card connected to SPI0 using a different CS to GPIO 17, could you please let me know.
Thanks for looking