Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add tones support for the RPi Pico #1239

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/NonArduino/Pico/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ add_executable(${PROJECT_NAME}
)

# Pull in common dependencies
target_link_libraries(${PROJECT_NAME} pico_stdlib hardware_spi hardware_gpio hardware_timer RadioLib)
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)

# Create map/bin/hex file etc.
pico_add_extra_outputs(${PROJECT_NAME})
pico_add_extra_outputs(${PROJECT_NAME})
61 changes: 58 additions & 3 deletions examples/NonArduino/Pico/PicoHal.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,45 @@
#include <pico/stdlib.h>
#include "hardware/spi.h"
#include "hardware/timer.h"
#include "hardware/pwm.h"
#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
Expand All @@ -21,8 +60,7 @@ class PicoHal : public RadioLibHal {
_spiSpeed(spiSpeed),
_misoPin(misoPin),
_mosiPin(mosiPin),
_sckPin(sckPin) {
}
_sckPin(sckPin){}

void init() override {
stdio_init_all();
Expand Down Expand Up @@ -110,6 +148,19 @@ class PicoHal : public RadioLibHal {
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 noTone(uint32_t pin) override {
multicore_reset_core1();
}

mosa11aei marked this conversation as resolved.
Show resolved Hide resolved
void spiBegin() {
spi_init(_spiChannel, _spiSpeed);
spi_set_format(_spiChannel, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
Expand All @@ -125,6 +176,10 @@ class PicoHal : public RadioLibHal {
spi_write_read_blocking(_spiChannel, out, in, len);
}

void yield() override {
tight_loop_contents();
}

void spiEndTransaction() {}

void spiEnd() {
Expand All @@ -140,4 +195,4 @@ class PicoHal : public RadioLibHal {
uint32_t _sckPin;
};

#endif
#endif
Loading