arduino-pico/cores/rp2040/tone2.pio
2025-01-21 10:03:31 -08:00

53 lines
1.8 KiB
Text

; Tone2 for the Raspberry Pi Pico RP2040
;
; Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
;
; This library is free software; you can redistribute it and/or
; modify it under the terms of the GNU Lesser General Public
; License as published by the Free Software Foundation; either
; version 2.1 of the License, or (at your option) any later version.
;
; This library is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
; Lesser General Public License for more details.
;
; You should have received a copy of the GNU Lesser General Public
; License along with this library; if not, write to the Free Software
; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
; Side-set pin 0 is used for Tone output
; OSR == Halfcycle count - 3
.program tone2
.side_set 1 opt
; pull ; TXFIFO -> OSR, or X -> OSR if no new period
; mov x, osr ; OSR -> X
.wrap_target
high:
pull noblock ; Potentially grab new HALFCYCLECOUNT, OTW copy from backup in X
mov x, osr side 1 ; OSR -> X
mov y, osr ; HALFCYCLECOUNT -> Y
highloop:
jmp y-- highloop ; while (y--) { /* noop delay */ }
low:
mov y, osr side 0 ; HALFCYCLECOUNT -> Y
lowloop:
jmp y-- lowloop ; while (y--) { /* noop delay */ }
.wrap ; GOTO high
% c-sdk {
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);
}
%}