Change duty cycle of PIO Tone to 50% (#2770)

This commit is contained in:
Markus Gyger 2025-01-22 01:03:31 +07:00 committed by GitHub
parent 5a34395f46
commit b3d0ccc7e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 17 deletions

View file

@ -71,10 +71,7 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
return; // Weird deadlock case return; // Weird deadlock case
} }
int us = 1'000'000 / frequency / 2; unsigned int delay = (RP2040::f_cpu() + frequency) / (frequency * 2) - 3; // rounded
if (us < 5) {
us = 5;
}
auto entry = _toneMap.find(pin); auto entry = _toneMap.find(pin);
Tone *newTone; Tone *newTone;
if (entry == _toneMap.end()) { if (entry == _toneMap.end()) {
@ -99,7 +96,7 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
tone2_program_init(newTone->pio, newTone->sm, newTone->off, pin); tone2_program_init(newTone->pio, newTone->sm, newTone->off, pin);
} }
pio_sm_clear_fifos(newTone->pio, newTone->sm); // Remove any old updates that haven't yet taken effect 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_put_blocking(newTone->pio, newTone->sm, delay);
pio_sm_exec(newTone->pio, newTone->sm, pio_encode_pull(false, false)); pio_sm_exec(newTone->pio, newTone->sm, pio_encode_pull(false, false));
pio_sm_exec(newTone->pio, newTone->sm, pio_encode_mov(pio_x, pio_osr)); pio_sm_exec(newTone->pio, newTone->sm, pio_encode_mov(pio_x, pio_osr));
pio_sm_set_enabled(newTone->pio, newTone->sm, true); pio_sm_set_enabled(newTone->pio, newTone->sm, true);

View file

@ -18,7 +18,7 @@
; Side-set pin 0 is used for Tone output ; Side-set pin 0 is used for Tone output
; OSR == Halfcycle count ; OSR == Halfcycle count - 3
.program tone2 .program tone2
.side_set 1 opt .side_set 1 opt
@ -26,10 +26,11 @@
; pull ; TXFIFO -> OSR, or X -> OSR if no new period ; pull ; TXFIFO -> OSR, or X -> OSR if no new period
; mov x, osr ; OSR -> X ; mov x, osr ; OSR -> X
.wrap_target
high: high:
pull noblock ; Potentially grab new HALFCYCLECOUNT, OTW copy from backup in X pull noblock ; Potentially grab new HALFCYCLECOUNT, OTW copy from backup in X
mov x, osr ; OSR -> X mov x, osr side 1 ; OSR -> X
mov y, osr side 1 ; HALFCYCLECOUNT -> Y mov y, osr ; HALFCYCLECOUNT -> Y
highloop: highloop:
jmp y-- highloop ; while (y--) { /* noop delay */ } jmp y-- highloop ; while (y--) { /* noop delay */ }
@ -38,7 +39,7 @@ low:
lowloop: lowloop:
jmp y-- lowloop ; while (y--) { /* noop delay */ } jmp y-- lowloop ; while (y--) { /* noop delay */ }
jmp high ; GOTO high .wrap ; GOTO high
% c-sdk { % c-sdk {
static inline void tone2_program_init(PIO pio, uint sm, uint offset, uint pin) { static inline void tone2_program_init(PIO pio, uint sm, uint offset, uint pin) {

View file

@ -13,27 +13,26 @@
// ----- // // ----- //
#define tone2_wrap_target 0 #define tone2_wrap_target 0
#define tone2_wrap 6 #define tone2_wrap 5
#define tone2_pio_version 0 #define tone2_pio_version 0
static const uint16_t tone2_program_instructions[] = { static const uint16_t tone2_program_instructions[] = {
// .wrap_target // .wrap_target
0x8080, // 0: pull noblock 0x8080, // 0: pull noblock
0xa027, // 1: mov x, osr 0xb827, // 1: mov x, osr side 1
0xb847, // 2: mov y, osr side 1 0xa047, // 2: mov y, osr
0x0083, // 3: jmp y--, 3 0x0083, // 3: jmp y--, 3
0xb047, // 4: mov y, osr side 0 0xb047, // 4: mov y, osr side 0
0x0085, // 5: jmp y--, 5 0x0085, // 5: jmp y--, 5
0x0000, // 6: jmp 0 // .wrap
// .wrap
}; };
#if !PICO_NO_HARDWARE #if !PICO_NO_HARDWARE
static const struct pio_program tone2_program = { static const struct pio_program tone2_program = {
.instructions = tone2_program_instructions, .instructions = tone2_program_instructions,
.length = 7, .length = 6,
.origin = -1, .origin = -1,
.pio_version = 0, .pio_version = tone2_pio_version,
#if PICO_PIO_VERSION > 0 #if PICO_PIO_VERSION > 0
.used_gpio_ranges = 0x0 .used_gpio_ranges = 0x0
#endif #endif