pimoroni-pico/drivers/pwm/pwm_cluster.pio

72 lines
2.5 KiB
Plaintext
Raw Normal View History

; --------------------------------------------------
; 32-Channel PWM using PIO
; by Christopher (@ZodiusInfuser) Parrott
; --------------------------------------------------
2022-02-11 11:40:04 +00:00
;
; Outputs PWM signals to up to 32 pins using a single
; state-machine. This is achieved by representing the
; problem as a sequence of state changes and time delays.
2022-02-11 11:40:04 +00:00
;
; This requires the use of DMA or a very fast loop to
; provide data at a sufficient rate to prevent stalling.
2022-02-11 11:40:04 +00:00
;
; The program pulls in two words:
; - The first is a 32-bit pin mask of the states to
; change the pins to. Only the pins configured for
; use by this SM will be affected.
; - The second is the time delay to wait before
; pulling in the next states.
;
; Each loop of the program takes 5 cycles, including an
; initial 5 cycles when new data is loaded in. As such,
; the time delay should be set to your "intended_delay - 1".
;
; To aid in debugging there is a variant program that uses
; a sideset pin to show when new data is pulled in, as well
; as when each loop has elapsed.These look like:
;
; New Data Loop
; ._|‾|_._._ |‾.‾.‾.‾|_
; 1 2 3 4 5 1 2 3 4 5
2022-02-11 11:40:04 +00:00
; Debounce Constants
; --------------------------------------------------
.define public PWM_CLUSTER_CYCLES 5
; PWM Program
; --------------------------------------------------
.program pwm_cluster
.wrap_target
pull ; Pull in the new pin states
out pins, 32 ; Immediately set the pins to their new state
pull ; Pull in the delay counter
out y, 32 ; Set the counter
count_check:
jmp y-- delay ; Check if the counter is 0, and if so wrap around.
; If not decrement the counter and jump to the delay
.wrap
delay:
jmp count_check [3] ; Wait a few cycles then jump back to the loop
; Debug PWM Program
; --------------------------------------------------
.program debug_pwm_cluster
2022-02-11 11:40:04 +00:00
.side_set 1
.wrap_target
pull side 0 ; Pull in the new pin states
out pins, 32 side 1 ; Immediately set the pins to their new state
pull side 0 ; Pull in the delay counter
out y, 32 side 0 ; Set the counter
2022-02-11 11:40:04 +00:00
count_check:
jmp y-- delay side 0 ; Check if the counter is 0, and if so wrap around.
;If not decrement the counter and jump to the delay
2022-02-11 11:40:04 +00:00
.wrap
delay:
jmp count_check [3] side 1 ; Wait a few cycles then jump back to the loop