Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Arduino Nano RP2040 Connect #4802

Merged
merged 6 commits into from
May 22, 2021
Merged

Adding Arduino Nano RP2040 Connect #4802

merged 6 commits into from
May 22, 2021

Conversation

BlitzCityDIY
Copy link

New PR to fix local GitHub config issues and running pre-commit. Adding CircuitPython support for the Arduino Nano RP2040 Connect. Have tested all digital and analog I/O, I2C, onboard LSM6DSOX. Uses the AT25SF128A external flash.

Re-commiting to fix local GitHub config issues. Adding CircuitPython support for the Arduino Nano RP2040 Connect. Have tested all digital and analog I/O, I2C, onboard LSM6DSOX. Uses the AT25SF128A external flash.
While testing WIFI realized MISO was defined incorrectly
@ladyada
Copy link
Member

ladyada commented May 22, 2021

example of displaying gyro data on oled

import time
import board
import displayio
import terminalio
from adafruit_display_text import label
import adafruit_displayio_ssd1306
from adafruit_lsm6ds.lsm6dsox import LSM6DSOX

displayio.release_displays()
i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3D, reset=None)
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=64)

sensor = LSM6DSOX(i2c)

while True:
    print("Acc: X:%.2f, Y: %.2f, Z: %.2f m/s^2" % (sensor.acceleration))
    print("Gyro X:%.2f, Y: %.2f, Z: %.2f r/s" % (sensor.gyro))
    print("")

@ladyada
Copy link
Member

ladyada commented May 22, 2021

example code for wifi connectivy

import time
import board
import busio
from digitalio import DigitalInOut
import adafruit_requests as requests
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import displayio
import terminalio
from adafruit_display_text import label
import adafruit_displayio_ssd1306
from adafruit_bitmap_font import bitmap_font

displayio.release_displays()
i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3D, reset=None)
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=64)


splash = displayio.Group(max_size=10)
display.show(splash)

color_bitmap = displayio.Bitmap(128, 64, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0x0 # black background color
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)

font = bitmap_font.load_font("ComicSansMS-17.pcf")
font.load_glyphs(b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: ')

# Draw a label
text = "doge get $"
text_area = label.Label(
    font, text=text, color=0xFFFFFF,
    anchor_point=(0.5, 0.5), anchored_position = (64, 32)
)
splash.append(text_area)

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

print("ESP32 SPI webclient test")

JSON_URL = "https://sochain.com//api/v2/get_price/DOGE/USD"


# If you are using a board with pre-defined ESP32 Pins:
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)
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])

text_area.text = "much connect"
print("Connecting to AP...")
while not esp.is_connected:
    try:
        esp.connect_AP(secrets["ssid"], secrets["password"])
    except RuntimeError as e:
        print("could not connect to AP, retrying: ", e)
        continue
text_area.text = "very connected"
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
print("My IP address is", esp.pretty_ip(esp.ip_address))

print()
print("Fetching json from", JSON_URL)
text_area.text = "so fetch"
r = requests.get(JSON_URL)
dogedata = r.json()
print(dogedata)
r.close()

price = dogedata['data']['prices'][0]['price']
text_area.text = "wow $%0.4f" % float(price)

print("Done!")
while True:
    time.sleep(1)

@ladyada ladyada requested review from ladyada and dhalbert May 22, 2021 17:18
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; too bad about the confusing pin naming

@ladyada
Copy link
Member

ladyada commented May 22, 2021

yeah its wierd because some analog pins are routed thru the esp32!

@ladyada ladyada merged commit 7df5d74 into adafruit:main May 22, 2021
dhalbert added a commit to dhalbert/circuitpython that referenced this pull request May 25, 2021
@elijahsgh
Copy link

yeah its wierd because some analog pins are routed thru the esp32!

I might be a week or two late on this but here's a very simple example in micropython of blinking the lights:

# WIFININA spi driver https://github.com/arduino-libraries/WiFiNINA/blob/master/src/utility/spi_drv.cpp
# Commands https://github.com/arduino-libraries/WiFiNINA/blob/master/src/utility/wifi_spi.h
# Adafruit esp32spi https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI

# NINA 27, 26, 25 are R, G, B
from machine import Pin
from machine import SPI
import time
from micropython import const

_START_CMD             = const(0xe0)
_END_CMD               = const(0xee)
_ERR_CMD               = const(0xef)
_REPLY_FLAG            = const(1<<7)
_CMD_FLAG              = const(0)

_SET_PIN_MODE		= 0x50
_SET_DIGITAL_WRITE	= 0x51
_SET_ANALOG_WRITE	= 0x52
_GET_DIGITAL_READ    = 0x53
_GET_ANALOG_READ     = 0x54

ESP_BUSY  = Pin(10, Pin.IN)
ESP_RESET = Pin(3, Pin.OUT, False)
ESP_CS    = Pin(9, Pin.OUT, True)
ESP_GPIO0 = Pin(2)
ESP_MISO  = Pin(8)
ESP_MOSI  = Pin(11)
ESP_SCK   = Pin(14) 

LEDR = 27
LEDG = 25
LEDB = 26

spiwifi = SPI(1, baudrate=100000, sck=ESP_SCK, mosi=ESP_MOSI, miso=ESP_MISO)

def reset():
    ESP_GPIO0.init(Pin.OUT)
    ESP_GPIO0.value(True)

    ESP_CS.high()
    ESP_RESET.low()

    time.sleep(0.01)
    ESP_RESET.high()
    time.sleep(0.75)

    ESP_GPIO0.init(Pin.IN)

    print("ESP32 reset")

def wait_ready():
    wait_status = ESP_BUSY.value()

    while wait_status:
        time.sleep(0.05)
        wait_status = ESP_BUSY.value()


def read_response():
    response = []
    response_bytes = []
    bytes_read = 0

    max_read = 32

    wait_ready()
    ESP_CS.low()

    while True:
        read_byte = spiwifi.read(1)
        response_bytes.append(read_byte)
        if read_byte == bytes([_END_CMD]):
            break
        if read_byte == bytes([_ERR_CMD]):
            raise RuntimeError('Error waiting on WiFi Command')

        bytes_read += 1
        if bytes_read > max_read:
            raise RuntimeError('Error waiting on WiFi command')

    ESP_CS.high()

    num_params = response_bytes[2]
    index = 3

    for _ in num_params:
        length = int.from_bytes(response_bytes[index], "little")
        newparam = response_bytes[index+1:index+1+length]
        index += 1 + length
        response.append(b''.join(newparam))

    return response

def send_command(cmd, params=[], num_params=-1):
    if num_params < 0:
        num_params = len(params)
    
    total_bytes = 3 + num_params + 1

    for p in params:
        pbytes = bytearray(p)
        total_bytes = total_bytes + len(pbytes)
    
    sendbuffer = bytearray(total_bytes)

    sendbuffer[0] = _START_CMD
    sendbuffer[1] = cmd & ~_REPLY_FLAG
    sendbuffer[2] = num_params

    index = 3

    for p in params:
        pbytes = bytearray(p)
        lenpbytes = len(pbytes)

        sendbuffer[index] = lenpbytes
        sendbuffer[index+1:index+1+lenpbytes] = pbytes
        index += 1+lenpbytes
    sendbuffer[index] = _END_CMD

    wait_ready()
    ESP_CS.low()
    spiwifi.write(sendbuffer)
    ESP_CS.high()

def set_ninapin_mode(pin, mode):
    send_command(_SET_PIN_MODE, [[pin], [mode]])
    return read_response()

def ninapin_digital_write(pin, value):
    if value == 0:
        value = 1
    else:
        value = 0

    send_command(_SET_DIGITAL_WRITE, [[pin], [value]])
    return read_response()

reset()

for pin in [LEDR, LEDG, LEDB]:
    set_ninapin_mode(pin, Pin.OUT)
    ninapin_digital_write(pin, 0)

while True:
    time.sleep(0.25)
    ninapin_digital_write(LEDR, 1)
    ninapin_digital_write(LEDG, 0)
    ninapin_digital_write(LEDB, 0)
    time.sleep(0.25)
    ninapin_digital_write(LEDR, 0)
    ninapin_digital_write(LEDG, 1)
    ninapin_digital_write(LEDB, 0)
    time.sleep(0.25)
    ninapin_digital_write(LEDR, 0)
    ninapin_digital_write(LEDG, 0)
    ninapin_digital_write(LEDB, 1)

@ebolisa
Copy link

ebolisa commented Sep 10, 2021

@elijahsgh Thanks for sharing! How do you use the wifi part of the code? TIA

@elijahsgh
Copy link

@elijahsgh Thanks for sharing! How do you use the wifi part of the code? TIA

With the code I pasted above? You can't. That code was just me goofing around with the weird pin mappings that were being discussed. You can only blink lights with it. :)

To use the wifi part you'd need the full Adafruit esp32spi, etc.

@ebolisa
Copy link

ebolisa commented Sep 10, 2021

@elijahsgh Thanks for sharing! How do you use the wifi part of the code? TIA

With the code I pasted above? You can't. That code was just me goofing around with the weird pin mappings that were being discussed. You can only blink lights with it. :)

Got it, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants