Compare commits

..

4 Commits

Author SHA1 Message Date
Radzhab Bisultanov
2b3e4129e8 Реализована функция lidar_update(), обновляющая данные 2026-02-20 15:16:59 +03:00
Radzhab Bisultanov
a6f47a2e73 Реализован обработчик прерывания USART3 2026-02-20 14:51:25 +03:00
Radzhab Bisultanov
6c9c1ba35b Реализована инциализация GPIOB11 и USART3 2026-02-20 12:07:24 +03:00
Radzhab Bisultanov
f9b7277a33 Наведён порядок в коде 2026-02-18 14:23:01 +03:00
22 changed files with 782 additions and 928 deletions

View File

@@ -8,7 +8,6 @@
#define REG_BANK_SEL 0x7F
#define REG_GYRO_CONFIG_1 0x01
#define REG_ACCEL_CONFIG 0x14
#define IMU_RATE_HZ 1000 // 1 ms
#define IMU_DT 0.002f // 2 ms
typedef struct
@@ -17,17 +16,21 @@ typedef struct
int16_t gx, gy, gz; // lsb
} imu_raw_t;
void imu_pow_init();
void i2c_gpio_init();
void i2c1_init();
void icm_init();
void imu_init();
void imu_tim6_init();
void i2c_read(uint8_t addr, uint8_t reg, uint8_t* buf, uint8_t len);
void i2c_write(uint8_t addr, uint8_t reg, uint8_t data);
void icm_read_raw(imu_raw_t* data);
void imu_read_raw(imu_raw_t* data);
static void i2c_wait_idle(I2C_TypeDef* i2c);

View File

@@ -0,0 +1,36 @@
#pragma once
#ifndef LIDAR_H
#define LIDAR_H
#include "stm32g431xx.h"
#define USART3_START_BYTE 0x59
#define USART3_FRAME_SIZE 9
#define LIDAR_MIN_DIST 0.01f
#define LIDAR_MAX_DIST 40.0f
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_buf;
typedef struct
{
uint16_t distance; // meters
uint16_t strength;
uint16_t temperature;
} lidar_data;
void lidar_init();
void lidar_update(lidar_data* lidar);
#endif

View File

@@ -1,5 +1,15 @@
#include "imu.h"
void imu_pow_init()
{
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOCEN;
GPIOC->MODER &= ~(3 << (13 * 2));
GPIOC->MODER |= 1 << (13 * 2);
GPIOC->OTYPER &= ~(1 << 13);
GPIOC->PUPDR &= ~(3 << (13 * 2));
GPIOC->BSRR = 1 << (13 + 16);
}
void i2c_gpio_init()
{
// enable GPIOB clock
@@ -32,7 +42,7 @@ void i2c1_init()
I2C1->CR1 |= I2C_CR1_PE;
}
void icm_init()
void imu_init()
{
// select bank 0
i2c_write(ICM_ADDR, REG_BANK_SEL, ~(3 << 4));
@@ -53,6 +63,19 @@ void icm_init()
i2c_write(ICM_ADDR, REG_BANK_SEL, ~(3 << 4));
}
void imu_tim6_init()
{
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM6EN;
TIM6->PSC = 16000 - 1; // 16 MHz / 16000 = 1000 Hz (1 ms)
TIM6->ARR = 2 - 1; // 2 ms
TIM6->DIER |= TIM_DIER_UIE; // interrupt enable
TIM6->CR1 |= TIM_CR1_CEN; // counter enable
NVIC_EnableIRQ(TIM6_DAC_IRQn);
}
void i2c_read(uint8_t addr, uint8_t reg, uint8_t* buf, uint8_t len)
{
i2c_wait_idle(I2C1);
@@ -94,7 +117,7 @@ void i2c_write(uint8_t addr, uint8_t reg, uint8_t data)
I2C1->CR2 |= I2C_CR2_STOP;
}
void icm_read_raw(imu_raw_t* data)
void imu_read_raw(imu_raw_t* data)
{
uint8_t buf[12];

View File

@@ -33,7 +33,7 @@ void imu_read_scaled(imu_scaled_t* out)
{
static imu_raw_t raw;
icm_read_raw(&raw);
imu_read_raw(&raw);
out->ax = raw.ax / ACCEL_SENS_SCALE_FACTOR - accel_bias_x;
out->ay = raw.ay / ACCEL_SENS_SCALE_FACTOR - accel_bias_y;
@@ -68,7 +68,7 @@ void imu_calibrate()
for (uint16_t i = 0; i < samples; ++i)
{
icm_read_raw(&imu);
imu_read_raw(&imu);
sum_ax += imu.ax / ACCEL_SENS_SCALE_FACTOR;
sum_ay += imu.ay / ACCEL_SENS_SCALE_FACTOR;

View File

@@ -0,0 +1,99 @@
#include "lidar.h"
volatile uint8_t usart3_index = 0;
volatile uint8_t usart3_checksum = 0;
volatile uint8_t usart3_frame_ready = 0;
static lidar_data_buf buffer;
static uint8_t* buff_data = (uint8_t*)&buffer;
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);
}
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;
}
}
}
void lidar_update(lidar_data* lidar)
{
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);
}

View File

@@ -21,7 +21,6 @@ void receiver_gpio_init()
// pull-up
GPIOA->PUPDR &= ~(3 << (3 * 2));
GPIOA->PUPDR |= 1 << (3 * 2);
}
void receiver_lpuart_clock_init()
@@ -167,39 +166,3 @@ int8_t bool_mapping_gt(int16_t x, int16_t boundary)
{
return x >= boundary;
}
//------------------------------------------------------------------------------
void toggle_led()
{
if (GPIOA->ODR & (1 << 15))
{
GPIOA->BSRR = 1 << (15 + 16);
}
else
{
GPIOA->BSRR = 1 << 15;
}
}
void led_init(void)
{
/* Enable GPIOA clock */
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
/* PA15 -> Output mode */
GPIOA->MODER &= ~(3U << (15 * 2));
GPIOA->MODER |= (1U << (15 * 2));
/* Push-pull */
GPIOA->OTYPER &= ~(1U << 15);
/* Low speed (?????????? ??? LED) */
GPIOA->OSPEEDR &= ~(3U << (15 * 2));
/* No pull-up / pull-down */
GPIOA->PUPDR &= ~(3U << (15 * 2));
/* Start with LED OFF */
GPIOA->BSRR = (1U << (15 + 16));
}

View File

@@ -1,8 +0,0 @@
#ifndef TIMER_H
#define TIMER_H
#include "stm32g431xx.h"
void tim6_init();
#endif

View File

@@ -1,166 +0,0 @@
/**
******************************************************************************
* @file Templates/Src/stm32g4xx_it.c
* @author MCD Application Team
* @brief Main Interrupt Service Routines.
* This file provides template for all exceptions handler and
* peripherals interrupt service routine.
******************************************************************************
* @attention
*
* Copyright (c) 2019 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32g4xx_it.h"
/** @addtogroup STM32G4xx_HAL_Examples
* @{
*/
/** @addtogroup Templates
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/******************************************************************************/
/* Cortex-M4 Processor Exceptions Handlers */
/******************************************************************************/
/**
* @brief This function handles NMI exception.
* @param None
* @retval None
*/
void NMI_Handler(void)
{
}
/**
* @brief This function handles Hard Fault exception.
* @param None
* @retval None
*/
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Memory Manage exception.
* @param None
* @retval None
*/
void MemManage_Handler(void)
{
/* Go to infinite loop when Memory Manage exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Bus Fault exception.
* @param None
* @retval None
*/
void BusFault_Handler(void)
{
/* Go to infinite loop when Bus Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Usage Fault exception.
* @param None
* @retval None
*/
void UsageFault_Handler(void)
{
/* Go to infinite loop when Usage Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles SVCall exception.
* @param None
* @retval None
*/
void SVC_Handler(void)
{
}
/**
* @brief This function handles Debug Monitor exception.
* @param None
* @retval None
*/
void DebugMon_Handler(void)
{
}
/**
* @brief This function handles PendSVC exception.
* @param None
* @retval None
*/
void PendSV_Handler(void)
{
}
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
}
/******************************************************************************/
/* STM32G4xx Peripherals Interrupt Handlers */
/* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
/* available peripheral interrupt handler's name please refer to the startup */
/* file (startup_stm32g4xxxx.s). */
/******************************************************************************/
/**
* @brief This function handles PPP interrupt request.
* @param None
* @retval None
*/
/*void PPP_IRQHandler(void)
{
}*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -1,49 +0,0 @@
/**
******************************************************************************
* @file Templates/Inc/stm32g4xx_it.h
* @author MCD Application Team
* @brief This file contains the headers of the interrupt handlers.
******************************************************************************
* @attention
*
* Copyright (c) 2019 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef STM32G4xx_IT_H
#define STM32G4xx_IT_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macros -----------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void NMI_Handler(void);
void HardFault_Handler(void);
void MemManage_Handler(void);
void BusFault_Handler(void);
void UsageFault_Handler(void);
void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
#ifdef __cplusplus
}
#endif
#endif /* STM32G4xx_IT_H */

View File

@@ -1,14 +0,0 @@
#include "timer.h"
void tim6_init()
{
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM6EN;
TIM6->PSC = 16000 - 1; // 16 MHz / 16000 = 1000 Hz (1 ms)
TIM6->ARR = 2 - 1; // 2 ms
TIM6->DIER |= TIM_DIER_UIE; // interrupt enable
TIM6->CR1 |= TIM_CR1_CEN; // counter enable
NVIC_EnableIRQ(TIM6_DAC_IRQn);
}

View File

@@ -1,7 +1,6 @@
#include "stm32g431xx.h"
#include "imu.h"
#include "imu_processing.h"
#include "timer.h"
#include "attitude.h"
#include "radio_receiver.h"
#include "motors.h"
@@ -20,23 +19,15 @@ void delay_ms(uint32_t ms);
{
__enable_irq();
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOCEN;
GPIOC->MODER &= ~(3 << (13 * 2));
GPIOC->MODER |= 1 << (13 * 2);
GPIOC->OTYPER &= ~(1 << 13);
GPIOC->PUPDR &= ~(3 << (13 * 2));
GPIOC->BSRR = 1 << (13 + 16);
delay_ms(200);
NVIC_SetPriority(TIM6_DAC_IRQn, 1);
NVIC_SetPriority(LPUART1_IRQn, 0);
imu_pow_init();
i2c_gpio_init();
i2c1_init();
icm_init();
imu_init();
imu_tim6_init();
imu_processing_init();
tim6_init();
imu_calibrate();

View File

@@ -2311,24 +2311,12 @@
<file>
<name>$PROJ_DIR$\Source\Core\Inc\system_stm32g4xx.h</name>
</file>
<file>
<name>$PROJ_DIR$\Source\Core\Inc\timer.h</name>
</file>
</group>
<group>
<name>Src</name>
<file>
<name>$PROJ_DIR$\Source\Core\Src\stm32g4xx_it.c</name>
</file>
<file>
<name>$PROJ_DIR$\Source\Core\Src\stm32g4xx_it.h</name>
</file>
<file>
<name>$PROJ_DIR$\Source\Core\Src\system_stm32g4xx.c</name>
</file>
<file>
<name>$PROJ_DIR$\Source\Core\Src\timer.c</name>
</file>
</group>
</group>
<group>

View File

@@ -3496,24 +3496,12 @@
<file>
<name>$PROJ_DIR$\Source\Core\Inc\system_stm32g4xx.h</name>
</file>
<file>
<name>$PROJ_DIR$\Source\Core\Inc\timer.h</name>
</file>
</group>
<group>
<name>Src</name>
<file>
<name>$PROJ_DIR$\Source\Core\Src\stm32g4xx_it.c</name>
</file>
<file>
<name>$PROJ_DIR$\Source\Core\Src\stm32g4xx_it.h</name>
</file>
<file>
<name>$PROJ_DIR$\Source\Core\Src\system_stm32g4xx.c</name>
</file>
<file>
<name>$PROJ_DIR$\Source\Core\Src\timer.c</name>
</file>
</group>
</group>
<group>