Compare commits
@@ -0,0 +1,219 @@
|
||||
# Run whenever a PR is generated or updated.
|
||||
|
||||
|
||||
name: Arduino-Pico CI
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
|
||||
env:
|
||||
TRAVIS_BUILD_DIR: ${{ github.workspace }}
|
||||
TRAVIS_TAG: ${{ github.ref }}
|
||||
|
||||
|
||||
jobs:
|
||||
|
||||
# Me no spell so good
|
||||
code-spell:
|
||||
name: Check spelling
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
- name: Run codespell
|
||||
uses: codespell-project/actions-codespell@master
|
||||
with:
|
||||
skip: ./ArduinoCore-API,./libraries/ESP8266SdFat,./libraries/Adafruit_TinyUSB_Arduino,./libraries/LittleFS/lib,./tools/pyserial,./pico-sdk,./.github,./docs/i2s.rst,./cores/rp2040/api,./libraries/FreeRTOS,./tools/libbearssl/bearssl,./include,./libraries/WiFi/examples/BearSSL_Server,./ota/uzlib,./libraries/http-parser/lib,./libraries/WebServer/examples/HelloServerBearSSL/HelloServerBearSSL.ino,./libraries/HTTPUpdateServer/examples/SecureBearSSLUpdater/SecureBearSSLUpdater.ino,./.git
|
||||
ignore_words_list: ser,dout
|
||||
|
||||
# Consistent style
|
||||
astyle:
|
||||
name: Style, Boards, Package
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: false
|
||||
- name: Check package references
|
||||
run: |
|
||||
./tests/ci/pkgrefs_test.sh
|
||||
- name: Check boards.txt was not edited after makeboards.py
|
||||
run: |
|
||||
./tools/makeboards.py
|
||||
# If anything changed, GIT should return an error and fail the test
|
||||
git diff --exit-code
|
||||
- name: Run astyle on all code/examples
|
||||
run: |
|
||||
sudo apt update
|
||||
sudo apt install astyle
|
||||
./tests/restyle.sh
|
||||
# If anything changed, GIT should return an error and fail the test
|
||||
git diff --exit-code
|
||||
|
||||
# Build all examples on linux (core and Arduino IDE)
|
||||
build-linux:
|
||||
name: Build ${{ matrix.chunk }}
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
chunk: [0, 1, 2, 3, 4, 5]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
- uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- name: Cache Linux toolchain
|
||||
id: cache-linux
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ./tools/dist
|
||||
key: ${{ runner.os }}-${{ hashFiles('package/package_pico_index.template.json', 'tests/common.sh') }}
|
||||
- name: Build Sketches
|
||||
env:
|
||||
BUILD_PARITY: custom
|
||||
mod: 6
|
||||
rem: ${{ matrix.chunk }}
|
||||
run: |
|
||||
cd pico-sdk
|
||||
git submodule update --init
|
||||
cd ..
|
||||
bash ./tests/build.sh
|
||||
|
||||
# Build TinyUSB examples, requires custom build command line
|
||||
build-tinyusb:
|
||||
name: Build TinyUSB Examples
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
- uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- name: Cache Linux toolchain
|
||||
id: cache-linux
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ./tools/dist
|
||||
key: ${{ runner.os }}-${{ hashFiles('package/package_pico_index.template.json', 'tests/common.sh') }}
|
||||
- name: Build Sketches
|
||||
env:
|
||||
BUILD_PARITY: custom
|
||||
run: |
|
||||
cd pico-sdk
|
||||
git submodule update --init
|
||||
cd ..
|
||||
bash ./tests/build-tinyusb.sh
|
||||
|
||||
# Single build under Windows to ensure the Win toolchain is good.
|
||||
build-windows:
|
||||
name: Windows
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
- uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- name: Cache Windows toolchain
|
||||
id: cache-windows
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ./tools/dist
|
||||
key: ${{ runner.os }}-${{ hashFiles('package/package_pico_index.template.json', 'tests/common.sh') }}
|
||||
- name: Build Sketch
|
||||
env:
|
||||
WINDOWS: 1
|
||||
BUILD_PARITY: custom
|
||||
mod: 500
|
||||
rem: 1
|
||||
run: |
|
||||
# Windows has python3 already installed, but it's called "python".
|
||||
# Copy python.exe to the proper name so scripts "just work".
|
||||
try { Get-Command python3 } catch { copy (get-command python).source (get-command python).source.Replace("python.exe", "python3.exe") }
|
||||
cd pico-sdk
|
||||
git submodule update --init
|
||||
cd ..
|
||||
bash ./tests/build.sh
|
||||
|
||||
|
||||
# Single build under macOS to ensure the Mac toolchain is good.
|
||||
build-mac:
|
||||
name: Mac
|
||||
runs-on: macOS-12
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
- uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- name: Cache Mac toolchain
|
||||
id: cache-mac
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ./tools/dist
|
||||
key: ${{ runner.os }}-${{ hashFiles('package/package_pico_index.template.json', 'tests/common.sh') }}
|
||||
- name: Build Sketch
|
||||
env:
|
||||
MACOSX: 1
|
||||
BUILD_PARITY: custom
|
||||
mod: 500
|
||||
rem: 1
|
||||
run: |
|
||||
cd pico-sdk
|
||||
git submodule update --init
|
||||
cd ..
|
||||
bash ./tests/build.sh
|
||||
|
||||
# Build a few examples with PlatformIO to test if integration works
|
||||
build-platformio:
|
||||
name: Build PlatformIO Examples
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: 'true'
|
||||
- name: Initialize needed submodules
|
||||
run: |
|
||||
cd pico-sdk
|
||||
git submodule update --init
|
||||
cd ../libraries/Adafruit_TinyUSB_Arduino
|
||||
git submodule update --init
|
||||
cd ../..
|
||||
- name: Cache pip
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/.cache/pip
|
||||
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-pip-
|
||||
- name: Cache PlatformIO
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/.platformio
|
||||
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- name: Install PlatformIO
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install --upgrade platformio
|
||||
pio pkg install --global --platform https://github.com/maxgerhardt/platform-raspberrypi.git
|
||||
pio pkg install --global --tool symlink://.
|
||||
- name: Build Multicore Example
|
||||
run: pio ci --board=rpipico --board=adafruit_feather -O "platform_packages=framework-arduinopico@symlink:///home/runner/work/arduino-pico/arduino-pico" libraries/rp2040/examples/Multicore/Multicore.ino
|
||||
- name: Build Fade Example
|
||||
run: pio ci --board=rpipico --board=adafruit_feather -O "platform_packages=framework-arduinopico@symlink:///home/runner/work/arduino-pico/arduino-pico" libraries/rp2040/examples/Fade/Fade.ino
|
||||
- name: Build TinyUSB Example
|
||||
run: pio ci --board=rpipico --board=adafruit_feather -O "platform_packages=framework-arduinopico@symlink:///home/runner/work/arduino-pico/arduino-pico" -O "build_flags=-DUSE_TINYUSB" libraries/Adafruit_TinyUSB_Arduino/examples/CDC/cdc_multi/cdc_multi.ino
|
||||
- name: Build WiFi Example
|
||||
run: pio ci --board=rpipicow -O "platform_packages=framework-arduinopico@symlink:///home/runner/work/arduino-pico/arduino-pico" libraries/WiFi/examples/ScanNetworks/ScanNetworks.ino
|
||||
- name: Build Signed OTA Example
|
||||
run: pio ci --board=rpipicow -O "platform_packages=framework-arduinopico@symlink:///home/runner/work/arduino-pico/arduino-pico" libraries/ArduinoOTA/examples/SignedOTA/SignedOTA.ino
|
||||
@@ -0,0 +1,47 @@
|
||||
name: Arduino-Pico Release Publisher
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
|
||||
jobs:
|
||||
package:
|
||||
name: Update master JSON file
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
- name: Cache pip
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/.cache/pip
|
||||
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-pip-
|
||||
- name: Cache PlatformIO
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/.platformio
|
||||
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
|
||||
- uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- name: Install PlatformIO
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install --upgrade platformio
|
||||
- name: Deploy updated JSON
|
||||
env:
|
||||
TRAVIS_BUILD_DIR: ${{ github.workspace }}
|
||||
BUILD_TYPE: package
|
||||
CI_GITHUB_API_KEY: ${{ secrets.GITHUB_TOKEN }}
|
||||
PLATFORMIO_AUTH_TOKEN: ${{ secrets.PLATFORMIO_AUTH_TOKEN }}
|
||||
run: |
|
||||
pip3 install PyGithub
|
||||
TAG=$(git describe --exact-match --tags)
|
||||
curl -L -o package_rp2040_index.json "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases/download/$TAG/package_rp2040_index.json"
|
||||
./package/update_release.py --token ${CI_GITHUB_API_KEY} --repo "$GITHUB_REPOSITORY" --tag global package_rp2040_index.json
|
||||
# Upload to Platform.IO
|
||||
curl -LO $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases/download/$TAG/rp2040-$TAG.zip
|
||||
pio package publish rp2040-$TAG.zip --non-interactive
|
||||
@@ -0,0 +1,42 @@
|
||||
# Whenever a tag of the form #.xxxx is pushed against master, generate a
|
||||
# draft release and upload the ZIP and JSON file to it. Maintainers then
|
||||
# will manually add the changelist and publish it.
|
||||
|
||||
name: Arduino-Pico Draft Release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
# Run for tags of the x.x.x* form (i.e. 3.0.0, 3.0.0-beta, etc.).
|
||||
- '[0-9]+.[0-9]+.[0-9]+*'
|
||||
|
||||
jobs:
|
||||
package:
|
||||
name: Package
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
fetch-depth: 0
|
||||
- uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- name: Build package JSON
|
||||
env:
|
||||
TRAVIS_BUILD_DIR: ${{ github.workspace }}
|
||||
BUILD_TYPE: package
|
||||
CI_GITHUB_API_KEY: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
(cd pico-sdk && git submodule update --init)
|
||||
(cd ./package && bash ./build_boards_manager_package.sh)
|
||||
pip3 install PyGithub
|
||||
# Create a draft release and upload the ZIP and JSON files.
|
||||
# This draft is not visible to normal users and needs to be
|
||||
# updated manually with release notes and published from the
|
||||
# GitHub web interface.
|
||||
json=$(find ./package/versions -name package_rp2040_index.json)
|
||||
log=$(find ./package/versions -name package_rp2040_index.log)
|
||||
zip=$(find ./package/versions -name rp2040*zip)
|
||||
tag=$(find ./package/versions -name package_rp2040_index.tag -exec cat \{\} \;)
|
||||
python3 ./package/upload_release.py --repo "$GITHUB_REPOSITORY" --token "$CI_GITHUB_API_KEY" --tag "$tag" --name "Release $tag" --msg "@$log" "$zip" "$json"
|
||||
+4
-2
@@ -1,5 +1,7 @@
|
||||
|
||||
.DS_Store
|
||||
system
|
||||
tools/dist
|
||||
cores/rp2040/api
|
||||
docs/_build
|
||||
ota/build
|
||||
tools/libpico/build
|
||||
platform.local.txt
|
||||
|
||||
+26
-5
@@ -3,7 +3,7 @@
|
||||
url = https://github.com/earlephilhower/ArduinoCore-API.git
|
||||
[submodule "pico-sdk"]
|
||||
path = pico-sdk
|
||||
url = https://github.com/raspberrypi/pico-sdk.git
|
||||
url = https://github.com/earlephilhower/pico-sdk.git
|
||||
[submodule "system/pyserial"]
|
||||
path = tools/pyserial
|
||||
url = https://github.com/pyserial/pyserial.git
|
||||
@@ -11,8 +11,29 @@
|
||||
path = libraries/LittleFS/lib/littlefs
|
||||
url = https://github.com/littlefs-project/littlefs.git
|
||||
[submodule "libraries/SdFat"]
|
||||
path = libraries/SdFat
|
||||
path = libraries/ESP8266SdFat
|
||||
url = https://github.com/earlephilhower/ESP8266SdFat.git
|
||||
[submodule "pico-extras"]
|
||||
path = pico-extras
|
||||
url = https://github.com/raspberrypi/pico-extras.git
|
||||
[submodule "libraries/Keyboard"]
|
||||
path = libraries/Keyboard
|
||||
url = https://github.com/earlephilhower/Keyboard
|
||||
[submodule "libraries/Mouse"]
|
||||
path = libraries/Mouse
|
||||
url = https://github.com/earlephilhower/Mouse
|
||||
[submodule "libraries/Joystick"]
|
||||
path = libraries/Joystick
|
||||
url = https://github.com/benjaminaigner/Joystick
|
||||
[submodule "libraries/Adafruit_TinyUSB_Arduino"]
|
||||
path = libraries/Adafruit_TinyUSB_Arduino
|
||||
url = https://github.com/adafruit/Adafruit_TinyUSB_Arduino.git
|
||||
[submodule "libraries/FreeRTOS/lib/FreeRTOS-Kernel"]
|
||||
path = libraries/FreeRTOS/lib/FreeRTOS-Kernel
|
||||
url = https://github.com/earlephilhower/FreeRTOS-Kernel.git
|
||||
[submodule "tools/libbearssl/bearssl"]
|
||||
path = tools/libbearssl/bearssl
|
||||
url = https://github.com/earlephilhower/bearssl-esp8266.git
|
||||
[submodule "ota/uzlib"]
|
||||
path = ota/uzlib
|
||||
url = https://github.com/pfalcon/uzlib.git
|
||||
[submodule "libraries/http_parser/lib/http-parser"]
|
||||
path = libraries/http-parser/lib/http-parser
|
||||
url = https://github.com/nodejs/http-parser.git
|
||||
|
||||
+1
-1
Submodule ArduinoCore-API updated: 9da5373517...d955d75747
@@ -1,15 +1,61 @@
|
||||
# Arduino-Pico [](https://gitter.im/arduino-pico/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
# Arduino-Pico
|
||||
[](https://github.com/earlephilhower/arduino-pico/releases)
|
||||
[](https://gitter.im/arduino-pico/community)
|
||||
|
||||
Raspberry Pi Pico Arduino core, for all RP2040 boards
|
||||
|
||||
This is a port of the RP2040 (Raspberry Pi Pico processor) to the Arduino ecosystem.
|
||||
This is a port of the RP2040 (Raspberry Pi Pico processor) to the Arduino ecosystem. It uses the bare Raspberry Pi Pico SDK and a custom GCC 10.3/Newlib 4.0 toolchain.
|
||||
|
||||
It uses a custom toolset with GCC 10.2 and Newlib 4.0.0, not depending on system-installed prerequisites. https://github.com/earlephilhower/pico-quick-toolchain
|
||||
# Documentation
|
||||
See https://arduino-pico.readthedocs.io/en/latest/ along with the examples for more detailed usage information.
|
||||
|
||||
There is automated discovery of boards in bootloader mode, so they show up in the IDE, and the upload command works using the Microsoft UF2 tool (included).
|
||||
|
||||
# Detailed Documentation
|
||||
Refer to the examples and https://arduino-pico.readthedocs.io/en/latest/ for more detailed usage information.
|
||||
# Supported Boards
|
||||
* Raspberry Pi Pico
|
||||
* Raspberry Pi Pico W
|
||||
* Adafruit Feather RP2040
|
||||
* Adafruit Feather RP2040 SCORPIO
|
||||
* Adafruit ItsyBitsy RP2040
|
||||
* Adafruit KB2040
|
||||
* Adafruit Macropad RP2040
|
||||
* Adafruit QTPy RP2040
|
||||
* Adafruit STEMMA Friend RP2040
|
||||
* Adafruit Trinkey RP2040 QT
|
||||
* Arduino Nano RP2040 Connect
|
||||
* BridgeTek IDM2040-7A
|
||||
* Cytron Maker Pi RP2040
|
||||
* Cytron Maker Nano RP2040
|
||||
* DatanoiseTV PicoADK+
|
||||
* DeRuiLab FlyBoard2040 Core
|
||||
* DFRobot Beetle RP2040
|
||||
* ElectronicCats Hunter Cat NFC
|
||||
* ExtremeElectronics RC2040
|
||||
* Invector Labs Challenger RP2040 WiFi
|
||||
* Invector Labs Challenger RP2040 WiFi/BLE
|
||||
* Invector Labs Challenger NB RP2040 WiFi
|
||||
* Invector Labs Challenger RP2040 LTE
|
||||
* Invector Labs Challenger RP2040 LoRa
|
||||
* Invector Labs Challenger RP2040 SubGHz
|
||||
* Invector Labs Challenger RP2040 SD/RTC
|
||||
* Invector Labs RPICO32
|
||||
* Melopero Cookie RP2040
|
||||
* Melopero Shake RP2040
|
||||
* nullbits Bit-C PRO
|
||||
* Pimoroni PGA2040
|
||||
* Seeed XIAO RP2040
|
||||
* Solder Party RP2040 Stamp
|
||||
* SparkFun ProMicro RP2040
|
||||
* SparkFun Thing Plus RP2040
|
||||
* uPesy RP2040 DevKit
|
||||
* Viyalab Mizu RP2040
|
||||
* Waveshare RP2040 Zero
|
||||
* Waveshare RP2040 One
|
||||
* Waveshare RP2040 Plus
|
||||
* Waveshare RP2040 LCD 0.96
|
||||
* Waveshare RP2040 LCD 1.28
|
||||
* WIZnet W5100S-EVB-Pico
|
||||
* WIZnet W5500-EVB-Pico
|
||||
* WIZnet WizFi360-EVB-Pico
|
||||
* Generic (configurable flash, I/O pins)
|
||||
|
||||
# Installing via Arduino Boards Manager
|
||||
**Windows Users**: Please do not use the Windows Store version of the actual Arduino application
|
||||
@@ -19,7 +65,6 @@ drivers it suggests. Otherwise the Pico board may not be detected. Also, if tr
|
||||
2.0 beta Arduino please install the release 1.8 version beforehand to ensure needed device drivers
|
||||
are present. (See #20 for more details.)
|
||||
|
||||
|
||||
Open up the Arduino IDE and go to File->Preferences.
|
||||
|
||||
In the dialog that pops up, enter the following URL in the "Additional Boards Manager URLs" field:
|
||||
@@ -45,8 +90,6 @@ cd ~/Arduino/hardware/pico/rp2040
|
||||
git submodule update --init
|
||||
cd pico-sdk
|
||||
git submodule update --init
|
||||
cd pico-extras
|
||||
git submodule update --init
|
||||
cd ../tools
|
||||
python3 ./get.py
|
||||
`````
|
||||
@@ -60,7 +103,7 @@ If you follow Les' step-by-step you will also have a fully functional `CMake`-ba
|
||||
To upload your first sketch, you will need to hold the BOOTSEL button down while plugging in the Pico to your computer.
|
||||
Then hit the upload button and the sketch should be transferred and start to run.
|
||||
|
||||
After the first upload, this should not be necessary as the `arduino-pico` core has auto-reset support.
|
||||
After the first upload, this should not be necessary as the `arduino-pico` core has auto-reset support.
|
||||
Select the appropriate serial port shown in the Arduino Tools->Port->Serial Port menu once (this setting will stick and does not need to be
|
||||
touched for multiple uploads). This selection allows the auto-reset tool to identify the proper device to reset.
|
||||
Them hit the upload button and your sketch should upload and run.
|
||||
@@ -72,8 +115,8 @@ follow the initial procedure of holding the BOOTSEL button down while plugging i
|
||||
The onboard flash filesystem for the Pico, LittleFS, lets you upload a filesystem image from the sketch directory for your sketch to use. Download the needed plugin from
|
||||
* https://github.com/earlephilhower/arduino-pico-littlefs-plugin/releases
|
||||
|
||||
To install, follow the directions in
|
||||
* https://github.com/earlephilhower/arduino-pico-littlefs-plugin/blob/master/README.md
|
||||
To install, follow the directions in
|
||||
* https://github.com/earlephilhower/arduino-pico-littlefs-plugin/blob/master/README.md
|
||||
|
||||
For detailed usage information, please check the ESP8266 repo documentation (ignore SPIFFS related notes) available at
|
||||
* https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html
|
||||
@@ -93,31 +136,47 @@ The first line creates a file with the USB vendor and ID of the Picoprobe and te
|
||||
|
||||
Once Picoprobe permissions are set up properly, then select the board "Raspberry Pi Pico (Picoprobe)" in the Tools menu and upload as normal.
|
||||
|
||||
# Debugging with Picoprobe, OpenOCD, and GDB
|
||||
The installed tools include a version of OpenOCD (in the pqt-openocd directory) and GDB (in the pqt-gcc directory). These may be used to run GDB in an interactive window as documented in the Pico Getting Started manuals from the Raspberry Pi Foundation.
|
||||
# Uploading Sketches with pico-debug
|
||||
[pico-debug](https://github.com/majbthrd/pico-debug/) differs from Picoprobe in that pico-debug is a virtual debug pod that runs side-by-side on the same RP2040 that you run your code on; so, you only need one RP2040 board instead of two. pico-debug also differs from Picoprobe in that pico-debug is standards-based; it uses the CMSIS-DAP protocol, which means even software not specially written for the Raspberry Pi Pico can support it. pico-debug uses OpenOCD to handle your sketch uploads, and debugging can be accomplished with CMSIS-DAP capable debuggers including GDB.
|
||||
|
||||
# Status of Port
|
||||
Relatively stable and very functional, but bug reports and PRs always accepted.
|
||||
* digitalWrite/Read
|
||||
* shiftIn/Out
|
||||
* SPI master
|
||||
* analogWrite/PWM
|
||||
* tone/noTone
|
||||
* Wire/I2C Master and Slave
|
||||
* EEPROM
|
||||
* USB Serial(ACM) w/automatic reboot-to-UF2 upload)
|
||||
* Hardware UART
|
||||
* Servo, glitchless
|
||||
* Overclocking and underclocking from the menus
|
||||
* analogRead and Pico chip temperature
|
||||
Under Windows and macOS, any user should be able to access pico-debug automatically, but under Linux `udev` must be told about the device and to allow normal users access.
|
||||
|
||||
To set up user-level access to all CMSIS-DAP adapters on Ubuntu (and other OSes which use `udev`):
|
||||
````
|
||||
echo 'ATTRS{product}=="*CMSIS-DAP*", MODE="664", GROUP="plugdev"' | sudo tee -a /etc/udev/rules.d/98-CMSIS-DAP.rules
|
||||
sudo udevadm control --reload
|
||||
````
|
||||
|
||||
The first line creates a file that recognizes all CMSIS-DAP adapters and tells UDEV to give users full access to it. The second causes `udev` to load this new rule. Note that you will need to unplug and re-plug in your device the first time you create this file, to allow udev to make the device node properly.
|
||||
|
||||
Once CMSIS-DAP permissions are set up properly, then select the board "Raspberry Pi Pico (pico-debug)" in the Tools menu.
|
||||
|
||||
When first connecting the USB port to your PC, you must copy [pico-debug-gimmecache.uf2](https://github.com/majbthrd/pico-debug/releases/) to the Pi Pico to load pico-debug into RAM; after this, upload as normal.
|
||||
|
||||
# Debugging with Picoprobe/pico-debug, OpenOCD, and GDB
|
||||
The installed tools include a version of OpenOCD (in the pqt-openocd directory) and GDB (in the pqt-gcc directory). These may be used to run GDB in an interactive window as documented in the Pico Getting Started manuals from the Raspberry Pi Foundation. For [pico-debug](https://github.com/majbthrd/pico-debug/), replace the raspberrypi-swd and picoprobe example OpenOCD arguments of "-f interface/raspberrypi-swd.cfg -f target/rp2040.cfg" or "-f interface/picoprobe.cfg -f target/rp2040.cfg" respectively in the Pico Getting Started manual with "-f board/pico-debug.cfg".
|
||||
|
||||
# Features
|
||||
* Adafruit TinyUSB Arduino (USB mouse, keyboard, flash drive, generic HID, CDC Serial, MIDI, WebUSB, others)
|
||||
* Generic Arduino USB Serial, Keyboard, and Mouse emulation
|
||||
* WiFi (Pico W)
|
||||
* HTTP client and server (WebServer)
|
||||
* SSL/TLS/HTTPS
|
||||
* Over-the-Air (OTA) upgrades
|
||||
* Filesystems (LittleFS and SD/SDFS)
|
||||
* I2S audio output
|
||||
* printf (i.e. debug) output over USB serial
|
||||
* Multicore support (setup1() and loop1())
|
||||
* FreeRTOS SMP support
|
||||
* Overclocking and underclocking from the menus
|
||||
* digitalWrite/Read, shiftIn/Out, tone, analogWrite(PWM)/Read, temperature
|
||||
* Peripherals: SPI master, Wire(I2C) master/slave, dual UART, emulated EEPROM, I2S audio input, I2S audio output, Servo
|
||||
* printf (i.e. debug) output over USB serial
|
||||
|
||||
The RP2040 PIO state machines (SMs) are used to generate jitter-free:
|
||||
* Servos
|
||||
* Tones
|
||||
* I2S Input
|
||||
* I2S Output
|
||||
* Software UARTs (Serial ports)
|
||||
|
||||
# Tutorials from Across the Web
|
||||
Here are some links to coverage and additional tutorials for using `arduino-pico`
|
||||
@@ -126,6 +185,7 @@ Here are some links to coverage and additional tutorials for using `arduino-pico
|
||||
* Pre-release Adafruit QT Py RP2040 - https://www.youtube.com/watch?v=sfC1msqXX0I
|
||||
* Adafruit Feather RP2040 running LCD + TMP117 - https://www.youtube.com/watch?v=fKDeqZiIwHg
|
||||
* Demonstration of Servos and I2C in Korean - https://cafe.naver.com/arduinoshield/1201
|
||||
* Home Assistant Pico W integration starter project using Arduino - https://github.com/daniloc/PicoW_HomeAssistant_Starter
|
||||
|
||||
# Contributing
|
||||
If you want to contribute or have bugfixes, drop me a note at <earlephilhower@yahoo.com> or open an issue/PR here.
|
||||
@@ -133,11 +193,20 @@ If you want to contribute or have bugfixes, drop me a note at <earlephilhower@ya
|
||||
# Licensing and Credits
|
||||
* The [Arduino IDE and ArduinoCore-API](https://arduino.cc) are developed and maintained by the Arduino team. The IDE is licensed under GPL.
|
||||
* The [RP2040 GCC-based toolchain](https://github.com/earlephilhower/pico-quick-toolchain) is licensed under under the GPL.
|
||||
* The [Pico-SDK](https://github.com/raspberrypi/pico-sdk) and [Pico-Extras](https://github.com/raspberrypi/pico-extras) are by Raspberry Pi (Trading) Ltd and licensed under the BSD 3-Clause license.
|
||||
* [Arduino-Pico](https://github.com/earlephilhower/arduino-pico) core files are licenses under the LGPL.
|
||||
* The [Pico-SDK](https://github.com/raspberrypi/pico-sdk) is by Raspberry Pi (Trading) Ltd and licensed under the BSD 3-Clause license.
|
||||
* [Arduino-Pico](https://github.com/earlephilhower/arduino-pico) core files are licensed under the LGPL.
|
||||
* [LittleFS](https://github.com/ARMmbed/littlefs) library written by ARM Limited and released under the [BSD 3-clause license](https://github.com/ARMmbed/littlefs/blob/master/LICENSE.md).
|
||||
* [UF2CONV.PY](https://github.com/microsoft/uf2) is by Microsoft Corporatio and licensed under the MIT license.
|
||||
* Some filesystem code taken from the [ESP8266 Arduino Core](https://github.com/esp8266/Arduino) and licensed under the LGPL.
|
||||
* [UF2CONV.PY](https://github.com/microsoft/uf2) is by Microsoft Corporation and licensed under the MIT license.
|
||||
* Networking and filesystem code taken from the [ESP8266 Arduino Core](https://github.com/esp8266/Arduino) and licensed under the LGPL.
|
||||
* DHCP server for AP host mode from the [Micropython Project](https://micropython.org), distributed under the MIT License.
|
||||
* [FreeRTOS](https://freertos.org) is Copyright Amazon.com, Inc. or its affiliates, and distributed under the MIT license.
|
||||
* [lwIP](https://savannah.nongnu.org/projects/lwip/) is (c) the Swedish Institute of Computer Science and licenced under the BSD license.
|
||||
* [BearSSL](https://bearssl.org) library written by Thomas Pornin, is distributed under the [MIT License](https://bearssl.org/#legal-details).
|
||||
* [UZLib](https://github.com/pfalcon/uzlib) is copyright (c) 2003 Joergen Ibsen and distributed under the zlib license.
|
||||
* [LEAmDNS](https://github.com/LaborEtArs/ESP8266mDNS) is copyright multiple authors and distributed under the MIT license.
|
||||
* [http-parser](https://github.com/nodejs/http-parser) is copyright Joyent, Inc. and other Node contributors.
|
||||
* WebServer code modified from the [ESP32 WebServer](https://github.com/espressif/arduino-esp32/tree/master/libraries/WebServer) and is copyright (c) 2015 Ivan Grokhotkov and others
|
||||
|
||||
-Earle F. Philhower, III
|
||||
|
||||
-Earle F. Philhower, III
|
||||
earlephilhower@yahoo.com
|
||||
|
||||
-315
@@ -1,315 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "hardware/regs/m0plus.h"
|
||||
#include "hardware/platform_defs.h"
|
||||
#include "hardware/regs/addressmap.h"
|
||||
#include "hardware/regs/sio.h"
|
||||
#include "pico/binary_info/defs.h"
|
||||
|
||||
#ifdef NDEBUG
|
||||
#ifndef COLLAPSE_IRQS
|
||||
#define COLLAPSE_IRQS
|
||||
#endif
|
||||
#endif
|
||||
|
||||
.syntax unified
|
||||
.cpu cortex-m0plus
|
||||
.thumb
|
||||
|
||||
.section .vectors, "ax"
|
||||
.align 2
|
||||
|
||||
.global __vectors
|
||||
__vectors:
|
||||
.word __StackTop
|
||||
.word _reset_handler
|
||||
.word isr_nmi
|
||||
.word isr_hardfault
|
||||
.word isr_invalid // Reserved, should never fire
|
||||
.word isr_invalid // Reserved, should never fire
|
||||
.word isr_invalid // Reserved, should never fire
|
||||
.word isr_invalid // Reserved, should never fire
|
||||
.word isr_invalid // Reserved, should never fire
|
||||
.word isr_invalid // Reserved, should never fire
|
||||
.word isr_invalid // Reserved, should never fire
|
||||
.word isr_svcall
|
||||
.word isr_invalid // Reserved, should never fire
|
||||
.word isr_invalid // Reserved, should never fire
|
||||
.word isr_pendsv
|
||||
.word isr_systick
|
||||
.word isr_irq0
|
||||
.word isr_irq1
|
||||
.word isr_irq2
|
||||
.word isr_irq3
|
||||
.word isr_irq4
|
||||
.word isr_irq5
|
||||
.word isr_irq6
|
||||
.word isr_irq7
|
||||
.word isr_irq8
|
||||
.word isr_irq9
|
||||
.word isr_irq10
|
||||
.word isr_irq11
|
||||
.word isr_irq12
|
||||
.word isr_irq13
|
||||
.word isr_irq14
|
||||
.word isr_irq15
|
||||
.word isr_irq16
|
||||
.word isr_irq17
|
||||
.word isr_irq18
|
||||
.word isr_irq19
|
||||
.word isr_irq20
|
||||
.word isr_irq21
|
||||
.word isr_irq22
|
||||
.word isr_irq23
|
||||
.word isr_irq24
|
||||
.word isr_irq25
|
||||
.word isr_irq26
|
||||
.word isr_irq27
|
||||
.word isr_irq28
|
||||
.word isr_irq29
|
||||
.word isr_irq30
|
||||
.word isr_irq31
|
||||
|
||||
// Declare a weak symbol for each ISR.
|
||||
// By default, they will fall through to the undefined IRQ handler below (breakpoint),
|
||||
// but can be overridden by C functions with correct name.
|
||||
|
||||
.macro decl_isr_bkpt name
|
||||
.weak \name
|
||||
.type \name,%function
|
||||
.thumb_func
|
||||
\name:
|
||||
bkpt #0
|
||||
.endm
|
||||
|
||||
// these are separated out for clarity
|
||||
decl_isr_bkpt isr_invalid
|
||||
decl_isr_bkpt isr_nmi
|
||||
decl_isr_bkpt isr_hardfault
|
||||
decl_isr_bkpt isr_svcall
|
||||
decl_isr_bkpt isr_pendsv
|
||||
decl_isr_bkpt isr_systick
|
||||
|
||||
.macro decl_isr name
|
||||
.weak \name
|
||||
.type \name,%function
|
||||
.thumb_func
|
||||
\name:
|
||||
.endm
|
||||
|
||||
decl_isr isr_irq0
|
||||
decl_isr isr_irq1
|
||||
decl_isr isr_irq2
|
||||
decl_isr isr_irq3
|
||||
decl_isr isr_irq4
|
||||
decl_isr isr_irq5
|
||||
decl_isr isr_irq6
|
||||
decl_isr isr_irq7
|
||||
decl_isr isr_irq8
|
||||
decl_isr isr_irq9
|
||||
decl_isr isr_irq10
|
||||
decl_isr isr_irq11
|
||||
decl_isr isr_irq12
|
||||
decl_isr isr_irq13
|
||||
decl_isr isr_irq14
|
||||
decl_isr isr_irq15
|
||||
decl_isr isr_irq16
|
||||
decl_isr isr_irq17
|
||||
decl_isr isr_irq18
|
||||
decl_isr isr_irq19
|
||||
decl_isr isr_irq20
|
||||
decl_isr isr_irq21
|
||||
decl_isr isr_irq22
|
||||
decl_isr isr_irq23
|
||||
decl_isr isr_irq24
|
||||
decl_isr isr_irq25
|
||||
decl_isr isr_irq26
|
||||
decl_isr isr_irq27
|
||||
decl_isr isr_irq28
|
||||
decl_isr isr_irq29
|
||||
decl_isr isr_irq30
|
||||
decl_isr isr_irq31
|
||||
|
||||
// All unhandled USER IRQs fall through to here
|
||||
.global __unhandled_user_irq
|
||||
.thumb_func
|
||||
__unhandled_user_irq:
|
||||
bl __get_current_exception
|
||||
subs r0, #16
|
||||
.global unhandled_user_irq_num_in_r0
|
||||
unhandled_user_irq_num_in_r0:
|
||||
bkpt #0
|
||||
|
||||
.section .reset, "ax"
|
||||
|
||||
// This is the beginning of the image, which is entered from stage2 or bootrom USB MSD watchdog reboot
|
||||
|
||||
// note if we are NO_FLASH then start: below is currently identical anyway, so save 4 bytes
|
||||
#if !PICO_NO_FLASH
|
||||
// We simply install our own vector table and redirect through it
|
||||
ldr r0, =__vectors
|
||||
b __vector_entry
|
||||
#endif
|
||||
|
||||
// ELF entry point generally called when we load an ELF via debugger
|
||||
.type _entry_point,%function
|
||||
.thumb_func
|
||||
.global _entry_point
|
||||
_entry_point:
|
||||
|
||||
#if PICO_NO_FLASH
|
||||
// non flash
|
||||
ldr r0, =__vectors
|
||||
#else
|
||||
// todo clear watchdog?
|
||||
// When using flash, we install and use the ROM vector table to go thru regular bootrom/stage2 flash sequence
|
||||
movs r0, #0
|
||||
#endif
|
||||
|
||||
__vector_entry:
|
||||
ldr r1, =(PPB_BASE + M0PLUS_CPUID_OFFSET)
|
||||
str r0, [r1, #8]
|
||||
ldmia r0!, {r1, r2}
|
||||
msr msp, r1
|
||||
bx r2
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Reset handler:
|
||||
// - initialises .data
|
||||
// - clears .bss
|
||||
// - calls runtime_init
|
||||
// - calls main
|
||||
// - calls exit (which should eventually hang the processor via _exit)
|
||||
|
||||
.type _reset_handler,%function
|
||||
.thumb_func
|
||||
_reset_handler:
|
||||
// Hang all cores except core 0
|
||||
ldr r0, =(SIO_BASE + SIO_CPUID_OFFSET)
|
||||
ldr r0, [r0]
|
||||
cmp r0, #0
|
||||
bne wait_for_vector
|
||||
|
||||
adr r4, data_cpy_table
|
||||
|
||||
// assume there is at least one entry
|
||||
1:
|
||||
ldmia r4!, {r1-r3}
|
||||
cmp r1, #0
|
||||
beq 2f
|
||||
bl data_cpy
|
||||
b 1b
|
||||
2:
|
||||
|
||||
// Zero out the BSS
|
||||
ldr r1, =__bss_start__
|
||||
ldr r2, =__bss_end__
|
||||
movs r0, #0
|
||||
b bss_fill_test
|
||||
bss_fill_loop:
|
||||
stm r1!, {r0}
|
||||
bss_fill_test:
|
||||
cmp r1, r2
|
||||
bne bss_fill_loop
|
||||
|
||||
platform_entry: // symbol for stack traces
|
||||
// Use 32-bit jumps, in case these symbols are moved out of branch range
|
||||
// (e.g. if main is in SRAM and crt0 in flash)
|
||||
ldr r1, =runtime_init
|
||||
blx r1
|
||||
ldr r1, =main
|
||||
blx r1
|
||||
ldr r1, =exit
|
||||
blx r1
|
||||
// exit should not return. If it does, hang the core.
|
||||
// (fall thru into our hang _exit impl
|
||||
.weak _exit
|
||||
.type _exit,%function
|
||||
.thumb_func
|
||||
_exit:
|
||||
1: // separate label because _exit can be moved out of branch range
|
||||
bkpt #0
|
||||
b 1b
|
||||
|
||||
data_cpy_loop:
|
||||
ldm r1!, {r0}
|
||||
stm r2!, {r0}
|
||||
data_cpy:
|
||||
cmp r2, r3
|
||||
blo data_cpy_loop
|
||||
bx lr
|
||||
|
||||
#if !PICO_NO_BINARY_INFO
|
||||
binary_info_header:
|
||||
.word BINARY_INFO_MARKER_START
|
||||
.word __binary_info_start
|
||||
.word __binary_info_end
|
||||
.word data_cpy_table // we may need to decode pointers that are in RAM at runtime.
|
||||
.word BINARY_INFO_MARKER_END
|
||||
#endif
|
||||
|
||||
.align 2
|
||||
data_cpy_table:
|
||||
#if PICO_COPY_TO_RAM
|
||||
.word __ram_text_source__
|
||||
.word __ram_text_start__
|
||||
.word __ram_text_end__
|
||||
#endif
|
||||
.word __etext
|
||||
.word __data_start__
|
||||
.word __data_end__
|
||||
|
||||
.word __scratch_x_source__
|
||||
.word __scratch_x_start__
|
||||
.word __scratch_x_end__
|
||||
|
||||
.word __scratch_y_source__
|
||||
.word __scratch_y_start__
|
||||
.word __scratch_y_end__
|
||||
|
||||
.word 0 // null terminator
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Provide safe defaults for _exit and runtime_init
|
||||
// Full implementations usually provided by platform.c
|
||||
|
||||
.weak runtime_init
|
||||
.type runtime_init,%function
|
||||
.thumb_func
|
||||
runtime_init:
|
||||
bx lr
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// In case core 1's VTOR has already been moved into flash, we need to handle
|
||||
// core 1 reset. However, we do so by just jumping back into bootrom version of
|
||||
// wait_for_vector
|
||||
|
||||
wait_for_vector:
|
||||
ldr r0, = 'W' | ('V' << 8)
|
||||
bl rom_func_lookup
|
||||
bx r0
|
||||
|
||||
.global __get_current_exception
|
||||
.thumb_func
|
||||
__get_current_exception:
|
||||
mrs r0, ipsr
|
||||
uxtb r0, r0
|
||||
bx lr
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Stack/heap dummies to set size
|
||||
|
||||
.section .stack
|
||||
// align to allow for memory protection (although this alignment is pretty much ignored by linker script)
|
||||
.align 5
|
||||
.equ StackSize, PICO_STACK_SIZE
|
||||
.space StackSize
|
||||
|
||||
.section .heap
|
||||
.align 2
|
||||
.equ HeapSize, PICO_HEAP_SIZE
|
||||
.space HeapSize
|
||||
+27395
-292
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,23 @@
|
||||
// Padded and checksummed version of: generated\Winbond\W25Q16JVxQ\boot2.bin
|
||||
|
||||
.cpu cortex-m0plus
|
||||
.thumb
|
||||
|
||||
.section .boot2, "ax"
|
||||
|
||||
.byte 0xf7, 0xb5, 0x73, 0x46, 0x21, 0x22, 0x02, 0x26, 0x01, 0x93, 0x29, 0x4b, 0xc0, 0x24, 0x5a, 0x60
|
||||
.byte 0x9a, 0x68, 0x00, 0x27, 0xb2, 0x43, 0xda, 0x60, 0x9a, 0x60, 0x1a, 0x61, 0x5a, 0x61, 0x04, 0x23
|
||||
.byte 0x01, 0x25, 0x64, 0x05, 0xa7, 0x60, 0x63, 0x61, 0x22, 0x4b, 0x30, 0x00, 0x1d, 0x60, 0xe0, 0x23
|
||||
.byte 0xdb, 0x02, 0x23, 0x60, 0x35, 0x23, 0xa5, 0x60, 0x23, 0x66, 0x23, 0x66, 0x00, 0xf0, 0x4a, 0xf8
|
||||
.byte 0xc0, 0xb2, 0xb0, 0x42, 0x12, 0xd0, 0x06, 0x23, 0x28, 0x00, 0x23, 0x66, 0x00, 0xf0, 0x42, 0xf8
|
||||
.byte 0x25, 0x66, 0x03, 0x20, 0x27, 0x66, 0x26, 0x66, 0x00, 0xf0, 0x3c, 0xf8, 0x03, 0x36, 0x26, 0x66
|
||||
.byte 0x02, 0x20, 0x26, 0x66, 0x00, 0xf0, 0x36, 0xf8, 0x28, 0x42, 0xf8, 0xd1, 0x00, 0x25, 0x12, 0x4b
|
||||
.byte 0xa5, 0x60, 0x12, 0x4f, 0x23, 0x60, 0x12, 0x4b, 0x65, 0x60, 0x01, 0x26, 0x3b, 0x60, 0xeb, 0x23
|
||||
.byte 0xa6, 0x60, 0x23, 0x66, 0x4b, 0x3b, 0x23, 0x66, 0x02, 0x20, 0x00, 0xf0, 0x23, 0xf8, 0x0d, 0x4b
|
||||
.byte 0xa5, 0x60, 0x3b, 0x60, 0xa6, 0x60, 0x01, 0x9b, 0xab, 0x42, 0x08, 0xd1, 0x0a, 0x4b, 0x0b, 0x4a
|
||||
.byte 0x13, 0x60, 0x1b, 0x68, 0x83, 0xf3, 0x08, 0x88, 0x09, 0x4b, 0x1b, 0x68, 0x18, 0x47, 0xf7, 0xbd
|
||||
.byte 0x00, 0x00, 0x02, 0x40, 0xf0, 0x00, 0x00, 0x18, 0x00, 0x03, 0x5f, 0x00, 0xf4, 0x00, 0x00, 0x18
|
||||
.byte 0x21, 0x22, 0x00, 0x00, 0x22, 0x20, 0x00, 0xa0, 0x00, 0x01, 0x00, 0x10, 0x08, 0xed, 0x00, 0xe0
|
||||
.byte 0x04, 0x01, 0x00, 0x10, 0xc0, 0x22, 0x03, 0x00, 0x04, 0x21, 0x52, 0x05, 0x90, 0x6a, 0x08, 0x42
|
||||
.byte 0xfc, 0xd0, 0x01, 0x21, 0x90, 0x6a, 0x08, 0x42, 0xfc, 0xd1, 0x01, 0x3b, 0xdb, 0xb2, 0x10, 0x6e
|
||||
.byte 0x00, 0x2b, 0xfa, 0xd1, 0x70, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0xa0, 0xf0, 0x2f
|
||||
@@ -0,0 +1,23 @@
|
||||
// Padded and checksummed version of: boot2_w25q64jv.4.bin
|
||||
|
||||
.cpu cortex-m0plus
|
||||
.thumb
|
||||
|
||||
.section .boot2, "ax"
|
||||
|
||||
.byte 0xf7, 0xb5, 0x73, 0x46, 0x21, 0x22, 0x02, 0x25, 0x01, 0x93, 0x29, 0x4b, 0xc0, 0x24, 0x5a, 0x60
|
||||
.byte 0x9a, 0x68, 0x01, 0x26, 0xaa, 0x43, 0xda, 0x60, 0x9a, 0x60, 0x1a, 0x61, 0x5a, 0x61, 0x00, 0x23
|
||||
.byte 0x64, 0x05, 0xa3, 0x60, 0x04, 0x33, 0x63, 0x61, 0x22, 0x4b, 0x28, 0x00, 0x1e, 0x60, 0xe0, 0x23
|
||||
.byte 0xdb, 0x02, 0x23, 0x60, 0x35, 0x23, 0xa6, 0x60, 0x23, 0x66, 0x23, 0x66, 0x00, 0xf0, 0x4a, 0xf8
|
||||
.byte 0xc0, 0xb2, 0xa8, 0x42, 0x12, 0xd0, 0x06, 0x23, 0x30, 0x00, 0x23, 0x66, 0x00, 0xf0, 0x42, 0xf8
|
||||
.byte 0x31, 0x23, 0x28, 0x00, 0x23, 0x66, 0x25, 0x66, 0x00, 0xf0, 0x3c, 0xf8, 0x03, 0x35, 0x25, 0x66
|
||||
.byte 0x02, 0x20, 0x25, 0x66, 0x00, 0xf0, 0x36, 0xf8, 0x30, 0x42, 0xf8, 0xd1, 0x00, 0x25, 0x12, 0x4b
|
||||
.byte 0xa5, 0x60, 0x12, 0x4f, 0x23, 0x60, 0x12, 0x4b, 0x65, 0x60, 0x01, 0x26, 0x3b, 0x60, 0xeb, 0x23
|
||||
.byte 0xa6, 0x60, 0x23, 0x66, 0x4b, 0x3b, 0x23, 0x66, 0x02, 0x20, 0x00, 0xf0, 0x23, 0xf8, 0x0d, 0x4b
|
||||
.byte 0xa5, 0x60, 0x3b, 0x60, 0xa6, 0x60, 0x01, 0x9b, 0xab, 0x42, 0x08, 0xd1, 0x0a, 0x4b, 0x0b, 0x4a
|
||||
.byte 0x13, 0x60, 0x1b, 0x68, 0x83, 0xf3, 0x08, 0x88, 0x09, 0x4b, 0x1b, 0x68, 0x18, 0x47, 0xf7, 0xbd
|
||||
.byte 0x00, 0x00, 0x02, 0x40, 0xf0, 0x00, 0x00, 0x18, 0x00, 0x03, 0x5f, 0x00, 0xf4, 0x00, 0x00, 0x18
|
||||
.byte 0x21, 0x22, 0x00, 0x00, 0x22, 0x20, 0x00, 0xa0, 0x00, 0x01, 0x00, 0x10, 0x08, 0xed, 0x00, 0xe0
|
||||
.byte 0x04, 0x01, 0x00, 0x10, 0xc0, 0x23, 0x02, 0x00, 0x04, 0x21, 0x5b, 0x05, 0x98, 0x6a, 0x08, 0x42
|
||||
.byte 0xfc, 0xd0, 0x01, 0x21, 0x98, 0x6a, 0x08, 0x42, 0xfc, 0xd1, 0x18, 0x6e, 0x01, 0x2a, 0x00, 0xd0
|
||||
.byte 0x18, 0x6e, 0x70, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x2d, 0x68, 0xca
|
||||
+77
-56
@@ -1,77 +1,87 @@
|
||||
/*
|
||||
* Arduino header for the Raspberry Pi Pico RP2040
|
||||
*
|
||||
* Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
Arduino header for the Raspberry Pi Pico RP2040
|
||||
|
||||
#ifndef Arduino_h
|
||||
#define Arduino_h
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "stdlib_noniso.h" // Wacky deprecated AVR compatibility functions
|
||||
#include "RP2040Version.h"
|
||||
#include "api/ArduinoAPI.h"
|
||||
#include "api/itoa.h" // ARM toolchain doesn't provide itoa etc, provide them
|
||||
#include <pins_arduino.h>
|
||||
|
||||
// Required for the port*Register macros
|
||||
#include "hardware/gpio.h"
|
||||
|
||||
#include "hardware/gpio.h" // Required for the port*Register macros
|
||||
#include "debug_internal.h"
|
||||
#include <RP2040.h> // CMSIS
|
||||
|
||||
// Try and make the best of the old Arduino abs() macro. When in C++, use
|
||||
// the sane std::abs() call, but for C code use their macro since stdlib abs()
|
||||
// is int but their macro "works" for everything (with potential side effects)
|
||||
#ifdef abs
|
||||
#undef abs
|
||||
#endif // abs
|
||||
#ifdef __cplusplus
|
||||
using std::abs;
|
||||
using std::round;
|
||||
#else
|
||||
#define abs(x) ({ __typeof__(x) _x = (x); _x >= 0 ? _x : -_x; })
|
||||
#define round(x) ({ __typeof__(x) _x = (x); _x >= 0 ? (long)(_x + 0.5) : (long)(_x - 0.5); })
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"{
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
// Disable/reenable all interrupts. Safely handles nested disables
|
||||
// For compatibility to many platforms and libraries
|
||||
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
|
||||
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
|
||||
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
|
||||
|
||||
// Disable/re-enable all interrupts. Safely handles nested disables
|
||||
void interrupts();
|
||||
void noInterrupts();
|
||||
|
||||
// GPIO change/value interrupts
|
||||
void attachInterrupt(pin_size_t pin, voidFuncPtr callback, PinStatus mode);
|
||||
void detachInterrupt(pin_size_t pin);
|
||||
|
||||
// AVR compatibilty macros...naughty and accesses the HW directly
|
||||
// AVR compatibility macros...naughty and accesses the HW directly
|
||||
#define digitalPinToPort(pin) (0)
|
||||
#define digitalPinToBitMask(pin) (1UL << (pin))
|
||||
#define digitalPinToTimer(pin) (0)
|
||||
#define digitalPinToInterrupt(pin) (pin)
|
||||
#define NOT_AN_INTERRUPT (-1)
|
||||
#define portOutputRegister(port) ((volatile uint32_t*) sio_hw->gpio_out)
|
||||
#define portInputRegister(port) ((volatile uint32_t*) sio_hw->gpio_in)
|
||||
#define portModeRegister(port) ((volatile uint32_t*) sio_hw->gpio_oe)
|
||||
#define digitalWriteFast(pin, val) (val ? sio_hw->gpio_set = (1 << pin) : sio_hw->gpio_clr = (1 << pin))
|
||||
#define digitalReadFast(pin) ((1 << pin) & sio_hw->gpio_in)
|
||||
|
||||
// IO config
|
||||
void pinMode(pin_size_t pinNumber, PinMode pinMode);
|
||||
// ADC RP2040-specific calls
|
||||
void analogReadResolution(int bits);
|
||||
float analogReadTemp(float vref = 3.3); // Returns core temp in Centigrade
|
||||
|
||||
// SIO (GPIO)
|
||||
void digitalWrite(pin_size_t pinNumber, PinStatus status);
|
||||
PinStatus digitalRead(pin_size_t pinNumber);
|
||||
// PWM RP2040-specific calls
|
||||
void analogWriteFreq(uint32_t freq);
|
||||
void analogWriteRange(uint32_t range);
|
||||
void analogWriteResolution(int res);
|
||||
|
||||
// ADC
|
||||
int analogRead(pin_size_t pinNumber);
|
||||
float analogReadTemp(); // Returns core temp in Centigrade
|
||||
|
||||
// PWM
|
||||
void analogWrite(pin_size_t pinNumber, int value);
|
||||
|
||||
// Timing
|
||||
void delay(unsigned long);
|
||||
void delayMicroseconds(unsigned int us);
|
||||
unsigned long millis();
|
||||
// FreeRTOS potential calls
|
||||
extern bool __isFreeRTOS;
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
@@ -82,22 +92,33 @@ unsigned long millis();
|
||||
#define HAVE_HWSERIAL1
|
||||
#define HAVE_HWSERIAL2
|
||||
|
||||
// PSTR/etc.
|
||||
#ifndef FPSTR
|
||||
#define FPSTR (const char *)
|
||||
#endif
|
||||
|
||||
#ifndef PGM_VOID_P
|
||||
#define PGM_VOID_P void *
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
#ifdef USE_TINYUSB
|
||||
// Needed for declaring Serial
|
||||
#include "Adafruit_USBD_CDC.h"
|
||||
#else
|
||||
#include "SerialUSB.h"
|
||||
#endif
|
||||
|
||||
#include "SerialUART.h"
|
||||
#include "RP2040.h"
|
||||
#include "RP2040Support.h"
|
||||
#include "SerialPIO.h"
|
||||
#include "Bootsel.h"
|
||||
|
||||
// Template which will evaluate at *compile time* to a single 32b number
|
||||
// with the specified bits set.
|
||||
template <size_t N>
|
||||
constexpr uint32_t __bitset(const int (&a)[N], size_t i = 0U) {
|
||||
return i < N ? (1L << a[i]) | __bitset(a, i+1) : 0;
|
||||
return i < N ? (1L << a[i]) | __bitset(a, i + 1) : 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// ARM toolchain doesn't provide itoa etc, provide them
|
||||
#include "api/itoa.h"
|
||||
|
||||
#endif // Arduino_h
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
|
||||
SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#include <Arduino.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/gpio.h"
|
||||
@@ -26,7 +26,8 @@ static bool __no_inline_not_in_flash_func(get_bootsel_button)() {
|
||||
|
||||
// Must disable interrupts, as interrupt handlers may be in flash, and we
|
||||
// are about to temporarily disable flash access!
|
||||
uint32_t flags = save_and_disable_interrupts();
|
||||
noInterrupts();
|
||||
rp2040.idleOtherCore();
|
||||
|
||||
// Set chip select to Hi-Z
|
||||
hw_write_masked(&ioqspi_hw->io[CS_PIN_INDEX].ctrl,
|
||||
@@ -46,7 +47,8 @@ static bool __no_inline_not_in_flash_func(get_bootsel_button)() {
|
||||
GPIO_OVERRIDE_NORMAL << IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_LSB,
|
||||
IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS);
|
||||
|
||||
restore_interrupts(flags);
|
||||
rp2040.resumeOtherCore();
|
||||
interrupts();
|
||||
|
||||
return button_state;
|
||||
}
|
||||
|
||||
+18
-18
@@ -1,22 +1,22 @@
|
||||
/*
|
||||
* Simple BOOTSEL reader object
|
||||
*
|
||||
* Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
Simple BOOTSEL reader object
|
||||
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
#include "api/Client.h"
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
CoreMutex for the Raspberry Pi Pico RP2040
|
||||
|
||||
Implements a deadlock-safe multicore mutex for sharing things like the
|
||||
USB or UARTs.
|
||||
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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 "CoreMutex.h"
|
||||
|
||||
CoreMutex::CoreMutex(mutex_t *mutex, uint8_t option) {
|
||||
_mutex = mutex;
|
||||
_acquired = false;
|
||||
_option = option;
|
||||
if (__isFreeRTOS) {
|
||||
auto m = __get_freertos_mutex_for_ptr(mutex);
|
||||
if (_option & FromISR) {
|
||||
__freertos_mutex_take_from_isr(m);
|
||||
} else {
|
||||
__freertos_mutex_take(m);
|
||||
}
|
||||
} else {
|
||||
uint32_t owner;
|
||||
if (!mutex_try_enter(_mutex, &owner)) {
|
||||
if (owner == get_core_num()) { // Deadlock!
|
||||
if (_option & DebugEnable) {
|
||||
DEBUGCORE("CoreMutex - Deadlock detected!\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
mutex_enter_blocking(_mutex);
|
||||
}
|
||||
}
|
||||
_acquired = true;
|
||||
}
|
||||
|
||||
CoreMutex::~CoreMutex() {
|
||||
if (_acquired) {
|
||||
if (__isFreeRTOS) {
|
||||
auto m = __get_freertos_mutex_for_ptr(_mutex);
|
||||
if (_option & FromISR) {
|
||||
__freertos_mutex_give_from_isr(m);
|
||||
} else {
|
||||
__freertos_mutex_give(m);
|
||||
}
|
||||
} else {
|
||||
mutex_exit(_mutex);
|
||||
}
|
||||
}
|
||||
}
|
||||
+30
-39
@@ -1,50 +1,40 @@
|
||||
/*
|
||||
* CoreMutex for the Raspberry Pi Pico RP2040
|
||||
*
|
||||
* Implements a deadlock-safe multicore mutex for sharing things like the
|
||||
* USB or UARTs.
|
||||
*
|
||||
* Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
CoreMutex for the Raspberry Pi Pico RP2040
|
||||
|
||||
Implements a deadlock-safe multicore mutex for sharing things like the
|
||||
USB or UARTs.
|
||||
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "pico/mutex.h"
|
||||
#include "_freertos.h"
|
||||
|
||||
enum {
|
||||
DebugEnable = 1,
|
||||
FromISR = 1 << 1,
|
||||
};
|
||||
|
||||
class CoreMutex {
|
||||
public:
|
||||
CoreMutex(mutex_t *mutex) {
|
||||
uint32_t owner;
|
||||
_mutex = mutex;
|
||||
_acquired = false;
|
||||
if (!mutex_try_enter(_mutex, &owner)) {
|
||||
if (owner == get_core_num()) { // Deadlock!
|
||||
return;
|
||||
}
|
||||
mutex_enter_blocking(_mutex);
|
||||
}
|
||||
_acquired = true;
|
||||
}
|
||||
|
||||
~CoreMutex() {
|
||||
if (_acquired) {
|
||||
mutex_exit(_mutex);
|
||||
}
|
||||
}
|
||||
CoreMutex(mutex_t *mutex, uint8_t option = DebugEnable);
|
||||
~CoreMutex();
|
||||
|
||||
operator bool() {
|
||||
return _acquired;
|
||||
@@ -53,4 +43,5 @@ public:
|
||||
private:
|
||||
mutex_t *_mutex;
|
||||
bool _acquired;
|
||||
u_int8_t _option;
|
||||
};
|
||||
|
||||
+101
-78
@@ -1,22 +1,22 @@
|
||||
/*
|
||||
FS.cpp - file system wrapper
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
FS.cpp - file system wrapper
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
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 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.
|
||||
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
|
||||
*/
|
||||
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 "FS.h"
|
||||
#include "FSImpl.h"
|
||||
@@ -26,37 +26,42 @@ using namespace fs;
|
||||
static bool sflags(const char* mode, OpenMode& om, AccessMode& am);
|
||||
|
||||
size_t File::write(uint8_t c) {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _p->write(&c, 1);
|
||||
}
|
||||
|
||||
size_t File::write(const uint8_t *buf, size_t size) {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _p->write(buf, size);
|
||||
}
|
||||
|
||||
int File::available() {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _p->size() - _p->position();
|
||||
}
|
||||
|
||||
int File::availableForWrite() {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _p->availableForWrite();
|
||||
}
|
||||
|
||||
|
||||
int File::read() {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t result;
|
||||
if (_p->read(&result, 1) != 1) {
|
||||
@@ -67,15 +72,17 @@ int File::read() {
|
||||
}
|
||||
|
||||
int File::read(uint8_t* buf, size_t size) {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _p->read(buf, size);
|
||||
}
|
||||
|
||||
int File::peek() {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t curPos = _p->position();
|
||||
int result = read();
|
||||
@@ -84,29 +91,33 @@ int File::peek() {
|
||||
}
|
||||
|
||||
void File::flush() {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return;
|
||||
}
|
||||
|
||||
_p->flush();
|
||||
}
|
||||
|
||||
bool File::seek(uint32_t pos, SeekMode mode) {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _p->seek(pos, mode);
|
||||
}
|
||||
|
||||
size_t File::position() const {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _p->position();
|
||||
}
|
||||
|
||||
size_t File::size() const {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _p->size();
|
||||
}
|
||||
@@ -123,36 +134,41 @@ File::operator bool() const {
|
||||
}
|
||||
|
||||
bool File::truncate(uint32_t size) {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _p->truncate(size);
|
||||
}
|
||||
|
||||
const char* File::name() const {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return _p->name();
|
||||
}
|
||||
|
||||
const char* File::fullName() const {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return _p->fullName();
|
||||
}
|
||||
|
||||
bool File::isFile() const {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _p->isFile();
|
||||
}
|
||||
|
||||
bool File::isDirectory() const {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _p->isDirectory();
|
||||
}
|
||||
@@ -162,7 +178,7 @@ void File::rewindDirectory() {
|
||||
_fakeDir = std::make_shared<Dir>(_baseFS->openDir(fullName()));
|
||||
} else {
|
||||
_fakeDir->rewind();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File File::openNextFile() {
|
||||
@@ -173,38 +189,38 @@ File File::openNextFile() {
|
||||
return _fakeDir->openFile("r");
|
||||
}
|
||||
|
||||
String File::readString()
|
||||
{
|
||||
String File::readString() {
|
||||
String ret;
|
||||
ret.reserve(size() - position());
|
||||
char temp[256+1];
|
||||
int countRead = readBytes(temp, sizeof(temp)-1);
|
||||
while (countRead > 0)
|
||||
{
|
||||
temp[countRead] = 0;
|
||||
ret += temp;
|
||||
countRead = readBytes(temp, sizeof(temp)-1);
|
||||
}
|
||||
uint8_t temp[256];
|
||||
int countRead;
|
||||
do {
|
||||
countRead = read(temp, sizeof(temp));
|
||||
ret.concat(temp, countRead);
|
||||
} while (countRead > 0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
time_t File::getLastWrite() {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _p->getLastWrite();
|
||||
}
|
||||
|
||||
time_t File::getCreationTime() {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _p->getCreationTime();
|
||||
}
|
||||
|
||||
void File::setTimeCallback(time_t (*cb)(void)) {
|
||||
if (!_p)
|
||||
if (!_p) {
|
||||
return;
|
||||
}
|
||||
_p->setTimeCallback(cb);
|
||||
_timeCallback = cb;
|
||||
}
|
||||
@@ -235,14 +251,16 @@ String Dir::fileName() {
|
||||
}
|
||||
|
||||
time_t Dir::fileTime() {
|
||||
if (!_impl)
|
||||
if (!_impl) {
|
||||
return 0;
|
||||
}
|
||||
return _impl->fileTime();
|
||||
}
|
||||
|
||||
time_t Dir::fileCreationTime() {
|
||||
if (!_impl)
|
||||
if (!_impl) {
|
||||
return 0;
|
||||
}
|
||||
return _impl->fileCreationTime();
|
||||
}
|
||||
|
||||
@@ -255,15 +273,17 @@ size_t Dir::fileSize() {
|
||||
}
|
||||
|
||||
bool Dir::isFile() const {
|
||||
if (!_impl)
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _impl->isFile();
|
||||
}
|
||||
|
||||
bool Dir::isDirectory() const {
|
||||
if (!_impl)
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _impl->isDirectory();
|
||||
}
|
||||
@@ -285,8 +305,9 @@ bool Dir::rewind() {
|
||||
}
|
||||
|
||||
void Dir::setTimeCallback(time_t (*cb)(void)) {
|
||||
if (!_impl)
|
||||
if (!_impl) {
|
||||
return;
|
||||
}
|
||||
_impl->setTimeCallback(cb);
|
||||
_timeCallback = cb;
|
||||
}
|
||||
@@ -307,7 +328,7 @@ bool FS::begin() {
|
||||
}
|
||||
_impl->setTimeCallback(_timeCallback);
|
||||
bool ret = _impl->begin();
|
||||
DEBUGV("%s\n", ret? "": "#error: FS could not start");
|
||||
DEBUGV("%s\n", ret ? "" : "#error: FS could not start");
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -338,14 +359,14 @@ bool FS::format() {
|
||||
return _impl->format();
|
||||
}
|
||||
|
||||
bool FS::info(FSInfo& info){
|
||||
bool FS::info(FSInfo& info) {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
return _impl->info(info);
|
||||
}
|
||||
|
||||
bool FS::info64(FSInfo64& info){
|
||||
bool FS::info64(FSInfo64& info) {
|
||||
if (!_impl) {
|
||||
return false;
|
||||
}
|
||||
@@ -449,8 +470,9 @@ time_t FS::getCreationTime() {
|
||||
}
|
||||
|
||||
void FS::setTimeCallback(time_t (*cb)(void)) {
|
||||
if (!_impl)
|
||||
if (!_impl) {
|
||||
return;
|
||||
}
|
||||
_impl->setTimeCallback(cb);
|
||||
_timeCallback = cb;
|
||||
}
|
||||
@@ -458,29 +480,29 @@ void FS::setTimeCallback(time_t (*cb)(void)) {
|
||||
|
||||
static bool sflags(const char* mode, OpenMode& om, AccessMode& am) {
|
||||
switch (mode[0]) {
|
||||
case 'r':
|
||||
am = AM_READ;
|
||||
om = OM_DEFAULT;
|
||||
break;
|
||||
case 'w':
|
||||
am = AM_WRITE;
|
||||
om = (OpenMode) (OM_CREATE | OM_TRUNCATE);
|
||||
break;
|
||||
case 'a':
|
||||
am = AM_WRITE;
|
||||
om = (OpenMode) (OM_CREATE | OM_APPEND);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
case 'r':
|
||||
am = AM_READ;
|
||||
om = OM_DEFAULT;
|
||||
break;
|
||||
case 'w':
|
||||
am = AM_WRITE;
|
||||
om = (OpenMode)(OM_CREATE | OM_TRUNCATE);
|
||||
break;
|
||||
case 'a':
|
||||
am = AM_WRITE;
|
||||
om = (OpenMode)(OM_CREATE | OM_APPEND);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
switch(mode[1]) {
|
||||
case '+':
|
||||
am = (AccessMode) (AM_WRITE | AM_READ);
|
||||
break;
|
||||
case 0:
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
switch (mode[1]) {
|
||||
case '+':
|
||||
am = (AccessMode)(AM_WRITE | AM_READ);
|
||||
break;
|
||||
case 0:
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -489,7 +511,7 @@ static bool sflags(const char* mode, OpenMode& om, AccessMode& am) {
|
||||
#if defined(FS_FREESTANDING_FUNCTIONS)
|
||||
|
||||
/*
|
||||
TODO: move these functions to public API:
|
||||
TODO: move these functions to public API:
|
||||
*/
|
||||
File open(const char* path, const char* mode);
|
||||
File open(String& path, const char* mode);
|
||||
@@ -545,8 +567,9 @@ File open(const char* path, const char* mode) {
|
||||
size_t offset = entry->path.length();
|
||||
if (strstr(path, entry->path.c_str())) {
|
||||
File result = entry->fs->open(path + offset);
|
||||
if (result)
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+53
-65
@@ -1,25 +1,24 @@
|
||||
/*
|
||||
FS.h - file system wrapper
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
FS.h - file system wrapper
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
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 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.
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
#ifndef FS_H
|
||||
#define FS_H
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <Arduino.h>
|
||||
@@ -49,10 +48,11 @@ enum SeekMode {
|
||||
SeekEnd = 2
|
||||
};
|
||||
|
||||
class File : public Stream
|
||||
{
|
||||
class File : public Stream {
|
||||
public:
|
||||
File(FileImplPtr p = FileImplPtr(), FS *baseFS = nullptr) : _p(p), _fakeDir(nullptr), _baseFS(baseFS) { }
|
||||
File(FileImplPtr p = FileImplPtr(), FS *baseFS = nullptr) : _p(p), _fakeDir(nullptr), _baseFS(baseFS) {
|
||||
_startMillis = millis(); /* workaround -O3 spurious warning #768 */
|
||||
}
|
||||
|
||||
// Print methods:
|
||||
size_t write(uint8_t) override;
|
||||
@@ -74,7 +74,9 @@ public:
|
||||
}
|
||||
size_t position() const;
|
||||
size_t size() const;
|
||||
virtual ssize_t streamRemaining() { return (ssize_t)size() - (ssize_t)position(); }
|
||||
virtual ssize_t streamRemaining() {
|
||||
return (ssize_t)size() - (ssize_t)position();
|
||||
}
|
||||
void close();
|
||||
operator bool() const;
|
||||
const char* name() const;
|
||||
@@ -86,26 +88,25 @@ public:
|
||||
|
||||
// Arduino "class SD" methods for compatibility
|
||||
//TODO use stream::send / check read(buf,size) result
|
||||
template<typename T> size_t write(T &src){
|
||||
uint8_t obuf[256];
|
||||
size_t doneLen = 0;
|
||||
size_t sentLen;
|
||||
int i;
|
||||
template<typename T> size_t write(T &src) {
|
||||
uint8_t obuf[256];
|
||||
size_t doneLen = 0;
|
||||
size_t sentLen;
|
||||
|
||||
while (src.available() > sizeof(obuf)){
|
||||
src.read(obuf, sizeof(obuf));
|
||||
sentLen = write(obuf, sizeof(obuf));
|
||||
doneLen = doneLen + sentLen;
|
||||
if(sentLen != sizeof(obuf)){
|
||||
return doneLen;
|
||||
while ((size_t)src.available() > sizeof(obuf)) {
|
||||
src.read(obuf, sizeof(obuf));
|
||||
sentLen = write(obuf, sizeof(obuf));
|
||||
doneLen = doneLen + sentLen;
|
||||
if (sentLen != sizeof(obuf)) {
|
||||
return doneLen;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t leftLen = src.available();
|
||||
src.read(obuf, leftLen);
|
||||
sentLen = write(obuf, leftLen);
|
||||
doneLen = doneLen + sentLen;
|
||||
return doneLen;
|
||||
size_t leftLen = src.available();
|
||||
src.read(obuf, leftLen);
|
||||
sentLen = write(obuf, leftLen);
|
||||
doneLen = doneLen + sentLen;
|
||||
return doneLen;
|
||||
}
|
||||
using Print::write;
|
||||
|
||||
@@ -172,8 +173,7 @@ struct FSInfo64 {
|
||||
};
|
||||
|
||||
|
||||
class FSConfig
|
||||
{
|
||||
class FSConfig {
|
||||
public:
|
||||
static constexpr uint32_t FSId = 0x00000000;
|
||||
|
||||
@@ -188,20 +188,11 @@ public:
|
||||
bool _autoFormat;
|
||||
};
|
||||
|
||||
class SPIFFSConfig : public FSConfig
|
||||
{
|
||||
class FS {
|
||||
public:
|
||||
static constexpr uint32_t FSId = 0x53504946;
|
||||
SPIFFSConfig(bool autoFormat = true) : FSConfig(FSId, autoFormat) { }
|
||||
|
||||
// Inherit _type and _autoFormat
|
||||
// nothing yet, enableTime TBD when SPIFFS has metadate
|
||||
};
|
||||
|
||||
class FS
|
||||
{
|
||||
public:
|
||||
FS(FSImplPtr impl) : _impl(impl) { _timeCallback = _defaultTimeCB; }
|
||||
FS(FSImplPtr impl) : _impl(impl) {
|
||||
_timeCallback = _defaultTimeCB;
|
||||
}
|
||||
|
||||
bool setConfig(const FSConfig &cfg);
|
||||
|
||||
@@ -244,18 +235,22 @@ public:
|
||||
friend class ::SDClass; // More of a frenemy, but SD needs internal implementation to get private FAT bits
|
||||
protected:
|
||||
FSImplPtr _impl;
|
||||
FSImplPtr getImpl() { return _impl; }
|
||||
FSImplPtr getImpl() {
|
||||
return _impl;
|
||||
}
|
||||
time_t (*_timeCallback)(void) = nullptr;
|
||||
static time_t _defaultTimeCB(void) { return time(NULL); }
|
||||
static time_t _defaultTimeCB(void) {
|
||||
return time(nullptr);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace fs
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void close_all_fs(void);
|
||||
void littlefs_request_end(void);
|
||||
void spiffs_request_end(void);
|
||||
void close_all_fs(void);
|
||||
void littlefs_request_end(void);
|
||||
void spiffs_request_end(void);
|
||||
}
|
||||
|
||||
#ifndef FS_NO_GLOBALS
|
||||
@@ -268,11 +263,4 @@ using fs::SeekCur;
|
||||
using fs::SeekEnd;
|
||||
using fs::FSInfo;
|
||||
using fs::FSConfig;
|
||||
using fs::SPIFFSConfig;
|
||||
#endif //FS_NO_GLOBALS
|
||||
|
||||
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SPIFFS)
|
||||
extern fs::FS SPIFFS __attribute__((deprecated("SPIFFS has been deprecated. Please consider moving to LittleFS or other filesystems.")));
|
||||
#endif
|
||||
|
||||
#endif //FS_H
|
||||
|
||||
+51
-31
@@ -1,24 +1,24 @@
|
||||
/*
|
||||
FSImpl.h - base file system interface
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
FSImpl.h - base file system interface
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
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 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.
|
||||
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
|
||||
*/
|
||||
#ifndef FSIMPL_H
|
||||
#define FSIMPL_H
|
||||
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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
@@ -35,7 +35,9 @@ public:
|
||||
virtual bool seek(uint32_t pos, SeekMode mode) = 0;
|
||||
virtual size_t position() const = 0;
|
||||
virtual size_t size() const = 0;
|
||||
virtual int availableForWrite() { return 0; }
|
||||
virtual int availableForWrite() {
|
||||
return 0;
|
||||
}
|
||||
virtual bool truncate(uint32_t size) = 0;
|
||||
virtual void close() = 0;
|
||||
virtual const char* name() const = 0;
|
||||
@@ -46,14 +48,20 @@ public:
|
||||
// Filesystems *may* support a timestamp per-file, so allow the user to override with
|
||||
// their own callback for *this specific* file (as opposed to the FSImpl call of the
|
||||
// same name. The default implementation simply returns time(null)
|
||||
virtual void setTimeCallback(time_t (*cb)(void)) { _timeCallback = cb; }
|
||||
virtual void setTimeCallback(time_t (*cb)(void)) {
|
||||
_timeCallback = cb;
|
||||
}
|
||||
|
||||
// Return the last written time for a file. Undefined when called on a writable file
|
||||
// as the FS is allowed to return either the time of the last write() operation or the
|
||||
// time present in the filesystem metadata (often the last time the file was closed)
|
||||
virtual time_t getLastWrite() { return 0; } // Default is to not support timestamps
|
||||
virtual time_t getLastWrite() {
|
||||
return 0; // Default is to not support timestamps
|
||||
}
|
||||
// Same for creation time.
|
||||
virtual time_t getCreationTime() { return 0; } // Default is to not support timestamps
|
||||
virtual time_t getCreationTime() {
|
||||
return 0; // Default is to not support timestamps
|
||||
}
|
||||
|
||||
protected:
|
||||
time_t (*_timeCallback)(void) = nullptr;
|
||||
@@ -81,8 +89,12 @@ public:
|
||||
// Return the last written time for a file. Undefined when called on a writable file
|
||||
// as the FS is allowed to return either the time of the last write() operation or the
|
||||
// time present in the filesystem metadata (often the last time the file was closed)
|
||||
virtual time_t fileTime() { return 0; } // By default, FS doesn't report file times
|
||||
virtual time_t fileCreationTime() { return 0; } // By default, FS doesn't report file times
|
||||
virtual time_t fileTime() {
|
||||
return 0; // By default, FS doesn't report file times
|
||||
}
|
||||
virtual time_t fileCreationTime() {
|
||||
return 0; // By default, FS doesn't report file times
|
||||
}
|
||||
virtual bool isFile() const = 0;
|
||||
virtual bool isDirectory() const = 0;
|
||||
virtual bool next() = 0;
|
||||
@@ -91,7 +103,9 @@ public:
|
||||
// Filesystems *may* support a timestamp per-file, so allow the user to override with
|
||||
// their own callback for *this specific* file (as opposed to the FSImpl call of the
|
||||
// same name. The default implementation simply returns time(null)
|
||||
virtual void setTimeCallback(time_t (*cb)(void)) { _timeCallback = cb; }
|
||||
virtual void setTimeCallback(time_t (*cb)(void)) {
|
||||
_timeCallback = cb;
|
||||
}
|
||||
|
||||
protected:
|
||||
time_t (*_timeCallback)(void) = nullptr;
|
||||
@@ -99,7 +113,7 @@ protected:
|
||||
|
||||
class FSImpl {
|
||||
public:
|
||||
virtual ~FSImpl () { }
|
||||
virtual ~FSImpl() { }
|
||||
virtual bool setConfig(const FSConfig &cfg) = 0;
|
||||
virtual bool begin() = 0;
|
||||
virtual void end() = 0;
|
||||
@@ -113,19 +127,25 @@ public:
|
||||
virtual bool remove(const char* path) = 0;
|
||||
virtual bool mkdir(const char* path) = 0;
|
||||
virtual bool rmdir(const char* path) = 0;
|
||||
virtual bool gc() { return true; } // May not be implemented in all file systems.
|
||||
virtual bool check() { return true; } // May not be implemented in all file systems.
|
||||
virtual time_t getCreationTime() { return 0; } // May not be implemented in all file systems.
|
||||
virtual bool gc() {
|
||||
return true; // May not be implemented in all file systems.
|
||||
}
|
||||
virtual bool check() {
|
||||
return true; // May not be implemented in all file systems.
|
||||
}
|
||||
virtual time_t getCreationTime() {
|
||||
return 0; // May not be implemented in all file systems.
|
||||
}
|
||||
|
||||
// Filesystems *may* support a timestamp per-file, so allow the user to override with
|
||||
// their own callback for all files on this FS. The default implementation simply
|
||||
// returns the present time as reported by time(null)
|
||||
virtual void setTimeCallback(time_t (*cb)(void)) { _timeCallback = cb; }
|
||||
virtual void setTimeCallback(time_t (*cb)(void)) {
|
||||
_timeCallback = cb;
|
||||
}
|
||||
|
||||
protected:
|
||||
time_t (*_timeCallback)(void) = nullptr;
|
||||
};
|
||||
|
||||
} // namespace fs
|
||||
|
||||
#endif //FSIMPL_H
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
#include "api/HardwareSerial.h"
|
||||
@@ -0,0 +1 @@
|
||||
#include "api/IPAddress.h"
|
||||
@@ -0,0 +1 @@
|
||||
#include "api/Interrupts.h"
|
||||
@@ -0,0 +1,290 @@
|
||||
/*
|
||||
PolledTimeout.h - Encapsulation of a polled Timeout
|
||||
|
||||
Copyright (c) 2018 Daniel Salazar. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <limits> // std::numeric_limits
|
||||
#include <type_traits> // std::is_unsigned
|
||||
|
||||
#define IRAM_ATTR
|
||||
|
||||
namespace esp8266 {
|
||||
|
||||
|
||||
namespace polledTimeout {
|
||||
|
||||
namespace YieldPolicy {
|
||||
|
||||
struct DoNothing {
|
||||
static void execute() {}
|
||||
};
|
||||
|
||||
struct YieldOrSkip {
|
||||
static void execute() {} //{esp_yield();}
|
||||
};
|
||||
|
||||
template <unsigned long delayMs>
|
||||
struct YieldAndDelayMs {
|
||||
static void execute() {
|
||||
delay(delayMs);
|
||||
}
|
||||
};
|
||||
|
||||
} //YieldPolicy
|
||||
|
||||
namespace TimePolicy {
|
||||
|
||||
struct TimeSourceMillis {
|
||||
// time policy in milli-seconds based on millis()
|
||||
|
||||
using timeType = decltype(millis());
|
||||
static timeType time() {
|
||||
return millis();
|
||||
}
|
||||
static constexpr timeType ticksPerSecond = 1000;
|
||||
static constexpr timeType ticksPerSecondMax = 1000;
|
||||
};
|
||||
|
||||
struct TimeSourceCycles {
|
||||
// time policy based on esp_get_cycle_count()
|
||||
// this particular time measurement is intended to be called very often
|
||||
// (every loop, every yield)
|
||||
|
||||
using timeType = decltype(rp2040.getCycleCount());
|
||||
static timeType time() {
|
||||
return rp2040.getCycleCount();
|
||||
}
|
||||
static constexpr timeType ticksPerSecond = F_CPU;
|
||||
static constexpr timeType ticksPerSecondMax = F_CPU;
|
||||
};
|
||||
|
||||
template <typename TimeSourceType, unsigned long long second_th>
|
||||
// "second_th" units of timeType for one second
|
||||
struct TimeUnit {
|
||||
using timeType = typename TimeSourceType::timeType;
|
||||
|
||||
#if __GNUC__ < 5
|
||||
// gcc-4.8 cannot compile the constexpr-only version of this function
|
||||
// using #defines instead luckily works
|
||||
static constexpr timeType computeRangeCompensation() {
|
||||
#define number_of_secondTh_in_one_tick ((1.0 * second_th) / ticksPerSecond)
|
||||
#define fractional (number_of_secondTh_in_one_tick - (long)number_of_secondTh_in_one_tick)
|
||||
|
||||
return ({
|
||||
fractional == 0 ?
|
||||
1 : // no need for compensation
|
||||
(number_of_secondTh_in_one_tick / fractional) + 0.5; // scalar multiplier allowing exact division
|
||||
});
|
||||
|
||||
#undef number_of_secondTh_in_one_tick
|
||||
#undef fractional
|
||||
}
|
||||
#else
|
||||
static constexpr timeType computeRangeCompensation() {
|
||||
return ({
|
||||
constexpr double number_of_secondTh_in_one_tick = (1.0 * second_th) / ticksPerSecond;
|
||||
constexpr double fractional = number_of_secondTh_in_one_tick - (long)number_of_secondTh_in_one_tick;
|
||||
fractional == 0 ?
|
||||
1 : // no need for compensation
|
||||
(number_of_secondTh_in_one_tick / fractional) + 0.5; // scalar multiplier allowing exact division
|
||||
});
|
||||
}
|
||||
#endif
|
||||
|
||||
static constexpr timeType ticksPerSecond = TimeSourceType::ticksPerSecond;
|
||||
static constexpr timeType ticksPerSecondMax = TimeSourceType::ticksPerSecondMax;
|
||||
static constexpr timeType rangeCompensate = computeRangeCompensation();
|
||||
static constexpr timeType user2UnitMultiplierMax = (ticksPerSecondMax * rangeCompensate) / second_th;
|
||||
static constexpr timeType user2UnitMultiplier = (ticksPerSecond * rangeCompensate) / second_th;
|
||||
static constexpr timeType user2UnitDivider = rangeCompensate;
|
||||
// std::numeric_limits<timeType>::max() is reserved
|
||||
static constexpr timeType timeMax = (std::numeric_limits<timeType>::max() - 1) / user2UnitMultiplierMax;
|
||||
|
||||
static timeType toTimeTypeUnit(const timeType userUnit) {
|
||||
return (userUnit * user2UnitMultiplier) / user2UnitDivider;
|
||||
}
|
||||
static timeType toUserUnit(const timeType internalUnit) {
|
||||
return (internalUnit * user2UnitDivider) / user2UnitMultiplier;
|
||||
}
|
||||
static timeType time() {
|
||||
return TimeSourceType::time();
|
||||
}
|
||||
};
|
||||
|
||||
using TimeMillis = TimeUnit < TimeSourceMillis, 1'000 >;
|
||||
using TimeFastMillis = TimeUnit < TimeSourceCycles, 1'000 >;
|
||||
using TimeFastMicros = TimeUnit < TimeSourceCycles, 1'000'000 >;
|
||||
using TimeFastNanos = TimeUnit < TimeSourceCycles, 1'000'000'000 >;
|
||||
|
||||
} //TimePolicy
|
||||
|
||||
template <bool PeriodicT, typename YieldPolicyT = YieldPolicy::DoNothing, typename TimePolicyT = TimePolicy::TimeMillis>
|
||||
class timeoutTemplate {
|
||||
public:
|
||||
using timeType = typename TimePolicyT::timeType;
|
||||
static_assert(std::is_unsigned<timeType>::value == true, "timeType must be unsigned");
|
||||
|
||||
static constexpr timeType alwaysExpired = 0;
|
||||
static constexpr timeType neverExpires = std::numeric_limits<timeType>::max();
|
||||
static constexpr timeType rangeCompensate = TimePolicyT::rangeCompensate; //debug
|
||||
|
||||
timeoutTemplate(const timeType userTimeout) {
|
||||
reset(userTimeout);
|
||||
}
|
||||
|
||||
IRAM_ATTR // fast
|
||||
bool expired() {
|
||||
YieldPolicyT::execute(); //in case of DoNothing: gets optimized away
|
||||
if (PeriodicT) { //in case of false: gets optimized away
|
||||
return expiredRetrigger();
|
||||
}
|
||||
return expiredOneShot();
|
||||
}
|
||||
|
||||
IRAM_ATTR // fast
|
||||
operator bool() {
|
||||
return expired();
|
||||
}
|
||||
|
||||
bool canExpire() const {
|
||||
return !_neverExpires;
|
||||
}
|
||||
|
||||
bool canWait() const {
|
||||
return _timeout != alwaysExpired;
|
||||
}
|
||||
|
||||
// Resets, will trigger after this new timeout.
|
||||
IRAM_ATTR // called from ISR
|
||||
void reset(const timeType newUserTimeout) {
|
||||
reset();
|
||||
_timeout = TimePolicyT::toTimeTypeUnit(newUserTimeout);
|
||||
_neverExpires = (newUserTimeout < 0) || (newUserTimeout > timeMax());
|
||||
}
|
||||
|
||||
// Resets, will trigger after the timeout previously set.
|
||||
IRAM_ATTR // called from ISR
|
||||
void reset() {
|
||||
_start = TimePolicyT::time();
|
||||
}
|
||||
|
||||
// Resets to just expired so that on next poll the check will immediately trigger for the user,
|
||||
// also change timeout (after next immediate trigger).
|
||||
IRAM_ATTR // called from ISR
|
||||
void resetAndSetExpired(const timeType newUserTimeout) {
|
||||
reset(newUserTimeout);
|
||||
_start -= _timeout;
|
||||
}
|
||||
|
||||
// Resets to just expired so that on next poll the check will immediately trigger for the user.
|
||||
IRAM_ATTR // called from ISR
|
||||
void resetAndSetExpired() {
|
||||
reset();
|
||||
_start -= _timeout;
|
||||
}
|
||||
|
||||
void resetToNeverExpires() {
|
||||
_timeout = alwaysExpired + 1; // because canWait() has precedence
|
||||
_neverExpires = true;
|
||||
}
|
||||
|
||||
timeType getTimeout() const {
|
||||
return TimePolicyT::toUserUnit(_timeout);
|
||||
}
|
||||
|
||||
static constexpr timeType timeMax() {
|
||||
return TimePolicyT::timeMax;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
IRAM_ATTR // fast
|
||||
bool checkExpired(const timeType internalUnit) const {
|
||||
// canWait() is not checked here
|
||||
// returns "can expire" and "time expired"
|
||||
return (!_neverExpires) && ((internalUnit - _start) >= _timeout);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
IRAM_ATTR // fast
|
||||
bool expiredRetrigger() {
|
||||
if (!canWait()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
timeType current = TimePolicyT::time();
|
||||
if (checkExpired(current)) {
|
||||
unsigned long n = (current - _start) / _timeout; //how many _timeouts periods have elapsed, will usually be 1 (current - _start >= _timeout)
|
||||
_start += n * _timeout;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
IRAM_ATTR // fast
|
||||
bool expiredOneShot() const {
|
||||
// returns "always expired" or "has expired"
|
||||
return !canWait() || checkExpired(TimePolicyT::time());
|
||||
}
|
||||
|
||||
timeType _timeout;
|
||||
timeType _start;
|
||||
bool _neverExpires;
|
||||
};
|
||||
|
||||
// legacy type names, deprecated (unit is milliseconds)
|
||||
|
||||
using oneShot = polledTimeout::timeoutTemplate<false> /*__attribute__((deprecated("use oneShotMs")))*/;
|
||||
using periodic = polledTimeout::timeoutTemplate<true> /*__attribute__((deprecated("use periodicMs")))*/;
|
||||
|
||||
// standard versions (based on millis())
|
||||
// timeMax() is 49.7 days ((2^32)-2 ms)
|
||||
|
||||
using oneShotMs = polledTimeout::timeoutTemplate<false>;
|
||||
using periodicMs = polledTimeout::timeoutTemplate<true>;
|
||||
|
||||
// Time policy based on esp_get_cycle_count(), and intended to be called very often:
|
||||
// "Fast" versions sacrifices time range for improved precision and reduced execution time (by 86%)
|
||||
// (cpu cycles for ::expired(): 372 (millis()) vs 52 (esp_get_cycle_count()))
|
||||
// timeMax() values:
|
||||
// Ms: max is 26843 ms (26.8 s)
|
||||
// Us: max is 26843545 us (26.8 s)
|
||||
// Ns: max is 1073741823 ns ( 1.07 s)
|
||||
// (time policy based on esp_get_cycle_count() is intended to be called very often)
|
||||
|
||||
using oneShotFastMs = polledTimeout::timeoutTemplate<false, YieldPolicy::DoNothing, TimePolicy::TimeFastMillis>;
|
||||
using periodicFastMs = polledTimeout::timeoutTemplate<true, YieldPolicy::DoNothing, TimePolicy::TimeFastMillis>;
|
||||
using oneShotFastUs = polledTimeout::timeoutTemplate<false, YieldPolicy::DoNothing, TimePolicy::TimeFastMicros>;
|
||||
using periodicFastUs = polledTimeout::timeoutTemplate<true, YieldPolicy::DoNothing, TimePolicy::TimeFastMicros>;
|
||||
using oneShotFastNs = polledTimeout::timeoutTemplate<false, YieldPolicy::DoNothing, TimePolicy::TimeFastNanos>;
|
||||
using periodicFastNs = polledTimeout::timeoutTemplate<true, YieldPolicy::DoNothing, TimePolicy::TimeFastNanos>;
|
||||
|
||||
} //polledTimeout
|
||||
|
||||
|
||||
/* A 1-shot timeout that auto-yields when in CONT can be built as follows:
|
||||
using oneShotYieldMs = esp8266::polledTimeout::timeoutTemplate<false, esp8266::polledTimeout::YieldPolicy::YieldOrSkip>;
|
||||
|
||||
Other policies can be implemented by the user, e.g.: simple yield that panics in SYS, and the polledTimeout types built as needed as shown above, without modifying this file.
|
||||
*/
|
||||
|
||||
}//esp8266
|
||||
@@ -0,0 +1 @@
|
||||
#include "api/Print.h"
|
||||
@@ -0,0 +1 @@
|
||||
#include "api/Printable.h"
|
||||
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
* RP2040 utility class
|
||||
*
|
||||
* Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
*
|
||||
* 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 <hardware/pio.h>
|
||||
#include <hardware/clocks.h>
|
||||
|
||||
class RP2040 {
|
||||
public:
|
||||
// Convert from microseconds to PIO clock cycles
|
||||
static int usToPIOCycles(int us) {
|
||||
// Parenthesis needed to guarantee order of operations to avoid 32bit overflow
|
||||
return (us * ( clock_get_hz(clk_sys) / 1000000 ));
|
||||
}
|
||||
|
||||
// Get current clock frequency
|
||||
static int f_cpu() {
|
||||
return clock_get_hz(clk_sys);
|
||||
}
|
||||
};
|
||||
|
||||
// Wrapper class for PIO programs, abstracting common operations out
|
||||
// TODO - Make dualcore safe
|
||||
// TODO - Add unload/destructor
|
||||
class PIOProgram {
|
||||
public:
|
||||
PIOProgram(const pio_program_t *pgm) { _pgm = pgm; }
|
||||
|
||||
// Possibly load into a PIO and allocate a SM
|
||||
bool prepare(PIO *pio, int *sm, int *offset) {
|
||||
// Is there an open slot to run in, first?
|
||||
if (!_findFreeSM(pio, sm)) return false;
|
||||
// Is it loaded on that PIO?
|
||||
if (_offset[pio_get_index(*pio)] < 0) {
|
||||
// Nope, need to load it
|
||||
if (!pio_can_add_program(*pio, _pgm)) return false;
|
||||
_offset[pio_get_index(*pio)] = pio_add_program(*pio, _pgm);
|
||||
}
|
||||
// Here it's guaranteed loaded, return values
|
||||
// PIO and SM already set
|
||||
*offset = _offset[pio_get_index(*pio)];
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
// Find an unused PIO state machine to grab, returns false when none available
|
||||
static bool _findFreeSM(PIO *pio, int *sm) {
|
||||
int idx = pio_claim_unused_sm(pio0, false);
|
||||
if (idx >= 0) {
|
||||
*pio = pio0;
|
||||
*sm = idx;
|
||||
return true;
|
||||
}
|
||||
idx = pio_claim_unused_sm(pio1, false);
|
||||
if (idx >= 0) {
|
||||
*pio = pio1;
|
||||
*sm = idx;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
int _offset[2] = { -1, -1 };
|
||||
const pio_program_t *_pgm;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
RP2040 utility class
|
||||
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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 <hardware/structs/psm.h>
|
||||
|
||||
extern "C" void boot_double_tap_check();
|
||||
|
||||
// The following check will never actually execute, but it will cause the boot reset
|
||||
// checker to be linked in as part of the constructors.
|
||||
|
||||
void RP2040::enableDoubleResetBootloader() {
|
||||
if (psm_hw->done == 0) {
|
||||
boot_double_tap_check();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,368 @@
|
||||
/*
|
||||
RP2040 utility class
|
||||
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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 <hardware/clocks.h>
|
||||
#include <hardware/irq.h>
|
||||
#include <hardware/pio.h>
|
||||
#include <pico/unique_id.h>
|
||||
#include <hardware/exception.h>
|
||||
#include <hardware/watchdog.h>
|
||||
#include <hardware/structs/rosc.h>
|
||||
#include <hardware/structs/systick.h>
|
||||
#include <pico/multicore.h>
|
||||
#include <pico/util/queue.h>
|
||||
#include "CoreMutex.h"
|
||||
#include "ccount.pio.h"
|
||||
#include <malloc.h>
|
||||
|
||||
#include "_freertos.h"
|
||||
|
||||
extern "C" volatile bool __otherCoreIdled;
|
||||
|
||||
class _MFIFO {
|
||||
public:
|
||||
_MFIFO() { /* noop */ };
|
||||
~_MFIFO() { /* noop */ };
|
||||
|
||||
void begin(int cores) {
|
||||
constexpr int FIFOCNT = 8;
|
||||
if (cores == 1) {
|
||||
_multicore = false;
|
||||
return;
|
||||
}
|
||||
mutex_init(&_idleMutex);
|
||||
queue_init(&_queue[0], sizeof(uint32_t), FIFOCNT);
|
||||
queue_init(&_queue[1], sizeof(uint32_t), FIFOCNT);
|
||||
_multicore = true;
|
||||
}
|
||||
|
||||
void registerCore() {
|
||||
if (!__isFreeRTOS) {
|
||||
multicore_fifo_clear_irq();
|
||||
irq_set_exclusive_handler(SIO_IRQ_PROC0 + get_core_num(), _irq);
|
||||
irq_set_enabled(SIO_IRQ_PROC0 + get_core_num(), true);
|
||||
}
|
||||
// FreeRTOS port.c will handle the IRQ hooking
|
||||
}
|
||||
|
||||
void push(uint32_t val) {
|
||||
while (!push_nb(val)) { /* noop */ }
|
||||
}
|
||||
|
||||
bool push_nb(uint32_t val) {
|
||||
// Push to the other FIFO
|
||||
return queue_try_add(&_queue[get_core_num() ^ 1], &val);
|
||||
}
|
||||
|
||||
uint32_t pop() {
|
||||
uint32_t ret;
|
||||
while (!pop_nb(&ret)) { /* noop */ }
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool pop_nb(uint32_t *val) {
|
||||
// Pop from my own FIFO
|
||||
return queue_try_remove(&_queue[get_core_num()], val);
|
||||
}
|
||||
|
||||
int available() {
|
||||
return queue_get_level(&_queue[get_core_num()]);
|
||||
}
|
||||
|
||||
void idleOtherCore() {
|
||||
if (!_multicore) {
|
||||
return;
|
||||
}
|
||||
__holdUpPendSV = 1;
|
||||
if (__isFreeRTOS) {
|
||||
vTaskPreemptionDisable(nullptr);
|
||||
vTaskSuspendAll();
|
||||
}
|
||||
mutex_enter_blocking(&_idleMutex);
|
||||
__otherCoreIdled = false;
|
||||
multicore_fifo_push_blocking(_GOTOSLEEP);
|
||||
while (!__otherCoreIdled) { /* noop */ }
|
||||
}
|
||||
|
||||
void resumeOtherCore() {
|
||||
if (!_multicore) {
|
||||
return;
|
||||
}
|
||||
mutex_exit(&_idleMutex);
|
||||
__otherCoreIdled = false;
|
||||
if (__isFreeRTOS) {
|
||||
xTaskResumeAll();
|
||||
vTaskPreemptionEnable(nullptr);
|
||||
}
|
||||
__holdUpPendSV = 0;
|
||||
|
||||
// Other core will exit busy-loop and return to operation
|
||||
// once __otherCoreIdled == false.
|
||||
}
|
||||
|
||||
void clear() {
|
||||
uint32_t val;
|
||||
|
||||
while (queue_try_remove(&_queue[0], &val)) {
|
||||
tight_loop_contents();
|
||||
}
|
||||
|
||||
while (queue_try_remove(&_queue[1], &val)) {
|
||||
tight_loop_contents();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static void __no_inline_not_in_flash_func(_irq)() {
|
||||
if (!__isFreeRTOS) {
|
||||
multicore_fifo_clear_irq();
|
||||
noInterrupts(); // We need total control, can't run anything
|
||||
while (multicore_fifo_rvalid()) {
|
||||
if (_GOTOSLEEP == multicore_fifo_pop_blocking()) {
|
||||
__otherCoreIdled = true;
|
||||
while (__otherCoreIdled) { /* noop */ }
|
||||
break;
|
||||
}
|
||||
}
|
||||
interrupts();
|
||||
}
|
||||
}
|
||||
|
||||
bool _multicore = false;
|
||||
mutex_t _idleMutex;
|
||||
queue_t _queue[2];
|
||||
static constexpr uint32_t _GOTOSLEEP = 0xC0DED02E;
|
||||
};
|
||||
|
||||
|
||||
class RP2040;
|
||||
extern RP2040 rp2040;
|
||||
extern "C" void main1();
|
||||
class PIOProgram;
|
||||
|
||||
extern "C" char __StackLimit;
|
||||
extern "C" char __bss_end__;
|
||||
|
||||
// Wrapper class for PIO programs, abstracting common operations out
|
||||
// TODO - Add unload/destructor
|
||||
class PIOProgram {
|
||||
public:
|
||||
PIOProgram(const pio_program_t *pgm) {
|
||||
_pgm = pgm;
|
||||
}
|
||||
|
||||
// Possibly load into a PIO and allocate a SM
|
||||
bool prepare(PIO *pio, int *sm, int *offset) {
|
||||
extern mutex_t _pioMutex;
|
||||
CoreMutex m(&_pioMutex);
|
||||
// Is there an open slot to run in, first?
|
||||
if (!_findFreeSM(pio, sm)) {
|
||||
return false;
|
||||
}
|
||||
// Is it loaded on that PIO?
|
||||
if (_offset[pio_get_index(*pio)] < 0) {
|
||||
// Nope, need to load it
|
||||
if (!pio_can_add_program(*pio, _pgm)) {
|
||||
return false;
|
||||
}
|
||||
_offset[pio_get_index(*pio)] = pio_add_program(*pio, _pgm);
|
||||
}
|
||||
// Here it's guaranteed loaded, return values
|
||||
// PIO and SM already set
|
||||
*offset = _offset[pio_get_index(*pio)];
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
// Find an unused PIO state machine to grab, returns false when none available
|
||||
static bool _findFreeSM(PIO *pio, int *sm) {
|
||||
int idx = pio_claim_unused_sm(pio0, false);
|
||||
if (idx >= 0) {
|
||||
*pio = pio0;
|
||||
*sm = idx;
|
||||
return true;
|
||||
}
|
||||
idx = pio_claim_unused_sm(pio1, false);
|
||||
if (idx >= 0) {
|
||||
*pio = pio1;
|
||||
*sm = idx;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
int _offset[2] = { -1, -1 };
|
||||
const pio_program_t *_pgm;
|
||||
};
|
||||
|
||||
class RP2040 {
|
||||
public:
|
||||
RP2040() { /* noop */ }
|
||||
~RP2040() { /* noop */ }
|
||||
|
||||
void begin() {
|
||||
_epoch = 0;
|
||||
if (!__isFreeRTOS) {
|
||||
// Enable SYSTICK exception
|
||||
exception_set_exclusive_handler(SYSTICK_EXCEPTION, _SystickHandler);
|
||||
systick_hw->csr = 0x7;
|
||||
systick_hw->rvr = 0x00FFFFFF;
|
||||
} else {
|
||||
int off = 0;
|
||||
_ccountPgm = new PIOProgram(&ccount_program);
|
||||
_ccountPgm->prepare(&_pio, &_sm, &off);
|
||||
ccount_program_init(_pio, _sm, off);
|
||||
pio_sm_set_enabled(_pio, _sm, true);
|
||||
}
|
||||
}
|
||||
|
||||
// Convert from microseconds to PIO clock cycles
|
||||
static int usToPIOCycles(int us) {
|
||||
// Parenthesis needed to guarantee order of operations to avoid 32bit overflow
|
||||
return (us * (clock_get_hz(clk_sys) / 1'000'000));
|
||||
}
|
||||
|
||||
// Get current clock frequency
|
||||
static int f_cpu() {
|
||||
return clock_get_hz(clk_sys);
|
||||
}
|
||||
|
||||
// Get CPU cycle count. Needs to do magic to extens 24b HW to something longer
|
||||
volatile uint64_t _epoch = 0;
|
||||
inline uint32_t getCycleCount() {
|
||||
if (!__isFreeRTOS) {
|
||||
uint32_t epoch;
|
||||
uint32_t ctr;
|
||||
do {
|
||||
epoch = (uint32_t)_epoch;
|
||||
ctr = systick_hw->cvr;
|
||||
} while (epoch != (uint32_t)_epoch);
|
||||
return epoch + (1 << 24) - ctr; /* CTR counts down from 1<<24-1 */
|
||||
} else {
|
||||
return ccount_read(_pio, _sm);
|
||||
}
|
||||
}
|
||||
|
||||
inline uint64_t getCycleCount64() {
|
||||
if (!__isFreeRTOS) {
|
||||
uint64_t epoch;
|
||||
uint64_t ctr;
|
||||
do {
|
||||
epoch = _epoch;
|
||||
ctr = systick_hw->cvr;
|
||||
} while (epoch != _epoch);
|
||||
return epoch + (1LL << 24) - ctr;
|
||||
} else {
|
||||
return ccount_read(_pio, _sm);
|
||||
}
|
||||
}
|
||||
|
||||
inline int getFreeHeap() {
|
||||
return getTotalHeap() - getUsedHeap();
|
||||
}
|
||||
|
||||
inline int getUsedHeap() {
|
||||
struct mallinfo m = mallinfo();
|
||||
return m.uordblks;
|
||||
}
|
||||
|
||||
inline int getTotalHeap() {
|
||||
return &__StackLimit - &__bss_end__;
|
||||
}
|
||||
|
||||
void idleOtherCore() {
|
||||
fifo.idleOtherCore();
|
||||
}
|
||||
|
||||
void resumeOtherCore() {
|
||||
fifo.resumeOtherCore();
|
||||
}
|
||||
|
||||
void restartCore1() {
|
||||
multicore_reset_core1();
|
||||
fifo.clear();
|
||||
multicore_launch_core1(main1);
|
||||
}
|
||||
|
||||
void reboot() {
|
||||
watchdog_reboot(0, 0, 10);
|
||||
while (1) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
inline void restart() {
|
||||
reboot();
|
||||
}
|
||||
|
||||
static void enableDoubleResetBootloader();
|
||||
|
||||
void wdt_begin(uint32_t delay_ms) {
|
||||
watchdog_enable(delay_ms, 1);
|
||||
}
|
||||
|
||||
void wdt_reset() {
|
||||
watchdog_update();
|
||||
}
|
||||
|
||||
const char *getChipID() {
|
||||
static char id[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1] = { 0 };
|
||||
if (!id[0]) {
|
||||
pico_get_unique_board_id_string(id, sizeof(id));
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
// Multicore comms FIFO
|
||||
_MFIFO fifo;
|
||||
|
||||
|
||||
// TODO - Not so great HW random generator. 32-bits wide. Cryptographers somewhere are crying
|
||||
uint32_t hwrand32() {
|
||||
// Try and whiten the HW ROSC bit
|
||||
uint32_t r = 0;
|
||||
for (int k = 0; k < 32; k++) {
|
||||
unsigned long int b;
|
||||
do {
|
||||
b = rosc_hw->randombit & 1;
|
||||
if (b != (rosc_hw->randombit & 1)) {
|
||||
break;
|
||||
}
|
||||
} while (true);
|
||||
r <<= 1;
|
||||
r |= b;
|
||||
}
|
||||
// Stir using the cycle count LSBs. In any WiFi use case this will be a random # since the connection time is not cycle-accurate
|
||||
uint64_t rr = (((uint64_t)~r) << 32LL) | r;
|
||||
rr >>= rp2040.getCycleCount() & 32LL;
|
||||
|
||||
return (uint32_t)rr;
|
||||
}
|
||||
|
||||
private:
|
||||
static void _SystickHandler() {
|
||||
rp2040._epoch += 1LL << 24;
|
||||
}
|
||||
PIO _pio;
|
||||
int _sm;
|
||||
PIOProgram *_ccountPgm;
|
||||
};
|
||||
@@ -0,0 +1,435 @@
|
||||
/*
|
||||
Shared USB for the Raspberry Pi Pico RP2040
|
||||
Allows for multiple endpoints to share the USB controller
|
||||
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
#if !defined(USE_TINYUSB) && !defined(NO_USB)
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "CoreMutex.h"
|
||||
#include "RP2040USB.h"
|
||||
|
||||
#include "tusb.h"
|
||||
#include "class/hid/hid_device.h"
|
||||
#include "class/audio/audio.h"
|
||||
#include "class/midi/midi.h"
|
||||
#include "pico/time.h"
|
||||
#include "hardware/irq.h"
|
||||
#include "pico/mutex.h"
|
||||
#include "pico/unique_id.h"
|
||||
|
||||
// Big, global USB mutex, shared with all USB devices to make sure we don't
|
||||
// have multiple cores updating the TUSB state in parallel
|
||||
mutex_t __usb_mutex;
|
||||
|
||||
// USB processing will be a periodic timer task
|
||||
#define USB_TASK_INTERVAL 1000
|
||||
static int __usb_task_irq;
|
||||
|
||||
// USB VID/PID (note that PID can change depending on the add'l interfaces)
|
||||
#define USBD_VID (0x2E8A) // Raspberry Pi
|
||||
|
||||
#ifdef SERIALUSB_PID
|
||||
#define USBD_PID (SERIALUSB_PID)
|
||||
#else
|
||||
#define USBD_PID (0x000a) // Raspberry Pi Pico SDK CDC
|
||||
#endif
|
||||
|
||||
#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
|
||||
|
||||
#define USBD_ITF_CDC (0) // needs 2 interfaces
|
||||
#define USBD_ITF_MAX (2)
|
||||
|
||||
#define USBD_CDC_EP_CMD (0x81)
|
||||
#define USBD_CDC_EP_OUT (0x02)
|
||||
#define USBD_CDC_EP_IN (0x82)
|
||||
#define USBD_CDC_CMD_MAX_SIZE (8)
|
||||
#define USBD_CDC_IN_OUT_MAX_SIZE (64)
|
||||
|
||||
#define USBD_STR_0 (0x00)
|
||||
#define USBD_STR_MANUF (0x01)
|
||||
#define USBD_STR_PRODUCT (0x02)
|
||||
#define USBD_STR_SERIAL (0x03)
|
||||
#define USBD_STR_CDC (0x04)
|
||||
|
||||
#define EPNUM_HID 0x83
|
||||
|
||||
#define USBD_MSC_EPOUT 0x03
|
||||
#define USBD_MSC_EPIN 0x84
|
||||
#define USBD_MSC_EPSIZE 64
|
||||
|
||||
const uint8_t *tud_descriptor_device_cb(void) {
|
||||
static tusb_desc_device_t usbd_desc_device = {
|
||||
.bLength = sizeof(tusb_desc_device_t),
|
||||
.bDescriptorType = TUSB_DESC_DEVICE,
|
||||
.bcdUSB = 0x0200,
|
||||
.bDeviceClass = TUSB_CLASS_CDC,
|
||||
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
|
||||
.bDeviceProtocol = MISC_PROTOCOL_IAD,
|
||||
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
|
||||
.idVendor = USBD_VID,
|
||||
.idProduct = USBD_PID,
|
||||
.bcdDevice = 0x0100,
|
||||
.iManufacturer = USBD_STR_MANUF,
|
||||
.iProduct = USBD_STR_PRODUCT,
|
||||
.iSerialNumber = USBD_STR_SERIAL,
|
||||
.bNumConfigurations = 1
|
||||
};
|
||||
if (__USBInstallSerial && !__USBInstallKeyboard && !__USBInstallMouse && !__USBInstallJoystick && !__USBInstallMassStorage) {
|
||||
// Can use as-is, this is the default USB case
|
||||
return (const uint8_t *)&usbd_desc_device;
|
||||
}
|
||||
// Need a multi-endpoint config which will require changing the PID to help Windows not barf
|
||||
if (__USBInstallKeyboard) {
|
||||
usbd_desc_device.idProduct |= 0x8000;
|
||||
}
|
||||
if (__USBInstallMouse) {
|
||||
usbd_desc_device.idProduct |= 0x4000;
|
||||
}
|
||||
if (__USBInstallJoystick) {
|
||||
usbd_desc_device.idProduct |= 0x0100;
|
||||
}
|
||||
if (__USBInstallMassStorage) {
|
||||
usbd_desc_device.idProduct ^= 0x2000;
|
||||
}
|
||||
// Set the device class to 0 to indicate multiple device classes
|
||||
usbd_desc_device.bDeviceClass = 0;
|
||||
usbd_desc_device.bDeviceSubClass = 0;
|
||||
usbd_desc_device.bDeviceProtocol = 0;
|
||||
return (const uint8_t *)&usbd_desc_device;
|
||||
}
|
||||
|
||||
int __USBGetKeyboardReportID() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int __USBGetMouseReportID() {
|
||||
return __USBInstallKeyboard ? 2 : 1;
|
||||
}
|
||||
|
||||
int __USBGetJoystickReportID() {
|
||||
int i = 1;
|
||||
if (__USBInstallKeyboard) {
|
||||
i++;
|
||||
}
|
||||
if (__USBInstallMouse) {
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
static int __hid_report_len = 0;
|
||||
static uint8_t *__hid_report = nullptr;
|
||||
|
||||
static uint8_t *GetDescHIDReport(int *len) {
|
||||
if (len) {
|
||||
*len = __hid_report_len;
|
||||
}
|
||||
return __hid_report;
|
||||
}
|
||||
|
||||
void __SetupDescHIDReport() {
|
||||
//allocate memory for the HID report descriptors. We don't use them, but need the size here.
|
||||
uint8_t desc_hid_report_mouse[] = { TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(1)) };
|
||||
uint8_t desc_hid_report_joystick[] = { TUD_HID_REPORT_DESC_GAMEPAD(HID_REPORT_ID(1)) };
|
||||
uint8_t desc_hid_report_keyboard[] = { TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1)) };
|
||||
int size = 0;
|
||||
|
||||
//accumulate the size of all used HID report descriptors
|
||||
if (__USBInstallKeyboard) {
|
||||
size += sizeof(desc_hid_report_keyboard);
|
||||
}
|
||||
if (__USBInstallMouse) {
|
||||
size += sizeof(desc_hid_report_mouse);
|
||||
}
|
||||
if (__USBInstallJoystick) {
|
||||
size += sizeof(desc_hid_report_joystick);
|
||||
}
|
||||
|
||||
//no HID used at all
|
||||
if (size == 0) {
|
||||
__hid_report = nullptr;
|
||||
__hid_report_len = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
//allocate the "real" HID report descriptor
|
||||
__hid_report = (uint8_t *)malloc(size);
|
||||
if (__hid_report) {
|
||||
__hid_report_len = size;
|
||||
|
||||
//now copy the descriptors
|
||||
|
||||
//1.) keyboard descriptor, if requested
|
||||
if (__USBInstallKeyboard) {
|
||||
memcpy(__hid_report, desc_hid_report_keyboard, sizeof(desc_hid_report_keyboard));
|
||||
}
|
||||
|
||||
//2.) mouse descriptor, if necessary. Additional offset & new array is necessary if there is a keyboard.
|
||||
if (__USBInstallMouse) {
|
||||
//determine if we need an offset (USB keyboard is installed)
|
||||
if (__USBInstallKeyboard) {
|
||||
uint8_t desc_local[] = { TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(2)) };
|
||||
memcpy(__hid_report + sizeof(desc_hid_report_keyboard), desc_local, sizeof(desc_local));
|
||||
} else {
|
||||
memcpy(__hid_report, desc_hid_report_mouse, sizeof(desc_hid_report_mouse));
|
||||
}
|
||||
}
|
||||
|
||||
//3.) joystick descriptor. 2 additional checks are necessary for mouse and/or keyboard
|
||||
if (__USBInstallJoystick) {
|
||||
uint8_t reportid = 1;
|
||||
int offset = 0;
|
||||
if (__USBInstallKeyboard) {
|
||||
reportid++;
|
||||
offset += sizeof(desc_hid_report_keyboard);
|
||||
}
|
||||
if (__USBInstallMouse) {
|
||||
reportid++;
|
||||
offset += sizeof(desc_hid_report_mouse);
|
||||
}
|
||||
uint8_t desc_local[] = { TUD_HID_REPORT_DESC_GAMEPAD(HID_REPORT_ID(reportid)) };
|
||||
memcpy(__hid_report + offset, desc_local, sizeof(desc_local));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Invoked when received GET HID REPORT DESCRIPTOR
|
||||
// Application return pointer to descriptor
|
||||
// Descriptor contents must exist long enough for transfer to complete
|
||||
uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance) {
|
||||
(void) instance;
|
||||
return GetDescHIDReport(nullptr);
|
||||
}
|
||||
|
||||
static uint8_t *usbd_desc_cfg = nullptr;
|
||||
const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
|
||||
(void)index;
|
||||
return usbd_desc_cfg;
|
||||
}
|
||||
|
||||
void __SetupUSBDescriptor() {
|
||||
if (!usbd_desc_cfg) {
|
||||
bool hasHID = __USBInstallKeyboard || __USBInstallMouse || __USBInstallJoystick;
|
||||
|
||||
uint8_t interface_count = (__USBInstallSerial ? 2 : 0) + (hasHID ? 1 : 0) + (__USBInstallMassStorage ? 1 : 0);
|
||||
|
||||
uint8_t cdc_desc[TUD_CDC_DESC_LEN] = {
|
||||
// Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval
|
||||
TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD, USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE)
|
||||
};
|
||||
|
||||
int hid_report_len;
|
||||
GetDescHIDReport(&hid_report_len);
|
||||
uint8_t hid_itf = __USBInstallSerial ? 2 : 0;
|
||||
uint8_t hid_desc[TUD_HID_DESC_LEN] = {
|
||||
// Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval
|
||||
TUD_HID_DESCRIPTOR(hid_itf, 0, HID_ITF_PROTOCOL_NONE, hid_report_len, EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10)
|
||||
};
|
||||
|
||||
uint8_t msd_itf = interface_count - 1;
|
||||
uint8_t msd_desc[TUD_MSC_DESC_LEN] = {
|
||||
TUD_MSC_DESCRIPTOR(msd_itf, 0, USBD_MSC_EPOUT, USBD_MSC_EPIN, USBD_MSC_EPSIZE)
|
||||
};
|
||||
|
||||
int usbd_desc_len = TUD_CONFIG_DESC_LEN + (__USBInstallSerial ? sizeof(cdc_desc) : 0) + (hasHID ? sizeof(hid_desc) : 0) + (__USBInstallMassStorage ? sizeof(msd_desc) : 0);
|
||||
|
||||
uint8_t tud_cfg_desc[TUD_CONFIG_DESC_LEN] = {
|
||||
// Config number, interface count, string index, total length, attribute, power in mA
|
||||
TUD_CONFIG_DESCRIPTOR(1, interface_count, USBD_STR_0, usbd_desc_len, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA)
|
||||
};
|
||||
|
||||
// Combine to one descriptor
|
||||
usbd_desc_cfg = (uint8_t *)malloc(usbd_desc_len);
|
||||
if (usbd_desc_cfg) {
|
||||
bzero(usbd_desc_cfg, usbd_desc_len);
|
||||
uint8_t *ptr = usbd_desc_cfg;
|
||||
memcpy(ptr, tud_cfg_desc, sizeof(tud_cfg_desc));
|
||||
ptr += sizeof(tud_cfg_desc);
|
||||
if (__USBInstallSerial) {
|
||||
memcpy(ptr, cdc_desc, sizeof(cdc_desc));
|
||||
ptr += sizeof(cdc_desc);
|
||||
}
|
||||
if (hasHID) {
|
||||
memcpy(ptr, hid_desc, sizeof(hid_desc));
|
||||
ptr += sizeof(hid_desc);
|
||||
}
|
||||
if (__USBInstallMassStorage) {
|
||||
memcpy(ptr, msd_desc, sizeof(msd_desc));
|
||||
ptr += sizeof(msd_desc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||
(void) langid;
|
||||
#define DESC_STR_MAX (20)
|
||||
static uint16_t desc_str[DESC_STR_MAX];
|
||||
|
||||
static char idString[PICO_UNIQUE_BOARD_ID_SIZE_BYTES * 2 + 1];
|
||||
|
||||
static const char *const usbd_desc_str[] = {
|
||||
[USBD_STR_0] = "",
|
||||
[USBD_STR_MANUF] = "Raspberry Pi",
|
||||
[USBD_STR_PRODUCT] = "PicoArduino",
|
||||
[USBD_STR_SERIAL] = idString,
|
||||
[USBD_STR_CDC] = "Board CDC",
|
||||
};
|
||||
|
||||
if (!idString[0]) {
|
||||
pico_get_unique_board_id_string(idString, sizeof(idString));
|
||||
}
|
||||
|
||||
uint8_t len;
|
||||
if (index == 0) {
|
||||
desc_str[1] = 0x0409; // supported language is English
|
||||
len = 1;
|
||||
} else {
|
||||
if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0])) {
|
||||
return nullptr;
|
||||
}
|
||||
const char *str = usbd_desc_str[index];
|
||||
for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) {
|
||||
desc_str[1 + len] = str[len];
|
||||
}
|
||||
}
|
||||
|
||||
// first byte is length (including header), second byte is string type
|
||||
desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * len + 2);
|
||||
|
||||
return desc_str;
|
||||
}
|
||||
|
||||
|
||||
static void usb_irq() {
|
||||
// if the mutex is already owned, then we are in user code
|
||||
// in this file which will do a tud_task itself, so we'll just do nothing
|
||||
// until the next tick; we won't starve
|
||||
if (mutex_try_enter(&__usb_mutex, nullptr)) {
|
||||
tud_task();
|
||||
mutex_exit(&__usb_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
static int64_t timer_task(__unused alarm_id_t id, __unused void *user_data) {
|
||||
irq_set_pending(__usb_task_irq);
|
||||
return USB_TASK_INTERVAL;
|
||||
}
|
||||
|
||||
void __USBStart() __attribute__((weak));
|
||||
|
||||
void __USBStart() {
|
||||
if (tusb_inited()) {
|
||||
// Already called
|
||||
return;
|
||||
}
|
||||
|
||||
__SetupDescHIDReport();
|
||||
__SetupUSBDescriptor();
|
||||
|
||||
mutex_init(&__usb_mutex);
|
||||
|
||||
tusb_init();
|
||||
|
||||
__usb_task_irq = user_irq_claim_unused(true);
|
||||
irq_set_exclusive_handler(__usb_task_irq, usb_irq);
|
||||
irq_set_enabled(__usb_task_irq, true);
|
||||
|
||||
add_alarm_in_us(USB_TASK_INTERVAL, timer_task, nullptr, true);
|
||||
}
|
||||
|
||||
|
||||
// Invoked when received GET_REPORT control request
|
||||
// Application must fill buffer report's content and return its length.
|
||||
// Return zero will cause the stack to STALL request
|
||||
extern "C" uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) {
|
||||
// TODO not implemented
|
||||
(void) instance;
|
||||
(void) report_id;
|
||||
(void) report_type;
|
||||
(void) buffer;
|
||||
(void) reqlen;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Invoked when received SET_REPORT control request or
|
||||
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
|
||||
extern "C" void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) {
|
||||
// TODO set LED based on CAPLOCK, NUMLOCK etc...
|
||||
(void) instance;
|
||||
(void) report_id;
|
||||
(void) report_type;
|
||||
(void) buffer;
|
||||
(void) bufsize;
|
||||
}
|
||||
|
||||
extern "C" int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) __attribute__((weak));
|
||||
extern "C" int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) {
|
||||
(void) lun;
|
||||
(void) lba;
|
||||
(void) offset;
|
||||
(void) buffer;
|
||||
(void) bufsize;
|
||||
return -1;
|
||||
}
|
||||
|
||||
extern "C" bool tud_msc_test_unit_ready_cb(uint8_t lun) __attribute__((weak));
|
||||
extern "C" bool tud_msc_test_unit_ready_cb(uint8_t lun) {
|
||||
(void) lun;
|
||||
return false;
|
||||
}
|
||||
|
||||
extern "C" int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize) __attribute__((weak));
|
||||
extern "C" int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize) {
|
||||
(void) lun;
|
||||
(void) lba;
|
||||
(void) offset;
|
||||
(void) buffer;
|
||||
(void) bufsize;
|
||||
return -1;
|
||||
}
|
||||
|
||||
extern "C" int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) __attribute__((weak));
|
||||
extern "C" int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) {
|
||||
(void) lun;
|
||||
(void) scsi_cmd;
|
||||
(void) buffer;
|
||||
(void) bufsize;
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size) __attribute__((weak));
|
||||
extern "C" void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size) {
|
||||
(void) lun;
|
||||
*block_count = 0;
|
||||
*block_size = 0;
|
||||
}
|
||||
|
||||
extern "C" void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) __attribute__((weak));
|
||||
extern "C" void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) {
|
||||
(void) lun;
|
||||
vendor_id[0] = 0;
|
||||
product_id[0] = 0;
|
||||
product_rev[0] = 0;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
Shared USB for the Raspberry Pi Pico RP2040
|
||||
Allows for multiple endpoints to share the USB controller
|
||||
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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 "pico/mutex.h"
|
||||
|
||||
// Weak function definitions for each type of endpoint
|
||||
extern void __USBInstallSerial() __attribute__((weak));
|
||||
extern void __USBInstallKeyboard() __attribute__((weak));
|
||||
extern void __USBInstallJoystick() __attribute__((weak));
|
||||
extern void __USBInstallMouse() __attribute__((weak));
|
||||
extern void __USBInstallMassStorage() __attribute__((weak));
|
||||
|
||||
// Big, global USB mutex, shared with all USB devices to make sure we don't
|
||||
// have multiple cores updating the TUSB state in parallel
|
||||
extern mutex_t __usb_mutex;
|
||||
|
||||
// HID report ID inquiry (report ID will vary depending on the number/type of other HID)
|
||||
int __USBGetKeyboardReportID();
|
||||
int __USBGetMouseReportID();
|
||||
int __USBGetJoystickReportID();
|
||||
|
||||
// Called by main() to init the USB HW/SW.
|
||||
void __USBStart();
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
#define ARDUINO_PICO_MAJOR 2
|
||||
#define ARDUINO_PICO_MINOR 7
|
||||
#define ARDUINO_PICO_REVISION 0
|
||||
#define ARDUINO_PICO_VERSION_STR "2.7.0"
|
||||
@@ -0,0 +1,379 @@
|
||||
/*
|
||||
Serial-over-PIO for the Raspberry Pi Pico RP2040
|
||||
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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 "SerialPIO.h"
|
||||
#include "CoreMutex.h"
|
||||
#include <hardware/gpio.h>
|
||||
#include <map>
|
||||
#include "pio_uart.pio.h"
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// -- Generates a unique program for differing bit lengths
|
||||
static std::map<int, PIOProgram*> _txMap;
|
||||
static std::map<int, PIOProgram*> _rxMap;
|
||||
|
||||
// Duplicate a program and replace the first insn with a "set x, repl"
|
||||
static pio_program_t *pio_make_uart_prog(int repl, const pio_program_t *pg) {
|
||||
pio_program_t *p = new pio_program_t;
|
||||
p->length = pg->length;
|
||||
p->origin = pg->origin;
|
||||
uint16_t *insn = (uint16_t *)malloc(p->length * 2);
|
||||
if (!insn) {
|
||||
delete p;
|
||||
return nullptr;
|
||||
}
|
||||
memcpy(insn, pg->instructions, p->length * 2);
|
||||
insn[0] = pio_encode_set(pio_x, repl);
|
||||
p->instructions = insn;
|
||||
return p;
|
||||
}
|
||||
|
||||
static PIOProgram *_getTxProgram(int bits) {
|
||||
auto f = _txMap.find(bits);
|
||||
if (f == _txMap.end()) {
|
||||
pio_program_t * p = pio_make_uart_prog(bits, &pio_tx_program);
|
||||
_txMap.insert({bits, new PIOProgram(p)});
|
||||
f = _txMap.find(bits);
|
||||
}
|
||||
return f->second;
|
||||
}
|
||||
|
||||
static PIOProgram *_getRxProgram(int bits) {
|
||||
auto f = _rxMap.find(bits);
|
||||
if (f == _rxMap.end()) {
|
||||
pio_program_t * p = pio_make_uart_prog(bits, &pio_rx_program);
|
||||
_rxMap.insert({bits, new PIOProgram(p)});
|
||||
f = _rxMap.find(bits);
|
||||
}
|
||||
return f->second;
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
// TODO - this works, but there must be a faster/better way...
|
||||
static int _parity(int bits, int data) {
|
||||
int p = 0;
|
||||
for (int b = 0; b < bits; b++) {
|
||||
p ^= (data & (1 << b)) ? 1 : 0;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
// We need to cache generated SerialPIOs so we can add data to them from
|
||||
// the shared handler
|
||||
static SerialPIO *_pioSP[2][4];
|
||||
static void __not_in_flash_func(_fifoIRQ)() {
|
||||
for (int p = 0; p < 2; p++) {
|
||||
for (int sm = 0; sm < 4; sm++) {
|
||||
SerialPIO *s = _pioSP[p][sm];
|
||||
if (s) {
|
||||
s->_handleIRQ();
|
||||
pio_interrupt_clear((p == 0) ? pio0 : pio1, sm);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void __not_in_flash_func(SerialPIO::_handleIRQ)() {
|
||||
if (_rx == NOPIN) {
|
||||
return;
|
||||
}
|
||||
while (!pio_sm_is_rx_fifo_empty(_rxPIO, _rxSM)) {
|
||||
uint32_t decode = _rxPIO->rxf[_rxSM];
|
||||
decode >>= 33 - _rxBits;
|
||||
uint32_t val = 0;
|
||||
for (int b = 0; b < _bits + 1; b++) {
|
||||
val |= (decode & (1 << (b * 2))) ? 1 << b : 0;
|
||||
}
|
||||
if (_parity == UART_PARITY_EVEN) {
|
||||
int p = ::_parity(_bits, val);
|
||||
int r = (val & (1 << _bits)) ? 1 : 0;
|
||||
if (p != r) {
|
||||
// TODO - parity error
|
||||
continue;
|
||||
}
|
||||
} else if (_parity == UART_PARITY_ODD) {
|
||||
int p = ::_parity(_bits, val);
|
||||
int r = (val & (1 << _bits)) ? 1 : 0;
|
||||
if (p == r) {
|
||||
// TODO - parity error
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
auto next_writer = _writer + 1;
|
||||
if (next_writer == _fifoSize) {
|
||||
next_writer = 0;
|
||||
}
|
||||
if (next_writer != _reader) {
|
||||
_queue[_writer] = val & ((1 << _bits) - 1);
|
||||
asm volatile("" ::: "memory"); // Ensure the queue is written before the written count advances
|
||||
_writer = next_writer;
|
||||
} else {
|
||||
_overflow = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SerialPIO::SerialPIO(pin_size_t tx, pin_size_t rx, size_t fifoSize) {
|
||||
_tx = tx;
|
||||
_rx = rx;
|
||||
_fifoSize = fifoSize + 1; // Always one unused entry
|
||||
_queue = new uint8_t[_fifoSize];
|
||||
mutex_init(&_mutex);
|
||||
}
|
||||
|
||||
SerialPIO::~SerialPIO() {
|
||||
end();
|
||||
delete[] _queue;
|
||||
}
|
||||
|
||||
void SerialPIO::begin(unsigned long baud, uint16_t config) {
|
||||
_overflow = false;
|
||||
_baud = baud;
|
||||
switch (config & SERIAL_PARITY_MASK) {
|
||||
case SERIAL_PARITY_EVEN:
|
||||
_parity = UART_PARITY_EVEN;
|
||||
break;
|
||||
case SERIAL_PARITY_ODD:
|
||||
_parity = UART_PARITY_ODD;
|
||||
break;
|
||||
default:
|
||||
_parity = UART_PARITY_NONE;
|
||||
break;
|
||||
}
|
||||
switch (config & SERIAL_STOP_BIT_MASK) {
|
||||
case SERIAL_STOP_BIT_1:
|
||||
_stop = 1;
|
||||
break;
|
||||
default:
|
||||
_stop = 2;
|
||||
break;
|
||||
}
|
||||
switch (config & SERIAL_DATA_MASK) {
|
||||
case SERIAL_DATA_5:
|
||||
_bits = 5;
|
||||
break;
|
||||
case SERIAL_DATA_6:
|
||||
_bits = 6;
|
||||
break;
|
||||
case SERIAL_DATA_7:
|
||||
_bits = 7;
|
||||
break;
|
||||
default:
|
||||
_bits = 8;
|
||||
break;
|
||||
}
|
||||
|
||||
if ((_tx == NOPIN) && (_rx == NOPIN)) {
|
||||
DEBUGCORE("ERROR: No pins specified for SerialPIO\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_tx != NOPIN) {
|
||||
_txBits = _bits + _stop + (_parity != UART_PARITY_NONE ? 1 : 0) + 1/*start bit*/;
|
||||
_txPgm = _getTxProgram(_txBits);
|
||||
int off;
|
||||
if (!_txPgm->prepare(&_txPIO, &_txSM, &off)) {
|
||||
DEBUGCORE("ERROR: Unable to allocate PIO TX UART, out of PIO resources\n");
|
||||
// ERROR, no free slots
|
||||
return;
|
||||
}
|
||||
|
||||
digitalWrite(_tx, HIGH);
|
||||
pinMode(_tx, OUTPUT);
|
||||
|
||||
pio_tx_program_init(_txPIO, _txSM, off, _tx);
|
||||
pio_sm_clear_fifos(_txPIO, _txSM); // Remove any existing data
|
||||
|
||||
// Put the divider into ISR w/o using up program space
|
||||
pio_sm_put_blocking(_txPIO, _txSM, clock_get_hz(clk_sys) / _baud - 2);
|
||||
pio_sm_exec(_txPIO, _txSM, pio_encode_pull(false, false));
|
||||
pio_sm_exec(_txPIO, _txSM, pio_encode_mov(pio_isr, pio_osr));
|
||||
|
||||
// Start running!
|
||||
pio_sm_set_enabled(_txPIO, _txSM, true);
|
||||
}
|
||||
if (_rx != NOPIN) {
|
||||
_writer = 0;
|
||||
_reader = 0;
|
||||
|
||||
_rxBits = 2 * (_bits + _stop + (_parity != UART_PARITY_NONE ? 1 : 0) + 1) - 1;
|
||||
_rxPgm = _getRxProgram(_rxBits);
|
||||
int off;
|
||||
if (!_rxPgm->prepare(&_rxPIO, &_rxSM, &off)) {
|
||||
DEBUGCORE("ERROR: Unable to allocate PIO RX UART, out of PIO resources\n");
|
||||
return;
|
||||
}
|
||||
// Stash away the created RX port for the IRQ handler
|
||||
_pioSP[pio_get_index(_rxPIO)][_rxSM] = this;
|
||||
|
||||
pinMode(_rx, INPUT);
|
||||
pio_rx_program_init(_rxPIO, _rxSM, off, _rx);
|
||||
pio_sm_clear_fifos(_rxPIO, _rxSM); // Remove any existing data
|
||||
|
||||
// Put phase divider into OSR w/o using add'l program memory
|
||||
pio_sm_put_blocking(_rxPIO, _rxSM, clock_get_hz(clk_sys) / (_baud * 2) - 7 /* insns in PIO halfbit loop */);
|
||||
pio_sm_exec(_rxPIO, _rxSM, pio_encode_pull(false, false));
|
||||
|
||||
// Join the TX FIFO to the RX one now that we don't need it
|
||||
_rxPIO->sm[_rxSM].shiftctrl |= 0x80000000;
|
||||
|
||||
// Enable interrupts on rxfifo
|
||||
switch (_rxSM) {
|
||||
case 0: pio_set_irq0_source_enabled(_rxPIO, pis_sm0_rx_fifo_not_empty, true); break;
|
||||
case 1: pio_set_irq0_source_enabled(_rxPIO, pis_sm1_rx_fifo_not_empty, true); break;
|
||||
case 2: pio_set_irq0_source_enabled(_rxPIO, pis_sm2_rx_fifo_not_empty, true); break;
|
||||
case 3: pio_set_irq0_source_enabled(_rxPIO, pis_sm3_rx_fifo_not_empty, true); break;
|
||||
}
|
||||
auto irqno = pio_get_index(_rxPIO) == 0 ? PIO0_IRQ_0 : PIO1_IRQ_0;
|
||||
irq_set_exclusive_handler(irqno, _fifoIRQ);
|
||||
irq_set_enabled(irqno, true);
|
||||
|
||||
pio_sm_set_enabled(_rxPIO, _rxSM, true);
|
||||
}
|
||||
|
||||
_running = true;
|
||||
}
|
||||
|
||||
void SerialPIO::end() {
|
||||
if (!_running) {
|
||||
return;
|
||||
}
|
||||
if (_tx != NOPIN) {
|
||||
pio_sm_set_enabled(_txPIO, _txSM, false);
|
||||
pio_sm_unclaim(_txPIO, _txSM);
|
||||
}
|
||||
if (_rx != NOPIN) {
|
||||
pio_sm_set_enabled(_rxPIO, _rxSM, false);
|
||||
pio_sm_unclaim(_rxPIO, _rxSM);
|
||||
_pioSP[pio_get_index(_rxPIO)][_rxSM] = nullptr;
|
||||
// If no more active, disable the IRQ
|
||||
auto pioNum = pio_get_index(_rxPIO);
|
||||
bool used = false;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
used = used || !!_pioSP[pioNum][i];
|
||||
}
|
||||
if (!used) {
|
||||
auto irqno = pioNum == 0 ? PIO0_IRQ_0 : PIO1_IRQ_0;
|
||||
irq_set_enabled(irqno, false);
|
||||
}
|
||||
}
|
||||
_running = false;
|
||||
}
|
||||
|
||||
int SerialPIO::peek() {
|
||||
CoreMutex m(&_mutex);
|
||||
if (!_running || !m || (_rx == NOPIN)) {
|
||||
return -1;
|
||||
}
|
||||
// If there's something in the FIFO now, just peek at it
|
||||
if (_writer != _reader) {
|
||||
return _queue[_reader];
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int SerialPIO::read() {
|
||||
CoreMutex m(&_mutex);
|
||||
if (!_running || !m || (_rx == NOPIN)) {
|
||||
return -1;
|
||||
}
|
||||
if (_writer != _reader) {
|
||||
auto ret = _queue[_reader];
|
||||
asm volatile("" ::: "memory"); // Ensure the value is read before advancing
|
||||
auto next_reader = (_reader + 1) % _fifoSize;
|
||||
asm volatile("" ::: "memory"); // Ensure the reader value is only written once, correctly
|
||||
_reader = next_reader;
|
||||
return ret;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool SerialPIO::overflow() {
|
||||
CoreMutex m(&_mutex);
|
||||
if (!_running || !m || (_rx == NOPIN)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool hold = _overflow;
|
||||
_overflow = false;
|
||||
return hold;
|
||||
}
|
||||
|
||||
int SerialPIO::available() {
|
||||
CoreMutex m(&_mutex);
|
||||
if (!_running || !m || (_rx == NOPIN)) {
|
||||
return 0;
|
||||
}
|
||||
return (_writer - _reader) % _fifoSize;
|
||||
}
|
||||
|
||||
int SerialPIO::availableForWrite() {
|
||||
CoreMutex m(&_mutex);
|
||||
if (!_running || !m || (_tx == NOPIN)) {
|
||||
return 0;
|
||||
}
|
||||
return 8 - pio_sm_get_tx_fifo_level(_txPIO, _txSM);
|
||||
}
|
||||
|
||||
void SerialPIO::flush() {
|
||||
CoreMutex m(&_mutex);
|
||||
if (!_running || !m || (_tx == NOPIN)) {
|
||||
return;
|
||||
}
|
||||
while (!pio_sm_is_tx_fifo_empty(_txPIO, _txSM)) {
|
||||
delay(1); // Wait for all FIFO to be read
|
||||
}
|
||||
// Could have 1 byte being transmitted, so wait for bit times
|
||||
delay((1000 * (_txBits + 1)) / _baud);
|
||||
}
|
||||
|
||||
size_t SerialPIO::write(uint8_t c) {
|
||||
CoreMutex m(&_mutex);
|
||||
if (!_running || !m || (_tx == NOPIN)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t val = c;
|
||||
if (_parity == UART_PARITY_NONE) {
|
||||
val |= 7 << _bits; // Set 2 stop bits, the HW will only transmit the required number
|
||||
} else if (_parity == UART_PARITY_EVEN) {
|
||||
val |= ::_parity(_bits, c) << _bits;
|
||||
val |= 7 << (_bits + 1);
|
||||
} else {
|
||||
val |= (1 ^ ::_parity(_bits, c)) << _bits;
|
||||
val |= 7 << (_bits + 1);
|
||||
}
|
||||
val <<= 1; // Start bit = low
|
||||
|
||||
pio_sm_put_blocking(_txPIO, _txSM, val);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
SerialPIO::operator bool() {
|
||||
return _running;
|
||||
}
|
||||
|
||||
#ifdef ARDUINO_NANO_RP2040_CONNECT
|
||||
// NINA updates
|
||||
SerialPIO Serial3(SERIAL3_TX, SERIAL3_RX);
|
||||
#endif
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
Serial-over-PIO for the Raspberry Pi Pico RP2040
|
||||
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "api/HardwareSerial.h"
|
||||
#include <stdarg.h>
|
||||
#include <queue>
|
||||
#include <hardware/uart.h>
|
||||
#include "CoreMutex.h"
|
||||
|
||||
extern "C" typedef struct uart_inst uart_inst_t;
|
||||
|
||||
class SerialPIO : public HardwareSerial {
|
||||
public:
|
||||
static const pin_size_t NOPIN = 0xff; // Use in constructor to disable RX or TX unit
|
||||
SerialPIO(pin_size_t tx, pin_size_t rx, size_t fifoSize = 32);
|
||||
~SerialPIO();
|
||||
|
||||
void begin(unsigned long baud = 115200) override {
|
||||
begin(baud, SERIAL_8N1);
|
||||
};
|
||||
void begin(unsigned long baud, uint16_t config) override;
|
||||
void end() override;
|
||||
|
||||
virtual int peek() override;
|
||||
virtual int read() override;
|
||||
virtual int available() override;
|
||||
virtual int availableForWrite() override;
|
||||
virtual void flush() override;
|
||||
virtual size_t write(uint8_t c) override;
|
||||
bool overflow();
|
||||
using Print::write;
|
||||
operator bool() override;
|
||||
|
||||
// Not to be called by users, only from the IRQ handler. In public so that the C-language IQR callback can access it
|
||||
void _handleIRQ();
|
||||
|
||||
protected:
|
||||
bool _running = false;
|
||||
pin_size_t _tx, _rx;
|
||||
int _baud;
|
||||
int _bits;
|
||||
uart_parity_t _parity;
|
||||
int _stop;
|
||||
bool _overflow;
|
||||
mutex_t _mutex;
|
||||
|
||||
PIOProgram *_txPgm;
|
||||
PIO _txPIO;
|
||||
int _txSM;
|
||||
int _txBits;
|
||||
|
||||
PIOProgram *_rxPgm;
|
||||
PIO _rxPIO;
|
||||
int _rxSM;
|
||||
int _rxBits;
|
||||
|
||||
// Lockless, IRQ-handled circular queue
|
||||
size_t _fifoSize;
|
||||
uint32_t _writer;
|
||||
uint32_t _reader;
|
||||
uint8_t *_queue;
|
||||
};
|
||||
|
||||
#ifdef ARDUINO_NANO_RP2040_CONNECT
|
||||
// NINA updates
|
||||
extern SerialPIO Serial3;
|
||||
#endif
|
||||
+322
-66
@@ -1,22 +1,22 @@
|
||||
/*
|
||||
* Serial-over-UART for the Raspberry Pi Pico RP2040
|
||||
*
|
||||
* Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
Serial-over-UART for the Raspberry Pi Pico RP2040
|
||||
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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 "SerialUART.h"
|
||||
#include "CoreMutex.h"
|
||||
@@ -31,76 +31,223 @@
|
||||
extern void serialEvent1() __attribute__((weak));
|
||||
extern void serialEvent2() __attribute__((weak));
|
||||
|
||||
bool SerialUART::setRX(pin_size_t rx) {
|
||||
bool SerialUART::setRX(pin_size_t pin) {
|
||||
constexpr uint32_t valid[2] = { __bitset({1, 13, 17, 29}) /* UART0 */,
|
||||
__bitset({5, 9, 21, 25}) /* UART1 */};
|
||||
if (_running) {
|
||||
DEBUGCORE("ERROR: SerialUART setRX while running\n");
|
||||
return false;
|
||||
} else if ((1 << rx) & valid[uart_get_index(_uart)]) {
|
||||
_rx = rx;
|
||||
__bitset({5, 9, 21, 25}) /* UART1 */
|
||||
};
|
||||
if ((!_running) && ((1 << pin) & valid[uart_get_index(_uart)])) {
|
||||
_rx = pin;
|
||||
return true;
|
||||
} else {
|
||||
DEBUGCORE("ERROR: SerialUART setRX illegal pin (%d)\n", rx);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_running) {
|
||||
panic("FATAL: Attempting to set Serial%d.RX while running", uart_get_index(_uart) + 1);
|
||||
} else {
|
||||
panic("FATAL: Attempting to set Serial%d.RX to illegal pin %d", uart_get_index(_uart) + 1, pin);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SerialUART::setTX(pin_size_t tx) {
|
||||
bool SerialUART::setTX(pin_size_t pin) {
|
||||
constexpr uint32_t valid[2] = { __bitset({0, 12, 16, 28}) /* UART0 */,
|
||||
__bitset({4, 8, 20, 24}) /* UART1 */};
|
||||
if (_running) {
|
||||
DEBUGCORE("ERROR: SerialUART setTX while running\n");
|
||||
return false;
|
||||
} else if ((1 << tx) & valid[uart_get_index(_uart)]) {
|
||||
_tx = tx;
|
||||
__bitset({4, 8, 20, 24}) /* UART1 */
|
||||
};
|
||||
if ((!_running) && ((1 << pin) & valid[uart_get_index(_uart)])) {
|
||||
_tx = pin;
|
||||
return true;
|
||||
} else {
|
||||
DEBUGCORE("ERROR: SerialUART setTX illegal pin (%d)\n", tx);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_running) {
|
||||
panic("FATAL: Attempting to set Serial%d.TX while running", uart_get_index(_uart) + 1);
|
||||
} else {
|
||||
panic("FATAL: Attempting to set Serial%d.TX to illegal pin %d", uart_get_index(_uart) + 1, pin);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
SerialUART::SerialUART(uart_inst_t *uart, pin_size_t tx, pin_size_t rx) {
|
||||
bool SerialUART::setRTS(pin_size_t pin) {
|
||||
constexpr uint32_t valid[2] = { __bitset({3, 15, 19}) /* UART0 */,
|
||||
__bitset({7, 11, 23, 27}) /* UART1 */
|
||||
};
|
||||
if ((!_running) && ((pin == UART_PIN_NOT_DEFINED) || ((1 << pin) & valid[uart_get_index(_uart)]))) {
|
||||
_rts = pin;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_running) {
|
||||
panic("FATAL: Attempting to set Serial%d.RTS while running", uart_get_index(_uart) + 1);
|
||||
} else {
|
||||
panic("FATAL: Attempting to set Serial%d.RTS to illegal pin %d", uart_get_index(_uart) + 1, pin);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SerialUART::setCTS(pin_size_t pin) {
|
||||
constexpr uint32_t valid[2] = { __bitset({2, 14, 18}) /* UART0 */,
|
||||
__bitset({6, 10, 22, 26}) /* UART1 */
|
||||
};
|
||||
if ((!_running) && ((pin == UART_PIN_NOT_DEFINED) || ((1 << pin) & valid[uart_get_index(_uart)]))) {
|
||||
_cts = pin;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_running) {
|
||||
panic("FATAL: Attempting to set Serial%d.CTS while running", uart_get_index(_uart) + 1);
|
||||
} else {
|
||||
panic("FATAL: Attempting to set Serial%d.CTS to illegal pin %d", uart_get_index(_uart) + 1, pin);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool SerialUART::setPollingMode(bool mode) {
|
||||
if (_running) {
|
||||
return false;
|
||||
}
|
||||
_polling = mode;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SerialUART::setFIFOSize(size_t size) {
|
||||
if (!size || _running) {
|
||||
return false;
|
||||
}
|
||||
_fifoSize = size + 1; // Always 1 unused entry
|
||||
return true;
|
||||
}
|
||||
|
||||
SerialUART::SerialUART(uart_inst_t *uart, pin_size_t tx, pin_size_t rx, pin_size_t rts, pin_size_t cts) {
|
||||
_uart = uart;
|
||||
_tx = tx;
|
||||
_rx = rx;
|
||||
_rts = rts;
|
||||
_cts = cts;
|
||||
mutex_init(&_mutex);
|
||||
mutex_init(&_fifoMutex);
|
||||
}
|
||||
|
||||
static void _uart0IRQ();
|
||||
static void _uart1IRQ();
|
||||
|
||||
void SerialUART::begin(unsigned long baud, uint16_t config) {
|
||||
if (_running) {
|
||||
end();
|
||||
}
|
||||
_overflow = false;
|
||||
_queue = new uint8_t[_fifoSize];
|
||||
_baud = baud;
|
||||
uart_init(_uart, baud);
|
||||
int bits, stop;
|
||||
uart_parity_t parity;
|
||||
switch (config & SERIAL_PARITY_MASK) {
|
||||
case SERIAL_PARITY_EVEN: parity = UART_PARITY_EVEN; break;
|
||||
case SERIAL_PARITY_ODD: parity = UART_PARITY_ODD; break;
|
||||
default: parity = UART_PARITY_NONE; break;
|
||||
case SERIAL_PARITY_EVEN:
|
||||
parity = UART_PARITY_EVEN;
|
||||
break;
|
||||
case SERIAL_PARITY_ODD:
|
||||
parity = UART_PARITY_ODD;
|
||||
break;
|
||||
default:
|
||||
parity = UART_PARITY_NONE;
|
||||
break;
|
||||
}
|
||||
switch ( config & SERIAL_STOP_BIT_MASK) {
|
||||
case SERIAL_STOP_BIT_1: stop = 1; break;
|
||||
default: stop = 2; break;
|
||||
switch (config & SERIAL_STOP_BIT_MASK) {
|
||||
case SERIAL_STOP_BIT_1:
|
||||
stop = 1;
|
||||
break;
|
||||
default:
|
||||
stop = 2;
|
||||
break;
|
||||
}
|
||||
switch (config & SERIAL_DATA_MASK) {
|
||||
case SERIAL_DATA_5: bits = 5; break;
|
||||
case SERIAL_DATA_6: bits = 6; break;
|
||||
case SERIAL_DATA_7: bits = 7; break;
|
||||
default: bits = 8; break;
|
||||
case SERIAL_DATA_5:
|
||||
bits = 5;
|
||||
break;
|
||||
case SERIAL_DATA_6:
|
||||
bits = 6;
|
||||
break;
|
||||
case SERIAL_DATA_7:
|
||||
bits = 7;
|
||||
break;
|
||||
default:
|
||||
bits = 8;
|
||||
break;
|
||||
}
|
||||
uart_set_format(_uart, bits, stop, parity);
|
||||
_fcnTx = gpio_get_function(_tx);
|
||||
_fcnRx = gpio_get_function(_rx);
|
||||
gpio_set_function(_tx, GPIO_FUNC_UART);
|
||||
gpio_set_function(_rx, GPIO_FUNC_UART);
|
||||
if (_rts != UART_PIN_NOT_DEFINED) {
|
||||
_fcnRts = gpio_get_function(_rts);
|
||||
gpio_set_function(_rts, GPIO_FUNC_UART);
|
||||
}
|
||||
if (_cts != UART_PIN_NOT_DEFINED) {
|
||||
_fcnCts = gpio_get_function(_cts);
|
||||
gpio_set_function(_cts, GPIO_FUNC_UART);
|
||||
}
|
||||
uart_set_hw_flow(_uart, _rts != UART_PIN_NOT_DEFINED, _cts != UART_PIN_NOT_DEFINED);
|
||||
_writer = 0;
|
||||
_reader = 0;
|
||||
|
||||
if (!_polling) {
|
||||
if (_uart == uart0) {
|
||||
irq_set_exclusive_handler(UART0_IRQ, _uart0IRQ);
|
||||
irq_set_enabled(UART0_IRQ, true);
|
||||
} else {
|
||||
irq_set_exclusive_handler(UART1_IRQ, _uart1IRQ);
|
||||
irq_set_enabled(UART1_IRQ, true);
|
||||
}
|
||||
// Set the IRQ enables and FIFO level to minimum
|
||||
uart_set_irq_enables(_uart, true, false);
|
||||
} else {
|
||||
// Polling mode has no IRQs used
|
||||
}
|
||||
_running = true;
|
||||
_peek = -1;
|
||||
}
|
||||
|
||||
void SerialUART::end() {
|
||||
if (!_running) {
|
||||
return;
|
||||
}
|
||||
uart_deinit(_uart);
|
||||
_running = false;
|
||||
if (!_polling) {
|
||||
if (_uart == uart0) {
|
||||
irq_set_enabled(UART0_IRQ, false);
|
||||
} else {
|
||||
irq_set_enabled(UART1_IRQ, false);
|
||||
}
|
||||
}
|
||||
|
||||
// Paranoia - ensure nobody else is using anything here at the same time
|
||||
mutex_enter_blocking(&_mutex);
|
||||
mutex_enter_blocking(&_fifoMutex);
|
||||
uart_deinit(_uart);
|
||||
delete[] _queue;
|
||||
// Reset the mutexes once all is off/cleaned up
|
||||
mutex_exit(&_fifoMutex);
|
||||
mutex_exit(&_mutex);
|
||||
|
||||
// Restore pin functions
|
||||
gpio_set_function(_tx, _fcnTx);
|
||||
gpio_set_function(_rx, _fcnRx);
|
||||
if (_rts != UART_PIN_NOT_DEFINED) {
|
||||
gpio_set_function(_rts, _fcnRts);
|
||||
}
|
||||
if (_cts != UART_PIN_NOT_DEFINED) {
|
||||
gpio_set_function(_cts, _fcnCts);
|
||||
}
|
||||
}
|
||||
|
||||
void SerialUART::_pumpFIFO() {
|
||||
// Use the _fifoMutex to guard against the other core potentially
|
||||
// running the IRQ (since we can't disable their IRQ handler).
|
||||
// We guard against this core by disabling the IRQ handler and
|
||||
// re-enabling if it was previously enabled at the end.
|
||||
auto irqno = (_uart == uart0) ? UART0_IRQ : UART1_IRQ;
|
||||
bool enabled = irq_is_enabled(irqno);
|
||||
irq_set_enabled(irqno, false);
|
||||
mutex_enter_blocking(&_fifoMutex);
|
||||
_handleIRQ(false);
|
||||
mutex_exit(&_fifoMutex);
|
||||
irq_set_enabled(irqno, enabled);
|
||||
}
|
||||
|
||||
int SerialUART::peek() {
|
||||
@@ -108,11 +255,15 @@ int SerialUART::peek() {
|
||||
if (!_running || !m) {
|
||||
return -1;
|
||||
}
|
||||
if (_peek >= 0) {
|
||||
return _peek;
|
||||
if (_polling) {
|
||||
_handleIRQ(false);
|
||||
} else {
|
||||
_pumpFIFO();
|
||||
}
|
||||
_peek = uart_getc(_uart);
|
||||
return _peek;
|
||||
if (_writer != _reader) {
|
||||
return _queue[_reader];
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int SerialUART::read() {
|
||||
@@ -120,12 +271,30 @@ int SerialUART::read() {
|
||||
if (!_running || !m) {
|
||||
return -1;
|
||||
}
|
||||
if (_peek >= 0) {
|
||||
int ret = _peek;
|
||||
_peek = -1;
|
||||
if (_polling) {
|
||||
_handleIRQ(false);
|
||||
} else {
|
||||
_pumpFIFO();
|
||||
}
|
||||
if (_writer != _reader) {
|
||||
auto ret = _queue[_reader];
|
||||
asm volatile("" ::: "memory"); // Ensure the value is read before advancing
|
||||
auto next_reader = (_reader + 1) % _fifoSize;
|
||||
asm volatile("" ::: "memory"); // Ensure the reader value is only written once, correctly
|
||||
_reader = next_reader;
|
||||
return ret;
|
||||
}
|
||||
return uart_getc(_uart);
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool SerialUART::overflow() {
|
||||
CoreMutex m(&_mutex);
|
||||
if (!_running || !m) {
|
||||
return false;
|
||||
}
|
||||
bool hold = _overflow;
|
||||
_overflow = false;
|
||||
return hold;
|
||||
}
|
||||
|
||||
int SerialUART::available() {
|
||||
@@ -133,7 +302,12 @@ int SerialUART::available() {
|
||||
if (!_running || !m) {
|
||||
return 0;
|
||||
}
|
||||
return (uart_is_readable(_uart)) ? 1 : 0;
|
||||
if (_polling) {
|
||||
_handleIRQ(false);
|
||||
} else {
|
||||
_pumpFIFO();
|
||||
}
|
||||
return (_fifoSize + _writer - _reader) % _fifoSize;
|
||||
}
|
||||
|
||||
int SerialUART::availableForWrite() {
|
||||
@@ -141,6 +315,9 @@ int SerialUART::availableForWrite() {
|
||||
if (!_running || !m) {
|
||||
return 0;
|
||||
}
|
||||
if (_polling) {
|
||||
_handleIRQ(false);
|
||||
}
|
||||
return (uart_is_writable(_uart)) ? 1 : 0;
|
||||
}
|
||||
|
||||
@@ -149,7 +326,10 @@ void SerialUART::flush() {
|
||||
if (!_running || !m) {
|
||||
return;
|
||||
}
|
||||
uart_default_tx_wait_blocking();
|
||||
if (_polling) {
|
||||
_handleIRQ(false);
|
||||
}
|
||||
uart_tx_wait_blocking(_uart);
|
||||
}
|
||||
|
||||
size_t SerialUART::write(uint8_t c) {
|
||||
@@ -157,6 +337,9 @@ size_t SerialUART::write(uint8_t c) {
|
||||
if (!_running || !m) {
|
||||
return 0;
|
||||
}
|
||||
if (_polling) {
|
||||
_handleIRQ(false);
|
||||
}
|
||||
uart_putc_raw(_uart, c);
|
||||
return 1;
|
||||
}
|
||||
@@ -166,6 +349,9 @@ size_t SerialUART::write(const uint8_t *p, size_t len) {
|
||||
if (!_running || !m) {
|
||||
return 0;
|
||||
}
|
||||
if (_polling) {
|
||||
_handleIRQ(false);
|
||||
}
|
||||
size_t cnt = len;
|
||||
while (cnt) {
|
||||
uart_putc_raw(_uart, *p);
|
||||
@@ -179,17 +365,87 @@ SerialUART::operator bool() {
|
||||
return _running;
|
||||
}
|
||||
|
||||
SerialUART Serial1(uart0, 0, 1);
|
||||
SerialUART Serial2(uart1, 4, 5);
|
||||
|
||||
void arduino::serialEvent1Run(void) {
|
||||
if (serialEvent1 && Serial1.available()) {
|
||||
serialEvent1();
|
||||
serialEvent1();
|
||||
}
|
||||
}
|
||||
|
||||
void arduino::serialEvent2Run(void) {
|
||||
if (serialEvent2 && Serial2.available()) {
|
||||
serialEvent2();
|
||||
serialEvent2();
|
||||
}
|
||||
}
|
||||
|
||||
// IRQ handler, called when FIFO > 1/8 full or when it had held unread data for >32 bit times
|
||||
void __not_in_flash_func(SerialUART::_handleIRQ)(bool inIRQ) {
|
||||
if (inIRQ) {
|
||||
uint32_t owner;
|
||||
if (!mutex_try_enter(&_fifoMutex, &owner)) {
|
||||
// Main app on the other core has the mutex so it is
|
||||
// in the process of pulling data out of the HW FIFO
|
||||
return;
|
||||
}
|
||||
}
|
||||
// ICR is write-to-clear
|
||||
uart_get_hw(_uart)->icr = UART_UARTICR_RTIC_BITS | UART_UARTICR_RXIC_BITS;
|
||||
while (uart_is_readable(_uart)) {
|
||||
uint32_t raw = uart_get_hw(_uart)->dr;
|
||||
if (raw & 0x700) {
|
||||
// Framing, Parity, or Break. Ignore this bad char
|
||||
continue;
|
||||
}
|
||||
uint8_t val = raw & 0xff;
|
||||
auto next_writer = _writer + 1;
|
||||
if (next_writer == _fifoSize) {
|
||||
next_writer = 0;
|
||||
}
|
||||
if (next_writer != _reader) {
|
||||
_queue[_writer] = val;
|
||||
asm volatile("" ::: "memory"); // Ensure the queue is written before the written count advances
|
||||
// Avoid using division or mod because the HW divider could be in use
|
||||
_writer = next_writer;
|
||||
} else {
|
||||
_overflow = true;
|
||||
}
|
||||
}
|
||||
if (inIRQ) {
|
||||
mutex_exit(&_fifoMutex);
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef __SERIAL1_DEVICE
|
||||
#define __SERIAL1_DEVICE uart0
|
||||
#endif
|
||||
#ifndef __SERIAL2_DEVICE
|
||||
#define __SERIAL2_DEVICE uart1
|
||||
#endif
|
||||
|
||||
#if defined(PIN_SERIAL1_RTS)
|
||||
SerialUART Serial1(__SERIAL1_DEVICE, PIN_SERIAL1_TX, PIN_SERIAL1_RX, PIN_SERIAL1_RTS, PIN_SERIAL1_CTS);
|
||||
#else
|
||||
SerialUART Serial1(__SERIAL1_DEVICE, PIN_SERIAL1_TX, PIN_SERIAL1_RX);
|
||||
#endif
|
||||
|
||||
#if defined(PIN_SERIAL2_RTS)
|
||||
SerialUART Serial2(__SERIAL2_DEVICE, PIN_SERIAL2_TX, PIN_SERIAL2_RX, PIN_SERIAL2_RTS, PIN_SERIAL2_CTS);
|
||||
#else
|
||||
SerialUART Serial2(__SERIAL2_DEVICE, PIN_SERIAL2_TX, PIN_SERIAL2_RX);
|
||||
#endif
|
||||
|
||||
|
||||
static void __not_in_flash_func(_uart0IRQ)() {
|
||||
if (__SERIAL1_DEVICE == uart0) {
|
||||
Serial1._handleIRQ();
|
||||
} else {
|
||||
Serial2._handleIRQ();
|
||||
}
|
||||
}
|
||||
|
||||
static void __not_in_flash_func(_uart1IRQ)() {
|
||||
if (__SERIAL2_DEVICE == uart1) {
|
||||
Serial2._handleIRQ();
|
||||
} else {
|
||||
Serial1._handleIRQ();
|
||||
}
|
||||
}
|
||||
|
||||
+53
-29
@@ -1,43 +1,54 @@
|
||||
/*
|
||||
* Serial-over-UART for the Raspberry Pi Pico RP2040
|
||||
*
|
||||
* Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
Serial-over-UART for the Raspberry Pi Pico RP2040
|
||||
|
||||
#ifndef __SERIALUART_H__
|
||||
#define __SERIALUART_H__
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "api/HardwareSerial.h"
|
||||
#include <stdarg.h>
|
||||
#include <queue>
|
||||
#include "CoreMutex.h"
|
||||
|
||||
extern "C" typedef struct uart_inst uart_inst_t;
|
||||
|
||||
#define UART_PIN_NOT_DEFINED (255u)
|
||||
class SerialUART : public HardwareSerial {
|
||||
public:
|
||||
SerialUART(uart_inst_t *uart, pin_size_t tx, pin_size_t rx);
|
||||
|
||||
SerialUART(uart_inst_t *uart, pin_size_t tx, pin_size_t rx, pin_size_t rts = UART_PIN_NOT_DEFINED, pin_size_t cts = UART_PIN_NOT_DEFINED);
|
||||
|
||||
// Select the pinout. Call before .begin()
|
||||
bool setRX(pin_size_t pin);
|
||||
bool setTX(pin_size_t pin);
|
||||
bool setPinout(pin_size_t tx, pin_size_t rx) { bool ret = setRX(rx); ret &= setTX(tx); return ret; }
|
||||
bool setRTS(pin_size_t pin);
|
||||
bool setCTS(pin_size_t pin);
|
||||
bool setPinout(pin_size_t tx, pin_size_t rx) {
|
||||
bool ret = setRX(rx);
|
||||
ret &= setTX(tx);
|
||||
return ret;
|
||||
}
|
||||
bool setFIFOSize(size_t size);
|
||||
bool setPollingMode(bool mode = true);
|
||||
|
||||
void begin(unsigned long baud = 115200) override { begin(baud, SERIAL_8N1); };
|
||||
void begin(unsigned long baud = 115200) override {
|
||||
begin(baud, SERIAL_8N1);
|
||||
};
|
||||
void begin(unsigned long baud, uint16_t config) override;
|
||||
void end() override;
|
||||
|
||||
@@ -49,23 +60,36 @@ public:
|
||||
virtual size_t write(uint8_t c) override;
|
||||
virtual size_t write(const uint8_t *p, size_t len) override;
|
||||
using Print::write;
|
||||
bool overflow();
|
||||
operator bool() override;
|
||||
|
||||
// Not to be called by users, only from the IRQ handler. In public so that the C-language IQR callback can access it
|
||||
void _handleIRQ(bool inIRQ = true);
|
||||
|
||||
private:
|
||||
bool _running = false;
|
||||
uart_inst_t *_uart;
|
||||
pin_size_t _tx, _rx;
|
||||
pin_size_t _rts, _cts;
|
||||
enum gpio_function _fcnTx, _fcnRx, _fcnRts, _fcnCts;
|
||||
int _baud;
|
||||
int _peek;
|
||||
mutex_t _mutex;
|
||||
bool _polling = false;
|
||||
bool _overflow;
|
||||
|
||||
// Lockless, IRQ-handled circular queue
|
||||
uint32_t _writer;
|
||||
uint32_t _reader;
|
||||
size_t _fifoSize = 32;
|
||||
uint8_t *_queue;
|
||||
mutex_t _fifoMutex; // Only needed when non-IRQ updates _writer
|
||||
void _pumpFIFO(); // User space FIFO transfer
|
||||
};
|
||||
|
||||
extern SerialUART Serial1; // HW UART 0
|
||||
extern SerialUART Serial2; // HW UART 1
|
||||
|
||||
namespace arduino {
|
||||
extern void serialEvent1Run(void) __attribute__((weak));
|
||||
extern void serialEvent2Run(void) __attribute__((weak));
|
||||
extern void serialEvent1Run(void) __attribute__((weak));
|
||||
extern void serialEvent2Run(void) __attribute__((weak));
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+59
-171
@@ -1,24 +1,26 @@
|
||||
/*
|
||||
* Serial-Over-USB for the Raspberry Pi Pico RP2040
|
||||
* Implements an ACM which will reboot into UF2 mode on a 1200bps DTR toggle.
|
||||
* Much of this was modified from the Raspberry Pi Pico SDK stdio_usb.c file.
|
||||
*
|
||||
* Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
Serial-Over-USB for the Raspberry Pi Pico RP2040
|
||||
Implements an ACM which will reboot into UF2 mode on a 1200bps DTR toggle.
|
||||
Much of this was modified from the Raspberry Pi Pico SDK stdio_usb.c file.
|
||||
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
#if !defined(USE_TINYUSB) && !defined(NO_USB)
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "CoreMutex.h"
|
||||
@@ -32,6 +34,11 @@
|
||||
#include "hardware/watchdog.h"
|
||||
#include "pico/unique_id.h"
|
||||
|
||||
#ifndef DISABLE_USB_SERIAL
|
||||
// Ensure we are installed in the USB chain
|
||||
void __USBInstallSerial() { /* noop */ }
|
||||
#endif
|
||||
|
||||
// SerialEvent functions are weak, so when the user doesn't define them,
|
||||
// the linker just sets their address to 0 (which is checked below).
|
||||
// The Serialx_available is just a wrapper around Serialx.available(),
|
||||
@@ -39,121 +46,8 @@
|
||||
// HardwareSerial instance if the user doesn't also refer to it.
|
||||
extern void serialEvent() __attribute__((weak));
|
||||
|
||||
#define PICO_STDIO_USB_TASK_INTERVAL_US 1000
|
||||
#define PICO_STDIO_USB_LOW_PRIORITY_IRQ 31
|
||||
|
||||
#define USBD_VID (0x2E8A) // Raspberry Pi
|
||||
|
||||
#ifdef SERIALUSB_PID
|
||||
#define USBD_PID (SERIALUSB_PID)
|
||||
#else
|
||||
#define USBD_PID (0x000a) // Raspberry Pi Pico SDK CDC
|
||||
#endif
|
||||
|
||||
#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
|
||||
#define USBD_MAX_POWER_MA (250)
|
||||
|
||||
#define USBD_ITF_CDC (0) // needs 2 interfaces
|
||||
#define USBD_ITF_MAX (2)
|
||||
|
||||
#define USBD_CDC_EP_CMD (0x81)
|
||||
#define USBD_CDC_EP_OUT (0x02)
|
||||
#define USBD_CDC_EP_IN (0x82)
|
||||
#define USBD_CDC_CMD_MAX_SIZE (8)
|
||||
#define USBD_CDC_IN_OUT_MAX_SIZE (64)
|
||||
|
||||
#define USBD_STR_0 (0x00)
|
||||
#define USBD_STR_MANUF (0x01)
|
||||
#define USBD_STR_PRODUCT (0x02)
|
||||
#define USBD_STR_SERIAL (0x03)
|
||||
#define USBD_STR_CDC (0x04)
|
||||
|
||||
// Note: descriptors returned from callbacks must exist long enough for transfer to complete
|
||||
|
||||
static const tusb_desc_device_t usbd_desc_device = {
|
||||
.bLength = sizeof(tusb_desc_device_t),
|
||||
.bDescriptorType = TUSB_DESC_DEVICE,
|
||||
.bcdUSB = 0x0200,
|
||||
.bDeviceClass = TUSB_CLASS_CDC,
|
||||
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
|
||||
.bDeviceProtocol = MISC_PROTOCOL_IAD,
|
||||
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
|
||||
.idVendor = USBD_VID,
|
||||
.idProduct = USBD_PID,
|
||||
.bcdDevice = 0x0100,
|
||||
.iManufacturer = USBD_STR_MANUF,
|
||||
.iProduct = USBD_STR_PRODUCT,
|
||||
.iSerialNumber = USBD_STR_SERIAL,
|
||||
.bNumConfigurations = 1,
|
||||
};
|
||||
|
||||
static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
|
||||
TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN,
|
||||
TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA),
|
||||
|
||||
TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD,
|
||||
USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE),
|
||||
};
|
||||
|
||||
static char _idString[PICO_UNIQUE_BOARD_ID_SIZE_BYTES * 2 + 1];
|
||||
|
||||
static const char *const usbd_desc_str[] = {
|
||||
[USBD_STR_0] = "",
|
||||
[USBD_STR_MANUF] = "Raspberry Pi",
|
||||
[USBD_STR_PRODUCT] = "PicoArduino",
|
||||
[USBD_STR_SERIAL] = _idString,
|
||||
[USBD_STR_CDC] = "Board CDC",
|
||||
};
|
||||
|
||||
const uint8_t *tud_descriptor_device_cb(void) {
|
||||
return (const uint8_t *)&usbd_desc_device;
|
||||
}
|
||||
|
||||
const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
|
||||
(void)index;
|
||||
return usbd_desc_cfg;
|
||||
}
|
||||
|
||||
const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||
#define DESC_STR_MAX (20)
|
||||
static uint16_t desc_str[DESC_STR_MAX];
|
||||
|
||||
uint8_t len;
|
||||
if (index == 0) {
|
||||
desc_str[1] = 0x0409; // supported language is English
|
||||
len = 1;
|
||||
} else {
|
||||
if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0])) {
|
||||
return NULL;
|
||||
}
|
||||
const char *str = usbd_desc_str[index];
|
||||
for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) {
|
||||
desc_str[1 + len] = str[len];
|
||||
}
|
||||
}
|
||||
|
||||
// first byte is length (including header), second byte is string type
|
||||
desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * len + 2);
|
||||
|
||||
return desc_str;
|
||||
}
|
||||
|
||||
static mutex_t usb_mutex;
|
||||
|
||||
static void low_priority_worker_irq() {
|
||||
// if the mutex is already owned, then we are in user code
|
||||
// in this file which will do a tud_task itself, so we'll just do nothing
|
||||
// until the next tick; we won't starve
|
||||
if (mutex_try_enter(&usb_mutex, NULL)) {
|
||||
tud_task();
|
||||
mutex_exit(&usb_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
static int64_t timer_task(__unused alarm_id_t id, __unused void *user_data) {
|
||||
irq_set_pending(PICO_STDIO_USB_LOW_PRIORITY_IRQ);
|
||||
return PICO_STDIO_USB_TASK_INTERVAL_US;
|
||||
}
|
||||
extern mutex_t __usb_mutex;
|
||||
|
||||
void SerialUSB::begin(unsigned long baud) {
|
||||
(void) baud; //ignored
|
||||
@@ -162,24 +56,6 @@ void SerialUSB::begin(unsigned long baud) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get ID string into human readable serial number
|
||||
pico_unique_board_id_t id;
|
||||
pico_get_unique_board_id(&id);
|
||||
_idString[0] = 0;
|
||||
for (auto i = 0; i < PICO_UNIQUE_BOARD_ID_SIZE_BYTES; i++) {
|
||||
char hx[3];
|
||||
sprintf(hx, "%02X", id.id[i]);
|
||||
strcat(_idString, hx);
|
||||
}
|
||||
|
||||
tusb_init();
|
||||
|
||||
irq_set_exclusive_handler(PICO_STDIO_USB_LOW_PRIORITY_IRQ, low_priority_worker_irq);
|
||||
irq_set_enabled(PICO_STDIO_USB_LOW_PRIORITY_IRQ, true);
|
||||
|
||||
mutex_init(&usb_mutex);
|
||||
add_alarm_in_us(PICO_STDIO_USB_TASK_INTERVAL_US, timer_task, NULL, true);
|
||||
|
||||
_running = true;
|
||||
}
|
||||
|
||||
@@ -188,52 +64,57 @@ void SerialUSB::end() {
|
||||
}
|
||||
|
||||
int SerialUSB::peek() {
|
||||
CoreMutex m(&usb_mutex);
|
||||
CoreMutex m(&__usb_mutex, false);
|
||||
if (!_running || !m) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t c;
|
||||
return tud_cdc_peek(0, &c) ? (int) c : -1;
|
||||
tud_task();
|
||||
return tud_cdc_peek(&c) ? (int) c : -1;
|
||||
}
|
||||
|
||||
int SerialUSB::read() {
|
||||
CoreMutex m(&usb_mutex);
|
||||
CoreMutex m(&__usb_mutex, false);
|
||||
if (!_running || !m) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tud_cdc_connected() && tud_cdc_available()) {
|
||||
tud_task();
|
||||
if (tud_cdc_available()) {
|
||||
return tud_cdc_read_char();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int SerialUSB::available() {
|
||||
CoreMutex m(&usb_mutex);
|
||||
CoreMutex m(&__usb_mutex, false);
|
||||
if (!_running || !m) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
tud_task();
|
||||
return tud_cdc_available();
|
||||
}
|
||||
|
||||
int SerialUSB::availableForWrite() {
|
||||
CoreMutex m(&usb_mutex);
|
||||
CoreMutex m(&__usb_mutex, false);
|
||||
if (!_running || !m) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
tud_task();
|
||||
return tud_cdc_write_available();
|
||||
}
|
||||
|
||||
void SerialUSB::flush() {
|
||||
CoreMutex m(&usb_mutex);
|
||||
CoreMutex m(&__usb_mutex, false);
|
||||
if (!_running || !m) {
|
||||
return;
|
||||
}
|
||||
|
||||
tud_cdc_write_flush();
|
||||
tud_task();
|
||||
}
|
||||
|
||||
size_t SerialUSB::write(uint8_t c) {
|
||||
@@ -241,29 +122,32 @@ size_t SerialUSB::write(uint8_t c) {
|
||||
}
|
||||
|
||||
size_t SerialUSB::write(const uint8_t *buf, size_t length) {
|
||||
CoreMutex m(&usb_mutex);
|
||||
CoreMutex m(&__usb_mutex, false);
|
||||
if (!_running || !m) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint64_t last_avail_time;
|
||||
int i = 0;
|
||||
int written = 0;
|
||||
if (tud_cdc_connected()) {
|
||||
for (int i = 0; i < length;) {
|
||||
for (size_t i = 0; i < length;) {
|
||||
int n = length - i;
|
||||
int avail = tud_cdc_write_available();
|
||||
if (n > avail) n = avail;
|
||||
if (n > avail) {
|
||||
n = avail;
|
||||
}
|
||||
if (n) {
|
||||
int n2 = tud_cdc_write(buf + i, n);
|
||||
tud_task();
|
||||
tud_cdc_write_flush();
|
||||
i += n2;
|
||||
written += n2;
|
||||
last_avail_time = time_us_64();
|
||||
} else {
|
||||
tud_task();
|
||||
tud_cdc_write_flush();
|
||||
if (!tud_cdc_connected() ||
|
||||
(!tud_cdc_write_available() && time_us_64() > last_avail_time + 1000000 /* 1 second */)) {
|
||||
(!tud_cdc_write_available() && time_us_64() > last_avail_time + 1'000'000 /* 1 second */)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -272,11 +156,12 @@ size_t SerialUSB::write(const uint8_t *buf, size_t length) {
|
||||
// reset our timeout
|
||||
last_avail_time = 0;
|
||||
}
|
||||
return i;
|
||||
tud_task();
|
||||
return written;
|
||||
}
|
||||
|
||||
SerialUSB::operator bool() {
|
||||
CoreMutex m(&usb_mutex);
|
||||
CoreMutex m(&__usb_mutex, false);
|
||||
if (!_running || !m) {
|
||||
return false;
|
||||
}
|
||||
@@ -291,27 +176,30 @@ static bool _rts = false;
|
||||
static int _bps = 115200;
|
||||
static void CheckSerialReset() {
|
||||
if ((_bps == 1200) && (!_dtr)) {
|
||||
reset_usb_boot(0,0);
|
||||
while (1); // WDT will fire here
|
||||
reset_usb_boot(0, 0);
|
||||
while (1); // WDT will fire here
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
|
||||
(void) itf;
|
||||
_dtr = dtr ? true : false;
|
||||
_rts = rts ? true : false;
|
||||
CheckSerialReset();
|
||||
}
|
||||
|
||||
extern "C" void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding) {
|
||||
(void) itf;
|
||||
_bps = p_line_coding->bit_rate;
|
||||
CheckSerialReset();
|
||||
}
|
||||
|
||||
SerialUSB Serial;
|
||||
|
||||
void arduino::serialEventRun(void)
|
||||
{
|
||||
void arduino::serialEventRun(void) {
|
||||
if (serialEvent && Serial.available()) {
|
||||
serialEvent();
|
||||
serialEvent();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
+24
-24
@@ -1,25 +1,24 @@
|
||||
/*
|
||||
* Serial-over-USB for the Raspberry Pi Pico RP2040
|
||||
*
|
||||
* Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
Serial-over-USB for the Raspberry Pi Pico RP2040
|
||||
|
||||
#ifndef __SERIALUSB_H__
|
||||
#define __SERIALUSB_H__
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "api/HardwareSerial.h"
|
||||
@@ -29,7 +28,10 @@ class SerialUSB : public HardwareSerial {
|
||||
public:
|
||||
SerialUSB() { }
|
||||
void begin(unsigned long baud = 115200) override;
|
||||
void begin(unsigned long baud, uint16_t config) override { begin(baud); };
|
||||
void begin(unsigned long baud, uint16_t config) override {
|
||||
(void) config;
|
||||
begin(baud);
|
||||
};
|
||||
void end() override;
|
||||
|
||||
virtual int peek() override;
|
||||
@@ -49,7 +51,5 @@ private:
|
||||
extern SerialUSB Serial;
|
||||
|
||||
namespace arduino {
|
||||
extern void serialEventRun(void) __attribute__((weak));
|
||||
extern void serialEventRun(void) __attribute__((weak));
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
#include "api/Server.h"
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
SoftwareSerial wrapper for SerialPIO
|
||||
|
||||
Copyright (c) 2022 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "SerialPIO.h"
|
||||
|
||||
class SoftwareSerial : public SerialPIO {
|
||||
public:
|
||||
// Note the rx/tx pins are swapped in PIO vs SWSerial
|
||||
SoftwareSerial(pin_size_t rx, pin_size_t tx, bool invert = false) : SerialPIO(tx, rx) {
|
||||
_invert = invert;
|
||||
}
|
||||
|
||||
~SoftwareSerial() {
|
||||
if (_invert) {
|
||||
gpio_set_outover(_tx, 0);
|
||||
gpio_set_outover(_rx, 0);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void begin(unsigned long baud = 115200) override {
|
||||
begin(baud, SERIAL_8N1);
|
||||
};
|
||||
|
||||
void begin(unsigned long baud, uint16_t config) override {
|
||||
SerialPIO::begin(baud, config);
|
||||
if (_invert) {
|
||||
gpio_set_outover(_tx, GPIO_OVERRIDE_INVERT);
|
||||
gpio_set_inover(_rx, GPIO_OVERRIDE_INVERT);
|
||||
}
|
||||
}
|
||||
|
||||
void listen() { /* noop */ }
|
||||
|
||||
bool isListening() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
bool _invert;
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
#include "api/Stream.h"
|
||||
@@ -0,0 +1,247 @@
|
||||
/**
|
||||
StreamString.h
|
||||
|
||||
Copyright (c) 2020 D. Gauchard. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
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
|
||||
|
||||
*/
|
||||
|
||||
#ifndef __STREAMSTRING_H
|
||||
#define __STREAMSTRING_H
|
||||
|
||||
#include <limits>
|
||||
#include <algorithm>
|
||||
#include "Stream.h"
|
||||
#include "api/String.h"
|
||||
|
||||
///////////////////////////////////////////////////////////////
|
||||
// S2Stream points to a String and makes it a Stream
|
||||
// (it is also the helper for StreamString)
|
||||
|
||||
class S2Stream: public Stream {
|
||||
public:
|
||||
S2Stream(String& string, int peekPointer = -1) : string(&string), peekPointer(peekPointer) { }
|
||||
|
||||
S2Stream(String* string, int peekPointer = -1) : string(string), peekPointer(peekPointer) { }
|
||||
|
||||
virtual int available() override {
|
||||
return string->length();
|
||||
}
|
||||
|
||||
virtual int availableForWrite() override {
|
||||
return std::numeric_limits<int16_t>::max();
|
||||
}
|
||||
|
||||
virtual int read() override {
|
||||
if (peekPointer < 0) {
|
||||
// consume chars
|
||||
if (string->length()) {
|
||||
char c = string->charAt(0);
|
||||
string->remove(0, 1);
|
||||
return c;
|
||||
}
|
||||
} else if (peekPointer < (int)string->length()) {
|
||||
// return pointed and move pointer
|
||||
return string->charAt(peekPointer++);
|
||||
}
|
||||
|
||||
// everything is read
|
||||
return -1;
|
||||
}
|
||||
|
||||
virtual size_t write(uint8_t data) override {
|
||||
return string->concat((char)data);
|
||||
}
|
||||
|
||||
// virtual int read(uint8_t* buffer, size_t len) override
|
||||
// {
|
||||
// if (peekPointer < 0)
|
||||
// {
|
||||
// // string will be consumed
|
||||
// size_t l = std::min(len, (size_t)string->length());
|
||||
// memcpy(buffer, string->c_str(), l);
|
||||
// string->remove(0, l);
|
||||
// return l;
|
||||
// }
|
||||
//
|
||||
// if (peekPointer >= (int)string->length())
|
||||
// {
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
// // only the pointer is moved
|
||||
// size_t l = std::min(len, (size_t)(string->length() - peekPointer));
|
||||
// memcpy(buffer, string->c_str() + peekPointer, l);
|
||||
// peekPointer += l;
|
||||
// return l;
|
||||
// }
|
||||
|
||||
virtual size_t write(const uint8_t* buffer, size_t len) override {
|
||||
return string->concat((const char*)buffer, len) ? len : 0;
|
||||
}
|
||||
|
||||
virtual int peek() override {
|
||||
if (peekPointer < 0) {
|
||||
if (string->length()) {
|
||||
return string->charAt(0);
|
||||
}
|
||||
} else if (peekPointer < (int)string->length()) {
|
||||
return string->charAt(peekPointer);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
virtual void flush() override {
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
#if 0
|
||||
virtual bool inputCanTimeout() override {
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual bool outputCanTimeout() override {
|
||||
return false;
|
||||
}
|
||||
|
||||
//// Stream's peekBufferAPI
|
||||
|
||||
virtual bool hasPeekBufferAPI() const override {
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual size_t peekAvailable() {
|
||||
if (peekPointer < 0) {
|
||||
return string->length();
|
||||
}
|
||||
return string->length() - peekPointer;
|
||||
}
|
||||
|
||||
virtual const char* peekBuffer() override {
|
||||
if (peekPointer < 0) {
|
||||
return string->c_str();
|
||||
}
|
||||
if (peekPointer < (int)string->length()) {
|
||||
return string->c_str() + peekPointer;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
virtual void peekConsume(size_t consume) override {
|
||||
if (peekPointer < 0) {
|
||||
// string is really consumed
|
||||
string->remove(0, consume);
|
||||
} else {
|
||||
// only the pointer is moved
|
||||
peekPointer = std::min((size_t)string->length(), peekPointer + consume);
|
||||
}
|
||||
}
|
||||
|
||||
virtual ssize_t streamRemaining() override {
|
||||
return peekPointer < 0 ? string->length() : string->length() - peekPointer;
|
||||
}
|
||||
|
||||
// calling setConsume() will consume bytes as the stream is read
|
||||
// (enabled by default)
|
||||
void setConsume() {
|
||||
peekPointer = -1;
|
||||
}
|
||||
#endif
|
||||
// Reading this stream will mark the string as read without consuming
|
||||
// (not enabled by default)
|
||||
// Calling resetPointer() resets the read state and allows rereading.
|
||||
void resetPointer(int pointer = 0) {
|
||||
peekPointer = pointer;
|
||||
}
|
||||
|
||||
protected:
|
||||
String* string;
|
||||
int peekPointer; // -1:String is consumed / >=0:resettable pointer
|
||||
};
|
||||
|
||||
// StreamString is a S2Stream holding the String
|
||||
|
||||
class StreamString: public String, public S2Stream {
|
||||
protected:
|
||||
void resetpp() {
|
||||
if (peekPointer > 0) {
|
||||
peekPointer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
StreamString(StreamString&& bro) : String(bro), S2Stream(this) { }
|
||||
StreamString(const StreamString& bro) : String(bro), S2Stream(this) { }
|
||||
|
||||
// duplicate String constructors and operator=:
|
||||
|
||||
StreamString(const char* text = nullptr) : String(text), S2Stream(this) { }
|
||||
StreamString(const String& string) : String(string), S2Stream(this) { }
|
||||
StreamString(const __FlashStringHelper* str) : String(str), S2Stream(this) { }
|
||||
StreamString(String&& string) : String(string), S2Stream(this) { }
|
||||
|
||||
explicit StreamString(char c) : String(c), S2Stream(this) { }
|
||||
explicit StreamString(unsigned char c, unsigned char base = 10) :
|
||||
String(c, base), S2Stream(this) {
|
||||
}
|
||||
explicit StreamString(int i, unsigned char base = 10) : String(i, base), S2Stream(this) { }
|
||||
explicit StreamString(unsigned int i, unsigned char base = 10) : String(i, base), S2Stream(this) {
|
||||
}
|
||||
explicit StreamString(long l, unsigned char base = 10) : String(l, base), S2Stream(this) { }
|
||||
explicit StreamString(unsigned long l, unsigned char base = 10) :
|
||||
String(l, base), S2Stream(this) {
|
||||
}
|
||||
explicit StreamString(float f, unsigned char decimalPlaces = 2) :
|
||||
String(f, decimalPlaces), S2Stream(this) {
|
||||
}
|
||||
explicit StreamString(double d, unsigned char decimalPlaces = 2) :
|
||||
String(d, decimalPlaces), S2Stream(this) {
|
||||
}
|
||||
|
||||
StreamString& operator=(const StreamString& rhs) {
|
||||
String::operator=(rhs);
|
||||
resetpp();
|
||||
return *this;
|
||||
}
|
||||
|
||||
StreamString& operator=(const String& rhs) {
|
||||
String::operator=(rhs);
|
||||
resetpp();
|
||||
return *this;
|
||||
}
|
||||
|
||||
StreamString& operator=(const char* cstr) {
|
||||
String::operator=(cstr);
|
||||
resetpp();
|
||||
return *this;
|
||||
}
|
||||
|
||||
StreamString& operator=(const __FlashStringHelper* str) {
|
||||
String::operator=(str);
|
||||
resetpp();
|
||||
return *this;
|
||||
}
|
||||
|
||||
StreamString& operator=(String&& rval) {
|
||||
String::operator=(rval);
|
||||
resetpp();
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // __STREAMSTRING_H
|
||||
+90
-43
@@ -1,24 +1,25 @@
|
||||
/*
|
||||
* Tone for the Raspberry Pi Pico RP2040
|
||||
*
|
||||
* Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
Tone for the Raspberry Pi Pico RP2040
|
||||
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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 "CoreMutex.h"
|
||||
#include <hardware/gpio.h>
|
||||
#include <pico/time.h>
|
||||
#include <map>
|
||||
@@ -27,15 +28,35 @@ typedef struct {
|
||||
pin_size_t pin;
|
||||
PIO pio;
|
||||
int sm;
|
||||
int off;
|
||||
alarm_id_t alarm;
|
||||
} Tone;
|
||||
|
||||
#include "tone.pio.h"
|
||||
static PIOProgram _tonePgm(&tone_program);
|
||||
// Keep std::map safe for multicore use
|
||||
auto_init_mutex(_toneMutex);
|
||||
|
||||
#include "tone2.pio.h"
|
||||
static PIOProgram _tone2Pgm(&tone2_program);
|
||||
static std::map<pin_size_t, Tone *> _toneMap;
|
||||
|
||||
static inline bool pio_sm_get_enabled(PIO pio, uint sm) {
|
||||
check_pio_param(pio);
|
||||
check_sm_param(sm);
|
||||
return (pio->ctrl & ~(1u << sm)) & (1 << sm);
|
||||
}
|
||||
|
||||
int64_t _stopTonePIO(alarm_id_t id, void *user_data) {
|
||||
(void) id;
|
||||
Tone *tone = (Tone *)user_data;
|
||||
tone->alarm = 0;
|
||||
digitalWrite(tone->pin, LOW);
|
||||
pinMode(tone->pin, OUTPUT);
|
||||
pio_sm_set_enabled(tone->pio, tone->sm, false);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
|
||||
if ((pin < 0) || (pin > 29)) {
|
||||
if (pin > 29) {
|
||||
DEBUGCORE("ERROR: Illegal pin in tone (%d)\n", pin);
|
||||
return;
|
||||
}
|
||||
@@ -43,47 +64,73 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
|
||||
noTone(pin);
|
||||
return;
|
||||
}
|
||||
int us = 1000000 / frequency / 2;
|
||||
|
||||
// Ensure only 1 core can start or stop at a time
|
||||
CoreMutex m(&_toneMutex);
|
||||
if (!m) {
|
||||
return; // Weird deadlock case
|
||||
}
|
||||
|
||||
int us = 1'000'000 / frequency / 2;
|
||||
if (us < 5) {
|
||||
us = 5;
|
||||
}
|
||||
// Even phases run forever, odd phases end after count...so ensure its odd
|
||||
int phases = duration ? (duration * 1000 / us) | 1 : 2;
|
||||
auto entry = _toneMap.find(pin);
|
||||
if (entry != _toneMap.end()) {
|
||||
noTone(pin);
|
||||
Tone *newTone;
|
||||
if (entry == _toneMap.end()) {
|
||||
newTone = new Tone();
|
||||
newTone->pin = pin;
|
||||
pinMode(pin, OUTPUT);
|
||||
if (!_tone2Pgm.prepare(&newTone->pio, &newTone->sm, &newTone->off)) {
|
||||
DEBUGCORE("ERROR: tone unable to start, out of PIO resources\n");
|
||||
// ERROR, no free slots
|
||||
delete newTone;
|
||||
return;
|
||||
}
|
||||
newTone->alarm = 0;
|
||||
} else {
|
||||
newTone = entry->second;
|
||||
if (newTone->alarm) {
|
||||
cancel_alarm(newTone->alarm);
|
||||
newTone->alarm = 0;
|
||||
}
|
||||
}
|
||||
|
||||
auto newTone = new Tone();
|
||||
newTone->pin = pin;
|
||||
pinMode(pin, OUTPUT);
|
||||
int off;
|
||||
if (!_tonePgm.prepare(&newTone->pio, &newTone->sm, &off)) {
|
||||
DEBUGCORE("ERROR: tone unable to start, out of PIO resources\n");
|
||||
// ERROR, no free slots
|
||||
delete newTone;
|
||||
return;
|
||||
if (!pio_sm_get_enabled(newTone->pio, newTone->sm)) {
|
||||
tone2_program_init(newTone->pio, newTone->sm, newTone->off, pin);
|
||||
}
|
||||
tone_program_init(newTone->pio, newTone->sm, off, pin);
|
||||
pio_sm_set_enabled(newTone->pio, newTone->sm, false);
|
||||
pio_sm_clear_fifos(newTone->pio, newTone->sm); // Remove any old updates that haven't yet taken effect
|
||||
pio_sm_put_blocking(newTone->pio, newTone->sm, RP2040::usToPIOCycles(us));
|
||||
pio_sm_exec(newTone->pio, newTone->sm, pio_encode_pull(false, false));
|
||||
pio_sm_exec(newTone->pio, newTone->sm, pio_encode_out(pio_isr, 32));
|
||||
pio_sm_set_enabled(newTone->pio, newTone->sm, true);
|
||||
pio_sm_put_blocking(newTone->pio, newTone->sm, phases);
|
||||
|
||||
_toneMap.insert({pin, newTone});
|
||||
|
||||
if (duration) {
|
||||
auto ret = add_alarm_in_ms(duration, _stopTonePIO, (void *)newTone, true);
|
||||
if (ret > 0) {
|
||||
newTone->alarm = ret;
|
||||
} else {
|
||||
DEBUGCORE("ERROR: Unable to allocate timer for tone(%d, %d, %d)\n",
|
||||
pin, frequency, duration);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void noTone(uint8_t pin) {
|
||||
if ((pin < 0) || (pin > 29)) {
|
||||
CoreMutex m(&_toneMutex);
|
||||
|
||||
if ((pin > 29) || !m) {
|
||||
DEBUGCORE("ERROR: Illegal pin in tone (%d)\n", pin);
|
||||
return;
|
||||
}
|
||||
auto entry = _toneMap.find(pin);
|
||||
if (entry != _toneMap.end()) {
|
||||
if (entry->second->alarm) {
|
||||
cancel_alarm(entry->second->alarm);
|
||||
entry->second->alarm = 0;
|
||||
}
|
||||
pio_sm_set_enabled(entry->second->pio, entry->second->sm, false);
|
||||
pio_sm_unclaim(entry->second->pio, entry->second->sm);
|
||||
pio_sm_unclaim(entry->second->pio, entry->second->sm);
|
||||
delete entry->second;
|
||||
_toneMap.erase(entry);
|
||||
pinMode(pin, OUTPUT);
|
||||
digitalWrite(pin, LOW);
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
#include "api/Udp.h"
|
||||
+27
-27
@@ -1,44 +1,44 @@
|
||||
/*
|
||||
Copyright (c) 2014 Arduino. All right reserved.
|
||||
Copyright (c) 2014 Arduino. All right reserved.
|
||||
|
||||
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 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.
|
||||
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
|
||||
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 "stdlib.h"
|
||||
#include "stdint.h"
|
||||
|
||||
void randomSeed( uint32_t dwSeed ) {
|
||||
if ( dwSeed != 0 ) {
|
||||
srand( dwSeed ) ;
|
||||
}
|
||||
void randomSeed(uint32_t dwSeed) {
|
||||
if (dwSeed != 0) {
|
||||
srand(dwSeed) ;
|
||||
}
|
||||
}
|
||||
|
||||
long random( long howbig ) {
|
||||
if ( howbig == 0 ) {
|
||||
return 0 ;
|
||||
}
|
||||
long random(long howbig) {
|
||||
if (howbig == 0) {
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
return rand() % howbig;
|
||||
return rand() % howbig;
|
||||
}
|
||||
|
||||
long random( long howsmall, long howbig ) {
|
||||
if (howsmall >= howbig) {
|
||||
return howsmall;
|
||||
}
|
||||
long random(long howsmall, long howbig) {
|
||||
if (howsmall >= howbig) {
|
||||
return howsmall;
|
||||
}
|
||||
|
||||
long diff = howbig - howsmall;
|
||||
long diff = howbig - howsmall;
|
||||
|
||||
return random(diff) + howsmall;
|
||||
return random(diff) + howsmall;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
_freertos.cpp - Internal core definitions for FreeRTOS
|
||||
|
||||
Copyright (c) 2022 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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 "_freertos.h"
|
||||
#include "pico/mutex.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
mutex_t *src;
|
||||
SemaphoreHandle_t dst;
|
||||
} FMMap;
|
||||
|
||||
static FMMap *_map = nullptr;
|
||||
extern "C" SemaphoreHandle_t __get_freertos_mutex_for_ptr(mutex_t *m) {
|
||||
if (!_map) {
|
||||
_map = (FMMap *)calloc(sizeof(FMMap), 16);
|
||||
}
|
||||
// Pre-existing map
|
||||
for (int i = 0; i < 16; i++) {
|
||||
if (m == _map[i].src) {
|
||||
return _map[i].dst;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 16; i++) {
|
||||
if (_map[i].src == nullptr) {
|
||||
// Make a new mutex
|
||||
SemaphoreHandle_t fm = __freertos_mutex_create();
|
||||
if (fm == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
_map[i].src = m;
|
||||
_map[i].dst = fm;
|
||||
return fm;
|
||||
}
|
||||
}
|
||||
return nullptr; // Need to make space for more mutex maps!
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
_freertos.h - Internal core definitions for FreeRTOS
|
||||
|
||||
Copyright (c) 2022 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "pico/mutex.h"
|
||||
|
||||
// Cannot include refs to FreeRTOS's actual semaphore calls because they
|
||||
// are implemented as macros, so we have a wrapper in our variant hook
|
||||
// to handle it.
|
||||
|
||||
extern bool __isFreeRTOS;
|
||||
|
||||
// FreeRTOS has been set up
|
||||
extern volatile bool __freeRTOSinitted;
|
||||
|
||||
extern "C" {
|
||||
#ifndef INC_FREERTOS_H
|
||||
struct QueueDefinition; /* Using old naming convention so as not to break kernel aware debuggers. */
|
||||
typedef struct QueueDefinition * QueueHandle_t;
|
||||
typedef QueueHandle_t SemaphoreHandle_t;
|
||||
#endif
|
||||
|
||||
extern SemaphoreHandle_t __freertos_mutex_create() __attribute__((weak));
|
||||
extern SemaphoreHandle_t _freertos_recursive_mutex_create() __attribute__((weak));
|
||||
|
||||
extern void __freertos_mutex_take(SemaphoreHandle_t mtx) __attribute__((weak));
|
||||
extern void __freertos_mutex_take_from_isr(SemaphoreHandle_t mtx) __attribute__((weak));
|
||||
extern int __freertos_mutex_try_take(SemaphoreHandle_t mtx) __attribute__((weak));
|
||||
extern void __freertos_mutex_give(SemaphoreHandle_t mtx) __attribute__((weak));
|
||||
extern void __freertos_mutex_give_from_isr(SemaphoreHandle_t mtx) __attribute__((weak));
|
||||
|
||||
extern void __freertos_recursive_mutex_take(SemaphoreHandle_t mtx) __attribute__((weak));
|
||||
extern int __freertos_recursive_mutex_try_take(SemaphoreHandle_t mtx) __attribute__((weak));
|
||||
extern void __freertos_recursive_mutex_give(SemaphoreHandle_t mtx) __attribute__((weak));
|
||||
|
||||
#ifndef INC_FREERTOS_H
|
||||
extern void vTaskSuspendAll() __attribute__((weak));
|
||||
extern int32_t xTaskResumeAll() __attribute__((weak));
|
||||
|
||||
typedef struct tskTaskControlBlock * TaskHandle_t;
|
||||
extern void vTaskPreemptionDisable(TaskHandle_t p) __attribute__((weak));
|
||||
extern void vTaskPreemptionEnable(TaskHandle_t p) __attribute__((weak));
|
||||
#endif
|
||||
|
||||
extern SemaphoreHandle_t __get_freertos_mutex_for_ptr(mutex_t *m);
|
||||
}
|
||||
|
||||
// Halt the FreeRTOS PendSV task switching magic
|
||||
extern "C" int __holdUpPendSV;
|
||||
@@ -1 +0,0 @@
|
||||
../../ArduinoCore-API/api/
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/ArduinoAPI.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/Binary.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/Client.h"
|
||||
@@ -0,0 +1 @@
|
||||
#include "../../../ArduinoCore-API/api/Common.cpp"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/Common.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/Compat.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/HardwareI2C.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/HardwareSPI.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/HardwareSerial.h"
|
||||
@@ -0,0 +1 @@
|
||||
#include "../../../ArduinoCore-API/api/IPAddress.cpp"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/IPAddress.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/Interrupts.h"
|
||||
@@ -0,0 +1 @@
|
||||
#include "../../../ArduinoCore-API/api/PluggableUSB.cpp"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/PluggableUSB.h"
|
||||
@@ -0,0 +1 @@
|
||||
#include "../../../ArduinoCore-API/api/Print.cpp"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/Print.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/Printable.h"
|
||||
@@ -0,0 +1 @@
|
||||
#include "../../../ArduinoCore-API/api/RingBuffer.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/Server.h"
|
||||
@@ -0,0 +1 @@
|
||||
#include "../../../ArduinoCore-API/api/Stream.cpp"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/Stream.h"
|
||||
@@ -0,0 +1 @@
|
||||
#include "../../../ArduinoCore-API/api/String.cpp"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/String.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/USBAPI.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/Udp.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/WCharacter.h"
|
||||
@@ -0,0 +1 @@
|
||||
#include "../../../../../ArduinoCore-API/api/deprecated-avr-comp/avr/dtostrf.c.impl"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../../../ArduinoCore-API/api/deprecated-avr-comp/avr/dtostrf.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../../../ArduinoCore-API/api/deprecated-avr-comp/avr/interrupt.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../../../ArduinoCore-API/api/deprecated-avr-comp/avr/pgmspace.h"
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
Copyright (c) 2016 Arduino LLC. All right reserved.
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
// including Client.h is deprecated, for all future projects use Arduino.h instead
|
||||
|
||||
// This include is added for compatibility, it will be remove on the next
|
||||
// major release of the API
|
||||
#include "../Client.h"
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
Copyright (c) 2016 Arduino LLC. All right reserved.
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
// including HardwareSerial.h is deprecated, for all future projects use Arduino.h instead
|
||||
|
||||
// This include is added for compatibility, it will be remove on the next
|
||||
// major release of the API
|
||||
#include "../HardwareSerial.h"
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
Copyright (c) 2016 Arduino LLC. All right reserved.
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
// including IPAddress.h is deprecated, for all future projects use Arduino.h instead
|
||||
|
||||
// This include is added for compatibility, it will be remove on the next
|
||||
// major release of the API
|
||||
#include "../IPAddress.h"
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
Copyright (c) 2016 Arduino LLC. All right reserved.
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
// including Print.h is deprecated, for all future projects use Arduino.h instead
|
||||
|
||||
// This include is added for compatibility, it will be remove on the next
|
||||
// major release of the API
|
||||
#include "../Print.h"
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
Copyright (c) 2016 Arduino LLC. All right reserved.
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
// including Printable.h is deprecated, for all future projects use Arduino.h instead
|
||||
|
||||
// This include is added for compatibility, it will be remove on the next
|
||||
// major release of the API
|
||||
#include "../Printable.h"
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
Copyright (c) 2016 Arduino LLC. All right reserved.
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
// including Server.h is deprecated, for all future projects use Arduino.h instead
|
||||
|
||||
// This include is added for compatibility, it will be remove on the next
|
||||
// major release of the API
|
||||
#include "../Server.h"
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
Copyright (c) 2016 Arduino LLC. All right reserved.
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
// including Stream.h is deprecated, for all future projects use Arduino.h instead
|
||||
|
||||
// This include is added for compatibility, it will be remove on the next
|
||||
// major release of the API
|
||||
#include "../Stream.h"
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
Copyright (c) 2016 Arduino LLC. All right reserved.
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
// including Udp.h is deprecated, for all future projects use Arduino.h instead
|
||||
|
||||
// This include is added for compatibility, it will be remove on the next
|
||||
// major release of the API
|
||||
#include "../Udp.h"
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
Copyright 2016, Arduino LLC. All Right Reserved.
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
// including WString.h is deprecated, for all future projects use Arduino.h instead
|
||||
|
||||
// This include is added for compatibility, it will be remove on the next
|
||||
// major release of the API
|
||||
#include "../String.h"
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "../../../ArduinoCore-API/api/itoa.h"
|
||||
@@ -0,0 +1,62 @@
|
||||
; Cycle Count for the Raspberry Pi Pico RP2040
|
||||
;
|
||||
; Copyright (c) 2022 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
;
|
||||
; 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
|
||||
|
||||
; Cycle count is stored in {-y, -x}
|
||||
|
||||
.program ccount
|
||||
cnt:
|
||||
jmp x-- cnt
|
||||
epoch:
|
||||
jmp y-- cnt
|
||||
|
||||
% c-sdk {
|
||||
static inline void ccount_program_init(PIO pio, uint sm, uint offset) {
|
||||
pio_sm_config c = ccount_program_get_default_config(offset);
|
||||
pio_sm_init(pio, sm, offset, &c);
|
||||
pio_sm_exec(pio, sm, pio_encode_set(pio_x, 0));
|
||||
pio_sm_exec(pio, sm, pio_encode_set(pio_y, 0));
|
||||
}
|
||||
|
||||
static inline uint64_t ccount_read(PIO pio, uint sm) {
|
||||
static uint64_t extra = 0;
|
||||
// Guard against having the epoch rollover while we're reading the time.
|
||||
// If epoch2 != epoch1, we looped in middle and get new LSW
|
||||
pio_sm_exec(pio, sm, pio_encode_mov(pio_isr, pio_y));
|
||||
pio_sm_exec(pio, sm, pio_encode_push(false, false));
|
||||
pio_sm_exec(pio, sm, pio_encode_mov(pio_isr, pio_x));
|
||||
pio_sm_exec(pio, sm, pio_encode_push(false, false));
|
||||
pio_sm_exec(pio, sm, pio_encode_mov(pio_isr, pio_y));
|
||||
pio_sm_exec(pio, sm, pio_encode_push(false, false));
|
||||
pio_sm_exec(pio, sm, pio_encode_mov(pio_isr, pio_x));
|
||||
pio_sm_exec(pio, sm, pio_encode_push(false, false));
|
||||
extra += 8;
|
||||
uint32_t y1 = -((int)pio_sm_get_blocking(pio, sm));
|
||||
uint32_t x1 = -((int)pio_sm_get_blocking(pio, sm));
|
||||
uint32_t y2 = -((int)pio_sm_get_blocking(pio, sm));
|
||||
uint32_t x2 = -((int)pio_sm_get_blocking(pio, sm));
|
||||
uint64_t val;
|
||||
if (y2 != y1) {
|
||||
val = ((((uint64_t)y2) << 32LL) | x2) + y2 /* adjust for missed cycle every epoch increment */;
|
||||
} else {
|
||||
val = ((((uint64_t)y1) << 32LL) | x1) + y1 /* adjust for missed cycle every epoch increment */;
|
||||
}
|
||||
return val + extra;
|
||||
}
|
||||
|
||||
%}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
// -------------------------------------------------- //
|
||||
// This file is autogenerated by pioasm; do not edit! //
|
||||
// -------------------------------------------------- //
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !PICO_NO_HARDWARE
|
||||
#include "hardware/pio.h"
|
||||
#endif
|
||||
|
||||
// ------ //
|
||||
// ccount //
|
||||
// ------ //
|
||||
|
||||
#define ccount_wrap_target 0
|
||||
#define ccount_wrap 1
|
||||
|
||||
static const uint16_t ccount_program_instructions[] = {
|
||||
// .wrap_target
|
||||
0x0040, // 0: jmp x--, 0
|
||||
0x0080, // 1: jmp y--, 0
|
||||
// .wrap
|
||||
};
|
||||
|
||||
#if !PICO_NO_HARDWARE
|
||||
static const struct pio_program ccount_program = {
|
||||
.instructions = ccount_program_instructions,
|
||||
.length = 2,
|
||||
.origin = -1,
|
||||
};
|
||||
|
||||
static inline pio_sm_config ccount_program_get_default_config(uint offset) {
|
||||
pio_sm_config c = pio_get_default_sm_config();
|
||||
sm_config_set_wrap(&c, offset + ccount_wrap_target, offset + ccount_wrap);
|
||||
return c;
|
||||
}
|
||||
|
||||
static inline void ccount_program_init(PIO pio, uint sm, uint offset) {
|
||||
pio_sm_config c = ccount_program_get_default_config(offset);
|
||||
pio_sm_init(pio, sm, offset, &c);
|
||||
pio_sm_exec(pio, sm, pio_encode_set(pio_x, 0));
|
||||
pio_sm_exec(pio, sm, pio_encode_set(pio_y, 0));
|
||||
}
|
||||
static inline uint64_t ccount_read(PIO pio, uint sm) {
|
||||
static uint64_t extra = 0;
|
||||
// Guard against having the epoch rollover while we're reading the time.
|
||||
// If epoch2 != epoch1, we looped in middle and get new LSW
|
||||
pio_sm_exec(pio, sm, pio_encode_mov(pio_isr, pio_y));
|
||||
pio_sm_exec(pio, sm, pio_encode_push(false, false));
|
||||
pio_sm_exec(pio, sm, pio_encode_mov(pio_isr, pio_x));
|
||||
pio_sm_exec(pio, sm, pio_encode_push(false, false));
|
||||
pio_sm_exec(pio, sm, pio_encode_mov(pio_isr, pio_y));
|
||||
pio_sm_exec(pio, sm, pio_encode_push(false, false));
|
||||
pio_sm_exec(pio, sm, pio_encode_mov(pio_isr, pio_x));
|
||||
pio_sm_exec(pio, sm, pio_encode_push(false, false));
|
||||
extra += 8;
|
||||
uint32_t y1 = -((int)pio_sm_get_blocking(pio, sm));
|
||||
uint32_t x1 = -((int)pio_sm_get_blocking(pio, sm));
|
||||
uint32_t y2 = -((int)pio_sm_get_blocking(pio, sm));
|
||||
uint32_t x2 = -((int)pio_sm_get_blocking(pio, sm));
|
||||
uint64_t val;
|
||||
if (y2 != y1) {
|
||||
val = ((((uint64_t)y2) << 32LL) | x2) + y2 /* adjust for missed cycle every epoch increment */;
|
||||
} else {
|
||||
val = ((((uint64_t)y1) << 32LL) | x1) + y1 /* adjust for missed cycle every epoch increment */;
|
||||
}
|
||||
return val + extra;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,46 +1,46 @@
|
||||
/*
|
||||
* Core debug macros header for the Raspberry Pi Pico RP2040
|
||||
*
|
||||
* Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
Core debug macros header for the Raspberry Pi Pico RP2040
|
||||
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
#if !defined(DEBUG_RP2040_PORT)
|
||||
#define DEBUGV(...) do { } while(0)
|
||||
#define DEBUGCORE(...) do { } while(0)
|
||||
#define DEBUGWIRE(...) do { } while(0)
|
||||
#define DEBUGSPI(...) do { } while(0)
|
||||
#define DEBUGV(...) do { } while(0)
|
||||
#define DEBUGCORE(...) do { } while(0)
|
||||
#define DEBUGWIRE(...) do { } while(0)
|
||||
#define DEBUGSPI(...) do { } while(0)
|
||||
#else
|
||||
#define DEBUGV(fmt, ...) do { DEBUG_RP2040_PORT.printf(fmt, ## __VA_ARGS__); DEBUG_RP2040_PORT.flush(); } while (0)
|
||||
#define DEBUGV(fmt, ...) do { DEBUG_RP2040_PORT.printf(fmt, ## __VA_ARGS__); DEBUG_RP2040_PORT.flush(); } while (0)
|
||||
|
||||
#if defined(DEBUG_RP2040_CORE)
|
||||
#define DEBUGCORE(fmt, ...) do { DEBUG_RP2040_PORT.printf(fmt, ## __VA_ARGS__); DEBUG_RP2040_PORT.flush(); } while (0)
|
||||
#else
|
||||
#define DEBUGCORE(...) do { } while(0)
|
||||
#endif
|
||||
#if defined(DEBUG_RP2040_CORE)
|
||||
#define DEBUGCORE(fmt, ...) do { DEBUG_RP2040_PORT.printf(fmt, ## __VA_ARGS__); DEBUG_RP2040_PORT.flush(); } while (0)
|
||||
#else
|
||||
#define DEBUGCORE(...) do { } while(0)
|
||||
#endif
|
||||
|
||||
#if defined(DEBUG_RP2040_WIRE)
|
||||
#define DEBUGWIRE(fmt, ...) do { DEBUG_RP2040_PORT.printf(fmt, ## __VA_ARGS__); DEBUG_RP2040_PORT.flush(); } while (0)
|
||||
#else
|
||||
#define DEBUGWIRE(...) do { } while(0)
|
||||
#endif
|
||||
#if defined(DEBUG_RP2040_WIRE)
|
||||
#define DEBUGWIRE(fmt, ...) do { DEBUG_RP2040_PORT.printf(fmt, ## __VA_ARGS__); DEBUG_RP2040_PORT.flush(); } while (0)
|
||||
#else
|
||||
#define DEBUGWIRE(...) do { } while(0)
|
||||
#endif
|
||||
|
||||
#if defined(DEBUG_RP2040_SPI)
|
||||
#define DEBUGSPI(fmt, ...) do { DEBUG_RP2040_PORT.printf(fmt, ## __VA_ARGS__); DEBUG_RP2040_PORT.flush(); } while (0)
|
||||
#else
|
||||
#define DEBUGSPI(...) do { } while(0)
|
||||
#endif
|
||||
#if defined(DEBUG_RP2040_SPI)
|
||||
#define DEBUGSPI(fmt, ...) do { DEBUG_RP2040_PORT.printf(fmt, ## __VA_ARGS__); DEBUG_RP2040_PORT.flush(); } while (0)
|
||||
#else
|
||||
#define DEBUGSPI(...) do { } while(0)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
+53
-38
@@ -1,50 +1,65 @@
|
||||
/*
|
||||
* delay() for the Raspberry Pi Pico RP2040
|
||||
*
|
||||
* Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
delay() for the Raspberry Pi Pico RP2040
|
||||
|
||||
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
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 <pico.h>
|
||||
#include <pico/time.h>
|
||||
|
||||
extern "C" void delay( unsigned long ms ) {
|
||||
if (!ms) {
|
||||
return;
|
||||
#ifdef USE_TINYUSB
|
||||
#include "Adafruit_TinyUSB_API.h"
|
||||
#endif
|
||||
|
||||
extern "C" void delay(unsigned long ms) __attribute__((weak));
|
||||
extern "C" void yield() __attribute__((weak));
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
void delay(unsigned long ms) {
|
||||
if (!ms) {
|
||||
return;
|
||||
}
|
||||
|
||||
sleep_ms(ms);
|
||||
}
|
||||
|
||||
sleep_ms(ms);
|
||||
}
|
||||
|
||||
extern "C" void delayMicroseconds( unsigned int usec ) {
|
||||
if (!usec) {
|
||||
return;
|
||||
void delayMicroseconds(unsigned int usec) {
|
||||
if (!usec) {
|
||||
return;
|
||||
}
|
||||
sleep_us(usec);
|
||||
}
|
||||
sleep_us(usec);
|
||||
}
|
||||
|
||||
extern "C" void yield() {
|
||||
// NOOP
|
||||
}
|
||||
void yield() {
|
||||
#ifdef USE_TINYUSB
|
||||
TinyUSB_Device_Task();
|
||||
TinyUSB_Device_FlushCDC();
|
||||
#endif
|
||||
}
|
||||
|
||||
extern "C" uint32_t millis() {
|
||||
return to_ms_since_boot(get_absolute_time());
|
||||
}
|
||||
|
||||
extern "C" uint32_t micros() {
|
||||
return to_us_since_boot(get_absolute_time());
|
||||
}
|
||||
uint32_t millis() {
|
||||
return to_ms_since_boot(get_absolute_time());
|
||||
}
|
||||
|
||||
uint32_t micros() {
|
||||
return to_us_since_boot(get_absolute_time());
|
||||
}
|
||||
|
||||
} // extern C
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user