Skip to content

Commit 67bf5a7

Browse files
Remove 2 instructions from tone PIO program (#2515)
Manually load the 2 needed registers using pio_execute to save a couple PIO instruction slots.
1 parent ec6bb7e commit 67bf5a7

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

cores/rp2040/Tone.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
100100
}
101101
pio_sm_clear_fifos(newTone->pio, newTone->sm); // Remove any old updates that haven't yet taken effect
102102
pio_sm_put_blocking(newTone->pio, newTone->sm, RP2040::usToPIOCycles(us));
103+
pio_sm_exec(newTone->pio, newTone->sm, pio_encode_pull(false, false));
104+
pio_sm_exec(newTone->pio, newTone->sm, pio_encode_mov(pio_x, pio_osr));
103105
pio_sm_set_enabled(newTone->pio, newTone->sm, true);
104106

105107
_toneMap.insert({pin, newTone});

cores/rp2040/tone2.pio

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
.program tone2
2424
.side_set 1 opt
2525

26-
pull ; TXFIFO -> OSR, or X -> OSR if no new period
27-
mov x, osr ; OSR -> X
26+
; pull ; TXFIFO -> OSR, or X -> OSR if no new period
27+
; mov x, osr ; OSR -> X
2828

2929
high:
3030
pull noblock ; Potentially grab new HALFCYCLECOUNT, OTW copy from backup in X

cores/rp2040/tone2.pio.h

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

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

1919
static const uint16_t tone2_program_instructions[] = {
2020
// .wrap_target
21-
0x80a0, // 0: pull block
21+
0x8080, // 0: pull noblock
2222
0xa027, // 1: mov x, osr
23-
0x8080, // 2: pull noblock
24-
0xa027, // 3: mov x, osr
25-
0xb847, // 4: mov y, osr side 1
23+
0xb847, // 2: mov y, osr side 1
24+
0x0083, // 3: jmp y--, 3
25+
0xb047, // 4: mov y, osr side 0
2626
0x0085, // 5: jmp y--, 5
27-
0xb047, // 6: mov y, osr side 0
28-
0x0087, // 7: jmp y--, 7
29-
0x0002, // 8: jmp 2
27+
0x0000, // 6: jmp 0
3028
// .wrap
3129
};
3230

3331
#if !PICO_NO_HARDWARE
3432
static const struct pio_program tone2_program = {
3533
.instructions = tone2_program_instructions,
36-
.length = 9,
34+
.length = 7,
3735
.origin = -1,
3836
.pio_version = 0,
3937
#if PICO_PIO_VERSION > 0

0 commit comments

Comments
 (0)