Implement Controlled Pulses via PIO
This commit is contained in:
@@ -13,6 +13,8 @@
|
||||
#include "pico/stdio.h"
|
||||
#include "hardware/uart.h"
|
||||
|
||||
#include "pulser.pio.h"
|
||||
|
||||
#define LED_PIN 25
|
||||
|
||||
#define ENABLE0_PIN 2
|
||||
@@ -32,9 +34,9 @@ int main() {
|
||||
gpio_set_dir(ENABLE1_PIN, GPIO_OUT);
|
||||
gpio_put(ENABLE1_PIN, 0);
|
||||
|
||||
gpio_init(STEP0_PIN);
|
||||
gpio_set_dir(STEP0_PIN, GPIO_OUT);
|
||||
gpio_put(STEP0_PIN, 0);
|
||||
//gpio_init(STEP0_PIN);
|
||||
//gpio_set_dir(STEP0_PIN, GPIO_OUT);
|
||||
//gpio_put(STEP0_PIN, 0);
|
||||
|
||||
gpio_init(STEP1_PIN);
|
||||
gpio_set_dir(STEP1_PIN, GPIO_OUT);
|
||||
@@ -48,6 +50,43 @@ int main() {
|
||||
gpio_set_dir(DIR1_PIN, GPIO_OUT);
|
||||
gpio_put(DIR1_PIN, 0);
|
||||
|
||||
|
||||
PIO pio_core = pio0;
|
||||
int SM_DIV0 = 0;
|
||||
int SM_COUNT0 = 2;
|
||||
|
||||
uint pulser_offset = pio_add_program(pio_core, &pulser_program);
|
||||
|
||||
pio_sm_config c_div0 = pulser_program_get_default_config(pulser_offset);
|
||||
pio_sm_config c_count0 = pulser_program_get_default_config(pulser_offset);
|
||||
|
||||
pio_gpio_init(pio_core, STEP0_PIN);
|
||||
pio_gpio_init(pio_core, STEP1_PIN);
|
||||
|
||||
pio_sm_set_consecutive_pindirs(pio_core, SM_COUNT0, STEP0_PIN, 1, true);
|
||||
sm_config_set_set_pins(&c_count0, STEP0_PIN, 1);
|
||||
|
||||
pio_sm_init(pio_core, SM_DIV0, pulser_offset, &c_div0);
|
||||
pio_sm_init(pio_core, SM_COUNT0, pulser_offset + 6, &c_count0);
|
||||
|
||||
const double target_freq_khz = 500;
|
||||
const double clock_div = ((double)(SYS_CLK_KHZ))/((double)(target_freq_khz));
|
||||
pio_sm_set_clkdiv(pio_core, SM_DIV0, clock_div);
|
||||
pio_sm_set_clkdiv(pio_core, SM_COUNT0, 1.0);
|
||||
|
||||
pio_sm_set_enabled(pio_core, SM_DIV0, true);
|
||||
pio_sm_set_enabled(pio_core, SM_COUNT0, true);
|
||||
pio_sm_put_blocking(pio_core, SM_DIV0, 0);
|
||||
|
||||
pio_sm_put_blocking(pio_core, SM_COUNT0, 0);
|
||||
|
||||
// freq = base / 6 + n cyles
|
||||
// n = (base/freq) - 6
|
||||
|
||||
const double double_div_freq_khz = 0.5;
|
||||
const uint32_t cycles = (target_freq_khz/double_div_freq_khz) - 6;
|
||||
pio_sm_put_blocking(pio_core, SM_DIV0, cycles);
|
||||
|
||||
TMC2209_UART uart_driver0 = TMC2209_UART(uart0, 0, 19200, 0, 1);
|
||||
TMC2209_UART uart_driver1 = TMC2209_UART(uart1, 0, 19200, 4, 5);
|
||||
uart_driver0.init();
|
||||
|
||||
Reference in New Issue
Block a user