更新了usb接口,修改了callback实现

This commit is contained in:
NeoZng 2023-02-09 21:42:16 +08:00
parent 4e2b750037
commit 8fa03012cf
5 changed files with 45 additions and 17 deletions

View File

@ -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 */
/**

View File

@ -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 */

View File

@ -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;
}

View File

@ -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); // 发送
}

View File

@ -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发送数据
void USBTransmit(uint8_t *buffer, uint16_t len); // 通过usb发送数据