更新项目结构,修改自述文件
This commit is contained in:
87
00-Driver/BMI088Middleware.c
Normal file
87
00-Driver/BMI088Middleware.c
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "BMI088Middleware.h"
|
||||
#include "main.h"
|
||||
|
||||
extern SPI_HandleTypeDef hspi1;
|
||||
|
||||
void BMI088_GPIO_init(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void BMI088_com_init(void)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
void BMI088_delay_ms(uint16_t ms)
|
||||
{
|
||||
while(ms--)
|
||||
{
|
||||
BMI088_delay_us(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void BMI088_delay_us(uint16_t us)
|
||||
{
|
||||
|
||||
uint32_t ticks = 0;
|
||||
uint32_t told = 0;
|
||||
uint32_t tnow = 0;
|
||||
uint32_t tcnt = 0;
|
||||
uint32_t reload = 0;
|
||||
reload = SysTick->LOAD;
|
||||
ticks = us * 168;
|
||||
told = SysTick->VAL;
|
||||
while (1)
|
||||
{
|
||||
tnow = SysTick->VAL;
|
||||
if (tnow != told)
|
||||
{
|
||||
if (tnow < told)
|
||||
{
|
||||
tcnt += told - tnow;
|
||||
}
|
||||
else
|
||||
{
|
||||
tcnt += reload - tnow + told;
|
||||
}
|
||||
told = tnow;
|
||||
if (tcnt >= ticks)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void BMI088_ACCEL_NS_L(void)
|
||||
{
|
||||
HAL_GPIO_WritePin(CS1_ACCEL_GPIO_Port, CS1_ACCEL_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
void BMI088_ACCEL_NS_H(void)
|
||||
{
|
||||
HAL_GPIO_WritePin(CS1_ACCEL_GPIO_Port, CS1_ACCEL_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
void BMI088_GYRO_NS_L(void)
|
||||
{
|
||||
HAL_GPIO_WritePin(CS1_GYRO_GPIO_Port, CS1_GYRO_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
void BMI088_GYRO_NS_H(void)
|
||||
{
|
||||
HAL_GPIO_WritePin(CS1_GYRO_GPIO_Port, CS1_GYRO_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
uint8_t BMI088_read_write_byte(uint8_t txdata)
|
||||
{
|
||||
uint8_t rx_data;
|
||||
HAL_SPI_TransmitReceive(&hspi1, &txdata, &rx_data, 1, 1000);
|
||||
return rx_data;
|
||||
}
|
||||
|
||||
20
00-Driver/BMI088Middleware.h
Normal file
20
00-Driver/BMI088Middleware.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef BMI088MIDDLEWARE_H
|
||||
#define BMI088MIDDLEWARE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define BMI088_USE_SPI
|
||||
|
||||
void BMI088_GPIO_init(void);
|
||||
void BMI088_com_init(void);
|
||||
void BMI088_delay_ms(uint16_t ms);
|
||||
void BMI088_delay_us(uint16_t us);
|
||||
|
||||
void BMI088_ACCEL_NS_L(void);
|
||||
void BMI088_ACCEL_NS_H(void);
|
||||
|
||||
void BMI088_GYRO_NS_L(void);
|
||||
void BMI088_GYRO_NS_H(void);
|
||||
|
||||
uint8_t BMI088_read_write_byte(uint8_t reg);
|
||||
#endif
|
||||
243
00-Driver/BMI088driver.c
Normal file
243
00-Driver/BMI088driver.c
Normal file
@@ -0,0 +1,243 @@
|
||||
#include "BMI088driver.h"
|
||||
#include "BMI088reg.h"
|
||||
#include "BMI088Middleware.h"
|
||||
|
||||
|
||||
float BMI088_ACCEL_SEN = BMI088_ACCEL_3G_SEN;
|
||||
float BMI088_GYRO_SEN = BMI088_GYRO_2000_SEN;
|
||||
//加速度计读写
|
||||
#define BMI088_accel_write_single_reg(reg, data) \
|
||||
{ \
|
||||
BMI088_ACCEL_NS_L(); \
|
||||
BMI088_write_single_reg((reg), (data)); \
|
||||
BMI088_ACCEL_NS_H(); \
|
||||
}
|
||||
#define BMI088_accel_read_single_reg(reg, data) \
|
||||
{ \
|
||||
BMI088_ACCEL_NS_L(); \
|
||||
BMI088_read_write_byte((reg) | 0x80); \
|
||||
BMI088_read_write_byte(0x55); \
|
||||
(data) = BMI088_read_write_byte(0x55); \
|
||||
BMI088_ACCEL_NS_H(); \
|
||||
}
|
||||
//#define BMI088_accel_write_muli_reg( reg, data, len) { BMI088_ACCEL_NS_L(); BMI088_write_muli_reg(reg, data, len); BMI088_ACCEL_NS_H(); }
|
||||
#define BMI088_accel_read_muli_reg(reg, data, len) \
|
||||
{ \
|
||||
BMI088_ACCEL_NS_L(); \
|
||||
BMI088_read_write_byte((reg) | 0x80); \
|
||||
BMI088_read_muli_reg(reg, data, len); \
|
||||
BMI088_ACCEL_NS_H(); \
|
||||
}
|
||||
//角速度寄存器读写函数
|
||||
#define BMI088_gyro_write_single_reg(reg, data) \
|
||||
{ \
|
||||
BMI088_GYRO_NS_L(); \
|
||||
BMI088_write_single_reg((reg), (data)); \
|
||||
BMI088_GYRO_NS_H(); \
|
||||
}
|
||||
#define BMI088_gyro_read_single_reg(reg, data) \
|
||||
{ \
|
||||
BMI088_GYRO_NS_L(); \
|
||||
BMI088_read_single_reg((reg), &(data)); \
|
||||
BMI088_GYRO_NS_H(); \
|
||||
}
|
||||
//#define BMI088_gyro_write_muli_reg( reg, data, len) { BMI088_GYRO_NS_L(); BMI088_write_muli_reg( ( reg ), ( data ), ( len ) ); BMI088_GYRO_NS_H(); }
|
||||
#define BMI088_gyro_read_muli_reg(reg, data, len) \
|
||||
{ \
|
||||
BMI088_GYRO_NS_L(); \
|
||||
BMI088_read_muli_reg((reg), (data), (len)); \
|
||||
BMI088_GYRO_NS_H(); \
|
||||
}
|
||||
|
||||
static void BMI088_write_single_reg(uint8_t reg, uint8_t data);
|
||||
static void BMI088_read_single_reg(uint8_t reg, uint8_t *return_data);
|
||||
//static void BMI088_write_muli_reg(uint8_t reg, uint8_t* buf, uint8_t len );
|
||||
static void BMI088_read_muli_reg(uint8_t reg, uint8_t *buf, uint8_t len);
|
||||
|
||||
//寄存器表
|
||||
static uint8_t write_BMI088_accel_reg_data_error[BMI088_WRITE_ACCEL_REG_NUM][3] =
|
||||
{
|
||||
{BMI088_ACC_PWR_CTRL, BMI088_ACC_ENABLE_ACC_ON, BMI088_ACC_PWR_CTRL_ERROR},
|
||||
{BMI088_ACC_PWR_CONF, BMI088_ACC_PWR_ACTIVE_MODE, BMI088_ACC_PWR_CONF_ERROR},
|
||||
{BMI088_ACC_CONF, BMI088_ACC_NORMAL| BMI088_ACC_800_HZ | BMI088_ACC_CONF_MUST_Set, BMI088_ACC_CONF_ERROR},
|
||||
{BMI088_ACC_RANGE, BMI088_ACC_RANGE_3G, BMI088_ACC_RANGE_ERROR},
|
||||
{BMI088_INT1_IO_CTRL, BMI088_ACC_INT1_IO_ENABLE | BMI088_ACC_INT1_GPIO_PP | BMI088_ACC_INT1_GPIO_LOW, BMI088_INT1_IO_CTRL_ERROR},
|
||||
{BMI088_INT_MAP_DATA, BMI088_ACC_INT1_DRDY_INTERRUPT, BMI088_INT_MAP_DATA_ERROR}
|
||||
|
||||
};
|
||||
|
||||
static uint8_t write_BMI088_gyro_reg_data_error[BMI088_WRITE_GYRO_REG_NUM][3] =
|
||||
{
|
||||
{BMI088_GYRO_RANGE, BMI088_GYRO_2000, BMI088_GYRO_RANGE_ERROR},
|
||||
{BMI088_GYRO_BANDWIDTH, BMI088_GYRO_1000_116_HZ | BMI088_GYRO_BANDWIDTH_MUST_Set, BMI088_GYRO_BANDWIDTH_ERROR},
|
||||
{BMI088_GYRO_LPM1, BMI088_GYRO_NORMAL_MODE, BMI088_GYRO_LPM1_ERROR},
|
||||
{BMI088_GYRO_CTRL, BMI088_DRDY_ON, BMI088_GYRO_CTRL_ERROR},
|
||||
{BMI088_GYRO_INT3_INT4_IO_CONF, BMI088_GYRO_INT3_GPIO_PP | BMI088_GYRO_INT3_GPIO_LOW, BMI088_GYRO_INT3_INT4_IO_CONF_ERROR},
|
||||
{BMI088_GYRO_INT3_INT4_IO_MAP, BMI088_GYRO_DRDY_IO_INT3, BMI088_GYRO_INT3_INT4_IO_MAP_ERROR}
|
||||
|
||||
};
|
||||
//初始化函数
|
||||
uint8_t BMI088_init(void)
|
||||
{
|
||||
uint8_t error = BMI088_NO_ERROR;
|
||||
// GPIO and SPI Init .
|
||||
BMI088_GPIO_init();
|
||||
BMI088_com_init();
|
||||
|
||||
error |= bmi088_accel_init();
|
||||
error |= bmi088_gyro_init();
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
unsigned char bmi088_accel_init(void)
|
||||
{
|
||||
uint8_t res = 0;
|
||||
uint8_t write_reg_num = 0;
|
||||
|
||||
//check commiunication
|
||||
BMI088_accel_read_single_reg(BMI088_ACC_CHIP_ID, res);
|
||||
BMI088_delay_us(BMI088_COM_WAIT_SENSOR_TIME);
|
||||
BMI088_accel_read_single_reg(BMI088_ACC_CHIP_ID, res);
|
||||
BMI088_delay_us(BMI088_COM_WAIT_SENSOR_TIME);
|
||||
|
||||
//accel software reset
|
||||
BMI088_accel_write_single_reg(BMI088_ACC_SOFTRESET, BMI088_ACC_SOFTRESET_VALUE);
|
||||
BMI088_delay_ms(BMI088_LONG_DELAY_TIME);
|
||||
|
||||
//check commiunication is normal after reset
|
||||
BMI088_accel_read_single_reg(BMI088_ACC_CHIP_ID, res);
|
||||
BMI088_delay_us(BMI088_COM_WAIT_SENSOR_TIME);
|
||||
BMI088_accel_read_single_reg(BMI088_ACC_CHIP_ID, res);
|
||||
BMI088_delay_us(BMI088_COM_WAIT_SENSOR_TIME);
|
||||
|
||||
// check the "who am I"
|
||||
if (res != BMI088_ACC_CHIP_ID_VALUE)
|
||||
{
|
||||
return BMI088_NO_SENSOR;
|
||||
}
|
||||
|
||||
//set accel sonsor config and check
|
||||
for (write_reg_num = 0; write_reg_num < BMI088_WRITE_ACCEL_REG_NUM; write_reg_num++)
|
||||
{
|
||||
|
||||
BMI088_accel_write_single_reg(write_BMI088_accel_reg_data_error[write_reg_num][0], write_BMI088_accel_reg_data_error[write_reg_num][1]);
|
||||
BMI088_delay_us(BMI088_COM_WAIT_SENSOR_TIME);
|
||||
|
||||
BMI088_accel_read_single_reg(write_BMI088_accel_reg_data_error[write_reg_num][0], res);
|
||||
BMI088_delay_us(BMI088_COM_WAIT_SENSOR_TIME);
|
||||
|
||||
if (res != write_BMI088_accel_reg_data_error[write_reg_num][1])
|
||||
{
|
||||
return write_BMI088_accel_reg_data_error[write_reg_num][2];
|
||||
}
|
||||
}
|
||||
return BMI088_NO_ERROR;
|
||||
}
|
||||
|
||||
unsigned char bmi088_gyro_init(void)
|
||||
{
|
||||
uint8_t write_reg_num = 0;
|
||||
uint8_t res = 0;
|
||||
|
||||
//check commiunication
|
||||
BMI088_gyro_read_single_reg(BMI088_GYRO_CHIP_ID, res);
|
||||
BMI088_delay_us(BMI088_COM_WAIT_SENSOR_TIME);
|
||||
BMI088_gyro_read_single_reg(BMI088_GYRO_CHIP_ID, res);
|
||||
BMI088_delay_us(BMI088_COM_WAIT_SENSOR_TIME);
|
||||
|
||||
//reset the gyro sensor
|
||||
BMI088_gyro_write_single_reg(BMI088_GYRO_SOFTRESET, BMI088_GYRO_SOFTRESET_VALUE);
|
||||
BMI088_delay_ms(BMI088_LONG_DELAY_TIME);
|
||||
//check commiunication is normal after reset
|
||||
BMI088_gyro_read_single_reg(BMI088_GYRO_CHIP_ID, res);
|
||||
BMI088_delay_us(BMI088_COM_WAIT_SENSOR_TIME);
|
||||
BMI088_gyro_read_single_reg(BMI088_GYRO_CHIP_ID, res);
|
||||
BMI088_delay_us(BMI088_COM_WAIT_SENSOR_TIME);
|
||||
|
||||
// check the "who am I"
|
||||
if (res != BMI088_GYRO_CHIP_ID_VALUE)
|
||||
{
|
||||
return BMI088_NO_SENSOR;
|
||||
}
|
||||
|
||||
//set gyro sonsor config and check
|
||||
for (write_reg_num = 0; write_reg_num < BMI088_WRITE_GYRO_REG_NUM; write_reg_num++)
|
||||
{
|
||||
|
||||
BMI088_gyro_write_single_reg(write_BMI088_gyro_reg_data_error[write_reg_num][0], write_BMI088_gyro_reg_data_error[write_reg_num][1]);
|
||||
BMI088_delay_us(BMI088_COM_WAIT_SENSOR_TIME);
|
||||
|
||||
BMI088_gyro_read_single_reg(write_BMI088_gyro_reg_data_error[write_reg_num][0], res);
|
||||
BMI088_delay_us(BMI088_COM_WAIT_SENSOR_TIME);
|
||||
|
||||
if (res != write_BMI088_gyro_reg_data_error[write_reg_num][1])
|
||||
{
|
||||
return write_BMI088_gyro_reg_data_error[write_reg_num][2];
|
||||
}
|
||||
}
|
||||
|
||||
return BMI088_NO_ERROR;
|
||||
}
|
||||
|
||||
//BMI088寄存器读写函数
|
||||
void BMI088_read(float gyro[3], float accel[3], float *temperate)
|
||||
{
|
||||
uint8_t buf[8] = {0, 0, 0, 0, 0, 0};
|
||||
int16_t bmi088_raw_temp;
|
||||
|
||||
BMI088_accel_read_muli_reg(BMI088_ACCEL_XOUT_L, buf, 6);
|
||||
|
||||
bmi088_raw_temp = (int16_t)((buf[1]) << 8) | buf[0];
|
||||
accel[0] = bmi088_raw_temp * BMI088_ACCEL_SEN;
|
||||
bmi088_raw_temp = (int16_t)((buf[3]) << 8) | buf[2];
|
||||
accel[1] = bmi088_raw_temp * BMI088_ACCEL_SEN;
|
||||
bmi088_raw_temp = (int16_t)((buf[5]) << 8) | buf[4];
|
||||
accel[2] = bmi088_raw_temp * BMI088_ACCEL_SEN;
|
||||
|
||||
BMI088_gyro_read_muli_reg(BMI088_GYRO_CHIP_ID, buf, 8);
|
||||
if(buf[0] == BMI088_GYRO_CHIP_ID_VALUE)
|
||||
{
|
||||
bmi088_raw_temp = (int16_t)((buf[3]) << 8) | buf[2];
|
||||
gyro[0] = bmi088_raw_temp * BMI088_GYRO_SEN;
|
||||
bmi088_raw_temp = (int16_t)((buf[5]) << 8) | buf[4];
|
||||
gyro[1] = bmi088_raw_temp * BMI088_GYRO_SEN;
|
||||
bmi088_raw_temp = (int16_t)((buf[7]) << 8) | buf[6];
|
||||
gyro[2] = bmi088_raw_temp * BMI088_GYRO_SEN;
|
||||
}
|
||||
BMI088_accel_read_muli_reg(BMI088_TEMP_M, buf, 2);
|
||||
|
||||
bmi088_raw_temp = (int16_t)((buf[0] << 3) | (buf[1] >> 5));
|
||||
|
||||
if (bmi088_raw_temp > 1023)
|
||||
{
|
||||
bmi088_raw_temp -= 2048;
|
||||
}
|
||||
|
||||
*temperate = bmi088_raw_temp * BMI088_TEMP_FACTOR + BMI088_TEMP_OFFSET;
|
||||
}
|
||||
|
||||
static void BMI088_write_single_reg(uint8_t reg, uint8_t data)
|
||||
{
|
||||
BMI088_read_write_byte(reg);
|
||||
BMI088_read_write_byte(data);
|
||||
}
|
||||
|
||||
static void BMI088_read_single_reg(uint8_t reg, uint8_t *return_data)
|
||||
{
|
||||
BMI088_read_write_byte(reg | 0x80);
|
||||
*return_data = BMI088_read_write_byte(0x55);
|
||||
}
|
||||
|
||||
static void BMI088_read_muli_reg(uint8_t reg, uint8_t *buf, uint8_t len)
|
||||
{
|
||||
BMI088_read_write_byte(reg | 0x80);
|
||||
|
||||
while (len != 0)
|
||||
{
|
||||
|
||||
*buf = BMI088_read_write_byte(0x55);
|
||||
buf++;
|
||||
len--;
|
||||
}
|
||||
}
|
||||
151
00-Driver/BMI088driver.h
Normal file
151
00-Driver/BMI088driver.h
Normal file
@@ -0,0 +1,151 @@
|
||||
#ifndef BMI088DRIVER_H
|
||||
#define BMI088DRIVER_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "main.h" // 包含 HAL 库等基础定义
|
||||
|
||||
// 温度计算参数(来自 BMI088 数据手册)
|
||||
#define BMI088_TEMP_FACTOR 0.125f // 温度分辨率:0.125°C/LSB
|
||||
#define BMI088_TEMP_OFFSET 23.0f // 温度偏移:0 LSB 对应 23°C
|
||||
|
||||
// 配置寄存器数量(用于初始化校验)
|
||||
#define BMI088_WRITE_ACCEL_REG_NUM 6 // 加速度计需配置的寄存器个数
|
||||
#define BMI088_WRITE_GYRO_REG_NUM 6 // 陀螺仪需配置的寄存器个数
|
||||
|
||||
// 数据就绪状态位(用于多传感器同步)
|
||||
#define BMI088_GYRO_DATA_READY_BIT 0 // 陀螺仪数据就绪标志位
|
||||
#define BMI088_ACCEL_DATA_READY_BIT 1 // 加速度计数据就绪标志位
|
||||
#define BMI088_ACCEL_TEMP_DATA_READY_BIT 2 // 加速度计温度数据就绪标志位
|
||||
|
||||
// 延时参数(单位:毫秒 / 微秒)
|
||||
#define BMI088_LONG_DELAY_TIME 80 // 软件复位后所需长延时(ms)
|
||||
#define BMI088_COM_WAIT_SENSOR_TIME 150 // 寄存器写入后等待传感器响应时间(us)
|
||||
|
||||
// I²C 地址(实际使用 SPI,此处可能为遗留定义或备用)
|
||||
// 注意:BMI088 通常通过 SPI 通信,I²C 地址在此驱动中未使用
|
||||
#define BMI088_ACCEL_IIC_ADDRESSE (0x18 << 1) // 加速度计 I²C 地址(7-bit 左移1位)
|
||||
#define BMI088_GYRO_IIC_ADDRESSE (0x68 << 1) // 陀螺仪 I²C 地址
|
||||
|
||||
// 量程选择(取消注释以切换量程,当前启用 3G / 2000°/s)
|
||||
// 注意:灵敏度(SEN)必须与量程匹配!
|
||||
#define BMI088_ACCEL_RANGE_3G
|
||||
// #define BMI088_ACCEL_RANGE_6G
|
||||
// #define BMI088_ACCEL_RANGE_12G
|
||||
// #define BMI088_ACCEL_RANGE_24G
|
||||
|
||||
#define BMI088_GYRO_RANGE_2000
|
||||
// #define BMI088_GYRO_RANGE_1000
|
||||
// #define BMI088_GYRO_RANGE_500
|
||||
// #define BMI088_GYRO_RANGE_250
|
||||
// #define BMI088_GYRO_RANGE_125
|
||||
|
||||
|
||||
// 灵敏度系数(单位转换:LSB → 物理单位)
|
||||
// 根据所选量程自动使用对应的 SEN 值
|
||||
// - 加速度单位:m/s² (注意:部分系统用 g,需确认)
|
||||
// - 角速度单位:rad/s (若需 °/s,需额外转换)
|
||||
|
||||
|
||||
// 加速度计灵敏度(m/s² per LSB)
|
||||
#define BMI088_ACCEL_3G_SEN 0.0008974358974f // ±3g 量程
|
||||
#define BMI088_ACCEL_6G_SEN 0.00179443359375f // ±6g
|
||||
#define BMI088_ACCEL_12G_SEN 0.0035888671875f // ±12g
|
||||
#define BMI088_ACCEL_24G_SEN 0.007177734375f // ±24g
|
||||
|
||||
// 陀螺仪灵敏度(rad/s per LSB)
|
||||
#define BMI088_GYRO_2000_SEN 0.00106526443603169529841533860381f // ±2000°/s
|
||||
#define BMI088_GYRO_1000_SEN 0.00053263221801584764920766930190693f // ±1000°/s
|
||||
#define BMI088_GYRO_500_SEN 0.00026631610900792382460383465095346f // ±500°/s
|
||||
#define BMI088_GYRO_250_SEN 0.00013315805450396191230191732547673f // ±250°/s
|
||||
#define BMI088_GYRO_125_SEN 0.000066579027251980956150958662738366f // ±125°/s
|
||||
|
||||
|
||||
// 原始数据结构体(直接从传感器读取的16位整型数据)
|
||||
// 使用 __packed 防止编译器对齐填充,确保内存布局紧凑
|
||||
|
||||
typedef __packed struct BMI088_RAW_DATA
|
||||
{
|
||||
uint8_t status; // 状态字节(如数据就绪标志)
|
||||
int16_t accel[3]; // 加速度原始值 [X, Y, Z]
|
||||
int16_t temp; // 温度原始值
|
||||
int16_t gyro[3]; // 陀螺仪原始值 [X, Y, Z]
|
||||
} bmi088_raw_data_t;
|
||||
|
||||
|
||||
// 物理数据结构体(转换后的浮点型物理量)
|
||||
|
||||
typedef struct BMI088_REAL_DATA
|
||||
{
|
||||
uint8_t status; // 状态字节
|
||||
float accel[3]; // 加速度 (m/s²)
|
||||
float temp; // 温度 (°C)
|
||||
float gyro[3]; // 角速度 (rad/s)
|
||||
float time; // 时间戳(秒,可由 HAL_GetTick() 转换)
|
||||
} bmi088_real_data_t;
|
||||
|
||||
|
||||
// 错误码定义(用于初始化返回值)
|
||||
// 高位用于标识模块,低位用于具体错误
|
||||
|
||||
enum
|
||||
{
|
||||
BMI088_NO_ERROR = 0x00, // 无错误
|
||||
|
||||
// 加速度计配置错误
|
||||
BMI088_ACC_PWR_CTRL_ERROR = 0x01,
|
||||
BMI088_ACC_PWR_CONF_ERROR = 0x02,
|
||||
BMI088_ACC_CONF_ERROR = 0x03,
|
||||
BMI088_ACC_SELF_TEST_ERROR = 0x04,
|
||||
BMI088_ACC_RANGE_ERROR = 0x05,
|
||||
BMI088_INT1_IO_CTRL_ERROR = 0x06,
|
||||
BMI088_INT_MAP_DATA_ERROR = 0x07,
|
||||
|
||||
// 陀螺仪配置错误
|
||||
BMI088_GYRO_RANGE_ERROR = 0x08,
|
||||
BMI088_GYRO_BANDWIDTH_ERROR = 0x09,
|
||||
BMI088_GYRO_LPM1_ERROR = 0x0A,
|
||||
BMI088_GYRO_CTRL_ERROR = 0x0B,
|
||||
BMI088_GYRO_INT3_INT4_IO_CONF_ERROR = 0x0C,
|
||||
BMI088_GYRO_INT3_INT4_IO_MAP_ERROR = 0x0D,
|
||||
|
||||
// 自检错误(高位标志)
|
||||
BMI088_SELF_TEST_ACCEL_ERROR = 0x80, // 加速度计自检失败
|
||||
BMI088_SELF_TEST_GYRO_ERROR = 0x40, // 陀螺仪自检失败
|
||||
|
||||
// 严重错误
|
||||
BMI088_NO_SENSOR = 0xFF, // 未检测到传感器(ID 不匹配)
|
||||
};
|
||||
|
||||
|
||||
// 公共函数声明
|
||||
|
||||
|
||||
/**
|
||||
* @brief 初始化 BMI088(加速度计 + 陀螺仪)
|
||||
* @return 错误码(0 表示成功,非0表示具体错误)
|
||||
*/
|
||||
uint8_t BMI088_init(void);
|
||||
|
||||
/**
|
||||
* @brief 单独初始化加速度计
|
||||
* @return 错误码
|
||||
*/
|
||||
unsigned char bmi088_accel_init(void);
|
||||
|
||||
/**
|
||||
* @brief 单独初始化陀螺仪
|
||||
* @return 错误码
|
||||
*/
|
||||
unsigned char bmi088_gyro_init(void);
|
||||
|
||||
/**
|
||||
* @brief 读取 BMI088 传感器数据
|
||||
* @param gyro[3] 输出:陀螺仪角速度(单位由 BMI088_GYRO_xxx_SEN 决定,通常为 rad/s)
|
||||
* @param accel[3] 输出:加速度(单位由 BMI088_ACCEL_xxx_SEN 决定,通常为 m/s²)
|
||||
* @param temperate 输出:温度(°C)
|
||||
*
|
||||
* @note 此函数内部已处理片选、SPI 通信、数据解析
|
||||
*/
|
||||
void BMI088_read(float gyro[3], float accel[3], float *temperate);
|
||||
|
||||
#endif // BMI088DRIVER_H
|
||||
180
00-Driver/BMI088reg.h
Normal file
180
00-Driver/BMI088reg.h
Normal file
@@ -0,0 +1,180 @@
|
||||
#ifndef BMI088REG_H
|
||||
#define BMI088REG_H
|
||||
|
||||
#define BMI088_ACC_CHIP_ID 0x00 // the register is " Who am I "
|
||||
#define BMI088_ACC_CHIP_ID_VALUE 0x1E
|
||||
|
||||
#define BMI088_ACC_ERR_REG 0x02
|
||||
#define BMI088_ACCEL_CONGIF_ERROR_SHFITS 0x2
|
||||
#define BMI088_ACCEL_CONGIF_ERROR (1 << BMI088_ACCEL_CONGIF_ERROR_SHFITS)
|
||||
#define BMI088_FATAL_ERROR_SHFITS 0x0
|
||||
#define BMI088_FATAL_ERROR (1 << BMI088_FATAL_ERROR)
|
||||
|
||||
#define BMI088_ACC_STATUS 0x03
|
||||
#define BMI088_ACCEL_DRDY_SHFITS 0x7
|
||||
#define BMI088_ACCEL_DRDY (1 << BMI088_ACCEL_DRDY_SHFITS)
|
||||
|
||||
#define BMI088_ACCEL_XOUT_L 0x12
|
||||
#define BMI088_ACCEL_XOUT_M 0x13
|
||||
#define BMI088_ACCEL_YOUT_L 0x14
|
||||
#define BMI088_ACCEL_YOUT_M 0x15
|
||||
#define BMI088_ACCEL_ZOUT_L 0x16
|
||||
#define BMI088_ACCEL_ZOUT_M 0x17
|
||||
|
||||
#define BMI088_SENSORTIME_DATA_L 0x18
|
||||
#define BMI088_SENSORTIME_DATA_M 0x19
|
||||
#define BMI088_SENSORTIME_DATA_H 0x1A
|
||||
|
||||
#define BMI088_ACC_INT_STAT_1 0x1D
|
||||
#define BMI088_ACCEL_DRDY_INTERRUPT_SHFITS 0x7
|
||||
#define BMI088_ACCEL_DRDY_INTERRUPT (1 << BMI088_ACCEL_DRDY_INTERRUPT_SHFITS)
|
||||
|
||||
#define BMI088_TEMP_M 0x22
|
||||
|
||||
#define BMI088_TEMP_L 0x23
|
||||
|
||||
#define BMI088_ACC_CONF 0x40
|
||||
#define BMI088_ACC_CONF_MUST_Set 0x80
|
||||
#define BMI088_ACC_BWP_SHFITS 0x4
|
||||
#define BMI088_ACC_OSR4 (0x0 << BMI088_ACC_BWP_SHFITS)
|
||||
#define BMI088_ACC_OSR2 (0x1 << BMI088_ACC_BWP_SHFITS)
|
||||
#define BMI088_ACC_NORMAL (0x2 << BMI088_ACC_BWP_SHFITS)
|
||||
|
||||
#define BMI088_ACC_ODR_SHFITS 0x0
|
||||
#define BMI088_ACC_12_5_HZ (0x5 << BMI088_ACC_ODR_SHFITS)
|
||||
#define BMI088_ACC_25_HZ (0x6 << BMI088_ACC_ODR_SHFITS)
|
||||
#define BMI088_ACC_50_HZ (0x7 << BMI088_ACC_ODR_SHFITS)
|
||||
#define BMI088_ACC_100_HZ (0x8 << BMI088_ACC_ODR_SHFITS)
|
||||
#define BMI088_ACC_200_HZ (0x9 << BMI088_ACC_ODR_SHFITS)
|
||||
#define BMI088_ACC_400_HZ (0xA << BMI088_ACC_ODR_SHFITS)
|
||||
#define BMI088_ACC_800_HZ (0xB << BMI088_ACC_ODR_SHFITS)
|
||||
#define BMI088_ACC_1600_HZ (0xC << BMI088_ACC_ODR_SHFITS)
|
||||
|
||||
#define BMI088_ACC_RANGE 0x41
|
||||
|
||||
#define BMI088_ACC_RANGE_SHFITS 0x0
|
||||
#define BMI088_ACC_RANGE_3G (0x0 << BMI088_ACC_RANGE_SHFITS)
|
||||
#define BMI088_ACC_RANGE_6G (0x1 << BMI088_ACC_RANGE_SHFITS)
|
||||
#define BMI088_ACC_RANGE_12G (0x2 << BMI088_ACC_RANGE_SHFITS)
|
||||
#define BMI088_ACC_RANGE_24G (0x3 << BMI088_ACC_RANGE_SHFITS)
|
||||
|
||||
#define BMI088_INT1_IO_CTRL 0x53
|
||||
#define BMI088_ACC_INT1_IO_ENABLE_SHFITS 0x3
|
||||
#define BMI088_ACC_INT1_IO_ENABLE (0x1 << BMI088_ACC_INT1_IO_ENABLE_SHFITS)
|
||||
#define BMI088_ACC_INT1_GPIO_MODE_SHFITS 0x2
|
||||
#define BMI088_ACC_INT1_GPIO_PP (0x0 << BMI088_ACC_INT1_GPIO_MODE_SHFITS)
|
||||
#define BMI088_ACC_INT1_GPIO_OD (0x1 << BMI088_ACC_INT1_GPIO_MODE_SHFITS)
|
||||
#define BMI088_ACC_INT1_GPIO_LVL_SHFITS 0x1
|
||||
#define BMI088_ACC_INT1_GPIO_LOW (0x0 << BMI088_ACC_INT1_GPIO_LVL_SHFITS)
|
||||
#define BMI088_ACC_INT1_GPIO_HIGH (0x1 << BMI088_ACC_INT1_GPIO_LVL_SHFITS)
|
||||
|
||||
#define BMI088_INT2_IO_CTRL 0x54
|
||||
#define BMI088_ACC_INT2_IO_ENABLE_SHFITS 0x3
|
||||
#define BMI088_ACC_INT2_IO_ENABLE (0x1 << BMI088_ACC_INT2_IO_ENABLE_SHFITS)
|
||||
#define BMI088_ACC_INT2_GPIO_MODE_SHFITS 0x2
|
||||
#define BMI088_ACC_INT2_GPIO_PP (0x0 << BMI088_ACC_INT2_GPIO_MODE_SHFITS)
|
||||
#define BMI088_ACC_INT2_GPIO_OD (0x1 << BMI088_ACC_INT2_GPIO_MODE_SHFITS)
|
||||
#define BMI088_ACC_INT2_GPIO_LVL_SHFITS 0x1
|
||||
#define BMI088_ACC_INT2_GPIO_LOW (0x0 << BMI088_ACC_INT2_GPIO_LVL_SHFITS)
|
||||
#define BMI088_ACC_INT2_GPIO_HIGH (0x1 << BMI088_ACC_INT2_GPIO_LVL_SHFITS)
|
||||
|
||||
#define BMI088_INT_MAP_DATA 0x58
|
||||
#define BMI088_ACC_INT2_DRDY_INTERRUPT_SHFITS 0x6
|
||||
#define BMI088_ACC_INT2_DRDY_INTERRUPT (0x1 << BMI088_ACC_INT2_DRDY_INTERRUPT_SHFITS)
|
||||
#define BMI088_ACC_INT1_DRDY_INTERRUPT_SHFITS 0x2
|
||||
#define BMI088_ACC_INT1_DRDY_INTERRUPT (0x1 << BMI088_ACC_INT1_DRDY_INTERRUPT_SHFITS)
|
||||
|
||||
#define BMI088_ACC_SELF_TEST 0x6D
|
||||
#define BMI088_ACC_SELF_TEST_OFF 0x00
|
||||
#define BMI088_ACC_SELF_TEST_POSITIVE_SIGNAL 0x0D
|
||||
#define BMI088_ACC_SELF_TEST_NEGATIVE_SIGNAL 0x09
|
||||
|
||||
#define BMI088_ACC_PWR_CONF 0x7C
|
||||
#define BMI088_ACC_PWR_SUSPEND_MODE 0x03
|
||||
#define BMI088_ACC_PWR_ACTIVE_MODE 0x00
|
||||
|
||||
#define BMI088_ACC_PWR_CTRL 0x7D
|
||||
#define BMI088_ACC_ENABLE_ACC_OFF 0x00
|
||||
#define BMI088_ACC_ENABLE_ACC_ON 0x04
|
||||
|
||||
#define BMI088_ACC_SOFTRESET 0x7E
|
||||
#define BMI088_ACC_SOFTRESET_VALUE 0xB6
|
||||
|
||||
#define BMI088_GYRO_CHIP_ID 0x00
|
||||
#define BMI088_GYRO_CHIP_ID_VALUE 0x0F
|
||||
|
||||
#define BMI088_GYRO_X_L 0x02
|
||||
#define BMI088_GYRO_X_H 0x03
|
||||
#define BMI088_GYRO_Y_L 0x04
|
||||
#define BMI088_GYRO_Y_H 0x05
|
||||
#define BMI088_GYRO_Z_L 0x06
|
||||
#define BMI088_GYRO_Z_H 0x07
|
||||
|
||||
#define BMI088_GYRO_INT_STAT_1 0x0A
|
||||
#define BMI088_GYRO_DYDR_SHFITS 0x7
|
||||
#define BMI088_GYRO_DYDR (0x1 << BMI088_GYRO_DYDR_SHFITS)
|
||||
|
||||
#define BMI088_GYRO_RANGE 0x0F
|
||||
#define BMI088_GYRO_RANGE_SHFITS 0x0
|
||||
#define BMI088_GYRO_2000 (0x0 << BMI088_GYRO_RANGE_SHFITS)
|
||||
#define BMI088_GYRO_1000 (0x1 << BMI088_GYRO_RANGE_SHFITS)
|
||||
#define BMI088_GYRO_500 (0x2 << BMI088_GYRO_RANGE_SHFITS)
|
||||
#define BMI088_GYRO_250 (0x3 << BMI088_GYRO_RANGE_SHFITS)
|
||||
#define BMI088_GYRO_125 (0x4 << BMI088_GYRO_RANGE_SHFITS)
|
||||
|
||||
#define BMI088_GYRO_BANDWIDTH 0x10
|
||||
// the first num means Output data rate, the second num means bandwidth
|
||||
#define BMI088_GYRO_BANDWIDTH_MUST_Set 0x80
|
||||
#define BMI088_GYRO_2000_532_HZ 0x00
|
||||
#define BMI088_GYRO_2000_230_HZ 0x01
|
||||
#define BMI088_GYRO_1000_116_HZ 0x02
|
||||
#define BMI088_GYRO_400_47_HZ 0x03
|
||||
#define BMI088_GYRO_200_23_HZ 0x04
|
||||
#define BMI088_GYRO_100_12_HZ 0x05
|
||||
#define BMI088_GYRO_200_64_HZ 0x06
|
||||
#define BMI088_GYRO_100_32_HZ 0x07
|
||||
|
||||
#define BMI088_GYRO_LPM1 0x11
|
||||
#define BMI088_GYRO_NORMAL_MODE 0x00
|
||||
#define BMI088_GYRO_SUSPEND_MODE 0x80
|
||||
#define BMI088_GYRO_DEEP_SUSPEND_MODE 0x20
|
||||
|
||||
#define BMI088_GYRO_SOFTRESET 0x14
|
||||
#define BMI088_GYRO_SOFTRESET_VALUE 0xB6
|
||||
|
||||
#define BMI088_GYRO_CTRL 0x15
|
||||
#define BMI088_DRDY_OFF 0x00
|
||||
#define BMI088_DRDY_ON 0x80
|
||||
|
||||
#define BMI088_GYRO_INT3_INT4_IO_CONF 0x16
|
||||
#define BMI088_GYRO_INT4_GPIO_MODE_SHFITS 0x3
|
||||
#define BMI088_GYRO_INT4_GPIO_PP (0x0 << BMI088_GYRO_INT4_GPIO_MODE_SHFITS)
|
||||
#define BMI088_GYRO_INT4_GPIO_OD (0x1 << BMI088_GYRO_INT4_GPIO_MODE_SHFITS)
|
||||
#define BMI088_GYRO_INT4_GPIO_LVL_SHFITS 0x2
|
||||
#define BMI088_GYRO_INT4_GPIO_LOW (0x0 << BMI088_GYRO_INT4_GPIO_LVL_SHFITS)
|
||||
#define BMI088_GYRO_INT4_GPIO_HIGH (0x1 << BMI088_GYRO_INT4_GPIO_LVL_SHFITS)
|
||||
#define BMI088_GYRO_INT3_GPIO_MODE_SHFITS 0x1
|
||||
#define BMI088_GYRO_INT3_GPIO_PP (0x0 << BMI088_GYRO_INT3_GPIO_MODE_SHFITS)
|
||||
#define BMI088_GYRO_INT3_GPIO_OD (0x1 << BMI088_GYRO_INT3_GPIO_MODE_SHFITS)
|
||||
#define BMI088_GYRO_INT3_GPIO_LVL_SHFITS 0x0
|
||||
#define BMI088_GYRO_INT3_GPIO_LOW (0x0 << BMI088_GYRO_INT3_GPIO_LVL_SHFITS)
|
||||
#define BMI088_GYRO_INT3_GPIO_HIGH (0x1 << BMI088_GYRO_INT3_GPIO_LVL_SHFITS)
|
||||
|
||||
#define BMI088_GYRO_INT3_INT4_IO_MAP 0x18
|
||||
|
||||
#define BMI088_GYRO_DRDY_IO_OFF 0x00
|
||||
#define BMI088_GYRO_DRDY_IO_INT3 0x01
|
||||
#define BMI088_GYRO_DRDY_IO_INT4 0x80
|
||||
#define BMI088_GYRO_DRDY_IO_BOTH (BMI088_GYRO_DRDY_IO_INT3 | BMI088_GYRO_DRDY_IO_INT4)
|
||||
|
||||
#define BMI088_GYRO_SELF_TEST 0x3C
|
||||
#define BMI088_GYRO_RATE_OK_SHFITS 0x4
|
||||
#define BMI088_GYRO_RATE_OK (0x1 << BMI088_GYRO_RATE_OK_SHFITS)
|
||||
#define BMI088_GYRO_BIST_FAIL_SHFITS 0x2
|
||||
#define BMI088_GYRO_BIST_FAIL (0x1 << BMI088_GYRO_BIST_FAIL_SHFITS)
|
||||
#define BMI088_GYRO_BIST_RDY_SHFITS 0x1
|
||||
#define BMI088_GYRO_BIST_RDY (0x1 << BMI088_GYRO_BIST_RDY_SHFITS)
|
||||
#define BMI088_GYRO_TRIG_BIST_SHFITS 0x0
|
||||
#define BMI088_GYRO_TRIG_BIST (0x1 << BMI088_GYRO_TRIG_BIST_SHFITS)
|
||||
|
||||
#endif
|
||||
102
00-Driver/BSP_CAN.c
Normal file
102
00-Driver/BSP_CAN.c
Normal file
@@ -0,0 +1,102 @@
|
||||
#include "bsp_can.h"
|
||||
#include "main.h"
|
||||
#include "Motor.h"
|
||||
//===============变量区
|
||||
extern CAN_HandleTypeDef hcan1;
|
||||
extern CAN_HandleTypeDef hcan2;
|
||||
|
||||
//===============公共函数
|
||||
void Can_Filter_Init(void)
|
||||
{
|
||||
CAN_FilterTypeDef can_filter_st;
|
||||
can_filter_st.FilterActivation = ENABLE;
|
||||
can_filter_st.FilterMode = CAN_FILTERMODE_IDMASK;//CAN_FILTERMODE_IDLIST//CAN_FILTERMODE_IDMASK
|
||||
can_filter_st.FilterScale = CAN_FILTERSCALE_16BIT;
|
||||
can_filter_st.FilterIdHigh = 0x0000; //(0x207<<5)
|
||||
can_filter_st.FilterIdLow = 0x0000;
|
||||
can_filter_st.FilterMaskIdHigh = 0x0000;//(0x1FF<<5)
|
||||
can_filter_st.FilterMaskIdLow = 0x0000;
|
||||
can_filter_st.FilterBank = 0;
|
||||
can_filter_st.FilterFIFOAssignment = CAN_RX_FIFO0;
|
||||
HAL_CAN_ConfigFilter(&hcan1, &can_filter_st);
|
||||
HAL_CAN_Start(&hcan1);
|
||||
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
|
||||
|
||||
can_filter_st.SlaveStartFilterBank = 14;
|
||||
can_filter_st.FilterBank = 14;
|
||||
|
||||
HAL_CAN_ConfigFilter(&hcan2, &can_filter_st);
|
||||
HAL_CAN_Start(&hcan2);
|
||||
HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);
|
||||
|
||||
}
|
||||
|
||||
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
|
||||
{
|
||||
CAN_RxHeaderTypeDef rx_header;
|
||||
uint8_t rx_data[8];
|
||||
|
||||
// 读取接收到的消息
|
||||
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rx_header, rx_data) != HAL_OK)
|
||||
return; // 安全检查
|
||||
|
||||
// 只处理标准帧
|
||||
if (rx_header.IDE != CAN_ID_STD)
|
||||
return;
|
||||
|
||||
// 根据 CAN 外设实例区分总线
|
||||
if (hcan == &hcan1)
|
||||
{
|
||||
switch (rx_header.StdId)
|
||||
{
|
||||
case 0x201:
|
||||
CAN1_M3508_DataProcess(0x201,rx_data);break;
|
||||
case 0x202:
|
||||
CAN1_M3508_DataProcess(0x202,rx_data);break;
|
||||
case 0x203:
|
||||
CAN1_M3508_DataProcess(0x203,rx_data);break;
|
||||
case 0x204:
|
||||
CAN1_M3508_DataProcess(0x204,rx_data);break;
|
||||
case 0x205:
|
||||
break;
|
||||
case 0x206:
|
||||
break;
|
||||
case 0x207:
|
||||
break;
|
||||
case 0x208:
|
||||
break;
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (hcan == &hcan2)
|
||||
{
|
||||
switch (rx_header.StdId)
|
||||
{
|
||||
case 0x201:
|
||||
break;
|
||||
case 0x202:
|
||||
break;
|
||||
case 0x203:
|
||||
break;
|
||||
case 0x204:
|
||||
break;
|
||||
case 0x205:
|
||||
CAN2_M6020_DataProcess(0x205,rx_data);break;
|
||||
case 0x206:
|
||||
CAN2_M6020_DataProcess(0x206,rx_data);break;
|
||||
case 0x207:
|
||||
break;
|
||||
case 0x208:
|
||||
break;
|
||||
case 0x149:
|
||||
CToC_CANDataProcess(0x149,rx_data);break;
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
10
00-Driver/BSP_CAN.h
Normal file
10
00-Driver/BSP_CAN.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef BSP_CAN_H
|
||||
#define BSP_CAN_H
|
||||
#include <stdint.h>
|
||||
#include "Motor.h"
|
||||
#include "Chassis_CtoC.h"
|
||||
|
||||
|
||||
void Can_Filter_Init(void);
|
||||
|
||||
#endif
|
||||
145
00-Driver/BSP_USART.c
Normal file
145
00-Driver/BSP_USART.c
Normal file
@@ -0,0 +1,145 @@
|
||||
#include "BSP_USART.h"
|
||||
#include "usart.h"
|
||||
#include "main.h"
|
||||
|
||||
extern UART_HandleTypeDef huart1; //usart.c
|
||||
extern DMA_HandleTypeDef hdma_usart1_tx; //usart.c
|
||||
extern UART_HandleTypeDef huart3; //usart.c
|
||||
extern DMA_HandleTypeDef hdma_usart3_rx; //usart.c
|
||||
|
||||
void UART2_TxDma_Init(void)
|
||||
{
|
||||
//enable the DMA transfer for the receiver request
|
||||
SET_BIT(huart1.Instance->CR3, USART_CR3_DMAT);
|
||||
}
|
||||
void UART2_TxDma_Enable(uint8_t *data, uint16_t len)
|
||||
{
|
||||
//disable DMA
|
||||
__HAL_DMA_DISABLE(&hdma_usart1_tx);
|
||||
while(hdma_usart1_tx.Instance->CR & DMA_SxCR_EN)
|
||||
{
|
||||
__HAL_DMA_DISABLE(&hdma_usart1_tx);
|
||||
}
|
||||
//clear flag
|
||||
__HAL_DMA_CLEAR_FLAG(&hdma_usart1_tx, DMA_HISR_TCIF7);
|
||||
__HAL_DMA_CLEAR_FLAG(&hdma_usart1_tx, DMA_HISR_HTIF7);
|
||||
//set data address
|
||||
hdma_usart1_tx.Instance->M0AR = (uint32_t)(data);
|
||||
//set data length
|
||||
hdma_usart1_tx.Instance->NDTR = len;
|
||||
//enable DMA
|
||||
__HAL_DMA_ENABLE(&hdma_usart1_tx);
|
||||
}
|
||||
|
||||
void UART2_SendByte(uint8_t Byte)
|
||||
{
|
||||
HAL_UART_Transmit(&huart1, &Byte, 1, 0xFFFF);
|
||||
}
|
||||
|
||||
void UART2_SendArray(uint8_t *Array,uint16_t Length)
|
||||
{
|
||||
for(uint16_t i=0;i<Length;i++)
|
||||
UART2_SendByte(Array[i]);
|
||||
}
|
||||
|
||||
void UART2_SendString(char *String)
|
||||
{
|
||||
for(uint8_t i=0;String[i]!='\0';i++)
|
||||
UART2_SendByte(String[i]);//依次发送字符串的每一位
|
||||
}
|
||||
|
||||
void UART2_SendNumber(uint32_t Number,uint8_t Length)
|
||||
{
|
||||
for(uint8_t i=0;i<Length;i++)
|
||||
UART2_SendByte(Number/USART_Pow(10,Length-i-1)%10+'0');//以文本形式发送数字每一位
|
||||
}
|
||||
|
||||
void UART2_SendNumber_Sign(int32_t Number, uint8_t Length)
|
||||
{
|
||||
// 处理负数
|
||||
if (Number < 0) {
|
||||
UART2_SendByte('-');
|
||||
Number = -Number; // 转为正数(注意:INT32_MIN 有边界问题,见下方说明)
|
||||
}
|
||||
|
||||
// 发送每一位数字
|
||||
for (uint8_t i = 0; i < Length; i++) {
|
||||
// 计算当前位的除数(10^(Length - i - 1))
|
||||
uint32_t divisor = 1;
|
||||
for (uint8_t j = 0; j < Length - i - 1; j++) {
|
||||
divisor *= 10;
|
||||
}
|
||||
uint8_t digit = (Number / divisor) % 10;
|
||||
UART2_SendByte(digit + '0');
|
||||
}
|
||||
}
|
||||
|
||||
void UART2_SendFloat_Sign(float value, uint8_t decimal_places)
|
||||
{
|
||||
char buffer[32]; // 足够容纳 -123456.789\0
|
||||
|
||||
// 安全格式化:防止缓冲区溢出
|
||||
int len = snprintf(buffer, sizeof(buffer), "%.*f", decimal_places, value);
|
||||
|
||||
// 防止 snprintf 出错(返回负数或超长)
|
||||
if (len < 0 || len >= (int)sizeof(buffer)) {
|
||||
UART2_SendString("ERR_FLOAT");
|
||||
return;
|
||||
}
|
||||
|
||||
// 发送字符串
|
||||
for (int i = 0; i < len; i++) {
|
||||
UART2_SendByte(buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint32_t USART_Pow(uint32_t X, uint32_t Y)
|
||||
{
|
||||
uint32_t Result = 1;
|
||||
while(Y--)
|
||||
{
|
||||
Result *= X;
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
|
||||
|
||||
void DBUS_DMA_Init(uint8_t *rx1_buf, uint8_t *rx2_buf, uint16_t dma_buf_num)
|
||||
{
|
||||
//enable the DMA transfer for the receiver request
|
||||
SET_BIT(huart3.Instance->CR3, USART_CR3_DMAR);
|
||||
|
||||
//enalbe idle interrupt
|
||||
__HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
|
||||
|
||||
//disable DMA
|
||||
__HAL_DMA_DISABLE(&hdma_usart3_rx);
|
||||
while(hdma_usart3_rx.Instance->CR & DMA_SxCR_EN)
|
||||
{
|
||||
__HAL_DMA_DISABLE(&hdma_usart3_rx);
|
||||
}
|
||||
|
||||
hdma_usart3_rx.Instance->PAR = (uint32_t) & (USART3->DR);
|
||||
//memory buffer 1
|
||||
hdma_usart3_rx.Instance->M0AR = (uint32_t)(rx1_buf);
|
||||
//memory buffer 2
|
||||
hdma_usart3_rx.Instance->M1AR = (uint32_t)(rx2_buf);
|
||||
//data length
|
||||
hdma_usart3_rx.Instance->NDTR = dma_buf_num;
|
||||
//enable double memory buffer
|
||||
SET_BIT(hdma_usart3_rx.Instance->CR, DMA_SxCR_DBM);
|
||||
|
||||
//enable DMA
|
||||
__HAL_DMA_ENABLE(&hdma_usart3_rx);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
24
00-Driver/BSP_USART.h
Normal file
24
00-Driver/BSP_USART.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef BSP_USART_H
|
||||
#define BSP_USART_H
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
//板子上的usart2,不是外设usart2
|
||||
void UART2_TxDma_Init(void);
|
||||
void UART2_TxDma_Enable(uint8_t *data, uint16_t len);
|
||||
void UART2_SendByte (uint8_t Byte);
|
||||
void UART2_SendArray (uint8_t *Array,uint16_t Length);
|
||||
void UART2_SendString (char *String);
|
||||
void UART2_SendNumber (uint32_t Number,uint8_t Length);
|
||||
void UART2_SendNumber_Sign(int32_t Number, uint8_t Length);
|
||||
void UART2_SendFloat_Sign(float value, uint8_t decimal_places);
|
||||
//void UART2_Printf(char *format,...);
|
||||
uint32_t USART_Pow(uint32_t X, uint32_t Y);
|
||||
//uint8_t UART2_GetRxFlag(void);
|
||||
|
||||
|
||||
void DBUS_DMA_Init(uint8_t *rx1_buf, uint8_t *rx2_buf, uint16_t dma_buf_num);
|
||||
|
||||
|
||||
#endif
|
||||
18
00-Driver/Parameter.h
Normal file
18
00-Driver/Parameter.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef __PARAMETER_H
|
||||
#define __PARAMETER_H
|
||||
/*=============================================数学参数=============================================*/
|
||||
#define PI 3.141592653589793238462643383279f
|
||||
/*=============================================结构参数=============================================*/
|
||||
|
||||
/*=============================================麦轮参数=============================================*/
|
||||
#define Mecanum_WheelRadius 7.0f//麦轮半径(单位cm)
|
||||
#define Mecanum_rx 24.00f//底盘中心到轮子中心的距离的x轴分量(单位cm)
|
||||
#define Mecanum_ry 24.00f//底盘中心到轮子中心的距离的y轴分量(单位cm)
|
||||
/*=============================================电机参数=============================================*/
|
||||
#define M3508_MotorReductionRatio 19.0f//麦轮电机减速比
|
||||
#define M3508_Encoder 8191.0f//麦轮电机编码器
|
||||
#define M6020_MotorReductionRatio 36.0f//云台电机减速比
|
||||
#define M6020_Encoder 8191.0f//云台电机编码器
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user