From 621d39437ae66951ebfcc13ecc66704efe356c00 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sat, 23 Dec 2023 16:44:12 +0100 Subject: Driver: Add basic remote control --- driver_fw/Makefile | 4 +- driver_fw/src/main.c | 123 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 86 insertions(+), 41 deletions(-) diff --git a/driver_fw/Makefile b/driver_fw/Makefile index fb4be18..9afcbb5 100644 --- a/driver_fw/Makefile +++ b/driver_fw/Makefile @@ -9,7 +9,7 @@ MUSL_DIR ?= upstream/musl # Algorithm parameters ######################################################################################################################## -# - none - +DRIVER_ADDR ?= 0 ######################################################################################################################## # High-level build parameters @@ -91,7 +91,7 @@ CFLAGS += -fno-common -ffunction-sections -fdata-sections COMMON_CFLAGS += -O$(OPT) -std=gnu2x -g COMMON_CFLAGS += $(DEVICE_DEFINES) -COMMON_CFLAGS += -DDEBUG=$(DEBUG) +COMMON_CFLAGS += -DDEBUG=$(DEBUG) -DDRIVER_ADDR=$(DRIVER_ADDR) GEN_HEADERS := $(BUILDDIR)/generated/waveform_tables.h diff --git a/driver_fw/src/main.c b/driver_fw/src/main.c index ba3ddb8..866cde5 100644 --- a/driver_fw/src/main.c +++ b/driver_fw/src/main.c @@ -36,9 +36,37 @@ static union tx_buf_union *tx_buf_write = &tx_buf[2]; static bool idle_buf_ready = false; void update_tx_buf(void); +int hex_to_int(char *hex, size_t len); + +int hex_to_int(char *hex, size_t len) { + int rv = 0; + while (len--) { + rv = rv<<4; + char c = hex[len]; + if ('0' <= c && c <= '9') + c -= '0'; + else if ('a' <= c && c <= 'f') + c -= 'a' + 0xa; + else if ('A' <= c && c <= 'F') + c -= 'A' + 0xa; + else + c = 0; + rv |= c; + } + return rv; +} -char usartbuf[256]; -int usartp = 0; +enum leds { + LED_ON = 1, + LED_PING = 2, + LED_OVERHEAT = 4, + LED_CONTROL_ERR = 8, + LED_INPUT_ERR = 16, + LED_OUTPUT_ERR = 32 +}; + +char rxbuf[256]; +size_t rxp = 0; int main(void) { /* Configure clocks for 64 MHz system clock. @@ -160,7 +188,12 @@ int main(void) { GPIOD->MODER = IN(0) | IN(1) | IN(2) | IN(3) | IN(4) | IN(5) | IN(6) | IN(8) | IN(9); - delay_us(5*1000*1000); + for (int i=0; i<20; i++) { + set_status_leds(LED_ON); + delay_us(500*1000); + set_status_leds(0); + delay_us(500*1000); + } TIM1->CCMR1 = (6<CCMR2 = (6<BRR = 6667; /* Set baudrate to 9600 Bd */ USART1->CR1 |= USART_CR1_UE; /* And... go! */ - int i = 0; - int j = 0; - int k = 0; - int n = 0; + int rj45_rx_ctr = 0; + int ping_ctr = 0; + int control_err_ctr = 0; while (23) { - if (USART1->ISR & USART_ISR_RXNE_RXFNE) { - usartbuf[usartp] = USART1->RDR; - usartp ++; - if (usartp >= sizeof(usartbuf)) { - usartp = 0; - } + if (rj45_rx_ctr) { + set_rj45_leds(0x5); + rj45_rx_ctr--; + + } else { + set_rj45_leds(0x0); } - } - { - i++; - j++; - i %= 6; - j %= 4; - delay_us(100000); - set_rj45_leds(1 << j); - set_status_leds(0x01); - if (i == 0) { - k++; - if (k == 10) { - k = 0; - n++; - if (n == 16) { - n = 0; + int leds = LED_ON; + if (ping_ctr) { + leds |= LED_PING; + ping_ctr--; + } + if (control_err_ctr) { + leds |= LED_CONTROL_ERR; + control_err_ctr--; + } + set_status_leds(leds); + + if (USART1->ISR & USART_ISR_RXNE_RXFNE) { + rj45_rx_ctr = 100000; + if (USART1->RDR == '\n') { + if (rxp != 4*16*3) { + rxp = 0; + control_err_ctr = 10000000; + continue; } - } - uint8_t b = (k < 8) ? (1<packet.channels); i++) { - tx_buf_write->packet.channels[i] = (k < 9) ? (1<packet.brightness); i++) { - tx_buf_write->packet.brightness[i] = 0xff; //(n<<4) | n; + for (size_t i=0; i<16; i++) { + int brightness = hex_to_int(&rxbuf[DRIVER_ADDR*16*3 + i*3], 1); + int channels = hex_to_int(&rxbuf[DRIVER_ADDR*16*3 + i*3 + 1], 2); + + tx_buf_write->packet.channels[i] = channels; + + if ((i&1) == 0) { + tx_buf_write->packet.brightness[i>>1] = brightness; + } else { + tx_buf_write->packet.brightness[i>>1] |= brightness<<4; + } + } + rxp = 0; + ping_ctr = 1000000; + update_tx_buf(); + + } else { + if (rxp <= sizeof(rxbuf)) { + rxbuf[rxp] = USART1->RDR; + rxp ++; + } } - update_tx_buf(); } } } -- cgit