From 637d7de1144a0c725a80efeca04b5d53957bcbd1 Mon Sep 17 00:00:00 2001 From: NeoZng Date: Sun, 5 Feb 2023 14:11:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86usb=E8=99=9A?= =?UTF-8?q?=E6=8B=9F=E4=B8=B2=E5=8F=A3=E7=9A=84=E6=94=AF=E6=8C=81,?= =?UTF-8?q?=E7=BC=96=E5=86=99=E4=BA=86=E9=83=A8=E5=88=86bmi088it=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HAL_N_Middlewares/Inc/usbd_cdc_if.h | 160 +++++----- HAL_N_Middlewares/Src/usbd_cdc_if.c | 375 ++++++++++++------------ Makefile | 2 + bsp/bsp_init.c | 2 + bsp/usb/bsp_usb.c | 28 +- bsp/usb/bsp_usb.h | 15 + bsp/usb/bsp_usb.md | 3 + modules/BMI088/bmi088.c | 87 +++++- modules/BMI088/bmi088.h | 14 +- modules/master_machine/master_process.c | 2 +- 10 files changed, 406 insertions(+), 282 deletions(-) create mode 100644 bsp/usb/bsp_usb.md diff --git a/HAL_N_Middlewares/Inc/usbd_cdc_if.h b/HAL_N_Middlewares/Inc/usbd_cdc_if.h index a9f47c1..2c9a669 100644 --- a/HAL_N_Middlewares/Inc/usbd_cdc_if.h +++ b/HAL_N_Middlewares/Inc/usbd_cdc_if.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file : usbd_cdc_if.h - * @version : v1.0_Cube - * @brief : Header for usbd_cdc_if.c file. - ****************************************************************************** - * @attention - * - * Copyright (c) 2023 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file : usbd_cdc_if.h + * @version : v1.0_Cube + * @brief : Header for usbd_cdc_if.c file. + ****************************************************************************** + * @attention + * + * Copyright (c) 2023 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. + * + ****************************************************************************** + */ /* USER CODE END Header */ /* Define to prevent recursive inclusion -------------------------------------*/ @@ -23,7 +23,8 @@ #define __USBD_CDC_IF_H__ #ifdef __cplusplus - extern "C" { +extern "C" +{ #endif /* Includes ------------------------------------------------------------------*/ @@ -34,98 +35,97 @@ /* USER CODE END INCLUDE */ /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY - * @brief For Usb device. - * @{ - */ + * @brief For Usb device. + * @{ + */ /** @defgroup USBD_CDC_IF USBD_CDC_IF - * @brief Usb VCP device module - * @{ - */ + * @brief Usb VCP device module + * @{ + */ /** @defgroup USBD_CDC_IF_Exported_Defines USBD_CDC_IF_Exported_Defines - * @brief Defines. - * @{ - */ + * @brief Defines. + * @{ + */ /* 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 */ +#define APP_RX_DATA_SIZE 2048 +#define APP_TX_DATA_SIZE 2048 + /* 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 - * @brief 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 - * @brief Aliases. - * @{ - */ + /** @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 - * @brief Public 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 - * @brief Public functions declaration. - * @{ - */ + /** @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 */ + /** + * @} + */ -/** - * @} - */ + /** + * @} + */ -/** - * @} - */ - -/** - * @} - */ + /** + * @} + */ #ifdef __cplusplus } #endif #endif /* __USBD_CDC_IF_H__ */ - diff --git a/HAL_N_Middlewares/Src/usbd_cdc_if.c b/HAL_N_Middlewares/Src/usbd_cdc_if.c index 2cb391e..3742471 100644 --- a/HAL_N_Middlewares/Src/usbd_cdc_if.c +++ b/HAL_N_Middlewares/Src/usbd_cdc_if.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file : usbd_cdc_if.c - * @version : v1.0_Cube - * @brief : Usb device for Virtual Com Port. - ****************************************************************************** - * @attention - * - * Copyright (c) 2023 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file : usbd_cdc_if.c + * @version : v1.0_Cube + * @brief : Usb device for Virtual Com Port. + ****************************************************************************** + * @attention + * + * Copyright (c) 2023 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. + * + ****************************************************************************** + */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ @@ -35,76 +35,75 @@ /* USER CODE END PV */ /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY - * @brief Usb device library. - * @{ - */ + * @brief Usb device library. + * @{ + */ /** @addtogroup USBD_CDC_IF - * @{ - */ + * @{ + */ /** @defgroup USBD_CDC_IF_Private_TypesDefinitions USBD_CDC_IF_Private_TypesDefinitions - * @brief Private types. - * @{ - */ + * @brief Private types. + * @{ + */ /* USER CODE BEGIN PRIVATE_TYPES */ - +void (*transmitcpltcallback)(uint32_t); /* USER CODE END PRIVATE_TYPES */ /** - * @} - */ + * @} + */ /** @defgroup USBD_CDC_IF_Private_Defines USBD_CDC_IF_Private_Defines - * @brief Private defines. - * @{ - */ + * @brief Private defines. + * @{ + */ /* USER CODE BEGIN PRIVATE_DEFINES */ /* USER CODE END PRIVATE_DEFINES */ /** - * @} - */ + * @} + */ /** @defgroup USBD_CDC_IF_Private_Macros USBD_CDC_IF_Private_Macros - * @brief Private macros. - * @{ - */ + * @brief Private macros. + * @{ + */ /* USER CODE BEGIN PRIVATE_MACRO */ /* USER CODE END PRIVATE_MACRO */ /** - * @} - */ + * @} + */ /** @defgroup USBD_CDC_IF_Private_Variables USBD_CDC_IF_Private_Variables - * @brief Private variables. - * @{ - */ + * @brief Private variables. + * @{ + */ /* 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 */ /** - * @} - */ + * @} + */ /** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables - * @brief Public variables. - * @{ - */ + * @brief Public variables. + * @{ + */ extern USBD_HandleTypeDef hUsbDeviceFS; @@ -113,18 +112,18 @@ extern USBD_HandleTypeDef hUsbDeviceFS; /* USER CODE END EXPORTED_VARIABLES */ /** - * @} - */ + * @} + */ /** @defgroup USBD_CDC_IF_Private_FunctionPrototypes USBD_CDC_IF_Private_FunctionPrototypes - * @brief Private functions declaration. - * @{ - */ + * @brief Private functions declaration. + * @{ + */ 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 */ @@ -132,187 +131,197 @@ static int8_t CDC_TransmitCplt_FS(uint8_t *pbuf, uint32_t *Len, uint8_t epnum); /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */ /** - * @} - */ + * @} + */ USBD_CDC_ItfTypeDef USBD_Interface_fops_FS = -{ - CDC_Init_FS, - CDC_DeInit_FS, - CDC_Control_FS, - CDC_Receive_FS, - CDC_TransmitCplt_FS -}; + { + CDC_Init_FS, + CDC_DeInit_FS, + CDC_Control_FS, + CDC_Receive_FS, + CDC_TransmitCplt_FS}; /* Private functions ---------------------------------------------------------*/ /** - * @brief Initializes the CDC media low layer over the FS USB IP - * @retval USBD_OK if all operations are OK else USBD_FAIL - */ + * @brief Initializes the CDC media low layer over the FS USB IP + * @retval USBD_OK if all operations are OK else USBD_FAIL + */ static int8_t CDC_Init_FS(void) { - /* USER CODE BEGIN 3 */ - /* Set Application Buffers */ - USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0); - USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS); - return (USBD_OK); - /* USER CODE END 3 */ + /* USER CODE BEGIN 3 */ + /* Set Application Buffers */ + USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0); + USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS); + return (USBD_OK); + /* USER CODE END 3 */ } /** - * @brief DeInitializes the CDC media low layer - * @retval USBD_OK if all operations are OK else USBD_FAIL - */ + * @brief DeInitializes the CDC media low layer + * @retval USBD_OK if all operations are OK else USBD_FAIL + */ static int8_t CDC_DeInit_FS(void) { - /* USER CODE BEGIN 4 */ - return (USBD_OK); - /* USER CODE END 4 */ + /* USER CODE BEGIN 4 */ + return (USBD_OK); + /* USER CODE END 4 */ } /** - * @brief Manage the CDC class requests - * @param cmd: Command code - * @param pbuf: Buffer containing command data (request parameters) - * @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) + * @brief Manage the CDC class requests + * @param cmd: Command code + * @param pbuf: Buffer containing command data (request parameters) + * @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) { - /* USER CODE BEGIN 5 */ - switch(cmd) - { + /* USER CODE BEGIN 5 */ + switch (cmd) + { case CDC_SEND_ENCAPSULATED_COMMAND: - break; + break; case CDC_GET_ENCAPSULATED_RESPONSE: - break; + break; case CDC_SET_COMM_FEATURE: - break; + break; case CDC_GET_COMM_FEATURE: - break; + break; case CDC_CLEAR_COMM_FEATURE: - break; + break; - /*******************************************************************************/ - /* Line Coding Structure */ - /*-----------------------------------------------------------------------------*/ - /* Offset | Field | Size | Value | Description */ - /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/ - /* 4 | bCharFormat | 1 | Number | Stop bits */ - /* 0 - 1 Stop bit */ - /* 1 - 1.5 Stop bits */ - /* 2 - 2 Stop bits */ - /* 5 | bParityType | 1 | Number | Parity */ - /* 0 - None */ - /* 1 - Odd */ - /* 2 - Even */ - /* 3 - Mark */ - /* 4 - Space */ - /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */ - /*******************************************************************************/ + /*******************************************************************************/ + /* Line Coding Structure */ + /*-----------------------------------------------------------------------------*/ + /* Offset | Field | Size | Value | Description */ + /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/ + /* 4 | bCharFormat | 1 | Number | Stop bits */ + /* 0 - 1 Stop bit */ + /* 1 - 1.5 Stop bits */ + /* 2 - 2 Stop bits */ + /* 5 | bParityType | 1 | Number | Parity */ + /* 0 - None */ + /* 1 - Odd */ + /* 2 - Even */ + /* 3 - Mark */ + /* 4 - Space */ + /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */ + /*******************************************************************************/ case CDC_SET_LINE_CODING: - break; + break; case CDC_GET_LINE_CODING: - break; + break; case CDC_SET_CONTROL_LINE_STATE: - break; + break; case CDC_SEND_BREAK: - break; + break; - default: - break; - } + default: + break; + } - return (USBD_OK); - /* USER CODE END 5 */ + return (USBD_OK); + /* USER CODE END 5 */ } /** - * @brief Data received over USB OUT endpoint are sent over CDC interface - * through this function. - * - * @note - * This function will issue a NAK packet on any OUT packet received on - * USB endpoint until exiting this function. If you exit this function - * before transfer is complete on CDC interface (ie. using DMA controller) - * it will result in receiving more data while previous ones are still - * not sent. - * - * @param Buf: Buffer of data to be received - * @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) + * @brief Data received over USB OUT endpoint are sent over CDC interface + * through this function. + * + * @note + * This function will issue a NAK packet on any OUT packet received on + * USB endpoint until exiting this function. If you exit this function + * before transfer is complete on CDC interface (ie. using DMA controller) + * it will result in receiving more data while previous ones are still + * not sent. + * + * @param Buf: Buffer of data to be received + * @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) { - /* USER CODE BEGIN 6 */ - USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]); - USBD_CDC_ReceivePacket(&hUsbDeviceFS); - return (USBD_OK); - /* USER CODE END 6 */ + /* USER CODE BEGIN 6 */ + USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]); + USBD_CDC_ReceivePacket(&hUsbDeviceFS); + return (USBD_OK); + /* USER CODE END 6 */ } /** - * @brief CDC_Transmit_FS - * Data to send over USB IN endpoint are sent over CDC interface - * through this function. - * @note - * - * - * @param Buf: Buffer of data to be sent - * @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) + * @brief CDC_Transmit_FS + * Data to send over USB IN endpoint are sent over CDC interface + * through this function. + * @note + * + * + * @param Buf: Buffer of data to be sent + * @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 result = USBD_OK; - /* USER CODE BEGIN 7 */ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData; - if (hcdc->TxState != 0){ - return USBD_BUSY; - } - USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len); - result = USBD_CDC_TransmitPacket(&hUsbDeviceFS); - /* USER CODE END 7 */ - return result; + uint8_t result = USBD_OK; + /* USER CODE BEGIN 7 */ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)hUsbDeviceFS.pClassData; + if (hcdc->TxState != 0) + { + return USBD_BUSY; + } + USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len); + result = USBD_CDC_TransmitPacket(&hUsbDeviceFS); + /* USER CODE END 7 */ + return result; +} + +uint8_t *CDCInitRxbufferNcallback(void (*callback)(uint32_t)) +{ + transmitcpltcallback=callback; + return (uint8_t *)UserRxBufferFS; } /** - * @brief CDC_TransmitCplt_FS - * Data transmitted callback - * - * @note - * This function is IN transfer complete callback used to inform user that - * the submitted Data is successfully sent over USB. - * - * @param Buf: Buffer of data to be received - * @param Len: Number of data received (in bytes) - * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL - */ + * @brief CDC_TransmitCplt_FS + * Data transmitted callback + * + * @note + * This function is IN transfer complete callback used to inform user that + * the submitted Data is successfully sent over USB. + * + * @param Buf: Buffer of data to be received + * @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_TransmitCplt_FS(uint8_t *Buf, uint32_t *Len, uint8_t epnum) { - uint8_t result = USBD_OK; - /* USER CODE BEGIN 13 */ - UNUSED(Buf); - UNUSED(Len); - UNUSED(epnum); - /* USER CODE END 13 */ - return result; + uint8_t result = USBD_OK; + /* USER CODE BEGIN 13 */ + UNUSED(Buf); + UNUSED(Len); + UNUSED(epnum); + if(transmitcpltcallback!=NULL) + { + transmitcpltcallback(*Len); + } + /* USER CODE END 13 */ + return result; } /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */ @@ -320,9 +329,9 @@ static int8_t CDC_TransmitCplt_FS(uint8_t *Buf, uint32_t *Len, uint8_t epnum) /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */ /** - * @} - */ + * @} + */ /** - * @} - */ + * @} + */ diff --git a/Makefile b/Makefile index 2353934..59c0c40 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/bsp/bsp_init.c b/bsp/bsp_init.c index 134dbe4..49d5bf2 100644 --- a/bsp/bsp_init.c +++ b/bsp/bsp_init.c @@ -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(); diff --git a/bsp/usb/bsp_usb.c b/bsp/usb/bsp_usb.c index cb7eb20..331f148 100644 --- a/bsp/usb/bsp_usb.c +++ b/bsp/usb/bsp_usb.c @@ -1 +1,27 @@ -#include "bsp_usb.h" \ No newline at end of file +#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); // 发送 +} + + diff --git a/bsp/usb/bsp_usb.h b/bsp/usb/bsp_usb.h index 2d7ad03..de8d76d 100644 --- a/bsp/usb/bsp_usb.h +++ b/bsp/usb/bsp_usb.h @@ -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发送数据 \ No newline at end of file diff --git a/bsp/usb/bsp_usb.md b/bsp/usb/bsp_usb.md new file mode 100644 index 0000000..41dd3c3 --- /dev/null +++ b/bsp/usb/bsp_usb.md @@ -0,0 +1,3 @@ +# bsp usb + +简单写点,有待优化. \ No newline at end of file diff --git a/modules/BMI088/bmi088.c b/modules/BMI088/bmi088.c index 0570b0d..03eb0d4 100644 --- a/modules/BMI088/bmi088.c +++ b/modules/BMI088/bmi088.c @@ -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 现在要考虑一下数据返回的方式,指针还是结构体? 7个float数据有点费时,不然用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); diff --git a/modules/BMI088/bmi088.h b/modules/BMI088/bmi088.h index 5b59f7f..25a2144 100644 --- a/modules/BMI088/bmi088.h +++ b/modules/BMI088/bmi088.h @@ -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__ \ No newline at end of file diff --git a/modules/master_machine/master_process.c b/modules/master_machine/master_process.c index 6ec15ba..09cc659 100644 --- a/modules/master_machine/master_process.c +++ b/modules/master_machine/master_process.c @@ -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;