Skip to content

Commit

Permalink
#20 Updates: Serial Protocol changes to mark frames
Browse files Browse the repository at this point in the history
  • Loading branch information
daveythacher committed May 27, 2023
1 parent eb5e440 commit b5a7756
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
1 change: 1 addition & 0 deletions LED_Matrix/include/Serial/serial_uart/serial_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@

void serial_uart_start(serial_uart_callback callback, int dma_chan);
void serial_uart_isr();
void serial_uart_task();

#endif
1 change: 1 addition & 0 deletions LED_Matrix/lib/Serial/serial_uart/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ target_link_libraries(serial_uart PRIVATE
hardware_irq
hardware_uart
hardware_dma # Cannot use full API!
hardware_timer
hardware_resets
hardware_clocks
)
7 changes: 6 additions & 1 deletion LED_Matrix/lib/Serial/serial_uart/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static packet buffers[2];
static volatile uint8_t buffer = 0;
static int serial_dma_chan;

void serial_task() {
static void __not_in_flash_func(test_driver)() {
#ifndef TEST
for (int x = 0; x < COLUMNS; x++) {
for (int y = 0; y < (2 * MULTIPLEX); y++) {
Expand All @@ -37,6 +37,11 @@ void serial_task() {
#endif
}

void __not_in_flash_func(serial_task)() {
test_driver();
serial_uart_task();
}

static void __not_in_flash_func(callback)(uint8_t **buf, uint16_t *len) {
*buf = (uint8_t *) &buffers[(buffer + 1) % 2];
*len = sizeof(packet);
Expand Down
68 changes: 47 additions & 21 deletions LED_Matrix/lib/Serial/serial_uart/serial_uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@
#include "hardware/gpio.h"
#include "hardware/dma.h"
#include "hardware/uart.h"
#include "hardware/timer.h"
#include "Serial/serial_uart/serial_uart.h"
#include "Matrix/matrix.h"

static serial_uart_callback func;
static int dma_chan;
static dma_channel_config c;
static volatile bool isIdle = true;

static void serial_uart_reload();
static void serial_uart_reload(bool reload_dma, bool process_msg);

#if __BYTE_ORDER == __LITTLE_ENDIAN
#define ntohs(x) __bswap16(x)
#else
#define ntohs(x) ((uint16_t)(x))
#endif

void serial_uart_start(serial_uart_callback callback, int dma)
{
void serial_uart_start(serial_uart_callback callback, int dma) {
func = callback;
dma_chan = dma;

Expand All @@ -47,33 +48,58 @@ void serial_uart_start(serial_uart_callback callback, int dma)
channel_config_set_dreq(&c, DREQ_UART0_RX);
dma_channel_set_irq1_enabled(dma_chan, true);

serial_uart_reload();
serial_uart_reload(false, true);
}

void __not_in_flash_func(serial_uart_reload)()
{
void __not_in_flash_func(serial_uart_task)() {
static uint64_t time = time_us_64();

if (isIdle) {
// Clear Errors
uart0_hw->icr = 0x7FF;

// Send idle token
if ((time + 10) < time_us_64()) {
uart_putc(uart0, 'i');
time = time_us_64();
}

// Look for start_token
if (uart_getc(uart0) == 's') {
serial_uart_reload(true, false);
isIdle = false;
}

}
}

void __not_in_flash_func(serial_uart_reload)(bool reload_dma, bool process_msg) {
static uint8_t *ptr = 0;
static uint8_t *buf;
static uint8_t *buf = 0;
static uint16_t len = 0;
uint16_t *p = (uint16_t *)buf;

if (reload_dma)
dma_channel_configure(dma_chan, &c, buf, &uart_get_hw(uart0)->dr, len, true);
else if (process_msg) {
if (p != 0) {
for (uint16_t i = 0; i < len; i += 2)
p[i / 2] = ntohs(p[i / 2]);
}

for (uint16_t i = 0; i < len; i += 2)
p[i / 2] = ntohs(p[i / 2]);
func(&buf, &len);

func(&buf, &len);
dma_channel_configure(dma_chan, &c, buf, &uart_get_hw(uart0)->dr, len, true);

if (ptr)
process((void *)ptr);

ptr = buf;
if (ptr)
process((void *)ptr);

ptr = buf;
}
}

void __not_in_flash_func(serial_uart_isr)()
{
if (dma_channel_get_irq1_status(dma_chan))
{
serial_uart_reload();
void __not_in_flash_func(serial_uart_isr)() {
if (dma_channel_get_irq1_status(dma_chan)) {
serial_uart_reload(false, (uart0_hw->ris & 0x380) == 0);
isIdle = true;
dma_hw->ints1 = 1 << dma_chan;
}
}

0 comments on commit b5a7756

Please sign in to comment.