ARGB_emulator/tx_rx.cpp
2025-02-23 21:59:28 +01:00

58 lines
1.4 KiB
C++

// --- C/C++ Integration Code ---
#include "hardware/irq.h"
#include "hardware/pio.h"
#include "pico/stdlib.h"
#include "argb_tx.pio.h"
#define PIN_BASE 0
#define PIN_COUNT 1
PIO pio = pio0;
uint sm = 0;
volatile bool transmission_done = false;
void pio_irq_handler() {
pio->irq = 1; // Clear interrupt
transmission_done = true;
}
void init_pio() {
uint offset = pio_add_program(pio, &argb_tx_program);
pio_sm_config c = argb_tx_program_get_default_config(offset);
sm_config_set_out_pins(&c, PIN_BASE, PIN_COUNT);
sm_config_set_sideset_pins(&c, PIN_BASE);
sm_config_set_out_shift(&c, true, true, 32);
pio_sm_init(pio, sm, offset, &c);
pio_set_irq0_source_enabled(pio, pis_interrupt0, true);
irq_set_exclusive_handler(PIO0_IRQ_0, pio_irq_handler);
irq_set_enabled(PIO0_IRQ_0, true);
}
void send_bits(const uint8_t *bits, uint num_bits) {
transmission_done = false;
// Send bit count (first word)
uint32_t count_word = num_bits;
pio_sm_put_blocking(pio, sm, count_word);
// Pack bits into 32-bit words
uint num_words = (num_bits + 31) / 32;
for (uint i = 0; i < num_words; ++i) {
uint32_t word = 0;
for (uint b = 0; b < 32; ++b) {
uint idx = i * 32 + b;
if (idx < num_bits) {
word |= (bits[idx] & 1) << b;
}
}
pio_sm_put_blocking(pio, sm, word);
}
// Wait for completion
while (!transmission_done)
;
}