From fff056ced75286753dc3cb23e20edf7f6134bb54 Mon Sep 17 00:00:00 2001 From: jgromes Date: Wed, 28 May 2025 20:24:12 +0200 Subject: [PATCH 1/5] [HAL] Make RPi Pico tone helpers static --- src/hal/RPiPico/PicoHal.cpp | 45 +++++++++++++++++++++++++++++++++++++ src/hal/RPiPico/PicoHal.h | 45 +------------------------------------ 2 files changed, 46 insertions(+), 44 deletions(-) create mode 100644 src/hal/RPiPico/PicoHal.cpp diff --git a/src/hal/RPiPico/PicoHal.cpp b/src/hal/RPiPico/PicoHal.cpp new file mode 100644 index 00000000..754eed71 --- /dev/null +++ b/src/hal/RPiPico/PicoHal.cpp @@ -0,0 +1,45 @@ +#include "PicoHal.h" + +// pre-calculated pulse-widths for 1200 and 2200Hz +// we do this to save calculation time (see https://github.com/khoih-prog/RP2040_PWM/issues/6) +#define SLEEP_1200 416.666 +#define SLEEP_2200 227.272 + +static uint32_t toneLoopPin; +static unsigned int toneLoopFrequency; +static unsigned long toneLoopDuration; + +// === NOTE === +// The tone(...) implementation uses the second core on the RPi Pico. This is to diminish as much +// jitter in the output tones as possible. + +static void toneLoop() { + gpio_set_dir(toneLoopPin, GPIO_OUT); + + uint32_t sleep_dur; + if(toneLoopFrequency == 1200) { + sleep_dur = SLEEP_1200; + } else if(toneLoopFrequency == 2200) { + sleep_dur = SLEEP_2200; + } else { + sleep_dur = 500000 / toneLoopFrequency; + } + + // tone bitbang + while(1) { + gpio_put(toneLoopPin, 1); + sleep_us(sleep_dur); + gpio_put(toneLoopPin, 0); + sleep_us(sleep_dur); + tight_loop_contents(); + } +} + +void PicoHal::tone(uint32_t pin, unsigned int frequency, unsigned long duration) { + // tones on the Pico are generated using bitbanging. This process is offloaded to the Pico's second core + multicore_reset_core1(); + toneLoopPin = pin; + toneLoopFrequency = frequency; + toneLoopDuration = duration; + multicore_launch_core1(toneLoop); +} diff --git a/src/hal/RPiPico/PicoHal.h b/src/hal/RPiPico/PicoHal.h index ec4d12e3..f5ba3ef9 100644 --- a/src/hal/RPiPico/PicoHal.h +++ b/src/hal/RPiPico/PicoHal.h @@ -12,42 +12,6 @@ #include "hardware/clocks.h" #include "pico/multicore.h" -uint32_t toneLoopPin; -unsigned int toneLoopFrequency; -unsigned long toneLoopDuration; - -// pre-calculated pulse-widths for 1200 and 2200Hz -// we do this to save calculation time (see https://github.com/khoih-prog/RP2040_PWM/issues/6) -#define SLEEP_1200 416.666 -#define SLEEP_2200 227.272 - -// === NOTE === -// The tone(...) implementation uses the second core on the RPi Pico. This is to diminish as much -// jitter in the output tones as possible. - -void toneLoop(){ - gpio_set_dir(toneLoopPin, GPIO_OUT); - - uint32_t sleep_dur; - if (toneLoopFrequency == 1200) { - sleep_dur = SLEEP_1200; - } else if (toneLoopFrequency == 2200) { - sleep_dur = SLEEP_2200; - } else { - sleep_dur = 500000 / toneLoopFrequency; - } - - - // tone bitbang - while(1){ - gpio_put(toneLoopPin, 1); - sleep_us(sleep_dur); - gpio_put(toneLoopPin, 0); - sleep_us(sleep_dur); - tight_loop_contents(); - } -} - // create a new Raspberry Pi Pico hardware abstraction // layer using the Pico SDK // the HAL must inherit from the base RadioLibHal class @@ -148,14 +112,7 @@ public: return (this->micros() - start); } - void tone(uint32_t pin, unsigned int frequency, unsigned long duration = 0) override { - // tones on the Pico are generated using bitbanging. This process is offloaded to the Pico's second core - multicore_reset_core1(); - toneLoopPin = pin; - toneLoopFrequency = frequency; - toneLoopDuration = duration; - multicore_launch_core1(toneLoop); - } + void tone(uint32_t pin, unsigned int frequency, unsigned long duration = 0) override; void noTone(uint32_t pin) override { multicore_reset_core1(); From 3b1747a9df7661511ea0cd7af9d8c34dec5f1420 Mon Sep 17 00:00:00 2001 From: jgromes Date: Thu, 29 May 2025 16:20:47 +0200 Subject: [PATCH 2/5] [HAL] Exclude HAL source files from default build --- CMakeLists.txt | 4 ++++ examples/NonArduino/Pico/CMakeLists.txt | 1 + 2 files changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index eabfb5d8..0d8a55d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,10 @@ file(GLOB_RECURSE RADIOLIB_SOURCES "src/*.cpp" ) +# exclude all HAL source files, except the default Arduino HAL +list(FILTER RADIOLIB_SOURCES EXCLUDE REGEX "src/hal/.*\\.cpp") +list(APPEND RADIOLIB_SOURCES "src/hal/Arduino/ArduinoHal.cpp") + add_library(RadioLib ${RADIOLIB_SOURCES}) target_include_directories(RadioLib diff --git a/examples/NonArduino/Pico/CMakeLists.txt b/examples/NonArduino/Pico/CMakeLists.txt index c86122fb..36e9c4e4 100644 --- a/examples/NonArduino/Pico/CMakeLists.txt +++ b/examples/NonArduino/Pico/CMakeLists.txt @@ -20,6 +20,7 @@ add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../../../../RadioLib" "${CMAKE_CUR add_executable(${PROJECT_NAME} main.cpp + "${CMAKE_CURRENT_SOURCE_DIR}/../../../../RadioLib/src/hal/RPiPico/PicoHal.cpp" ) # Pull in common dependencies From 776ab477c4b08f4721788d0ac473d748d0711669 Mon Sep 17 00:00:00 2001 From: jgromes Date: Thu, 29 May 2025 16:29:50 +0200 Subject: [PATCH 3/5] Fix source files globbing for ESP-IDF --- CMakeLists.txt | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d8a55d1..1d34a0ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,18 @@ cmake_minimum_required(VERSION 3.13) +# first gather the files since this is needed for both ESP-IDF as well as other builds +file(GLOB_RECURSE RADIOLIB_SOURCES + "src/*.cpp" +) + +# exclude all HAL source files, except the default Arduino HAL +list(FILTER RADIOLIB_SOURCES EXCLUDE REGEX "src/hal/.*\\.cpp") +list(APPEND RADIOLIB_SOURCES "src/hal/Arduino/ArduinoHal.cpp") + if(ESP_PLATFORM) # Build RadioLib as an ESP-IDF component # required because ESP-IDF runs cmake in script mode # and needs idf_component_register() - file(GLOB_RECURSE RADIOLIB_ESP_SOURCES - "src/*.*" - ) idf_component_register( SRCS ${RADIOLIB_ESP_SOURCES} @@ -22,14 +28,6 @@ endif() project(radiolib) -file(GLOB_RECURSE RADIOLIB_SOURCES - "src/*.cpp" -) - -# exclude all HAL source files, except the default Arduino HAL -list(FILTER RADIOLIB_SOURCES EXCLUDE REGEX "src/hal/.*\\.cpp") -list(APPEND RADIOLIB_SOURCES "src/hal/Arduino/ArduinoHal.cpp") - add_library(RadioLib ${RADIOLIB_SOURCES}) target_include_directories(RadioLib From d0624f7b85fab6e7d4f6eaa0eda4c6e7fc612666 Mon Sep 17 00:00:00 2001 From: jgromes Date: Thu, 29 May 2025 17:09:08 +0200 Subject: [PATCH 4/5] Fix sources for IDF component --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d34a0ea..fa896a92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ if(ESP_PLATFORM) # and needs idf_component_register() idf_component_register( - SRCS ${RADIOLIB_ESP_SOURCES} + SRCS ${RADIOLIB_SOURCES} INCLUDE_DIRS . src ) From faa5ab11d2b7beb8fa2d4d2a098f06910740c714 Mon Sep 17 00:00:00 2001 From: jgromes Date: Thu, 29 May 2025 17:31:27 +0200 Subject: [PATCH 5/5] [HAL] Add macro guards to PicoHal --- CMakeLists.txt | 3 +-- examples/NonArduino/Pico/CMakeLists.txt | 2 +- src/hal/RPiPico/PicoHal.cpp | 4 ++++ src/hal/RPiPico/PicoHal.h | 4 ++++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fa896a92..b208f79d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,9 +5,8 @@ file(GLOB_RECURSE RADIOLIB_SOURCES "src/*.cpp" ) -# exclude all HAL source files, except the default Arduino HAL +# exclude all HAL source files list(FILTER RADIOLIB_SOURCES EXCLUDE REGEX "src/hal/.*\\.cpp") -list(APPEND RADIOLIB_SOURCES "src/hal/Arduino/ArduinoHal.cpp") if(ESP_PLATFORM) # Build RadioLib as an ESP-IDF component diff --git a/examples/NonArduino/Pico/CMakeLists.txt b/examples/NonArduino/Pico/CMakeLists.txt index 36e9c4e4..84729dbe 100644 --- a/examples/NonArduino/Pico/CMakeLists.txt +++ b/examples/NonArduino/Pico/CMakeLists.txt @@ -22,11 +22,11 @@ add_executable(${PROJECT_NAME} main.cpp "${CMAKE_CURRENT_SOURCE_DIR}/../../../../RadioLib/src/hal/RPiPico/PicoHal.cpp" ) +target_compile_definitions(${PROJECT_NAME} PUBLIC RADIOLIB_BUILD_RPI_PICO) # Pull in common dependencies target_link_libraries(${PROJECT_NAME} pico_stdlib hardware_spi hardware_gpio hardware_timer pico_multicore hardware_pwm RadioLib) - pico_enable_stdio_usb(${PROJECT_NAME} 1) pico_enable_stdio_uart(${PROJECT_NAME} 0) diff --git a/src/hal/RPiPico/PicoHal.cpp b/src/hal/RPiPico/PicoHal.cpp index 754eed71..03e6733f 100644 --- a/src/hal/RPiPico/PicoHal.cpp +++ b/src/hal/RPiPico/PicoHal.cpp @@ -1,5 +1,7 @@ #include "PicoHal.h" +#if defined(RADIOLIB_BUILD_RPI_PICO) + // pre-calculated pulse-widths for 1200 and 2200Hz // we do this to save calculation time (see https://github.com/khoih-prog/RP2040_PWM/issues/6) #define SLEEP_1200 416.666 @@ -43,3 +45,5 @@ void PicoHal::tone(uint32_t pin, unsigned int frequency, unsigned long duration) toneLoopDuration = duration; multicore_launch_core1(toneLoop); } + +#endif diff --git a/src/hal/RPiPico/PicoHal.h b/src/hal/RPiPico/PicoHal.h index f5ba3ef9..dfa06f1c 100644 --- a/src/hal/RPiPico/PicoHal.h +++ b/src/hal/RPiPico/PicoHal.h @@ -1,6 +1,8 @@ #ifndef PICO_HAL_H #define PICO_HAL_H +#if defined(RADIOLIB_BUILD_RPI_PICO) + // include RadioLib #include @@ -153,3 +155,5 @@ private: }; #endif + +#endif