From bea5523e02575abf61acc27ea8f70c17ec2f8196 Mon Sep 17 00:00:00 2001 From: Vitalii Popov Date: Sat, 17 May 2025 02:39:25 +0300 Subject: [PATCH] make it work with radar --- src/main.c | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/src/main.c b/src/main.c index 8ff24ae..8b8ca26 100644 --- a/src/main.c +++ b/src/main.c @@ -10,8 +10,8 @@ // UART buffer size #define BUFFER_LEN 48 -#define AMOUNT_DISTANCE_MEASURE 8 -#define ACTIVATION_DISTANCE 40 +#define AMOUNT_DISTANCE_MEASURE 8 +#define ACTIVATION_DISTANCE 40 // LED pin info #define LED_PIN_DIR PORT_C.DDR.Pin3 @@ -31,15 +31,15 @@ #define TX_PIN_CR2 PORT_D.CR2.Pin5 #define TX_PIN_OUT PORT_D.ODR.Pin5 -#define PRESS_ACTIVATION_TIME 30 * 4 -#define SENSE_ACTIVATION_TIME PRESS_ACTIVATION_TIME +#define PRESS_ACTIVATION_TIME 30 * 4 +#define SENSE_ACTIVATION_TIME PRESS_ACTIVATION_TIME const char *RANGE = "Range "; // Time defines -#define SHORT_PRESS_MS 100 -#define LONG_PRESS_MS 2000 -#define LED_FADE_TIME 888 +#define SHORT_PRESS_MS 100 +#define LONG_PRESS_MS 2000 +#define LED_FADE_TIME 888 // Countdown timestamp (2^15 max, sign used to notify "unitialized") i16 countdown_tick = -1; @@ -130,7 +130,8 @@ void send_some_command(void) { tx_buffer[0] = 'C'; tx_buffer[1] = 'M'; tx_buffer[2] = 'D'; - start_cmd(3); + tx_buffer[3] = '\n'; + start_cmd(4); } /** @@ -248,7 +249,7 @@ void tick_250ms(void) { */ int is_rx_finished(void) { // head + len + cmd + ver + tail - if (rx_pos < 6 || (rx_buffer[0] != RANGE[0] && rx_pos < 4 + 2 + 2 + 2 + 4)) { + if (rx_pos < 6) { return false; } // Check head, tail and just in case - length @@ -260,16 +261,7 @@ int is_rx_finished(void) { rx_buffer[4] == RANGE[4] && rx_buffer[5] == RANGE[5] && (rx_buffer[rx_pos - 1] == '\r' || - rx_buffer[rx_pos - 1] == '\n')) || - (rx_buffer[0] == 0xFD && - rx_buffer[1] == 0xFC && - rx_buffer[2] == 0xFB && - rx_buffer[3] == 0xFA && - rx_buffer[rx_pos - 1] == 0x01 && - rx_buffer[rx_pos - 2] == 0x02 && - rx_buffer[rx_pos - 3] == 0x03 && - rx_buffer[rx_pos - 4] == 0x04 && - (rx_buffer[4] | (rx_buffer[5] << 8)) == rx_pos - 12); + rx_buffer[rx_pos - 1] == '\n')); } /** @@ -288,7 +280,7 @@ void add_distance(u16 distance) { */ void process_rx(void) { if (rx_buffer[0] == RANGE[0]) { - u16 distance = 0; + u16 distance = 0; for (u08 i = 6; i <= rx_pos; i++) { if (rx_buffer[i] >= '0' && rx_buffer[i] <= '9') { distance = distance * 10 + (rx_buffer[i] - '0'); @@ -310,14 +302,14 @@ interrupt(IRQ_UART1_RX_F, uart_recv) { if ((s & UART_RX_NOT_EMPTY) == 0) { return; } - - if (rx_pos < 4) { - if (rx_pos == 0xFD - c || RANGE[rx_pos] == c) { + if (rx_pos <= 4) { + if (RANGE[rx_pos] == c) { rx_buffer[rx_pos] = c; } else { // Something wrong - reset rx_pos = 0; memset(rx_buffer, 0, BUFFER_LEN); + return; } } else { rx_buffer[rx_pos] = c; @@ -414,8 +406,8 @@ void main(void) { UART1.CR3 &= ~(UART_CR3_STOP1 | UART_CR3_STOP2); // UART1.BRR2 = 0x0B; // 0x03; // 0x11; // UART1.BRR1 = 0x08; // 0x68; // 0x06; - UART1.BRR2 = 0x03; - UART1.BRR1 = 0x68; + UART1.BRR2 = 0x0B; + UART1.BRR1 = 0x08; UART1.CR2 = UART_CR2_TEN | UART_CR2_REN | UART_CR2_RXNE_IVT | UART_CR2_SBK; CLK.Peripheral1.Serial1 = 1;