Compare commits
9 Commits
main
...
feature/ca
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
944efefacd | ||
|
|
e23f64056e | ||
|
|
e0d5daee47 | ||
|
|
a9107a5ca8 | ||
|
|
c29c98b2b1 | ||
|
|
544f1cae8c | ||
|
|
fd7621fdcb | ||
|
|
e5515981ea | ||
|
|
ac34474435 |
3
.github/workflows/micropython.yml
vendored
3
.github/workflows/micropython.yml
vendored
@ -30,6 +30,9 @@ jobs:
|
||||
- name: pico_plus2_rp2350_psram
|
||||
board: PIMORONI_PICO_PLUS2
|
||||
variant: PSRAM
|
||||
- name: pico_plus2_rp2350_wireless
|
||||
board: PIMORONI_PICO_PLUS2
|
||||
variant: WIRELESS
|
||||
- name: pico_plus2_rp2350
|
||||
board: PIMORONI_PICO_PLUS2
|
||||
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
require("bundle-networking")
|
||||
|
||||
# Bluetooth
|
||||
require("aioble")
|
||||
|
||||
include("manifest.py")
|
||||
|
||||
freeze("$(BOARD_DIR)/../../modules_py", "lte.py")
|
||||
@ -6,3 +6,34 @@
|
||||
#define MICROPY_HW_FLASH_STORAGE_BYTES (PICO_FLASH_SIZE_BYTES - (2 * 1024 * 1024))
|
||||
|
||||
#define MICROPY_HW_PSRAM_CS_PIN PIMORONI_PICO_PLUS2_PSRAM_CS_PIN
|
||||
|
||||
// Might be defined in mpconfigvariant_PSRAM.cmake
|
||||
// or mpconfigvariant_PPP.cmake
|
||||
#if defined(MICROPY_HW_ENABLE_PSRAM)
|
||||
|
||||
#define MICROPY_GC_SPLIT_HEAP (1)
|
||||
|
||||
#endif
|
||||
|
||||
// Set up networking.
|
||||
#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "PPP2"
|
||||
|
||||
#if defined(MICROPY_PY_NETWORK_CYW43)
|
||||
|
||||
// CYW43 driver configuration.
|
||||
#define CYW43_USE_SPI (1)
|
||||
#define CYW43_LWIP (1)
|
||||
#define CYW43_GPIO (1)
|
||||
#define CYW43_SPI_PIO (1)
|
||||
|
||||
#define MICROPY_HW_PIN_EXT_COUNT CYW43_WL_GPIO_COUNT
|
||||
|
||||
#endif
|
||||
|
||||
// Might be defined in mpconfigvariant_PPP.cmake
|
||||
// This is not checked by mpconfigport.h so we must set up networking below
|
||||
#if defined(MICROPY_PY_NETWORK_PPP_LWIP)
|
||||
|
||||
// Nothing to do here?
|
||||
|
||||
#endif
|
||||
@ -1,6 +1,6 @@
|
||||
# Override the MicroPython board name
|
||||
# And set basic options which are expanded upon in mpconfigboard.h
|
||||
list(APPEND MICROPY_DEF_BOARD
|
||||
"MICROPY_HW_ENABLE_PSRAM=1"
|
||||
"MICROPY_GC_SPLIT_HEAP=1"
|
||||
"MICROPY_HW_BOARD_NAME=\"Pimoroni Pico Plus 2 (PSRAM)\""
|
||||
"MICROPY_HW_ENABLE_PSRAM=1"
|
||||
)
|
||||
|
||||
@ -0,0 +1,40 @@
|
||||
# Override the MicroPython board name
|
||||
# And set basic options which are expanded upon in mpconfigboard.h
|
||||
list(APPEND MICROPY_DEF_BOARD
|
||||
"MICROPY_HW_BOARD_NAME=\"Pimoroni Pico Plus 2 (Wireless + PSRAM)\""
|
||||
"MICROPY_HW_ENABLE_PSRAM=1"
|
||||
"MICROPY_PY_NETWORK=1"
|
||||
"MICROPY_PY_NETWORK_PPP_LWIP=1"
|
||||
)
|
||||
|
||||
# Links micropy_lib_lwip and sets MICROPY_PY_LWIP = 1
|
||||
# Picked up and expanded upon in mpconfigboard.h
|
||||
set(MICROPY_PY_LWIP ON)
|
||||
|
||||
# Links cyw43-driver and sets:
|
||||
# MICROPY_PY_NETWORK_CYW43 = 1,
|
||||
# MICROPY_PY_SOCKET_DEFAULT_TIMEOUT_MS = 30000
|
||||
set(MICROPY_PY_NETWORK_CYW43 ON)
|
||||
|
||||
# Adds mpbthciport.c
|
||||
# And sets:
|
||||
# MICROPY_PY_BLUETOOTH = 1,
|
||||
# MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS = 1,
|
||||
# MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE = 1
|
||||
set(MICROPY_PY_BLUETOOTH ON)
|
||||
|
||||
# Links pico_btstack_hci_transport_cyw43
|
||||
# And sets:
|
||||
# MICROPY_BLUETOOTH_BTSTACK = 1,
|
||||
# MICROPY_BLUETOOTH_BTSTACK_CONFIG_FILE =
|
||||
set(MICROPY_BLUETOOTH_BTSTACK ON)
|
||||
|
||||
# Sets:
|
||||
# CYW43_ENABLE_BLUETOOTH = 1,
|
||||
# MICROPY_PY_BLUETOOTH_CYW43 = 1
|
||||
set(MICROPY_PY_BLUETOOTH_CYW43 ON)
|
||||
|
||||
set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest-wireless.py)
|
||||
|
||||
set(PICO_BOARD "pimoroni_pico_plus2w_rp2350")
|
||||
set(PICO_BOARD_HEADER_DIRS ${CMAKE_CURRENT_LIST_DIR})
|
||||
@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
// -----------------------------------------------------
|
||||
// NOTE: THIS HEADER IS ALSO INCLUDED BY ASSEMBLER SO
|
||||
// SHOULD ONLY CONSIST OF PREPROCESSOR DIRECTIVES
|
||||
// -----------------------------------------------------
|
||||
|
||||
// This header may be included by other board headers as "boards/pimoroni_pico_plus2w_rp2350.h"
|
||||
|
||||
// pico_cmake_set PICO_PLATFORM=rp2350
|
||||
|
||||
#ifndef _BOARDS_PIMORONI_PICO_PLUS2W_RP2350_H
|
||||
#define _BOARDS_PIMORONI_PICO_PLUS2W_RP2350_H
|
||||
|
||||
// For board detection
|
||||
#define PIMORONI_PICO_PLUS2_RP2350
|
||||
#define PIMORONI_PICO_PLUS2W_RP2350
|
||||
|
||||
// --- BOARD SPECIFIC ---
|
||||
#define SPICE_SPI 0
|
||||
#define SPICE_TX_MISO_PIN 32
|
||||
#define SPICE_RX_CS_PIN 33
|
||||
#define SPICE_NETLIGHT_SCK_PIN 34
|
||||
#define SPICE_RESET_MOSI_PIN 35
|
||||
#define SPICE_PWRKEY_BL_PIN 36
|
||||
|
||||
#define PIMORONI_PICO_PLUS2_USER_SW_PIN 45
|
||||
#define PIMORONI_PICO_PLUS2_PSRAM_CS_PIN 47
|
||||
|
||||
// -- CYW43 Wireless --
|
||||
#ifndef CYW43_PIN_WL_HOST_WAKE
|
||||
#define CYW43_PIN_WL_HOST_WAKE 24
|
||||
#endif
|
||||
|
||||
#ifndef CYW43_PIN_WL_REG_ON
|
||||
#define CYW43_PIN_WL_REG_ON 23
|
||||
#endif
|
||||
|
||||
#ifndef CYW43_WL_GPIO_COUNT
|
||||
#define CYW43_WL_GPIO_COUNT 3
|
||||
#endif
|
||||
|
||||
#ifndef CYW43_WL_GPIO_LED_PIN
|
||||
#define CYW43_WL_GPIO_LED_PIN 0
|
||||
#endif
|
||||
|
||||
// If CYW43_WL_GPIO_VBUS_PIN is defined then a CYW43 GPIO has to be used to read VBUS.
|
||||
// This can be passed to cyw43_arch_gpio_get to determine if the device is battery powered.
|
||||
// PICO_VBUS_PIN and CYW43_WL_GPIO_VBUS_PIN should not both be defined.
|
||||
|
||||
// no CYW43_WL_GPIO_VBUS_PIN
|
||||
|
||||
// If CYW43_USES_VSYS_PIN is defined then CYW43 uses the VSYS GPIO (defined by PICO_VSYS_PIN) for other purposes.
|
||||
// If this is the case, to use the VSYS GPIO it's necessary to ensure CYW43 is not using it.
|
||||
// This can be achieved by wrapping the use of the VSYS GPIO in cyw43_thread_enter / cyw43_thread_exit.
|
||||
|
||||
// no CYW43_USES_VSYS_PIN
|
||||
|
||||
// --- UART ---
|
||||
#ifndef PICO_DEFAULT_UART
|
||||
#define PICO_DEFAULT_UART 0
|
||||
#endif
|
||||
#ifndef PICO_DEFAULT_UART_TX_PIN
|
||||
#define PICO_DEFAULT_UART_TX_PIN 0
|
||||
#endif
|
||||
#ifndef PICO_DEFAULT_UART_RX_PIN
|
||||
#define PICO_DEFAULT_UART_RX_PIN 1
|
||||
#endif
|
||||
|
||||
// --- LED ---
|
||||
#ifndef PICO_DEFAULT_LED_PIN
|
||||
#define PICO_DEFAULT_LED_PIN 25
|
||||
#endif
|
||||
// no PICO_DEFAULT_WS2812_PIN
|
||||
|
||||
// --- I2C ---
|
||||
#ifndef PICO_DEFAULT_I2C
|
||||
#define PICO_DEFAULT_I2C 0
|
||||
#endif
|
||||
#ifndef PICO_DEFAULT_I2C_SDA_PIN
|
||||
#define PICO_DEFAULT_I2C_SDA_PIN 4
|
||||
#endif
|
||||
#ifndef PICO_DEFAULT_I2C_SCL_PIN
|
||||
#define PICO_DEFAULT_I2C_SCL_PIN 5
|
||||
#endif
|
||||
|
||||
// --- SPI ---
|
||||
#ifndef PICO_DEFAULT_SPI
|
||||
#define PICO_DEFAULT_SPI 0
|
||||
#endif
|
||||
#ifndef PICO_DEFAULT_SPI_SCK_PIN
|
||||
#define PICO_DEFAULT_SPI_SCK_PIN SPICE_NETLIGHT_SCK_PIN
|
||||
#endif
|
||||
#ifndef PICO_DEFAULT_SPI_TX_PIN
|
||||
#define PICO_DEFAULT_SPI_TX_PIN SPICE_RESET_MOSI_PIN
|
||||
#endif
|
||||
#ifndef PICO_DEFAULT_SPI_RX_PIN
|
||||
#define PICO_DEFAULT_SPI_RX_PIN SPICE_TX_MISO_PIN
|
||||
#endif
|
||||
#ifndef PICO_DEFAULT_SPI_CSN_PIN
|
||||
#define PICO_DEFAULT_SPI_CSN_PIN SPICE_RX_CS_PIN
|
||||
#endif
|
||||
|
||||
// --- FLASH ---
|
||||
|
||||
#define PICO_BOOT_STAGE2_CHOOSE_W25Q080 1
|
||||
|
||||
#ifndef PICO_FLASH_SPI_CLKDIV
|
||||
#define PICO_FLASH_SPI_CLKDIV 2
|
||||
#endif
|
||||
|
||||
// pico_cmake_set_default PICO_FLASH_SIZE_BYTES = (16 * 1024 * 1024)
|
||||
#ifndef PICO_FLASH_SIZE_BYTES
|
||||
#define PICO_FLASH_SIZE_BYTES (16 * 1024 * 1024)
|
||||
#endif
|
||||
|
||||
// The GPIO Pin used to read VBUS to determine if the device is battery powered.
|
||||
#ifndef PICO_VBUS_PIN
|
||||
#define PICO_VBUS_PIN 24
|
||||
#endif
|
||||
|
||||
// The GPIO Pin used to monitor VSYS. Typically you would use this with ADC.
|
||||
// There is an example in adc/read_vsys in pico-examples.
|
||||
#ifndef PICO_VSYS_PIN
|
||||
#define PICO_VSYS_PIN 43
|
||||
#endif
|
||||
|
||||
#ifndef PICO_RP2350_A2_SUPPORTED
|
||||
#define PICO_RP2350_A2_SUPPORTED 1
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@ -44,4 +44,9 @@ GP44,GPIO44
|
||||
GP45,GPIO45
|
||||
GP46,GPIO46
|
||||
GP47,GPIO47
|
||||
LED,GPIO25
|
||||
LED,GPIO25
|
||||
SPICE_TX,GPIO32
|
||||
SPICE_RX,GPIO33
|
||||
SPICE_NETLIGHT,GPIO34
|
||||
SPICE_RESET,GPIO35
|
||||
SPICE_PWRKEY,GPIO36
|
||||
|
38
micropython/examples/pico_plus_2/breakouts/lte-breakout.py
Normal file
38
micropython/examples/pico_plus_2/breakouts/lte-breakout.py
Normal file
@ -0,0 +1,38 @@
|
||||
import lte
|
||||
import time
|
||||
import requests
|
||||
from machine import Pin, PWM
|
||||
|
||||
|
||||
MOBILE_APN = "Your APN Here"
|
||||
|
||||
# Setting this to True will attempt to resume an existing connection
|
||||
RESUME = False
|
||||
|
||||
# Fix the eye-searing brightness of the onboard LED with PWM
|
||||
class Netlight:
|
||||
def __init__(self):
|
||||
self.pin = PWM(Pin("LED", Pin.OUT), freq=1000)
|
||||
|
||||
def value(self, value):
|
||||
self.pin.duty_u16(value * 2000)
|
||||
|
||||
|
||||
con = lte.LTE(MOBILE_APN, netlight_led=Netlight(), skip_reset=RESUME)
|
||||
con.start_ppp(connect=not RESUME)
|
||||
|
||||
# Do some requests! Internet stuff should just work now.
|
||||
try:
|
||||
t_start = time.time()
|
||||
for x in range(2):
|
||||
req = requests.get("https://shop.pimoroni.com/robots.txt")
|
||||
print(req)
|
||||
|
||||
finally:
|
||||
t_end = time.time()
|
||||
|
||||
print(f"Took: {t_end - t_start} seconds")
|
||||
|
||||
print("Disconnecting...")
|
||||
con.stop_ppp()
|
||||
print("Done!")
|
||||
209
micropython/modules_py/lte.py
Normal file
209
micropython/modules_py/lte.py
Normal file
@ -0,0 +1,209 @@
|
||||
from machine import UART, Pin
|
||||
from network import PPP
|
||||
from micropython import const
|
||||
import time
|
||||
|
||||
|
||||
DEFAULT_PIN_RST = 35
|
||||
DEFAULT_PIN_NETLIGHT = 34
|
||||
DEFAULT_PIN_RX = 33
|
||||
DEFAULT_PIN_TX = 32
|
||||
DEFAULT_UART_ID = 0
|
||||
|
||||
DEFAULT_UART_TIMEOUT = const(1)
|
||||
DEFAULT_UART_TIMEOUT_CHAR = const(1)
|
||||
DEFAULT_UART_RXBUF = const(1024)
|
||||
DEFAULT_UART_STARTUP_BAUD = const(115200)
|
||||
DEFAULT_UART_BAUD = const(460800)
|
||||
|
||||
|
||||
class CellularError(Exception):
|
||||
def __init__(self, message=None):
|
||||
self.message = "CellularError: " + message
|
||||
|
||||
|
||||
class LTE():
|
||||
def __init__(self, apn, uart=None, reset_pin=None, netlight_pin=None, netlight_led=None, skip_reset=False):
|
||||
self._apn = apn
|
||||
self._reset = reset_pin or Pin(DEFAULT_PIN_RST, Pin.OUT)
|
||||
self._uart = uart or UART(
|
||||
DEFAULT_UART_ID,
|
||||
tx=Pin(DEFAULT_PIN_TX, Pin.OUT),
|
||||
rx=Pin(DEFAULT_PIN_RX, Pin.OUT))
|
||||
|
||||
# Set PPP timeouts and rxbuf
|
||||
self._uart.init(
|
||||
timeout=DEFAULT_UART_TIMEOUT,
|
||||
timeout_char=DEFAULT_UART_TIMEOUT_CHAR,
|
||||
rxbuf=DEFAULT_UART_RXBUF)
|
||||
|
||||
if not skip_reset:
|
||||
self._reset.value(0)
|
||||
time.sleep(1.0)
|
||||
self._reset.value(1)
|
||||
|
||||
if netlight_led:
|
||||
self._led = netlight_led
|
||||
self._netlight = netlight_pin or Pin(DEFAULT_PIN_NETLIGHT, Pin.IN)
|
||||
self._netlight.irq(self._netlight_irq)
|
||||
|
||||
def _netlight_irq(self, pin):
|
||||
self._led.value(pin.value())
|
||||
|
||||
def iccid(self):
|
||||
try:
|
||||
return self._send_at_command("AT+CICCID", 1)
|
||||
except CellularError:
|
||||
return None
|
||||
|
||||
def status(self):
|
||||
lte_status = self._send_at_command("AT+CEREG?", 1)
|
||||
gsm_status = self._send_at_command("AT+CGREG?", 1)
|
||||
return lte_status, gsm_status
|
||||
|
||||
def signal_quality(self):
|
||||
try:
|
||||
response = self._send_at_command("AT+CSQ", 1)
|
||||
quality = int(response.split(":")[1].split(",")[0])
|
||||
db = -113 + (2 * quality) # conversion as per AT command set datasheet
|
||||
return db
|
||||
except CellularError:
|
||||
pass
|
||||
return None
|
||||
|
||||
def stop_ppp(self):
|
||||
self._ppp.disconnect()
|
||||
self._send_at_command(f"AT+IPR={DEFAULT_UART_STARTUP_BAUD}")
|
||||
self._flush_uart()
|
||||
|
||||
def start_ppp(self, baudrate=DEFAULT_UART_BAUD, connect=True):
|
||||
self._wait_ready(poll_time=1.0, timeout=30)
|
||||
|
||||
# Switch to a faster baudrate
|
||||
self._send_at_command(f"AT+IPR={baudrate}")
|
||||
self._flush_uart()
|
||||
self._uart.init(
|
||||
baudrate=baudrate,
|
||||
timeout=DEFAULT_UART_TIMEOUT,
|
||||
timeout_char=DEFAULT_UART_TIMEOUT_CHAR,
|
||||
rxbuf=DEFAULT_UART_RXBUF)
|
||||
self._wait_ready(poll_time=1.0)
|
||||
|
||||
# Connect!
|
||||
if connect:
|
||||
self.connect()
|
||||
|
||||
# This will just always time out!?
|
||||
# try:
|
||||
# self._send_at_command("ATD*99#", timeout=300)
|
||||
# except CellularError as e:
|
||||
# print(e)
|
||||
|
||||
# Force PPP to use modem's default settings...
|
||||
#time.sleep(2.0)
|
||||
self._flush_uart()
|
||||
self._uart.write("ATD*99#\r")
|
||||
self._uart.flush()
|
||||
#time.sleep(2.0)
|
||||
|
||||
self._ppp = PPP(self._uart)
|
||||
self._ppp.connect()
|
||||
while self._ppp.status() != 4:
|
||||
time.sleep(1.0)
|
||||
|
||||
return self._ppp.ifconfig()
|
||||
|
||||
def connect(self, timeout=60):
|
||||
print(" - setting up cellular uart")
|
||||
# connect to and flush the uart
|
||||
# consume any unsolicited messages first, we don't need those
|
||||
self._flush_uart()
|
||||
|
||||
print(" - waiting for cellular module to be ready")
|
||||
|
||||
# wait for the cellular module to respond to AT commands
|
||||
self._wait_ready()
|
||||
|
||||
self._send_at_command("ATE0") # disable local echo
|
||||
self._send_at_command(f"AT+CGDCONT=1,\"IP\",\"{self._apn}\"") # set apn and activate pdp context
|
||||
|
||||
# wait for roaming lte connection to be established
|
||||
giveup = time.time() + timeout
|
||||
status = None
|
||||
while status != "+CEREG: 0,5" and status != "+CEREG: 0,1":
|
||||
status = self._send_at_command("AT+CEREG?", 1)
|
||||
time.sleep(0.25)
|
||||
if time.time() > giveup:
|
||||
raise CellularError("timed out getting network registration")
|
||||
|
||||
# disable server and client certification validation
|
||||
self._send_at_command("AT+CSSLCFG=\"authmode\",0,0")
|
||||
self._send_at_command("AT+CSSLCFG=\"enableSNI\",0,1")
|
||||
|
||||
print(f" - SIM ICCID is {self.iccid()}")
|
||||
|
||||
def _wait_ready(self, poll_time=0.25, timeout=10):
|
||||
giveup = time.time() + timeout
|
||||
while time.time() <= giveup:
|
||||
try:
|
||||
self._send_at_command("AT")
|
||||
return # if __send_at_command doesn't throw an exception then we're good!
|
||||
except CellularError as e:
|
||||
print(e)
|
||||
time.sleep(poll_time)
|
||||
|
||||
raise CellularError("timed out waiting for AT response")
|
||||
|
||||
def _flush_uart(self):
|
||||
self._uart.flush()
|
||||
time.sleep(0.25)
|
||||
while self._uart.any():
|
||||
self._uart.read(self._uart.any())
|
||||
time.sleep(0.25)
|
||||
|
||||
def _send_at_command(self, command, result_lines=0, timeout=5.0):
|
||||
# consume any unsolicited messages first, we don't need those
|
||||
self._flush_uart()
|
||||
|
||||
self._uart.write(command + "\r")
|
||||
#print(f" - tx: {command}")
|
||||
self._uart.flush()
|
||||
status, data = self._read_result(result_lines, timeout=timeout)
|
||||
|
||||
print(" -", command, status, data)
|
||||
|
||||
if status == "TIMEOUT":
|
||||
#print.error(" !", command, status, data)
|
||||
raise CellularError(f"cellular module timed out for command {command}")
|
||||
|
||||
if status not in ["OK", "DOWNLOAD"]:
|
||||
#print(" !", command, status, data)
|
||||
raise CellularError(f"non 'OK' or 'DOWNLOAD' result for command {command}")
|
||||
|
||||
if result_lines == 1:
|
||||
return data[0]
|
||||
if result_lines > 1:
|
||||
return data
|
||||
return None
|
||||
|
||||
def _read_result(self, result_lines, timeout=1.0):
|
||||
status = None
|
||||
result = []
|
||||
start = time.ticks_ms()
|
||||
timeout *= 1000
|
||||
while len(result) < result_lines or status is None:
|
||||
if (time.ticks_ms() - start) > timeout:
|
||||
return "TIMEOUT", []
|
||||
|
||||
line = self._uart.readline()
|
||||
|
||||
if line:
|
||||
line = line.strip()
|
||||
if line in [b"OK", b"ERROR", b"DOWNLOAD"]:
|
||||
status = line.decode("ascii")
|
||||
elif line != b"":
|
||||
result.append(str(line, "ascii"))
|
||||
start = time.ticks_ms()
|
||||
|
||||
return status, result
|
||||
|
||||
Loading…
Reference in New Issue
Block a user