RP2040-code/Function Generator/DAC.pio

38 wiersze
2.4 KiB
Plaintext

.program pio_DAC
; Repeatedly get one word of data from the TX FIFO, stalling when the FIFO is
; empty. Write the data to the OUT pin group.
.wrap_target
; Get data bits from DMA via Output Shift Register (OSR) to PINS.
out PINS, 8 ; 1 machine cycle
; ============================
;.pseudo_wrap Fast loop = 1 machine cycle ; Frequencies >= 34Hz are configured to wrap at this point
; ============================
set x,1 ; 1 machine cycle
loop01: ; Loop will run 2 times
nop [29] ; 30 machine cycles
jmp x-- loop01 ; 1 machine cycle Bump x and Jump
; ==============================
; Slow loop = 64 machine cycles
; ==============================
; Frequencies < 34Hz are configured to wrap at this point
.wrap
% c-sdk {
// This is a raw helper function for use by the user which sets up the GPIO output, and configures the SM
// to output on a particular pin.
// Note: 1) No divider is specified for the SM, so it will default to the same speed as the CPU.
// 2) Hard coded to use 8 bit DAC hardware
void pio_DAC_program_init(PIO pio, uint sm, uint offset, uint start_pin) {
for (uint i=start_pin; i<(start_pin+8); i++) { pio_gpio_init(pio, i); } // Allow PIO to control GPIO pins as outputs
pio_sm_set_consecutive_pindirs(pio, sm, start_pin, 8, true); // Set the pin direction to output (in PIO)
pio_sm_config c = pio_DAC_program_get_default_config(offset); // Define PIO Configuration structure
sm_config_set_out_pins(&c, start_pin, 8); // Configure pins to be targeted by the OUT (and MOV) commands
sm_config_set_out_shift(&c, true, true, 8); // Shift right, Autopull enabled, 6/8 bits
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX); // Set TX_FIFO to 8. Improves stability at high frequencies
pio_sm_init(pio, sm, offset, &c); // Load configuration and jump to start of the program
pio_sm_set_enabled(pio, sm, true);
}
%}