-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow continuous tone frequency changes (#186)
Fixes #121 Supersedes #185 Redo the PIO program to allow the tone generator on a pin to be updated without interruption, at waveform boundaries. This allows for things like sirens or slurs to be implemented simply. Use an alarm, not the PIO hardware, to manage time-limited tones(). Add a simple siren example.
- Loading branch information
1 parent
6431b81
commit c65c4bf
Showing
5 changed files
with
127 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
; Tone for the Raspberry Pi Pico RP2040 | ||
; Tone2 for the Raspberry Pi Pico RP2040 | ||
; | ||
; Copyright (c) 2021 Earle F. Philhower, III <[email protected]> | ||
; | ||
|
@@ -18,32 +18,35 @@ | |
|
||
; Side-set pin 0 is used for Tone output | ||
|
||
.program tone | ||
; OSR == Halfcycle count | ||
|
||
.program tone2 | ||
.side_set 1 opt | ||
|
||
pull | ||
mov x, osr | ||
pull ; TXFIFO -> OSR, or X -> OSR if no new period | ||
mov x, osr ; OSR -> X | ||
|
||
high: | ||
mov y, isr side 1 | ||
pull noblock ; Potentially grab new HALFCYCLECOUNT, OTW copy from backup in X | ||
mov x, osr ; OSR -> X | ||
mov y, osr side 1 ; HALFCYCLECOUNT -> Y | ||
highloop: | ||
jmp y-- highloop | ||
|
||
jmp x-- low | ||
jmp y-- highloop ; while (y--) { /* noop delay */ } | ||
|
||
low: | ||
mov y, isr side 0 | ||
mov y, osr side 0 ; HALFCYCLECOUNT -> Y | ||
lowloop: | ||
jmp y-- lowloop | ||
jmp y-- lowloop ; while (y--) { /* noop delay */ } | ||
|
||
jmp x-- high | ||
jmp high ; GOTO high | ||
|
||
% c-sdk { | ||
static inline void tone_program_init(PIO pio, uint sm, uint offset, uint pin) { | ||
static inline void tone2_program_init(PIO pio, uint sm, uint offset, uint pin) { | ||
pio_gpio_init(pio, pin); | ||
pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true); | ||
pio_sm_config c = tone_program_get_default_config(offset); | ||
pio_sm_config c = tone2_program_get_default_config(offset); | ||
sm_config_set_sideset_pins(&c, pin); | ||
pio_sm_init(pio, sm, offset, &c); | ||
} | ||
%} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// -------------------------------------------------- // | ||
// This file is autogenerated by pioasm; do not edit! // | ||
// -------------------------------------------------- // | ||
|
||
#if !PICO_NO_HARDWARE | ||
#include "hardware/pio.h" | ||
#endif | ||
|
||
// ----- // | ||
// tone2 // | ||
// ----- // | ||
|
||
#define tone2_wrap_target 0 | ||
#define tone2_wrap 8 | ||
|
||
static const uint16_t tone2_program_instructions[] = { | ||
// .wrap_target | ||
0x80a0, // 0: pull block | ||
0xa027, // 1: mov x, osr | ||
0x8080, // 2: pull noblock | ||
0xa027, // 3: mov x, osr | ||
0xb847, // 4: mov y, osr side 1 | ||
0x0085, // 5: jmp y--, 5 | ||
0xb047, // 6: mov y, osr side 0 | ||
0x0087, // 7: jmp y--, 7 | ||
0x0002, // 8: jmp 2 | ||
// .wrap | ||
}; | ||
|
||
#if !PICO_NO_HARDWARE | ||
static const struct pio_program tone2_program = { | ||
.instructions = tone2_program_instructions, | ||
.length = 9, | ||
.origin = -1, | ||
}; | ||
|
||
static inline pio_sm_config tone2_program_get_default_config(uint offset) { | ||
pio_sm_config c = pio_get_default_sm_config(); | ||
sm_config_set_wrap(&c, offset + tone2_wrap_target, offset + tone2_wrap); | ||
sm_config_set_sideset(&c, 2, true, false); | ||
return c; | ||
} | ||
|
||
static inline void tone2_program_init(PIO pio, uint sm, uint offset, uint pin) { | ||
pio_gpio_init(pio, pin); | ||
pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true); | ||
pio_sm_config c = tone2_program_get_default_config(offset); | ||
sm_config_set_sideset_pins(&c, pin); | ||
pio_sm_init(pio, sm, offset, &c); | ||
} | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* Simple annoying siren example using tone() */ | ||
/* Released to the public domain by Earle F. Philhower, III */ | ||
|
||
#define TONEPIN 7 | ||
|
||
void setup() { | ||
} | ||
|
||
void loop() { | ||
for (int i = 100; i < 10000; i += 5) { | ||
tone(TONEPIN, i); | ||
} | ||
} |