From c1a099328aa1ac9b84708466ece296ad3d86f45e Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 17 Nov 2024 12:06:06 -0800 Subject: [PATCH] Support LED digitalWrite on RP2350+CYW Also move "special GPIO" to 64 since the Pimoroni Pico 2W uses the RP2350B with 48 GPIOs. --- cores/rp2040/cyw43_wrappers.cpp | 37 +++++++++++++++++++ cores/rp2040/cyw43_wrappers.h | 13 +++++-- .../digital.cpp} | 35 ++---------------- variants/pimoroni_pico_plus_2w/init.cpp | 2 +- variants/rpipicow/digital.cpp | 33 +++++++++++++++++ variants/rpipicow/init.cpp | 2 +- variants/rpipicow/pins_arduino.h | 2 +- variants/sparkfun_thingplusrp2350/digital.cpp | 33 +++++++++++++++++ variants/sparkfun_thingplusrp2350/init.cpp | 2 +- 9 files changed, 121 insertions(+), 38 deletions(-) rename variants/{rpipicow/picow_digital.cpp => pimoroni_pico_plus_2w/digital.cpp} (53%) create mode 100644 variants/rpipicow/digital.cpp create mode 100644 variants/sparkfun_thingplusrp2350/digital.cpp diff --git a/cores/rp2040/cyw43_wrappers.cpp b/cores/rp2040/cyw43_wrappers.cpp index 0d80aab8c..ee98930ab 100644 --- a/cores/rp2040/cyw43_wrappers.cpp +++ b/cores/rp2040/cyw43_wrappers.cpp @@ -166,4 +166,41 @@ extern "C" void __unlockBluetooth() { async_context_release_lock(cyw43_arch_async_context()); } +extern "C" void __pinMode(pin_size_t pin, PinMode mode); +extern "C" void __digitalWrite(pin_size_t pin, PinStatus val); +extern "C" PinStatus __digitalRead(pin_size_t pin); + +extern "C" void cyw43_pinMode(pin_size_t pin, PinMode mode) { + if (!__isPicoW && (pin == PIN_LED)) { + pin = 25; // Silently swap in the Pico's LED + } + if (pin < 64) { + __pinMode(pin, mode); + } else { + // TBD - There is no GPIO direction control in the driver + } +} + +extern "C" void cyw43_digitalWrite(pin_size_t pin, PinStatus val) { + if (!__isPicoW && (pin == PIN_LED)) { + pin = 25; // Silently swap in the Pico's LED + } + if (pin < 64) { + __digitalWrite(pin, val); + } else { + cyw43_arch_gpio_put(pin - 64, val == HIGH ? 1 : 0); + } +} + +extern "C" PinStatus cyw43_digitalRead(pin_size_t pin) { + if (!__isPicoW && (pin == PIN_LED)) { + pin = 25; // Silently swap in the Pico's LED + } + if (pin < 64) { + return __digitalRead(pin); + } else { + return cyw43_arch_gpio_get(pin - 64) ? HIGH : LOW; + } +} + #endif diff --git a/cores/rp2040/cyw43_wrappers.h b/cores/rp2040/cyw43_wrappers.h index 58cb2f354..46d8a48c2 100644 --- a/cores/rp2040/cyw43_wrappers.h +++ b/cores/rp2040/cyw43_wrappers.h @@ -19,7 +19,14 @@ #pragma once +#include + extern bool __isPicoW; -extern "C" void init_cyw43_wifi(); -extern "C" void __lockBluetooth(); -extern "C" void __unlockBluetooth(); +extern "C" { + void init_cyw43_wifi(); + void __lockBluetooth(); + void __unlockBluetooth(); + void cyw43_pinMode(pin_size_t pin, PinMode mode); + void cyw43_digitalWrite(pin_size_t pin, PinStatus val); + PinStatus cyw43_digitalRead(pin_size_t pin); +} diff --git a/variants/rpipicow/picow_digital.cpp b/variants/pimoroni_pico_plus_2w/digital.cpp similarity index 53% rename from variants/rpipicow/picow_digital.cpp rename to variants/pimoroni_pico_plus_2w/digital.cpp index 468791162..2dc7a878e 100644 --- a/variants/rpipicow/picow_digital.cpp +++ b/variants/pimoroni_pico_plus_2w/digital.cpp @@ -18,43 +18,16 @@ */ #include "Arduino.h" -#include - -extern "C" void __pinMode(pin_size_t pin, PinMode mode); -extern "C" void __digitalWrite(pin_size_t pin, PinStatus val); -extern "C" PinStatus __digitalRead(pin_size_t pin); - -extern bool __isPicoW; +#include extern "C" void pinMode(pin_size_t pin, PinMode mode) { - if (!__isPicoW && (pin == PIN_LED)) { - pin = 25; // Silently swap in the Pico's LED - } - if (pin < 32) { - __pinMode(pin, mode); - } else { - // TBD - There is no GPIO direction control in the driver - } + cyw43_pinMode(pin, mode); } extern "C" void digitalWrite(pin_size_t pin, PinStatus val) { - if (!__isPicoW && (pin == PIN_LED)) { - pin = 25; // Silently swap in the Pico's LED - } - if (pin < 32) { - __digitalWrite(pin, val); - } else { - cyw43_arch_gpio_put(pin - 32, val == HIGH ? 1 : 0); - } + cyw43_digitalWrite(pin, val); } extern "C" PinStatus digitalRead(pin_size_t pin) { - if (!__isPicoW && (pin == PIN_LED)) { - pin = 25; // Silently swap in the Pico's LED - } - if (pin < 32) { - return __digitalRead(pin); - } else { - return cyw43_arch_gpio_get(pin - 32) ? HIGH : LOW; - } + return cyw43_digitalRead(pin); } diff --git a/variants/pimoroni_pico_plus_2w/init.cpp b/variants/pimoroni_pico_plus_2w/init.cpp index edb86a3ad..568ccdca8 100644 --- a/variants/pimoroni_pico_plus_2w/init.cpp +++ b/variants/pimoroni_pico_plus_2w/init.cpp @@ -18,7 +18,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -extern "C" void init_cyw43_wifi(); +#include extern "C" void initVariant() { init_cyw43_wifi(); diff --git a/variants/rpipicow/digital.cpp b/variants/rpipicow/digital.cpp new file mode 100644 index 000000000..2dc7a878e --- /dev/null +++ b/variants/rpipicow/digital.cpp @@ -0,0 +1,33 @@ +/* + pinMode and digitalRead/Write for the Raspberry Pi Pico W RP2040 + Copyright (c) 2022 Earle F. Philhower, III + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "Arduino.h" +#include + +extern "C" void pinMode(pin_size_t pin, PinMode mode) { + cyw43_pinMode(pin, mode); +} + +extern "C" void digitalWrite(pin_size_t pin, PinStatus val) { + cyw43_digitalWrite(pin, val); +} + +extern "C" PinStatus digitalRead(pin_size_t pin) { + return cyw43_digitalRead(pin); +} diff --git a/variants/rpipicow/init.cpp b/variants/rpipicow/init.cpp index edb86a3ad..568ccdca8 100644 --- a/variants/rpipicow/init.cpp +++ b/variants/rpipicow/init.cpp @@ -18,7 +18,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -extern "C" void init_cyw43_wifi(); +#include extern "C" void initVariant() { init_cyw43_wifi(); diff --git a/variants/rpipicow/pins_arduino.h b/variants/rpipicow/pins_arduino.h index bf9d6baa1..639ae582d 100644 --- a/variants/rpipicow/pins_arduino.h +++ b/variants/rpipicow/pins_arduino.h @@ -6,7 +6,7 @@ #include // LEDs -#define PIN_LED (32u) +#define PIN_LED (64u) // Serial #define PIN_SERIAL1_TX (0u) diff --git a/variants/sparkfun_thingplusrp2350/digital.cpp b/variants/sparkfun_thingplusrp2350/digital.cpp new file mode 100644 index 000000000..2dc7a878e --- /dev/null +++ b/variants/sparkfun_thingplusrp2350/digital.cpp @@ -0,0 +1,33 @@ +/* + pinMode and digitalRead/Write for the Raspberry Pi Pico W RP2040 + Copyright (c) 2022 Earle F. Philhower, III + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "Arduino.h" +#include + +extern "C" void pinMode(pin_size_t pin, PinMode mode) { + cyw43_pinMode(pin, mode); +} + +extern "C" void digitalWrite(pin_size_t pin, PinStatus val) { + cyw43_digitalWrite(pin, val); +} + +extern "C" PinStatus digitalRead(pin_size_t pin) { + return cyw43_digitalRead(pin); +} diff --git a/variants/sparkfun_thingplusrp2350/init.cpp b/variants/sparkfun_thingplusrp2350/init.cpp index edb86a3ad..568ccdca8 100644 --- a/variants/sparkfun_thingplusrp2350/init.cpp +++ b/variants/sparkfun_thingplusrp2350/init.cpp @@ -18,7 +18,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -extern "C" void init_cyw43_wifi(); +#include extern "C" void initVariant() { init_cyw43_wifi();