diff --git a/CMakeLists.txt b/CMakeLists.txt index eabfb5d8..b208f79d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,15 +1,20 @@ 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 +list(FILTER RADIOLIB_SOURCES EXCLUDE REGEX "src/hal/.*\\.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} + SRCS ${RADIOLIB_SOURCES} INCLUDE_DIRS . src ) @@ -22,10 +27,6 @@ endif() project(radiolib) -file(GLOB_RECURSE RADIOLIB_SOURCES - "src/*.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..84729dbe 100644 --- a/examples/NonArduino/Pico/CMakeLists.txt +++ b/examples/NonArduino/Pico/CMakeLists.txt @@ -20,12 +20,13 @@ 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" ) +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 new file mode 100644 index 00000000..03e6733f --- /dev/null +++ b/src/hal/RPiPico/PicoHal.cpp @@ -0,0 +1,49 @@ +#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 +#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); +} + +#endif diff --git a/src/hal/RPiPico/PicoHal.h b/src/hal/RPiPico/PicoHal.h index ec4d12e3..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 @@ -12,42 +14,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 +114,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(); @@ -196,3 +155,5 @@ private: }; #endif + +#endif