From 385aa66ffc8d11a225d5e26069ad0fdc63beb5ef Mon Sep 17 00:00:00 2001 From: Radzhab Bisultanov Date: Thu, 26 Feb 2026 17:01:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20I2C=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B8=D0=B4=D0=B0=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/BSP/Inc/lidar.h | 4 ++++ Source/BSP/Src/lidar.c | 34 +++++++++++++++++++++++++++++++--- Source/main.c | 3 ++- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Source/BSP/Inc/lidar.h b/Source/BSP/Inc/lidar.h index 40c4e8e..9930b77 100644 --- a/Source/BSP/Inc/lidar.h +++ b/Source/BSP/Inc/lidar.h @@ -31,6 +31,10 @@ typedef struct } lidar_data; void lidar_init(); +void USART3_IRQHandler(); void lidar_update(lidar_data* lidar); +void lidar_i2c2_init(); +void lidar_i2c2_update(); + #endif \ No newline at end of file diff --git a/Source/BSP/Src/lidar.c b/Source/BSP/Src/lidar.c index 231cd7c..9c54be4 100644 --- a/Source/BSP/Src/lidar.c +++ b/Source/BSP/Src/lidar.c @@ -37,7 +37,7 @@ void lidar_init() USART3->BRR = 16000000UL / 115200UL; // parity control disable - USART3->CR1 &= ~(USART_CR1_PCE); + USART3->CR1 &= ~USART_CR1_PCE; // word length 8 bit USART3->CR1 &= ~USART_CR1_M1 & ~USART_CR1_M0; @@ -50,7 +50,7 @@ void lidar_init() USART3->CR1 |= USART_CR1_RE | USART_CR1_RXNEIE; // overrun disable - USART3->CR3 |= USART_CR3_OVRDIS; + // USART3->CR3 |= USART_CR3_OVRDIS; // USART3 enable USART3->CR1 |= USART_CR1_UE; @@ -59,8 +59,10 @@ void lidar_init() NVIC_EnableIRQ(USART3_IRQn); } +uint32_t test; void USART3_IRQHandler() { + ++test; if (USART3->ISR & USART_ISR_RXNE) { uint8_t b = USART3->RDR; @@ -96,4 +98,30 @@ void lidar_update(lidar_data* lidar) 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); -} \ No newline at end of file +} + +void lidar_i2c2_init() +{ + RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN; + + GPIOA->MODER &= ~(3 << (8 * 2)) & ~(3 << (9 * 2)); + GPIOA->MODER |= 2 << (8 * 2) | 2 << (9 * 2); // alt func mode + + GPIOA->AFR[1] &= ~(0xF << 0) & ~(0xF << 4); + GPIOA->AFR[1] |= 4 << 0 | 4 << 4; // AF4 + + GPIOA->OTYPER |= 1 << 8 | 1 << 9; // open-drain + + GPIOA->PUPDR &= ~(3 << (8 * 2)) & ~(3 << (9 * 2)); + GPIOA->PUPDR |= 1 << (8 * 2) | 1 << (9 * 2); // pull-up + + RCC->APB1ENR1 |= RCC_APB1ENR1_I2C2EN; // enable I2C2 + I2C2->TIMINGR = 0x00303D5B; // 400 kHz @ 16 MHz + I2C2->CR1 |= I2C_CR1_PE; +} + + + + + + diff --git a/Source/main.c b/Source/main.c index b12f4d0..fa18086 100644 --- a/Source/main.c +++ b/Source/main.c @@ -21,7 +21,8 @@ int main(void) { __enable_irq(); - NVIC_SetPriority(TIM6_DAC_IRQn, 1); + NVIC_SetPriority(TIM6_DAC_IRQn, 2); + NVIC_SetPriority(USART3_IRQn, 1); NVIC_SetPriority(LPUART1_IRQn, 0); imu_pow_init();