How to change SPI_CS line to stop interface clashes

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

Try SPIO_SD_CS = 21 # conn 27
As per, Raspberry Pi Pico W GPIO Pinout
GP 22 doesn’t support the CSn function.

Thanks @alphanumeric for your information. It works - I said it would be my fault!

I now have meters, mini-scope, RTC and SD-Card storage working, with sending info via the Internet using the remarkable Pico W and Display Pack 2.0.

Thank you for promptly helping me, which I appreciate.
Steve

1 Like

It’s nice when its an easy fix. Code wise anyway. I’m thinking you had to warm up your soldering iron to move the connection.

I used links for this part of the test :)

Thanks again.

I went this way with dual Display Packs,

Soldered in jumpers from each display to a Pico on a third Perf board.
Both are on SPi 0 but they each use a different CS pin.

I’m showing off a bit now, but I did this with a Pi Zero.

This one sits on my main computers desk. Temperature and humidity are indoor, but are most relevant in the summer months. We don’t have air conditioning. The pressure display is the one I look at the most. I’m on a sea coast, if its dropping its a big deal. ;)

The code is based on an Enviro+ example.

That’s a really interesting test rig. Thanks @alphanumeric for sharing as it helps us all progress immensely.

Since 1975 I used 4-bit then 8-bit micros then changed to using the 6502 for my industrrial projects. From the 1990s I used PICs in every project. Unfortunately Microchip started charging silly annual subscription prices for their C compilers and so I changed over to using Raspberry Pi boards and stopped using PICs. I had to design opto isolated 24-bit ADC interfaces and 128 in/out IO boards for the RPI to be used in my harsh industrial environments.

Due to age I needed to retired but then came across the Pico. And so it starts all over again!

All the best and please share what and how you have created your projects as it is really interesting to read and learn.

Steve.

I’m really into Pico / RP2040 based projects these days. Mostly environmental monitoring type of stuff.
Pi Pico based Weather Station Project - Discussion / Projects - Pimoroni Buccaneers
This was fun to play with.
Pimoroni sent me this prototype to tinker with. =) - Discussion - Pimoroni Buccaneers

On the Pi side its mostly remote video surveillance stuff. Motion Eye OS on a Pi 3B+ or 4B. A couple are Pan tilt setups.
I have a Pi5 with NVMe, a Pi 400, a couple Pi 4B’s, some 3B+'s 3A’s and Zeros.

I have just read your links - all excellent info, thanks.

1 Like

Not true. CS is just a digital IO, you can use any pin for that. As long as it is high, the device has to ignore everything on the bus. Pulled low, the device knows that data/commands have to be processed. In fact, my own datalogger uses GP22 as chip-select for the SD-card.

I think the problem is that the pinout you are linking to is more or less a reproduction of the table from the datasheet. The datasheet recommends a few GP pins for CS, but if you dig into the details and read all the gliberish further down about SPI, you will see no more reference at all to these pins.

Noted, thanks.bablokb. I have just tried with the latest UF2 (1.22.2), set the line high to start with and it works.
Regards
Steve