添加了ist8310磁力计的支持,同时修复了gpio初始化先于exti的bug
This commit is contained in:
parent
73cdc66de5
commit
1262f9a516
|
@ -34,7 +34,6 @@
|
|||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "robot.h"
|
||||
#include "oled.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
|
@ -115,6 +114,7 @@ int main(void)
|
|||
MX_I2C3_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
RobotInit();
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Call init function for freertos objects (in freertos.c) */
|
||||
|
|
|
@ -107,13 +107,14 @@ void NMI_Handler(void)
|
|||
void HardFault_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN HardFault_IRQn 0 */
|
||||
|
||||
// 发生hardfault,点击step over会自动跳转回出错的指令,方便调试
|
||||
asm("bx lr");
|
||||
/* USER CODE END HardFault_IRQn 0 */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE BEGIN W1_HardFault_IRQn 0 */
|
||||
// 发生hardfault,点击step over会自动跳转回出错的指令,方便调试
|
||||
asm("bx lr");
|
||||
/* USER CODE BEGIN W1_HardFault_IRQn 0 */
|
||||
|
||||
/* USER CODE END W1_HardFault_IRQn 0 */
|
||||
}
|
||||
}
|
||||
|
@ -185,12 +186,12 @@ void SysTick_Handler(void)
|
|||
|
||||
/* USER CODE END SysTick_IRQn 0 */
|
||||
HAL_IncTick();
|
||||
#if (INCLUDE_xTaskGetSchedulerState == 1 )
|
||||
#if (INCLUDE_xTaskGetSchedulerState == 1)
|
||||
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
|
||||
{
|
||||
#endif /* INCLUDE_xTaskGetSchedulerState */
|
||||
xPortSysTickHandler();
|
||||
#if (INCLUDE_xTaskGetSchedulerState == 1 )
|
||||
#if (INCLUDE_xTaskGetSchedulerState == 1)
|
||||
}
|
||||
#endif /* INCLUDE_xTaskGetSchedulerState */
|
||||
/* USER CODE BEGIN SysTick_IRQn 1 */
|
||||
|
|
2
Makefile
2
Makefile
|
@ -119,6 +119,7 @@ modules/algorithm/QuaternionEKF.c \
|
|||
modules/algorithm/crc8.c \
|
||||
modules/algorithm/crc16.c \
|
||||
modules/algorithm/user_lib.c \
|
||||
modules/BMI088/bmi088.c \
|
||||
modules/imu/BMI088driver.c \
|
||||
modules/imu/BMI088Middleware.c \
|
||||
modules/imu/ins_task.c \
|
||||
|
@ -239,6 +240,7 @@ C_INCLUDES = \
|
|||
-Ibsp/bsp_legacy_support \
|
||||
-Ibsp \
|
||||
-Imodules/algorithm \
|
||||
-Imodules/BMI088 \
|
||||
-Imodules/imu \
|
||||
-Imodules/ist8310 \
|
||||
-Imodules/led \
|
||||
|
|
|
@ -49,7 +49,7 @@ static DJIMotorInstance *motor_lb;
|
|||
static DJIMotorInstance *motor_rb;
|
||||
|
||||
/* 用于自旋变速策略的时间变量,后续考虑查表加速 */
|
||||
static float t;
|
||||
// static float t;
|
||||
|
||||
/* 私有函数计算的中介变量,设为静态避免参数传递的开销 */
|
||||
static float chassis_vx, chassis_vy; // 将云台系的速度投影到底盘
|
||||
|
|
|
@ -14,6 +14,9 @@
|
|||
|
||||
void RobotInit()
|
||||
{
|
||||
// 关闭中断,防止在初始化过程中发生中断
|
||||
// 请不要在初始化过程中使用中断!!!
|
||||
__disable_irq();
|
||||
BSPInit();
|
||||
|
||||
#if defined(ONE_BOARD) || defined(GIMBAL_BOARD)
|
||||
|
@ -25,6 +28,8 @@ void RobotInit()
|
|||
#if defined(ONE_BOARD) || defined(CHASSIS_BOARD)
|
||||
ChassisInit();
|
||||
#endif
|
||||
// 初始化完成,开启中断
|
||||
__enable_irq();
|
||||
}
|
||||
|
||||
void RobotTask()
|
||||
|
|
|
@ -293,8 +293,9 @@ NVIC.DMA2_Stream5_IRQn=true\:5\:0\:false\:false\:true\:false\:false\:true\:true
|
|||
NVIC.DMA2_Stream6_IRQn=true\:5\:0\:true\:false\:true\:true\:false\:true\:true
|
||||
NVIC.DMA2_Stream7_IRQn=true\:5\:0\:true\:false\:true\:true\:false\:true\:true
|
||||
NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
|
||||
NVIC.EXTI3_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
|
||||
NVIC.EXTI3_IRQn=true\:5\:0\:true\:false\:true\:true\:true\:true\:true
|
||||
NVIC.EXTI4_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
|
||||
NVIC.EXTI9_5_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
|
||||
NVIC.ForceEnableDMAVector=true
|
||||
NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
|
||||
NVIC.I2C2_ER_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
|
||||
|
@ -396,7 +397,7 @@ PC11.Locked=true
|
|||
PC11.Mode=Asynchronous
|
||||
PC11.Signal=USART3_RX
|
||||
PC4.GPIOParameters=GPIO_Label,GPIO_ModeDefaultEXTI
|
||||
PC4.GPIO_Label=INT_ACCEL
|
||||
PC4.GPIO_Label=INT_ACC
|
||||
PC4.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_FALLING
|
||||
PC4.Locked=true
|
||||
PC4.Signal=GPXTI4
|
||||
|
|
|
@ -18,6 +18,7 @@ void BuzzerOn(uint16_t psc, uint16_t pwm, uint8_t level)
|
|||
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, pwm);
|
||||
}
|
||||
}
|
||||
|
||||
void BuzzerOff(void)
|
||||
{
|
||||
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, 0);
|
||||
|
|
|
@ -38,7 +38,6 @@ GPIOInstance *GPIORegister(GPIO_Init_Config_s *GPIO_config)
|
|||
ins->exti_mode=GPIO_config->exti_mode;
|
||||
ins->id=GPIO_config->id;
|
||||
ins->gpio_model_callback=GPIO_config->gpio_model_callback;
|
||||
|
||||
gpio_instance[idx++]=ins;
|
||||
return ins;
|
||||
}
|
||||
|
@ -65,3 +64,4 @@ GPIO_PinState GPIORead(GPIOInstance *_instance)
|
|||
{
|
||||
return HAL_GPIO_ReadPin(_instance->GPIOx,_instance->GPIO_Pin);
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,8 @@ typedef struct tmpgpio
|
|||
GPIO_TypeDef *GPIOx; // GPIOA,GPIOB,GPIOC...
|
||||
GPIO_PinState pin_state; // 引脚状态,Set,Reset;not frequently used
|
||||
GPIO_EXTI_MODE_e exti_mode; // 外部中断模式 not frequently used
|
||||
uint16_t GPIO_Pin; // 引脚号
|
||||
uint16_t GPIO_Pin; // 引脚号,
|
||||
// 这些引脚是stm32f4xx_hal_gpio.h中定义的宏!!! 一定要注意
|
||||
// 随便取个名字当临时声明
|
||||
void (*gpio_model_callback)(struct tmpgpio *); // exti中断回调函数
|
||||
void *id; // 区分不同的GPIO实例
|
||||
|
@ -40,7 +41,8 @@ typedef struct
|
|||
GPIO_TypeDef *GPIOx; // GPIOA,GPIOB,GPIOC...
|
||||
GPIO_PinState pin_state; // 引脚状态,Set,Reset not frequently used
|
||||
GPIO_EXTI_MODE_e exti_mode; // 外部中断模式 not frequently used
|
||||
uint16_t GPIO_Pin; // 引脚号
|
||||
uint16_t GPIO_Pin; // 引脚号,@note 这里的引脚号是GPIO_PIN_0,GPIO_PIN_1...
|
||||
// 这些引脚是stm32f4xx_hal_gpio.h中定义的宏!!! 一定要注意
|
||||
|
||||
void (*gpio_model_callback)(GPIOInstance *); // exti中断回调函数
|
||||
void *id; // 区分不同的GPIO实例
|
||||
|
|
|
@ -1,3 +1,22 @@
|
|||
|
||||
可以作为io接口,也可以处理外部中断.
|
||||
|
||||
![image-20230202151939109](../../assets/image-20230202151939109.png)
|
||||
|
||||
![img](../../assets/00937839b59a4c039ee8ecb8a5136e3c.png)
|
||||
|
||||
|
||||
使用示例
|
||||
|
||||
```c
|
||||
|
||||
GPIO_Init_Config_s gpio_init = {
|
||||
.exti_mode = GPIO_EXTI_MODE_FALLING, // 注意和CUBEMX的配置一致
|
||||
.GPIO_Pin = GPIO_PIN_6, // GPIO引脚
|
||||
.GPIOx = GPIOG, // GPIO外设
|
||||
.gpio_model_callback = NULL, // EXTI回调函数
|
||||
},
|
||||
|
||||
GPIOInstance* test_example = GPIORegister(&gpio_init);
|
||||
GPIOSet(test_example);
|
||||
```
|
|
@ -15,7 +15,7 @@ IICInstance *IICRegister(IIC_Init_Config_s *conf)
|
|||
instance = (IICInstance *)malloc(sizeof(IICInstance));
|
||||
memset(instance, 0, sizeof(IICInstance));
|
||||
|
||||
instance->dev_address = conf->dev_address;
|
||||
instance->dev_address = conf->dev_address << 1;
|
||||
instance->callback = conf->callback;
|
||||
instance->work_mode = conf->work_mode;
|
||||
instance->handle = conf->handle;
|
||||
|
@ -103,7 +103,7 @@ void IICReceive(IICInstance *iic, uint8_t *data, uint16_t size, IIC_Seq_Mode_e s
|
|||
}
|
||||
}
|
||||
|
||||
void IICAcessMem(IICInstance *iic, uint8_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mem_mode)
|
||||
void IICAccessMem(IICInstance *iic, uint8_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mem_mode)
|
||||
{
|
||||
if (mem_mode == IIC_WRITE_MEM)
|
||||
{
|
||||
|
|
|
@ -102,4 +102,4 @@ void IICReceive(IICInstance *iic, uint8_t *data, uint16_t size,IIC_Seq_Mode_e mo
|
|||
* @param size 要读取或写入的数据长度
|
||||
* @param mode 写入或读取模式: IIC_READ_MEM or IIC_WRITE_MEM
|
||||
*/
|
||||
void IICAcessMem(IICInstance *iic, uint8_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mode);
|
||||
void IICAccessMem(IICInstance *iic, uint8_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mode);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
Loading…
Reference in New Issue