From eaad822677079cfc7bd1248531418a5a9b94708b Mon Sep 17 00:00:00 2001 From: Radzhab Bisultanov Date: Tue, 3 Mar 2026 17:51:24 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D1=8B=20=D1=81=20UART=20=D0=B8=20=D0=BE?= =?UTF-8?q?=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B8=20=D0=B4=D0=B0?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D1=85.=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=BA=D0=BE=D0=BB=D1=8C=D1=86=D0=B5=D0=B2?= =?UTF-8?q?=D0=BE=D0=B9=20=D0=B1=D1=83=D1=84=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/BSP/Inc/lidar.h | 6 +++- Source/BSP/Src/lidar.c | 78 ++++++++++++++++++++++-------------------- Source/main.c | 2 +- 3 files changed, 47 insertions(+), 39 deletions(-) diff --git a/Source/BSP/Inc/lidar.h b/Source/BSP/Inc/lidar.h index 32d0848..07c0fc0 100644 --- a/Source/BSP/Inc/lidar.h +++ b/Source/BSP/Inc/lidar.h @@ -6,6 +6,7 @@ #include "stm32g431xx.h" #define USART3_START_BYTE 0x59 +#define USART3_BUF_SIZE 64 #define USART3_FRAME_SIZE 9 #define LIDAR_MIN_DIST 0.01f #define LIDAR_MAX_DIST 40.0f @@ -26,7 +27,7 @@ typedef struct typedef struct { - uint16_t distance; // meters + uint16_t distance; // cm uint16_t strength; uint16_t temperature; } lidar_data; @@ -37,6 +38,9 @@ void TIM7_DAC_IRQHandler(); void USART3_IRQHandler(); void lidar_update(lidar_data* lidar); +uint8_t usart_available(); +uint8_t usart_read(); + void lidar_i2c2_init(); static void i2c2_wait_txis(); static void i2c2_wait_stop(); diff --git a/Source/BSP/Src/lidar.c b/Source/BSP/Src/lidar.c index 9cdcb41..4ff2ea1 100644 --- a/Source/BSP/Src/lidar.c +++ b/Source/BSP/Src/lidar.c @@ -1,11 +1,8 @@ #include "lidar.h" -volatile uint8_t usart3_index = 0; -volatile uint8_t usart3_checksum = 0; -volatile uint8_t usart3_frame_ready = 0; -volatile uint8_t lidar_update_flag = 0; -static lidar_data_buf buffer; -static uint8_t* buff_data = (uint8_t*)&buffer; +volatile uint8_t usart3_rx_buf[USART3_BUF_SIZE]; +static uint8_t usart3_rx_head = 0; +static uint8_t usart3_rx_tail = 0; void lidar_init() { @@ -78,7 +75,7 @@ void TIM7_DAC_IRQHandler() if (TIM7->SR & TIM_SR_UIF) { TIM7->SR &= ~TIM_SR_UIF; - lidar_update_flag = 1; + //lidar_update_flag = 1; } } @@ -86,42 +83,49 @@ void USART3_IRQHandler() { if (USART3->ISR & USART_ISR_RXNE) { - uint8_t b = USART3->RDR; - - if (usart3_index < 2) - { - if (b == USART3_START_BYTE) - buff_data[usart3_index++] = b; - } - else if (usart3_index < USART3_FRAME_SIZE) - buff_data[usart3_index++] = b; - - if (usart3_index == USART3_FRAME_SIZE) - { - usart3_index = 0; - usart3_frame_ready = 1; - } + usart3_rx_buf[usart3_rx_head] = USART3->RDR; + usart3_rx_head = (usart3_rx_head + 1) % USART3_BUF_SIZE; } } +uint8_t usart_available() +{ + return usart3_rx_head != usart3_rx_tail; +} + +uint8_t usart_read() +{ + uint8_t data = usart3_rx_buf[usart3_rx_tail]; + usart3_rx_tail = (usart3_rx_tail + 1) % USART3_BUF_SIZE; + return data; +} + void lidar_update(lidar_data* lidar) { - if (!lidar_update_flag) - return; + static uint8_t frame[USART3_FRAME_SIZE]; + static uint8_t index = 0; - if (!usart3_frame_ready) - return; - - usart3_frame_ready = 0; - - for (uint8_t i = 0; i < USART3_FRAME_SIZE; ++i) usart3_checksum += buff_data[i]; - - if (buffer.checksum != usart3_checksum) - return; - - lidar->distance = buffer.distance_l | (buffer.distance_h << 8); - lidar->strength = buffer.strength_l | (buffer.strength_h << 8); - lidar->temperature = buffer.temp_l | (buffer.temp_h << 8); + while(usart_available()) + { + uint8_t c = usart_read(); + + frame[index++] = c; + + if (index == USART3_FRAME_SIZE) + { + uint8_t checksum = 0; + for (uint8_t i = 0; i < USART3_FRAME_SIZE - 1; ++i) checksum += frame[i]; + + if (checksum == frame[USART3_FRAME_SIZE - 1]) + { + lidar->distance = frame[2] | (frame[3] << 8); + lidar->strength = frame[4] | (frame[5] << 8); + lidar->temperature = frame[6] | (frame[7] << 8); + } + + index = 0; + } + } } void lidar_i2c2_init() diff --git a/Source/main.c b/Source/main.c index edbf6be..e2637f6 100644 --- a/Source/main.c +++ b/Source/main.c @@ -39,7 +39,7 @@ int main(void) motors_init();*/ lidar_init(); - lidar_tim7_init(); + // lidar_tim7_init(); while (1) {