2022-02-15 15:15:52 +00:00
|
|
|
; --------------------------------------------------
|
|
|
|
; 32-Channel PWM using PIO
|
|
|
|
; by Christopher (@ZodiusInfuser) Parrott
|
|
|
|
; --------------------------------------------------
|
2022-02-11 11:40:04 +00:00
|
|
|
;
|
2022-02-15 15:15:52 +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
|
|
|
;
|
2022-02-15 15:15:52 +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
|
|
|
;
|
2022-02-15 15:15:52 +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
|
|
|
|
2022-02-15 15:15:52 +00:00
|
|
|
; Debounce Constants
|
|
|
|
; --------------------------------------------------
|
2022-02-20 15:12:02 +00:00
|
|
|
.define public PWM_CLUSTER_CYCLES 5
|
2022-02-15 15:15:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
; PWM Program
|
|
|
|
; --------------------------------------------------
|
2022-02-20 11:00:36 +00:00
|
|
|
.program pwm_cluster
|
2022-02-15 15:15:52 +00:00
|
|
|
|
|
|
|
.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
|
|
|
|
; --------------------------------------------------
|
2022-02-20 11:00:36 +00:00
|
|
|
.program debug_pwm_cluster
|
2022-02-11 11:40:04 +00:00
|
|
|
.side_set 1
|
|
|
|
|
|
|
|
.wrap_target
|
2022-02-15 15:15:52 +00:00
|
|
|
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:
|
2022-02-15 15:15:52 +00:00
|
|
|
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:
|
2022-02-15 15:15:52 +00:00
|
|
|
jmp count_check [3] side 1 ; Wait a few cycles then jump back to the loop
|