adafruit / Adafruit_CircuitPython_ESP32SPI

ESP32 as wifi with SPI interface
MIT License
104 stars 75 forks source link

Arduion Nano RP2040 Connect Issues #153

Open fivdi opened 2 years ago

fivdi commented 2 years ago

When working with CircuitPython 7.1.1 on an Arduion Nano RP2040 Connect it should be possible to access a number of pins on the ESP32. However, accessing two of these pins results in errors being thrown. The pins on the ESP32 that can't be successfully accessed are pins A6 and A7, both of which are broken out to the GPIO header on the Arduino. A6 and A7 correspond to GPIO36 and GPIO35 on the ESP32 respectively (see page 3 of this document).

Issue With Digital Read

Attempting to use GPIO35 as a digital input with the following program:

import board
import busio
from digitalio import DigitalInOut 
from adafruit_esp32spi import adafruit_esp32spi

# Configure ESP32
esp32_cs = DigitalInOut(board.CS1)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
esp32 = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

# Configure GPIO35 on the ESP32 as an input, then read and print its value
esp32.set_pin_mode(35, 0)
print(esp32.set_digital_read(35))

results in the following error being thrown:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 867, in set_digital_read
AssertionError: Please update nina-fw to 1.5.0 or above.

The error message suggests updating to nina-fw 1.5.0 or above. This issue is occuring on a Arduino Nano RP2040 Connect which uses the Arduino version of nina-fw and not the Adafruit fork of nina-fw. Today, the latest version of the Arduino version of nina-fw is 1.4.8 so the error is suggesting an update to the Adafruit fork, 1.5.0 or above. However, according to this issue the Adafruit fork of nina-fw isn't compatible with the Arduino Nano RP2040 Connect.

This issue can be resolved by removing these two lines of method set_digital_read. Version 1.4.8 of the Arduino version of nina-fw has everything that is needed to perform digital reads successfully.

Issue With Analog Read

There is a similar issue with analog read. Attempting to use GPIO35 (A7) as an analog input with the following program:

import board
import busio
from digitalio import DigitalInOut 
from adafruit_esp32spi import adafruit_esp32spi

# Configure ESP32
esp32_cs = DigitalInOut(board.CS1)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
esp32 = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

# Read and print the analog value of GPIO35 (A7)
print(esp32.set_analog_read(7))

results in the following error being thrown:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 887, in set_analog_read
AssertionError: Please update nina-fw to 1.5.0 or above.

This issue can be resolved by removing these two lines of method set_analog_read. Version 1.4.8 of the Arduino version of nina-fw has everything that is needed to perform analog reads successfully.

Now a new error is thrown:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 890, in set_analog_read
RuntimeError: buffer size must match format

This issue can be resolved by changing this line:

        resp_analog = struct.unpack("<i", resp[0])

to this:

        resp_analog = struct.unpack("<H", resp[0])

This change is needed because the Arduino version of nina-fw uses a 2-byte uint16_t to store analog values and the Adafruit fork uses a 4 byte int to store analog values. I don't know if this change would break things for other boards.

As can be seen, the implementation details of the Arduino and Adafruit versions of nina-fw are drifting apart in ways that can make things difficult for people using CircuitPython on the Arduion Nano RP2040 Connect. If would be good if these issues were resolved.

anecdata commented 2 years ago

The Arduino and Adafruit versions of NINA have diverged somewhat, particularly with the specific customizations for the Arduino RP2040 Nano Connect, including conditionals for pin definitions and use.

There was an issue opened in April 2019 in the Arduino repo to add Digital Read, which was closed as wontfix in September 2019, but added with the RP2040 Nano Connect merge. There was an issue opened in May 2019 in the Arduino repo to add Analog Read, which was closed with the RP2040 Nano Connect merge. These two features were added to the Adafruit repo in October 2019.

The ESP32SPI library would need some board detection code to determine if it is an RP 2040 Connect (or possibly other boards in the future), to avoid the AssertionError: Please update nina-fw to 1.5.0 or above.

A6 & A7 do seem to map to GPIO36 and GPIO35 on the p.3 version of the schematic (not clear why there are multiple schematics):

ADC3 GPIO39
ADC4 GPIO36
ADC34 GPIO35

However GPIO36 & GPIO39 are the Hall Effect sensor. In general, GPIO35 should be fine to use, but the ADC34 naming for GPIO35 makes me wonder if it is also used in some way in this instance for the Hall Effect sensor. I didn't dig in too deeply into the documentation to resolve this question. Without knowing more, I would not use these pins for general purpose analog or digital input.

I'll leave it to others to comment on the RuntimeError: buffer size must match format situation.