Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Metro M4 Airlift Lte - ValueError: SCK in use
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Metro M4 Airlift Lte - ValueError: SCK in use

by lxuser on Wed Feb 26, 2020 4:47 pm

I am using metro m4 airlift lite to connect to the internet and display some text on eink display.

I am getting error message "ValueError: SCK in use".

# spi for esp chip
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

# spi for eink display
spi = board.SPI()
epd_cs = board.D10
epd_dc = board.D9
display_bus = displayio.FourWire(spi, command=epd_dc, chip_select=epd_cs,
baudrate=1000000)

Also, if I go to repl and try to create two spi objects, i see the same exact error.
>>> import busio
>>> import board
>>> spi1 = busio.SPI(board.SCK, board.MOSI, board.MISO)
>>> spi2 = busio.SPI(board.SCK, board.MOSI, board.MISO)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: SCK in use

Code: Select all | TOGGLE FULL SIZE
 cat boot_out.txt
Adafruit CircuitPython 5.0.0-beta.4 on 2020-01-23; Adafruit Metro M4 Airlift Lite with samd51j19


Code: Select all | TOGGLE FULL SIZE
 cat code.py
import time
import board
import displayio
import adafruit_il91874
import terminalio
from adafruit_display_text import label
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests

print("ESP32 SPI webclient test")

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json"


# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)

# If you have an ItsyBitsy Airlift:
# esp32_cs = DigitalInOut(board.D13)
# esp32_ready = DigitalInOut(board.D11)
# esp32_reset = DigitalInOut(board.D12)

# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

requests.set_socket(socket, esp)

if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
    print("ESP32 found and in idle mode")
print("Firmware vers.", esp.firmware_version)
print("MAC addr:", [hex(i) for i in esp.MAC_address])

for ap in esp.scan_networks():
    print("\t%s\t\tRSSI: %d" % (str(ap['ssid'], 'utf-8'), ap['rssi']))

print("Connecting to AP...")
while not esp.is_connected:
    try:
        esp.connect_AP(b'TP-Link_8435', b'49111712')
    except RuntimeError as e:
        print("could not connect to AP, retrying: ",e)
        continue
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
print("My IP address is", esp.pretty_ip(esp.ip_address))
print("IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com")))
print("Ping google.com: %d ms" % esp.ping("google.com"))

#esp._debug = True
print("Fetching text from", TEXT_URL)
r = requests.get(TEXT_URL)
print('-'*40)
print(r.text)
print('-'*40)
r.close()

print()
print("Fetching json from", JSON_URL)
r = requests.get(JSON_URL)
print('-'*40)
print(r.json())
print('-'*40)
r.close()

print("Done!")
time.sleep(3)
spi.unlock()
time.sleep(3)

# ----------------------------------------------------------------------

BLACK = 0x000000
WHITE = 0xFFFFFF
RED = 0xFF0000

# Change text colors, choose from the following values:
# BLACK, WHITE, RED (note red on this display is not vivid)

FOREGROUND_COLOR = BLACK
BACKGROUND_COLOR = WHITE

# Used to ensure the display is free in CircuitPython
displayio.release_displays()

# Define the pins needed for display use on the Metro

spi = board.SPI()
epd_cs = board.D10
epd_dc = board.D9

# Create the displayio connection to the display pins
display_bus = displayio.FourWire(spi, command=epd_dc, chip_select=epd_cs,
                                 baudrate=1000000)
time.sleep(1)  # Wait a bit

# Create the display object - the third color is red (0xff0000)
DISPLAY_WIDTH = 264
DISPLAY_HEIGHT = 176

# Create the display object - the third color is red (0xff0000)
display = adafruit_il91874.IL91874(display_bus, width=DISPLAY_WIDTH,
                                   height=DISPLAY_HEIGHT,
                                   highlight_color=0xff0000, rotation=90)

# Create a display group for our screen objects
g = displayio.Group(max_size=10)

# Set a white background
background_bitmap = displayio.Bitmap(DISPLAY_WIDTH, DISPLAY_HEIGHT, 1)
# Map colors in a palette
palette = displayio.Palette(1)
palette[0] = BACKGROUND_COLOR

# Create a Tilegrid with the background and put in the displayio group
t = displayio.TileGrid(background_bitmap, pixel_shader=palette)
g.append(t)

# Draw simple text using the built-in font into a displayio group
text_group = displayio.Group(max_size=10, scale=2, x=40, y=40)
text = "Hello World!"
text_area = label.Label(terminalio.FONT, text=text, color=FOREGROUND_COLOR)
text_group.append(text_area)  # Add this text to the text group
g.append(text_group)

# Place the display group on the screen
display.show(g)

# Refresh the display to have everything show on the display
# NOTE: Do not refresh eInk displays more often than 180 seconds!
display.refresh()

time.sleep(180)
while True:
    pass


lxuser
 
Posts: 2
Joined: Mon Mar 05, 2012 5:26 pm

Re: Metro M4 Airlift Lte - ValueError: SCK in use

by dastels on Wed Feb 26, 2020 8:33 pm

You are getting the error because you're trying to reuse the SPI pins. You only need one SPI object as SPI is a shared bus and the other pins (specifically the CS (chip select) take care of enabling devices.

So you create one SPI object, either explicited using board.SCK, board.MOSI, and board.MISO, or implicitly using board.SPI

Use that object for creating both adafruit_esp32spi.ESP_SPIcontrol and displayio.FourWire (along with their respective control pins).

Dave

dastels
 
Posts: 3781
Joined: Tue Oct 20, 2015 3:22 pm

Re: Metro M4 Airlift Lte - ValueError: SCK in use

by lxuser on Wed Mar 11, 2020 8:56 am

Thanks that worked wonderfully!
lxuser
 
Posts: 2
Joined: Mon Mar 05, 2012 5:26 pm

Please be positive and constructive with your questions and comments.