I am trying to use PIO to detect change of hall signal from a BLDC motor. So I wrote this program
hall.pio:main.c:When I run this program I got continuous output from the terminal like this:It seems that x and y registers are oscillating between 101101 and 101. I can't figure out why. Shouldn't the "in pins 3" only move 3 bits into ISR register?
BTW, I connect GPIO 26, 27, 28 to the three hall signals. When I got the above print out I measure these signal and they are correct (they are not changing):
26: high
27: low
28: high
Could anyone shed some light on what I did wrong?
hall.pio:
Code:
.program hall.wrap_targetloop: in pins 3 mov y ISR jmp x != y send jmp loop send: push mov x y.wrap% c-sdk {static inline void hall_program_init(PIO pio, uint sm, uint offset, uint pin) { for (uint i = 0; i < 3; i++) { pio_gpio_init(pio, pin + i); } pio_sm_set_consecutive_pindirs(pio, sm, pin, 3, false); pio_sm_config c = hall_program_get_default_config(offset); sm_config_set_in_shift(&c, false, false, 3); sm_config_set_in_pins(&c, pin); pio_sm_init(pio, sm, offset, &c);}%}
Code:
#include <stdio.h>#include "pico/stdlib.h"#include "hardware/pio.h"#include "hall.pio.h"int main() { stdio_init_all(); PIO pio = pio0; int smHall = pio_claim_unused_sm(pio, true); uint offsetHall = pio_add_program(pio, &hall_program); const uint8_t hall_pin = 26; const uint8_t hall_enable = 9; printf("Loaded hall program at %d\n", offsetHall); gpio_init(hall_enable); gpio_set_dir(hall_enable, GPIO_OUT); gpio_put(hall_enable, 0); for (uint8_t i = 0; i < 3; i++) { gpio_init(hall_pin + i); gpio_set_dir(hall_pin + i, GPIO_IN); } hall_program_init(pio, smHall, offsetHall, hall_pin); pio_sm_set_enabled(pio, smHall, true); while (true) { if (!pio_sm_is_rx_fifo_empty(pio, smHall)) { uint32_t hallPattern = pio_sm_get(pio, smHall); printf("Hall pattern: %b\n", hallPattern); } }}
Code:
Hall pattern: 101101Hall pattern: 101Hall pattern: 101101Hall pattern: 101Hall pattern: 101101Hall pattern: 101
BTW, I connect GPIO 26, 27, 28 to the three hall signals. When I got the above print out I measure these signal and they are correct (they are not changing):
26: high
27: low
28: high
Could anyone shed some light on what I did wrong?
Statistics: Posted by seanchenca — Fri Mar 22, 2024 12:57 am