增加了usb虚拟串口的支持,编写了部分bmi088it读取

This commit is contained in:
NeoZng 2023-02-05 14:11:59 +08:00
parent 80df9bca15
commit 637d7de114
10 changed files with 406 additions and 282 deletions

View File

@ -23,7 +23,8 @@
#define __USBD_CDC_IF_H__
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
/* Includes ------------------------------------------------------------------*/
@ -50,76 +51,76 @@
/* Define size for the receive and transmit buffer over CDC */
#define APP_RX_DATA_SIZE 2048
#define APP_TX_DATA_SIZE 2048
/* USER CODE BEGIN EXPORTED_DEFINES */
/* USER CODE BEGIN EXPORTED_DEFINES */
/* USER CODE END EXPORTED_DEFINES */
/* USER CODE END EXPORTED_DEFINES */
/**
/**
* @}
*/
/** @defgroup USBD_CDC_IF_Exported_Types USBD_CDC_IF_Exported_Types
/** @defgroup USBD_CDC_IF_Exported_Types USBD_CDC_IF_Exported_Types
* @brief Types.
* @{
*/
/* USER CODE BEGIN EXPORTED_TYPES */
/* USER CODE BEGIN EXPORTED_TYPES */
/* USER CODE END EXPORTED_TYPES */
/* USER CODE END EXPORTED_TYPES */
/**
/**
* @}
*/
/** @defgroup USBD_CDC_IF_Exported_Macros USBD_CDC_IF_Exported_Macros
/** @defgroup USBD_CDC_IF_Exported_Macros USBD_CDC_IF_Exported_Macros
* @brief Aliases.
* @{
*/
/* USER CODE BEGIN EXPORTED_MACRO */
/* USER CODE BEGIN EXPORTED_MACRO */
/* USER CODE END EXPORTED_MACRO */
/* USER CODE END EXPORTED_MACRO */
/**
/**
* @}
*/
/** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables
/** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables
* @brief Public variables.
* @{
*/
/** CDC Interface callback. */
extern USBD_CDC_ItfTypeDef USBD_Interface_fops_FS;
/** CDC Interface callback. */
extern USBD_CDC_ItfTypeDef USBD_Interface_fops_FS;
/* USER CODE BEGIN EXPORTED_VARIABLES */
/* USER CODE BEGIN EXPORTED_VARIABLES */
/* USER CODE END EXPORTED_VARIABLES */
/* USER CODE END EXPORTED_VARIABLES */
/**
/**
* @}
*/
/** @defgroup USBD_CDC_IF_Exported_FunctionsPrototype USBD_CDC_IF_Exported_FunctionsPrototype
/** @defgroup USBD_CDC_IF_Exported_FunctionsPrototype USBD_CDC_IF_Exported_FunctionsPrototype
* @brief Public functions declaration.
* @{
*/
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len);
uint8_t CDC_Transmit_FS(uint8_t *Buf, uint16_t Len);
/* USER CODE BEGIN EXPORTED_FUNCTIONS */
/* USER CODE BEGIN EXPORTED_FUNCTIONS */
uint8_t *CDCInitRxbufferNcallback(void (*callback)(uint32_t));
/* USER CODE END EXPORTED_FUNCTIONS */
/* USER CODE END EXPORTED_FUNCTIONS */
/**
/**
* @}
*/
/**
/**
* @}
*/
/**
/**
* @}
*/
@ -128,4 +129,3 @@ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len);
#endif
#endif /* __USBD_CDC_IF_H__ */

View File

@ -49,7 +49,7 @@
*/
/* USER CODE BEGIN PRIVATE_TYPES */
void (*transmitcpltcallback)(uint32_t);
/* USER CODE END PRIVATE_TYPES */
/**
@ -87,14 +87,13 @@
*/
/* Create buffer for reception and transmission */
/* It's up to user to redefine and/or remove those define */
/* USER CODE BEGIN PRIVATE_VARIABLES */
/** Received data over USB are stored in this buffer */
uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
/** Data to send over USB CDC are stored in this buffer */
uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
/* USER CODE BEGIN PRIVATE_VARIABLES */
/* USER CODE END PRIVATE_VARIABLES */
/**
@ -123,8 +122,8 @@ extern USBD_HandleTypeDef hUsbDeviceFS;
static int8_t CDC_Init_FS(void);
static int8_t CDC_DeInit_FS(void);
static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length);
static int8_t CDC_Receive_FS(uint8_t* pbuf, uint32_t *Len);
static int8_t CDC_Control_FS(uint8_t cmd, uint8_t *pbuf, uint16_t length);
static int8_t CDC_Receive_FS(uint8_t *pbuf, uint32_t *Len);
static int8_t CDC_TransmitCplt_FS(uint8_t *pbuf, uint32_t *Len, uint8_t epnum);
/* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
@ -136,13 +135,12 @@ static int8_t CDC_TransmitCplt_FS(uint8_t *pbuf, uint32_t *Len, uint8_t epnum);
*/
USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
{
{
CDC_Init_FS,
CDC_DeInit_FS,
CDC_Control_FS,
CDC_Receive_FS,
CDC_TransmitCplt_FS
};
CDC_TransmitCplt_FS};
/* Private functions ---------------------------------------------------------*/
/**
@ -177,10 +175,10 @@ static int8_t CDC_DeInit_FS(void)
* @param length: Number of data to be sent (in bytes)
* @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
*/
static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length)
static int8_t CDC_Control_FS(uint8_t cmd, uint8_t *pbuf, uint16_t length)
{
/* USER CODE BEGIN 5 */
switch(cmd)
switch (cmd)
{
case CDC_SEND_ENCAPSULATED_COMMAND:
@ -258,7 +256,7 @@ static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length)
* @param Len: Number of data received (in bytes)
* @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
*/
static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
static int8_t CDC_Receive_FS(uint8_t *Buf, uint32_t *Len)
{
/* USER CODE BEGIN 6 */
USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
@ -278,12 +276,13 @@ static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
* @param Len: Number of data to be sent (in bytes)
* @retval USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
*/
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
uint8_t CDC_Transmit_FS(uint8_t *Buf, uint16_t Len)
{
uint8_t result = USBD_OK;
/* USER CODE BEGIN 7 */
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
if (hcdc->TxState != 0){
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)hUsbDeviceFS.pClassData;
if (hcdc->TxState != 0)
{
return USBD_BUSY;
}
USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
@ -292,6 +291,12 @@ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
return result;
}
uint8_t *CDCInitRxbufferNcallback(void (*callback)(uint32_t))
{
transmitcpltcallback=callback;
return (uint8_t *)UserRxBufferFS;
}
/**
* @brief CDC_TransmitCplt_FS
* Data transmitted callback
@ -311,6 +316,10 @@ static int8_t CDC_TransmitCplt_FS(uint8_t *Buf, uint32_t *Len, uint8_t epnum)
UNUSED(Buf);
UNUSED(Len);
UNUSED(epnum);
if(transmitcpltcallback!=NULL)
{
transmitcpltcallback(*Len);
}
/* USER CODE END 13 */
return result;
}

View File

@ -116,6 +116,7 @@ bsp/spi/bsp_spi.c \
bsp/iic/bsp_iic.c \
bsp/can/bsp_can.c \
bsp/usart/bsp_usart.c \
bsp/usb/bsp_usb.c \
bsp/log/bsp_log.c \
bsp/bsp_init.c \
modules/algorithm/controller.c \
@ -237,6 +238,7 @@ C_INCLUDES = \
-Ibsp/dwt \
-Ibsp/can \
-Ibsp/usart \
-Ibsp/usb \
-Ibsp/gpio \
-Ibsp/spi \
-Ibsp/iic \

View File

@ -1,6 +1,7 @@
#include "bsp_init.h"
#include "bsp_log.h"
#include "bsp_dwt.h"
#include "bsp_usb.h"
#include "bsp_buzzer.h"
#include "bsp_led.h"
#include "bsp_temperature.h"
@ -10,6 +11,7 @@ void BSPInit()
{
DWT_Init(168);
BSPLogInit();
USBInit(); // 务必在进入操作系统之前执行USB初始化
// legacy support待删除,将在实现了led/tempctrl/buzzer的module之后移动到app层
LEDInit();

View File

@ -1 +1,27 @@
#include "bsp_usb.h"
static uint8_t *bsp_usb_rx_buffer; // 接收到的数据会被放在这里,buffer size为2028
// 注意usb单个数据包(Full speed模式下)最大为64byte,超出可能会出现丢包情况
__weak void USBTransmitCpltCallback(uint32_t len)
{
// 本次发送的数据
UNUSED(len);
// 传输完成会调用此函数,to do something
}
uint8_t *USBInit()
{
USB_ResetPort(USB_OTG_FS); // 上电后重新枚举usb设备
USBTransmit((uint8_t *)"USB DEVICE READY", sizeof("USB DEVICE READY")); // 发送初始化完成信息
bsp_usb_rx_buffer = CDCInitRxbufferNcallback(USBTransmitCpltCallback); // 获取接收数据指针
return bsp_usb_rx_buffer;
}
void USBTransmit(uint8_t *buffer, uint16_t len)
{
CDC_Transmit_FS(buffer, len); // 发送
}

View File

@ -2,4 +2,19 @@
#include "usbd_cdc.h"
#include "usbd_conf.h"
#include "usbd_desc.h"
#include "usbd_cdc_if.h"
typedef enum
{
USB_CONNECTION_RAWDATA = 0,
USB_CONNECTION_VOFA,
USB_CONNECTION_SEASKY,
} USB_Connection_Type_e; // 选择usb连接模式,默认为原始数据
// 方便调试,也可以不用
/* @note 虚拟串口的波特率/校验位/数据位等动态可变,取决于上位机的设定 */
/* 使用时不需要关心这些设置(作为从机) */
uint8_t *USBInit(); // bsp初始化时调用会重新枚举设备
void USBTransmit(uint8_t *buffer, uint16_t len); // 通过usb发送数据

3
bsp/usb/bsp_usb.md Normal file
View File

@ -0,0 +1,3 @@
# bsp usb
简单写点,有待优化.

View File

@ -148,29 +148,65 @@ static uint8_t BMI088GyroInit(BMI088Instance *bmi088)
reg = BMI088_Gyro_Init_Table[i][REG];
data = BMI088_Gyro_Init_Table[i][DATA];
BMI088GyroWrite(bmi088, reg, data); // 写入寄存器
BMI088GyroRead(bmi088, reg, &data, 1); // 写完之后立刻读回检查
BMI088GyroRead(bmi088, reg, &data, 1); // 写完之后立刻读回对应寄存器检查是否写入成功
if (data != BMI088_Gyro_Init_Table[i][DATA])
error |= BMI088_Gyro_Init_Table[i][ERROR];
//{i--;} 可以设置retry次数,如果retry次数用完了,则返回error
//{i--;} 可以设置retry次数,尝试重新写入.如果retry次数用完了,则返回error
}
bmi088->acc_coef = 1.0; // 尚未初始化时设定为1,使得BMI088Acquire可以正常使用
bmi088->BMI088_GYRO_SEN = BMI088_GYRO_2000_SEN; // 后续改为从initTable中获取
bmi088->BMI088_ACCEL_SEN = BMI088_ACCEL_6G_SEN; // 用宏字符串拼接
bmi088->gNorm
return error;
}
// -------------------------以上为私有函数,用于初始化BMI088acc和gyro的硬件和配置--------------------------------//
// -------------------------以下为私有函数,private用于IT模式下的中断处理---------------------------------//
/**
* @brief ,!
*
*/
static void BMI088AccSPIFinishCallback(SPIInstance *spi)
{
static BMI088Instance *bmi088;
bmi088 = (BMI088Instance *)(spi->id);
// code to go here ...
}
static void BMI088GyroSPIFinishCallback(SPIInstance *spi)
{
static BMI088Instance *bmi088;
bmi088 = (BMI088Instance *)(spi->id);
}
static void BMI088AccINTCallback(GPIOInstance *gpio)
{
static BMI088Instance *bmi088;
bmi088 = (BMI088Instance *)(gpio->id);
}
static void BMI088GyroINTCallback(GPIOInstance *gpio)
{
static BMI088Instance *bmi088;
bmi088 = (BMI088Instance *)(gpio->id);
}
// -------------------------以上为私有函数,private用于IT模式下的中断处理---------------------------------//
// -------------------------以下为公有函数,用于注册BMI088,标定和数据读取--------------------------------//
/**
* @brief
* @todo ,? 7float数据有点费时,DMA? or memcpy
*
* @param bmi088
* @return BMI088_Data_t
*/
BMI088_Data_t BMI088Acquire(BMI088Instance *bmi088)
{
// 分配空间保存返回的数据,指针传递
static BMI088_Data_t data_store;
static float dt_imu = 1.0; // 初始化为1,这样也可以不用first_read_flag,各有优劣
// 如果是blocking模式,则主动触发一次读取并返回数据
static uint8_t buf[6] = {0}; // 最多读取6个byte(gyro)
static uint8_t buf[6] = {0}; // 最多读取6个byte(gyro/acc,temp是2)
static uint8_t first_read_flag; // 判断是否时第一次进入此函数(第一次读取)
// 用于初始化DWT的计数,暂时没想到更好的方法
if (!first_read_flag)
@ -182,7 +218,7 @@ BMI088_Data_t BMI088Acquire(BMI088Instance *bmi088)
BMI088AccelRead(bmi088, BMI088_ACCEL_XOUT_L, buf, 6);
static float calc_coef_acc; // 防止重复计算
if (!first_read_flag) // 初始化的时候赋值
calc_coef_acc = bmi088->BMI088_ACCEL_SEN * bmi088->acc_coef;
calc_coef_acc = bmi088->BMI088_ACCEL_SEN * bmi088->acc_coef; // 你要是不爽可以用宏或者全局变量,但我认为你现在很爽
bmi088->acc[0] = calc_coef_acc * (float)(int16_t)(((buf[1]) << 8) | buf[0]);
bmi088->acc[1] = calc_coef_acc * (float)(int16_t)(((buf[3]) << 8) | buf[2]);
bmi088->acc[3] = calc_coef_acc * (float)(int16_t)(((buf[5]) << 8) | buf[4]);
@ -235,7 +271,7 @@ BMI088_Data_t BMI088Acquire(BMI088Instance *bmi088)
*/
void BMI088CalibrateIMU(BMI088Instance *_bmi088)
{
if (_bmi088->cali_mode == BMI088_CALIBRATE_ONLINE_MODE)
if (_bmi088->cali_mode == BMI088_CALIBRATE_ONLINE_MODE) // 性感bmi088在线标定,耗时6s
{
// 一次性参数用完就丢,不用static
float startTime; // 开始标定时间,用于确定是否超时
@ -288,7 +324,7 @@ void BMI088CalibrateIMU(BMI088Instance *_bmi088)
_bmi088->gyro_offset[2] += _bmi088->gyro[2]; // 累加当前值,最后除以calib times获得零飘
// 因为标定时传感器静止,所以采集到的值就是漂移
if (i == 0)
if (i == 0) // 避免未定义的行为(else中)
{
gNormMax = gNormTemp; // 初始化成当前的重力加速度模长
gNormMin = gNormTemp;
@ -340,9 +376,10 @@ void BMI088CalibrateIMU(BMI088Instance *_bmi088)
gyroDiff[2] > 0.15f ||
fabsf(_bmi088->gyro_offset[0]) > 0.01f ||
fabsf(_bmi088->gyro_offset[1]) > 0.01f ||
fabsf(_bmi088->gyro_offset[2]) > 0.01f); // 条件
fabsf(_bmi088->gyro_offset[2]) > 0.01f); // 满足条件说明标定环境不好
}
if (_bmi088->cali_mode == BMI088_LOAD_PRE_CALI_MODE) // 如果标定失败也会进来
if (_bmi088->cali_mode == BMI088_LOAD_PRE_CALI_MODE) // 如果标定失败也会进来,直接使用离线数据
{
// 读取标定数据
// code to go here ...
@ -385,7 +422,23 @@ BMI088Instance *BMI088Register(BMI088_Init_Config_s *config)
// 还有其他方案可用,比如阻塞等待传输完成,但是比较笨.
// 注册实例
// 根据参数选择工作模式
if (config->work_mode == BMI088_BLOCK_PERIODIC_MODE)
{
config->spi_acc_config.spi_work_mode = SPI_BLOCK_MODE;
config->spi_gyro_config.spi_work_mode = SPI_BLOCK_MODE;
// callbacks are all NULL
}
else if (config->work_mode == BMI088_BLOCK_TRIGGER_MODE)
{
config->spi_gyro_config.spi_work_mode = SPI_DMA_MODE; // 如果DMA资源不够,可以用SPI_IT_MODE
config->spi_gyro_config.spi_work_mode = SPI_DMA_MODE;
// 设置回调函数
config->spi_acc_config.callback = BMI088AccSPIFinishCallback;
config->spi_gyro_config.callback = BMI088GyroSPIFinishCallback;
config->acc_int_config.gpio_model_callback = BMI088AccINTCallback;
config->gyro_int_config.gpio_model_callback = BMI088GyroINTCallback;
} // 注册实例
bmi088_instance->spi_acc = SPIRegister(&config->spi_acc_config);
bmi088_instance->spi_gyro = SPIRegister(&config->spi_gyro_config);
bmi088_instance->acc_int = GPIORegister(&config->acc_int_config);
@ -396,6 +449,12 @@ BMI088Instance *BMI088Register(BMI088_Init_Config_s *config)
error |= BMI088AccelInit(bmi088_instance);
error |= BMI088GyroInit(bmi088_instance);
// 尚未标定时先设置为默认值,使得数据拼接和缩放可以正常进行
bmi088_instance->acc_coef = 1.0; // 尚未初始化时设定为1,使得BMI088Acquire可以正常使用
bmi088_instance->BMI088_GYRO_SEN = BMI088_GYRO_2000_SEN; // 后续改为从initTable中获取
bmi088_instance->BMI088_ACCEL_SEN = BMI088_ACCEL_6G_SEN; // 用宏字符串拼接
// bmi088->gNorm =
// 标定acc和gyro
BMI088CalibrateIMU(bmi088_instance);

View File

@ -72,19 +72,19 @@ typedef struct
uint8_t acc_overrun : 1;
uint8_t temp_overrun : 1;
uint8_t imu_ready : 1; // 1:IMU数据准备好,0:IMU数据未准备好(gyro+acc)
// 后续可添加其他标志位
// 后续可添加其他标志位,不够用可以扩充16or32,太多可以删
} update_flag;
} BMI088Instance;
/* BMI088初始化配置 */
typedef struct
{
BMI088_Work_Mode_e work_mode;
BMI088_Calibrate_Mode_e cali_mode;
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 work_mode;
BMI088_Calibrate_Mode_e cali_mode;
PID_Init_Config_s heat_pid_config;
PWM_Init_Config_s heat_pwm_config;
} BMI088_Init_Config_s;
@ -105,4 +105,12 @@ BMI088Instance *BMI088Register(BMI088_Init_Config_s *config);
* @return BMI088_Data_t
*/
BMI088_Data_t BMI088Acquire(BMI088Instance *bmi088);
/**
* @brief
*
* @param _bmi088
*/
void BMI088CalibrateIMU(BMI088Instance *_bmi088);
#endif // !__BMI088_H__

View File

@ -15,7 +15,7 @@
static Vision_Recv_s recv_data;
// @todo:由于后续需要进行IMU-Cam的硬件触发采集控制,因此可能需要将发送设置为定时任务,或由IMU采集完成产生的中断唤醒的任务,
// 使得时间戳对齐. 因此,在send_data中设定其他的标志位数据,让ins_task填充姿态值.
// 后者显然更nice,使得时间戳对齐. 因此,在send_data中设定其他的标志位数据,让ins_task填充姿态值.
// static Vision_Send_s send_data;
static USARTInstance *vision_usart_instance;