Merge pull request #1516 from jgromes/rpi-pico-hal-fix

[HAL] Rpi pico hal fix
lr2021
Jan Gromeš 2025-05-29 17:43:20 +02:00 zatwierdzone przez GitHub
commit 764eeabd3a
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
4 zmienionych plików z 65 dodań i 53 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -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)

Wyświetl plik

@ -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

Wyświetl plik

@ -1,6 +1,8 @@
#ifndef PICO_HAL_H
#define PICO_HAL_H
#if defined(RADIOLIB_BUILD_RPI_PICO)
// include RadioLib
#include <RadioLib.h>
@ -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