From 6c9c1ba35b1cca4fc17a07b780f0bd4fb83de755 Mon Sep 17 00:00:00 2001 From: Radzhab Bisultanov Date: Fri, 20 Feb 2026 12:07:24 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=B8=D0=BD=D1=86=D0=B8=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20GPIOB11=20=D0=B8=20USART?= =?UTF-8?q?3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/BSP/Inc/lidar.h | 24 +++++++++++++++++++ Source/BSP/Src/lidar.c | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/Source/BSP/Inc/lidar.h b/Source/BSP/Inc/lidar.h index e69de29..9777017 100644 --- a/Source/BSP/Inc/lidar.h +++ b/Source/BSP/Inc/lidar.h @@ -0,0 +1,24 @@ +#pragma once + +#ifndef LIDAR_H +#define LIDAR_H + +#include "stm32g431xx.h" + +typedef struct +{ + uint8_t header1; // 0x59 + uint8_t header2; // 0x59 + uint8_t distance_l; // cm + uint8_t distance_h; // cm + uint8_t strength_l; + uint8_t strength_h; + uint8_t temp_l; + uint8_t temp_h; + uint8_t checksum; +} lidar_data; + +void lidar_init(); +void lidar_update(); + +#endif \ No newline at end of file diff --git a/Source/BSP/Src/lidar.c b/Source/BSP/Src/lidar.c index e69de29..d50b85b 100644 --- a/Source/BSP/Src/lidar.c +++ b/Source/BSP/Src/lidar.c @@ -0,0 +1,54 @@ +#include "lidar.h" + +void lidar_init() +{ + RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN; + + // port 11 alt func mode + GPIOB->MODER &= ~(3 << (11 * 2)); + GPIOB->MODER |= 2 << (11 * 2); + + // set AF7 on AFRegister for GPIOB11 + GPIOB->AFR[1] &= ~(0xF << 12); + GPIOB->AFR[1] |= 7 << 12; + + // very high speed + GPIOB->OSPEEDR |= 3 << (11 * 2); + + // pull-up + GPIOB->PUPDR &= ~(3 << (11 * 2)); + GPIOB->PUPDR |= 1 << (11 * 2); + + // SYSCLK selected as USART3 clock + RCC->CCIPR &= ~(RCC_CCIPR_USART3SEL); + RCC->CCIPR |= 1 << RCC_CCIPR_USART3SEL_Pos; + RCC->APB1ENR1 |= RCC_APB1ENR1_USART3EN; + + USART3->CR1 = 0; + USART3->CR2 = 0; + USART3->CR3 = 0; + + USART3->BRR = 16000000UL / 115200UL; + + // parity control disable + USART3->CR1 &= ~(USART_CR1_PCE); + + // word length 8 bit + USART3->CR1 &= ~USART_CR1_M1 & ~USART_CR1_M0; + + // 1 stop bit + USART3->CR2 &= ~USART_CR2_STOP; + + // receiver enable + // interrupt generated whenever ORE = 1 or RXNE = 1 + USART3->CR1 |= USART_CR1_RE | USART_CR1_RXNEIE; + + // overrun disable + USART3->CR3 |= USART_CR3_OVRDIS; + + // USART3 enable + USART3->CR1 |= USART_CR1_UE; + + // Interrupt enable + NVIC_EnableIRQ(USART3_IRQn); +} \ No newline at end of file