From 941d3c44bbe2062ca71c6a6557650884ea0a3ae7 Mon Sep 17 00:00:00 2001 From: Radzhab Bisultanov Date: Thu, 9 Apr 2026 14:38:38 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D0=BA=D0=B0=D0=B7=20=D0=BE=D1=82?= =?UTF-8?q?=20=D0=BE=D1=87=D0=B5=D1=80=D0=B5=D0=B4=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/BSP/Inc/imu.h | 15 ++-- Source/BSP/Src/imu.c | 112 +++++++++++++++-------------- Source/Core/Src/system_stm32g4xx.c | 4 ++ Source/INS/IRS.c | 2 +- Source/main.c | 2 - 5 files changed, 72 insertions(+), 63 deletions(-) diff --git a/Source/BSP/Inc/imu.h b/Source/BSP/Inc/imu.h index fff886e..951c5ee 100644 --- a/Source/BSP/Inc/imu.h +++ b/Source/BSP/Inc/imu.h @@ -24,11 +24,11 @@ #define ACCEL_FCHOICE_ON 1 #define ACCEL_FCHOICE_OFF 0 -/*static volatile uint8_t i2c_busy = 0; +static volatile uint8_t i2c_busy = 0; static uint8_t i2c_buf[16]; static uint8_t i2c_len = 0; static uint8_t i2c_reg = 0; -static uint8_t i2c_addr = 0;*/ +static uint8_t i2c_addr = 0; typedef struct { @@ -36,7 +36,7 @@ typedef struct int16_t gx, gy, gz; // lsb } imu_raw_t; -typedef struct I2C_Request +/*typedef struct I2C_Request { uint8_t addr; uint8_t reg; @@ -49,9 +49,9 @@ typedef struct I2C_Request } I2C_Request; static I2C_Request* i2c_head = 0; -static uint8_t i2c_busy = 0; +static uint8_t i2c_busy = 0;*/ -//static void (*i2c_callback)(uint8_t* buf) = 0; +static void (*i2c_callback)(uint8_t* buf) = 0; void imu_pow_init(); @@ -65,8 +65,9 @@ void imu_tim6_init(const uint16_t freq); void i2c_read(uint8_t addr, uint8_t reg, uint8_t* buf, uint8_t len); -void i2c_enqueue(I2C_Request* req); -void i2c_start_next(); +/*void i2c_enqueue(I2C_Request* req); +void i2c_start_next();*/ + void i2c_read_async(uint8_t addr, uint8_t reg, uint8_t len, void (*cb)(uint8_t*)); void i2c_write(uint8_t addr, uint8_t reg, uint8_t data); diff --git a/Source/BSP/Src/imu.c b/Source/BSP/Src/imu.c index f00ab0a..da06f64 100644 --- a/Source/BSP/Src/imu.c +++ b/Source/BSP/Src/imu.c @@ -1,8 +1,8 @@ #include "imu.h" -static I2C_Request* current_req = 0; +/*static I2C_Request* current_req = 0; static uint8_t i2c_buf[16]; -static uint8_t i2c_index = 0; +static uint8_t i2c_index = 0;*/ void imu_pow_init() { @@ -46,13 +46,6 @@ void i2c1_init() I2C1->TIMINGR = 0x10802D9BUL; // 400 kHz @ 16 MHz I2C1->CR1 |= I2C_CR1_PE; - - I2C1->CR1 |= I2C_CR1_TXIE | - I2C_CR1_RXIE | - I2C_CR1_TCIE | - I2C_CR1_STOPIE; - - NVIC_EnableIRQ(I2C1_EV_IRQn); } void imu_init() @@ -114,7 +107,7 @@ void i2c_read(uint8_t addr, uint8_t reg, uint8_t* buf, uint8_t len) I2C1->ICR |= I2C_ICR_STOPCF; } -void i2c_enqueue(I2C_Request* req) +/*void i2c_enqueue(I2C_Request* req) { req->next = 0; @@ -159,21 +152,30 @@ void i2c_start_next() I2C1->CR2 = (current_req->addr << 1) | (1 << I2C_CR2_NBYTES_Pos) | I2C_CR2_START; -} +}*/ void i2c_read_async(uint8_t addr, uint8_t reg, uint8_t len, void (*cb)(uint8_t*)) { - static I2C_Request req; + if (i2c_busy) return; // ❗ ВАЖНО - static uint8_t buf[16]; + i2c_busy = 1; - req.addr = addr; - req.reg = reg; - req.buf = buf; - req.len = len; - req.callback = cb; + i2c_addr = addr; + i2c_reg = reg; + i2c_len = len; + i2c_callback = cb; + + I2C1->CR1 |= I2C_CR1_TXIE | + I2C_CR1_RXIE | + I2C_CR1_TCIE | + I2C_CR1_STOPIE; - i2c_enqueue(&req); + NVIC_EnableIRQ(I2C1_EV_IRQn); + + // старт записи регистра + I2C1->CR2 = (addr << 1) | + (1 << I2C_CR2_NBYTES_Pos) | + I2C_CR2_START; } void i2c_write(uint8_t addr, uint8_t reg, uint8_t data) @@ -195,47 +197,51 @@ void i2c_write(uint8_t addr, uint8_t reg, uint8_t data) void I2C1_EV_IRQHandler() { - uint32_t isr = I2C1->ISR; + uint32_t isr = I2C1->ISR; - // --- TXIS --- - if (isr & I2C_ISR_TXIS) - { - I2C1->TXDR = current_req->reg; - } + // TXIS — отправляем регистр + if (isr & I2C_ISR_TXIS) + { + I2C1->TXDR = i2c_reg; + } - // --- TC --- - else if (isr & I2C_ISR_TC) - { - // запускаем чтение - I2C1->CR2 = (current_req->addr << 1) | - I2C_CR2_RD_WRN | - (current_req->len << I2C_CR2_NBYTES_Pos) | - I2C_CR2_AUTOEND | - I2C_CR2_START; - } + // TC — запускаем чтение + else if (isr & I2C_ISR_TC) + { + I2C1->CR2 = (i2c_addr << 1) | + I2C_CR2_RD_WRN | + (i2c_len << I2C_CR2_NBYTES_Pos) | + I2C_CR2_AUTOEND | + I2C_CR2_START; + } - // --- RXNE --- - else if (isr & I2C_ISR_RXNE) - { - i2c_buf[i2c_index++] = I2C1->RXDR; - } + // RXNE — читаем байты + else if (isr & I2C_ISR_RXNE) + { + static uint8_t index = 0; + i2c_buf[index++] = I2C1->RXDR; - // --- STOP --- - else if (isr & I2C_ISR_STOPF) - { - I2C1->ICR |= I2C_ICR_STOPCF; + if (index >= i2c_len) + index = 0; + } - // копируем данные - for (uint8_t i = 0; i < current_req->len; i++) - current_req->buf[i] = i2c_buf[i]; + // STOP — завершение + else if (isr & I2C_ISR_STOPF) + { + I2C1->ICR |= I2C_ICR_STOPCF; - // callback - if (current_req->callback) - current_req->callback(current_req->buf); + i2c_busy = 0; + + I2C1->CR1 |= I2C_CR1_TXIE | + I2C_CR1_RXIE | + I2C_CR1_TCIE | + I2C_CR1_STOPIE; - // следующий запрос - i2c_start_next(); - } + NVIC_EnableIRQ(I2C1_EV_IRQn); + + if (i2c_callback) + i2c_callback(i2c_buf); + } } void imu_read_raw(imu_raw_t* data) diff --git a/Source/Core/Src/system_stm32g4xx.c b/Source/Core/Src/system_stm32g4xx.c index 68ea500..1ad655f 100644 --- a/Source/Core/Src/system_stm32g4xx.c +++ b/Source/Core/Src/system_stm32g4xx.c @@ -283,3 +283,7 @@ void SystemCoreClockUpdate(void) */ + + + + diff --git a/Source/INS/IRS.c b/Source/INS/IRS.c index f0f49b6..78538bb 100644 --- a/Source/INS/IRS.c +++ b/Source/INS/IRS.c @@ -28,7 +28,7 @@ void IRS_update(IRS* irs, float dt) // /gyro update // accel update - //restoreQuat(irs); + // restoreQuat(irs); // /accel update } diff --git a/Source/main.c b/Source/main.c index b58fdfc..b771f7c 100644 --- a/Source/main.c +++ b/Source/main.c @@ -115,8 +115,6 @@ void imu_callback(uint8_t* buf) euler = QuatToEuler(&irs.q); } -static uint8_t div = 0; - void TIM6_DAC_IRQHandler() { if (TIM6->SR & TIM_SR_UIF)