Skip to content

Commit b3d0ccc

Browse files
authored
Change duty cycle of PIO Tone to 50% (#2770)
1 parent 5a34395 commit b3d0ccc

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

cores/rp2040/Tone.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
7171
return; // Weird deadlock case
7272
}
7373

74-
int us = 1'000'000 / frequency / 2;
75-
if (us < 5) {
76-
us = 5;
77-
}
74+
unsigned int delay = (RP2040::f_cpu() + frequency) / (frequency * 2) - 3; // rounded
7875
auto entry = _toneMap.find(pin);
7976
Tone *newTone;
8077
if (entry == _toneMap.end()) {
@@ -99,7 +96,7 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
9996
tone2_program_init(newTone->pio, newTone->sm, newTone->off, pin);
10097
}
10198
pio_sm_clear_fifos(newTone->pio, newTone->sm); // Remove any old updates that haven't yet taken effect
102-
pio_sm_put_blocking(newTone->pio, newTone->sm, RP2040::usToPIOCycles(us));
99+
pio_sm_put_blocking(newTone->pio, newTone->sm, delay);
103100
pio_sm_exec(newTone->pio, newTone->sm, pio_encode_pull(false, false));
104101
pio_sm_exec(newTone->pio, newTone->sm, pio_encode_mov(pio_x, pio_osr));
105102
pio_sm_set_enabled(newTone->pio, newTone->sm, true);

cores/rp2040/tone2.pio

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818

1919
; Side-set pin 0 is used for Tone output
2020

21-
; OSR == Halfcycle count
21+
; OSR == Halfcycle count - 3
2222

2323
.program tone2
2424
.side_set 1 opt
2525

2626
; pull ; TXFIFO -> OSR, or X -> OSR if no new period
2727
; mov x, osr ; OSR -> X
2828

29+
.wrap_target
2930
high:
3031
pull noblock ; Potentially grab new HALFCYCLECOUNT, OTW copy from backup in X
31-
mov x, osr ; OSR -> X
32-
mov y, osr side 1 ; HALFCYCLECOUNT -> Y
32+
mov x, osr side 1 ; OSR -> X
33+
mov y, osr ; HALFCYCLECOUNT -> Y
3334
highloop:
3435
jmp y-- highloop ; while (y--) { /* noop delay */ }
3536

@@ -38,7 +39,7 @@ low:
3839
lowloop:
3940
jmp y-- lowloop ; while (y--) { /* noop delay */ }
4041

41-
jmp high ; GOTO high
42+
.wrap ; GOTO high
4243

4344
% c-sdk {
4445
static inline void tone2_program_init(PIO pio, uint sm, uint offset, uint pin) {

cores/rp2040/tone2.pio.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,26 @@
1313
// ----- //
1414

1515
#define tone2_wrap_target 0
16-
#define tone2_wrap 6
16+
#define tone2_wrap 5
1717
#define tone2_pio_version 0
1818

1919
static const uint16_t tone2_program_instructions[] = {
20-
// .wrap_target
20+
// .wrap_target
2121
0x8080, // 0: pull noblock
22-
0xa027, // 1: mov x, osr
23-
0xb847, // 2: mov y, osr side 1
22+
0xb827, // 1: mov x, osr side 1
23+
0xa047, // 2: mov y, osr
2424
0x0083, // 3: jmp y--, 3
2525
0xb047, // 4: mov y, osr side 0
2626
0x0085, // 5: jmp y--, 5
27-
0x0000, // 6: jmp 0
28-
// .wrap
27+
// .wrap
2928
};
3029

3130
#if !PICO_NO_HARDWARE
3231
static const struct pio_program tone2_program = {
3332
.instructions = tone2_program_instructions,
34-
.length = 7,
33+
.length = 6,
3534
.origin = -1,
36-
.pio_version = 0,
35+
.pio_version = tone2_pio_version,
3736
#if PICO_PIO_VERSION > 0
3837
.used_gpio_ranges = 0x0
3938
#endif

0 commit comments

Comments
 (0)