mirror of
https://gitee.com/dlmu-cone/bf_original_balance_chassis
synced 2026-07-24 03:27:45 +08:00
添加了ist8310磁力计的支持,同时修复了gpio初始化先于exti的bug
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#include "bmi088.h"
|
||||
#include "bmi088_regNdef.h"
|
||||
#include "bmi088.h"
|
||||
#include "stdlib.h"
|
||||
#include "memory.h"
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define __BMI088_H__
|
||||
|
||||
#include "bsp_spi.h"
|
||||
#include "bsp_gpio.h"
|
||||
#include "controller.h"
|
||||
#include "bsp_pwm.h"
|
||||
#include "stdint.h"
|
||||
@@ -16,8 +17,13 @@ typedef enum
|
||||
typedef struct
|
||||
{
|
||||
// 传输模式和工作模式控制
|
||||
SPIInstance *spi_gyro;
|
||||
SPIInstance *spi_acc;
|
||||
BMI088_Work_Mode_e mode;
|
||||
// SPI接口
|
||||
SPIInstance *spi_gyro; // 注意,SPIInstnace内部也有一个GPIOInstance,用于控制片选CS
|
||||
SPIInstance *spi_acc; // 注意,SPIInstnace内部也有一个GPIOInstance,用于控制片选CS
|
||||
// EXTI GPIO,如果BMI088工作在中断模式,则需要配置中断引脚(有数据产生时触发解算)
|
||||
GPIOInstance *gyro_int;
|
||||
GPIOInstance *acc_int;
|
||||
// 温度控制
|
||||
PIDInstance *heat_pid; // 恒温PID
|
||||
PWMInstance *heat_pwm; // 加热PWM
|
||||
@@ -38,6 +44,8 @@ typedef struct
|
||||
{
|
||||
SPI_Init_Config_s spi_gyro_config;
|
||||
SPI_Init_Config_s spi_acc_config;
|
||||
GPIO_Init_Config_s gyro_int_config;
|
||||
GPIO_Init_Config_s acc_int_config;
|
||||
BMI088_Work_Mode_e mode;
|
||||
PID_Init_Config_s heat_pid_config;
|
||||
PWM_Init_Config_s heat_pwm_config;
|
||||
|
||||
0
modules/BMI088/bmi088.md
Normal file
0
modules/BMI088/bmi088.md
Normal file
@@ -1 +1,90 @@
|
||||
#include "bsp_dwt.h"
|
||||
#include "ist8310.h"
|
||||
#include "memory.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
static IST8310Instance *ist8310_instance = NULL;
|
||||
|
||||
// -------------------初始化写入数组,只使用一次,详见datasheet-------------------------
|
||||
// the first column:the registers of IST8310. 第一列:IST8310的寄存器
|
||||
// the second column: the value to be writed to the registers.第二列:需要写入的寄存器值
|
||||
// the third column: return error value.第三列:返回的错误码
|
||||
#define IST8310_WRITE_REG_NUM 4
|
||||
#define IST8310_DATA_REG 0x03 // 数据寄存器
|
||||
#define IST8310_WHO_AM_I 0x00 // ist8310 id 寄存器值
|
||||
#define IST8310_WHO_AM_I_VALUE 0x10 // ist8310 id 寄存器值,用于检测是否连接成功
|
||||
static uint8_t ist8310_write_reg_data_error[IST8310_WRITE_REG_NUM][3] = {
|
||||
{0x0B, 0x08, 0x01}, // enalbe interrupt and low pin polarity.开启中断,并且设置低电平
|
||||
{0x41, 0x09, 0x02}, // average 2 times.平均采样两次
|
||||
{0x42, 0xC0, 0x03}, // must be 0xC0. 必须是0xC0
|
||||
{0x0A, 0x0B, 0x04}}; // 200Hz output rate.200Hz输出频率
|
||||
|
||||
/**
|
||||
* @brief IST8310解码函数,EXTI中断来临时被调用,将数据放到ist.mag中
|
||||
* @note 如果使用IT或DMA方式传输IIC,则传输完成后也会进入此函数
|
||||
*
|
||||
* @param ist 发生中断的IST8310实例
|
||||
*/
|
||||
static void IST8310Decode(IICInstance *iic)
|
||||
{
|
||||
static int16_t temp[3];
|
||||
static IST8310Instance *ist;
|
||||
ist = (IST8310Instance *)(iic->id);
|
||||
|
||||
memcpy(temp, ist->iic_buffer, 6 * sizeof(uint8_t));
|
||||
for (uint8_t i = 0; i < 3; i++)
|
||||
ist->mag[i] = (float)temp[i] * MAG_SEN;
|
||||
}
|
||||
|
||||
// EXTI中断回调函数,说明DRDY拉低.主机启动传输并在结束后调用IST8310Decode进行数据解析
|
||||
// 注意IICAccessMem是阻塞的
|
||||
static void IST8310StartTransfer(GPIOInstance *gpio)
|
||||
{
|
||||
IST8310Instance *ist_for_transfer = (IST8310Instance *)gpio->id;
|
||||
IICAccessMem(ist_for_transfer->iic, IST8310_DATA_REG, ist_for_transfer->iic_buffer, 6, IIC_READ_MEM);
|
||||
IST8310Decode(ist_for_transfer->iic);
|
||||
}
|
||||
|
||||
IST8310Instance *IST8310Init(IST8310_Init_Config_s *config)
|
||||
{
|
||||
static const uint8_t sleepTime = 50;
|
||||
uint8_t check_who_i_am = 0;
|
||||
|
||||
// 分配空间,清除flash
|
||||
IST8310Instance *ist = (IST8310Instance *)malloc(sizeof(IST8310Instance));
|
||||
memset(ist, 0, sizeof(IST8310Instance));
|
||||
|
||||
// c语言赋值从右到左
|
||||
config->iic_config.id = config->gpio_conf_exti.id = config->gpio_conf_rst.id = ist;
|
||||
// 传入回调函数
|
||||
config->iic_config.callback = IST8310Decode;
|
||||
config->gpio_conf_exti.gpio_model_callback = IST8310StartTransfer;
|
||||
// 注册两个GPIO和IIC
|
||||
ist->iic = IICRegister(&config->iic_config);
|
||||
ist->gpio_exti = GPIORegister(&config->gpio_conf_exti);
|
||||
ist->gpio_rst = GPIORegister(&config->gpio_conf_rst);
|
||||
|
||||
// 重置IST8310,需要HAL_Delay等待完成Reset
|
||||
GPIOReset(ist->gpio_rst);
|
||||
HAL_Delay(sleepTime);
|
||||
GPIOSet(ist->gpio_rst);
|
||||
HAL_Delay(sleepTime);
|
||||
|
||||
// 读取IST8310的ID,如果不是0x10,则返回错误
|
||||
IICAccessMem(ist->iic, IST8310_WHO_AM_I, &check_who_i_am, 1, IIC_READ_MEM);
|
||||
if (check_who_i_am != IST8310_WHO_AM_I_VALUE)
|
||||
return NULL; // while(1)
|
||||
|
||||
// 进行初始化配置写入并检查是否写入成功
|
||||
for (uint8_t i = 0; i < IST8310_WRITE_REG_NUM; i++)
|
||||
{ // 写入配置
|
||||
IICAccessMem(ist->iic, ist8310_write_reg_data_error[i][0], &ist8310_write_reg_data_error[i][1], 1, IIC_WRITE_MEM);
|
||||
IICAccessMem(ist->iic, ist8310_write_reg_data_error[i][0], &check_who_i_am, 1, IIC_READ_MEM); // 读回自身id
|
||||
if (check_who_i_am != ist8310_write_reg_data_error[i][1])
|
||||
while (1)
|
||||
ist8310_write_reg_data_error[i][2]; // 掉线/写入失败/未知错误,返回对应的错误码
|
||||
}
|
||||
|
||||
ist8310_instance = ist;
|
||||
return ist;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include "bsp_iic.h"
|
||||
#include "bsp_gpio.h"
|
||||
#include "stdint.h"
|
||||
|
||||
// 传感器灵敏度系数
|
||||
#define MAG_SEN 0.3f // raw int16 data change to uT unit. 原始整型数据变成 单位ut
|
||||
|
||||
// IST8310 ret ERROR CODE
|
||||
#define IST8310_NO_ERROR 0x00 // seldom used. 一般不会用到
|
||||
#define IST8310_NO_SENSOR 0x40 // seldom used. 一般不会用到
|
||||
#define IST8310_IIC_ADDRESS 0x0E // the I2C slave address of IST8310
|
||||
|
||||
/**
|
||||
* @brief IST8310 实例定义
|
||||
*
|
||||
*/
|
||||
typedef struct tempist8310
|
||||
{
|
||||
IICInstance *iic; // iic实例
|
||||
GPIOInstance* gpio_rst; // gpio实例,用于复位
|
||||
GPIOInstance* gpio_exti; // gpio实例,用于获取MAG_DRDY引脚状态,判断数据是否准备好(EXTI外部中断)
|
||||
uint8_t iic_buffer[8]; // iic接收缓冲区
|
||||
float mag[3]; // 三轴磁力计数据,[x,y,z]
|
||||
|
||||
void (*ist_module_callback)(IICInstance *); // 模块回调函数
|
||||
// 后续有需要再添加
|
||||
// ...
|
||||
} IST8310Instance;
|
||||
|
||||
/**
|
||||
* @brief IST8310 初始化配置结构体
|
||||
*
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
IIC_Init_Config_s iic_config; // iic初始化配置
|
||||
GPIO_Init_Config_s gpio_conf_rst; // gpio初始化配置,用于复位
|
||||
GPIO_Init_Config_s gpio_conf_exti; // gpio初始化配置,用于获取MAG_DRDY引脚状态,判断数据是否准备好(EXTI外部中断)
|
||||
} IST8310_Init_Config_s;
|
||||
|
||||
/**
|
||||
* @brief IST8310 初始化.
|
||||
* @note 一条i2c总线只能挂载一个IST8310
|
||||
*
|
||||
*/
|
||||
IST8310Instance *IST8310Init(IST8310_Init_Config_s *config);
|
||||
|
||||
void rest(int aa);
|
||||
|
||||
int nit(int bb);
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
使用示例
|
||||
|
||||
```c
|
||||
IST8310_Init_Config_s ist8310_conf = {
|
||||
.gpio_conf_exti = {
|
||||
.exti_mode = GPIO_EXTI_MODE_RISING,
|
||||
.GPIO_Pin = GPIO_PIN_3,
|
||||
.GPIOx = GPIOG,
|
||||
.gpio_model_callback = NULL,
|
||||
},
|
||||
.gpio_conf_rst = {
|
||||
.exti_mode = GPIO_EXTI_MODE_NONE,
|
||||
.GPIO_Pin = GPIO_PIN_6,
|
||||
.GPIOx = GPIOG,
|
||||
.gpio_model_callback = NULL,
|
||||
},
|
||||
.iic_config = {
|
||||
.handle = &hi2c3,
|
||||
.dev_address = IST8310_IIC_ADDRESS,
|
||||
.work_mode = IIC_BLOCK_MODE,
|
||||
},
|
||||
};
|
||||
IST8310Instance *asdf = IST8310Init(&ist8310_conf);
|
||||
|
||||
// 随后数据会被放到asdf.mag[i]中
|
||||
```
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "referee.h"
|
||||
#include "string.h"
|
||||
#include "crc.h"
|
||||
#include "crc_ref.h"
|
||||
#include "bsp_usart.h"
|
||||
#include "dma.h"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user