From 8fa03012cfd553045b28a6143bddabc7ac3f7ece Mon Sep 17 00:00:00 2001 From: NeoZng Date: Thu, 9 Feb 2023 21:42:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86usb=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3,=E4=BF=AE=E6=94=B9=E4=BA=86callback=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HAL_N_Middlewares/Inc/usbd_cdc_if.h | 2 +- HAL_N_Middlewares/Src/freertos.c | 1 - HAL_N_Middlewares/Src/usbd_cdc_if.c | 9 ++++++++- bsp/usb/bsp_usb.c | 27 ++++++++++++++++++++++----- bsp/usb/bsp_usb.h | 23 ++++++++++++++--------- 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/HAL_N_Middlewares/Inc/usbd_cdc_if.h b/HAL_N_Middlewares/Inc/usbd_cdc_if.h index 2c9a669..88a4e86 100644 --- a/HAL_N_Middlewares/Inc/usbd_cdc_if.h +++ b/HAL_N_Middlewares/Inc/usbd_cdc_if.h @@ -109,7 +109,7 @@ extern "C" uint8_t CDC_Transmit_FS(uint8_t *Buf, uint16_t Len); /* USER CODE BEGIN EXPORTED_FUNCTIONS */ -uint8_t *CDCInitRxbufferNcallback(void (*callback)(uint32_t)); +uint8_t *CDCInitRxbufferNcallback(void (*callback)(uint32_t),void (*callback2)(uint32_t)); /* USER CODE END EXPORTED_FUNCTIONS */ /** diff --git a/HAL_N_Middlewares/Src/freertos.c b/HAL_N_Middlewares/Src/freertos.c index e0a893a..99e9a51 100644 --- a/HAL_N_Middlewares/Src/freertos.c +++ b/HAL_N_Middlewares/Src/freertos.c @@ -147,7 +147,6 @@ void StartDefaultTask(void const * argument) { /* init code for USB_DEVICE */ USB_ResetPort(USB_OTG_FS); - USBD_FS_SPEED MX_USB_DEVICE_Init(); /* USER CODE BEGIN StartDefaultTask */ /* Infinite loop */ diff --git a/HAL_N_Middlewares/Src/usbd_cdc_if.c b/HAL_N_Middlewares/Src/usbd_cdc_if.c index 3742471..69e68fd 100644 --- a/HAL_N_Middlewares/Src/usbd_cdc_if.c +++ b/HAL_N_Middlewares/Src/usbd_cdc_if.c @@ -50,6 +50,8 @@ /* USER CODE BEGIN PRIVATE_TYPES */ void (*transmitcpltcallback)(uint32_t); + +void (*receivecpltcallback)(uint32_t); /* USER CODE END PRIVATE_TYPES */ /** @@ -261,6 +263,10 @@ 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); + if(receivecpltcallback != NULL) + { + receivecpltcallback(*Len); + } return (USBD_OK); /* USER CODE END 6 */ } @@ -291,9 +297,10 @@ uint8_t CDC_Transmit_FS(uint8_t *Buf, uint16_t Len) return result; } -uint8_t *CDCInitRxbufferNcallback(void (*callback)(uint32_t)) +uint8_t *CDCInitRxbufferNcallback(void (*callback)(uint32_t),void (*callback2)(uint32_t)) { transmitcpltcallback=callback; + receivecpltcallback=callback2; return (uint8_t *)UserRxBufferFS; } diff --git a/bsp/usb/bsp_usb.c b/bsp/usb/bsp_usb.c index 279a89c..aade217 100644 --- a/bsp/usb/bsp_usb.c +++ b/bsp/usb/bsp_usb.c @@ -1,8 +1,20 @@ +/** + * @file bsp_usb.c + * @author your name (you@domain.com) + * @brief usb是单例bsp,因此不保存实例 + * @version 0.1 + * @date 2023-02-09 + * + * @copyright Copyright (c) 2023 + * + */ + #include "bsp_usb.h" static uint8_t *bsp_usb_rx_buffer; // 接收到的数据会被放在这里,buffer size为2028 // 注意usb单个数据包(Full speed模式下)最大为64byte,超出可能会出现丢包情况 +// 这是传输完成的回调函数,在usbd_cdc_if.c中被调用 __weak void USBTransmitCpltCallback(uint32_t len) { // 本次发送的数据 @@ -10,12 +22,19 @@ __weak void USBTransmitCpltCallback(uint32_t len) // 传输完成会调用此函数,to do something } +__weak void USBReceiveCpltCallback(uint32_t len) +{ + // 本次接收的数据 + UNUSED(len); + // 传输完成会调用此函数,to do something +} + uint8_t *USBInit() { - // 上电后重新枚举usb设备 + // 上电后重新枚举usb设备 USBTransmit((uint8_t *)"USB DEVICE READY", sizeof("USB DEVICE READY")); // 发送初始化完成信息 - bsp_usb_rx_buffer = CDCInitRxbufferNcallback(USBTransmitCpltCallback); // 获取接收数据指针 - + bsp_usb_rx_buffer = CDCInitRxbufferNcallback(USBTransmitCpltCallback,USBReceiveCpltCallback); // 获取接收数据指针 + // usb的接收回调函数会在这里被设置,并将数据保存在bsp_usb_rx_buffer中 return bsp_usb_rx_buffer; } @@ -23,5 +42,3 @@ 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 de8d76d..61753b7 100644 --- a/bsp/usb/bsp_usb.h +++ b/bsp/usb/bsp_usb.h @@ -1,20 +1,25 @@ +/** + * @file bsp_usb.h + * @author your name (you@domain.com) + * @brief 提供对usb vpc(virtal com port)的操作接口,hid和msf考虑后续添加 + * @todo 与usart的接口统一完成兼容 + * @attention 这一版修改了usbd_cdc_if.c中的CDC_Receive_FS函数,cube生成后会被覆盖.后续需要由usbcdciftemplate创建一套自己的模板 + * @version 0.1 + * @date 2023-02-09 + * + * @copyright Copyright (c) 2023 + * + */ + #include "usb_device.h" #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 +void USBTransmit(uint8_t *buffer, uint16_t len); // 通过usb发送数据