Initial Release
This commit is contained in:
11
pio/pwm/CMakeLists.txt
Normal file
11
pio/pwm/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
add_executable(pio_pwm)
|
||||
|
||||
pico_generate_pio_header(pio_pwm ${CMAKE_CURRENT_LIST_DIR}/pwm.pio)
|
||||
|
||||
target_sources(pio_pwm PRIVATE pwm.c)
|
||||
|
||||
target_link_libraries(pio_pwm PRIVATE pico_stdlib hardware_pio)
|
||||
pico_add_extra_outputs(pio_pwm)
|
||||
|
||||
# add url via pico_set_program_url
|
||||
example_auto_set_url(pio_pwm)
|
||||
46
pio/pwm/pwm.c
Normal file
46
pio/pwm/pwm.c
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/pio.h"
|
||||
#include "pwm.pio.h"
|
||||
|
||||
// Write `period` to the input shift register
|
||||
void pio_pwm_set_period(PIO pio, uint sm, uint32_t period) {
|
||||
pio_sm_set_enabled(pio, sm, false);
|
||||
pio_sm_put_blocking(pio, sm, period);
|
||||
pio_sm_exec(pio, sm, pio_encode_pull(false, false));
|
||||
pio_sm_exec(pio, sm, pio_encode_out(pio_isr, 32));
|
||||
pio_sm_set_enabled(pio, sm, true);
|
||||
}
|
||||
|
||||
// Write `level` to TX FIFO. State machine will copy this into X.
|
||||
void pio_pwm_set_level(PIO pio, uint sm, uint32_t level) {
|
||||
pio_sm_put_blocking(pio, sm, level);
|
||||
}
|
||||
|
||||
int main() {
|
||||
stdio_init_all();
|
||||
|
||||
// todo get free sm
|
||||
PIO pio = pio0;
|
||||
int sm = 0;
|
||||
uint offset = pio_add_program(pio, &pwm_program);
|
||||
printf("Loaded program at %d\n", offset);
|
||||
|
||||
pwm_program_init(pio, sm, offset, 25);
|
||||
pio_pwm_set_period(pio, sm, (1u << 16) - 1);
|
||||
|
||||
int level = 0;
|
||||
while (true) {
|
||||
printf("Level = %d\n", level);
|
||||
pio_pwm_set_level(pio, sm, level * level);
|
||||
level = (level + 1) % 256;
|
||||
sleep_ms(10);
|
||||
}
|
||||
}
|
||||
31
pio/pwm/pwm.pio
Normal file
31
pio/pwm/pwm.pio
Normal file
@@ -0,0 +1,31 @@
|
||||
;
|
||||
; Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
;
|
||||
; SPDX-License-Identifier: BSD-3-Clause
|
||||
;
|
||||
|
||||
; Side-set pin 0 is used for PWM output
|
||||
|
||||
.program pwm
|
||||
.side_set 1 opt
|
||||
|
||||
pull noblock side 0 ; Pull from FIFO to OSR if available, else copy X to OSR.
|
||||
mov x, osr ; Copy most-recently-pulled value back to scratch X
|
||||
mov y, isr ; ISR contains PWM period. Y used as counter.
|
||||
countloop:
|
||||
jmp x!=y noset ; Set pin high if X == Y, keep the two paths length matched
|
||||
jmp skip side 1
|
||||
noset:
|
||||
nop ; Single dummy cycle to keep the two paths the same length
|
||||
skip:
|
||||
jmp y-- countloop ; Loop until Y hits 0, then pull a fresh PWM value from FIFO
|
||||
|
||||
% c-sdk {
|
||||
static inline void pwm_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 = pwm_program_get_default_config(offset);
|
||||
sm_config_set_sideset_pins(&c, pin);
|
||||
pio_sm_init(pio, sm, offset, &c);
|
||||
}
|
||||
%}
|
||||
Reference in New Issue
Block a user