你好 想请教下写关于变量的定义 无法理解变量为什么这么定义 谢谢~~~



你好,我在查看USB的底层驱动的时候看到变量的定义不知道为什么这么些,但是我知道都是有意思的,我不太明白为什么这么写

希望能得到您的帮助,谢谢。

unsigned long
USBBufferEventCallback(void *pvCBData, unsigned long ulEvent,
unsigned long ulMsgValue, void *pvMsgData)

问题一:

这里面有void *pvCBData 给出的定义是pvCBData is the client-supplied callback pointer associated with this buffer instance

这个我能明白是什么意思,也知道变量的意思,但是我不太明白pvCB 代表的是什么意思?是什么单词的缩写。

pvMsgData is an event-specific data pointer.这个里面的pv 又是代表什么意思,像unsigned long ulEvent 中的ulEvent 中的ul代表的是unsigned long 这个我一看

就能明白什么意思。我不太明白这些指针的英文缩写。

问题二:

//
//! A pointer to the callback function which will be called to notify
//! the application of all asynchronous control events related to the
//! operation of the device.
//
tUSBCallback pfnControlCallback; 这个里面的pfn 是代表的point function notify 么?

问题三:

//
//! A client-supplied pointer which will be sent as the first
//! parameter in all calls made to the control channel callback,
//! pfnControlCallback.
//
void *pvControlCBData;这个函数指针 pv 又是什么。

问题四:

我在做USB 开发的时候 

//
//! A pointer to the callback function which will be called to notify
//! the application of all asynchronous control events related to the
//! operation of the device.
//
tUSBCallback pfnControlCallback; 为什么自己重新写这个函数,也就是说我为什么要写这个函数,而不是调用库里面的函数,

我看见这个函数指针是用tUSBCallback 定义的,tUSBCallback的声明

typedef unsigned long (* tUSBCallback)(void *pvCBData, unsigned long ulEvent,
unsigned long ulMsgParam,
void *pvMsgData);

//
//! A pointer to the callback function which will be called to notify
//! the application of events related to the device's data receive channel.
//
tUSBCallback pfnRxCallback; 这个函数指针却不需要我在重新写,用库里面的定义就行了,

extern unsigned long USBBufferEventCallback(void *pvCBData,
unsigned long ulEvent,
unsigned long ulMsgValue,
void *pvMsgData);也就是这个函数。

TI 头文件的中结构体的定义:

//*****************************************************************************
//
//! The structure used by the application to define operating parameters for
//! the CDC device.
//
//*****************************************************************************
typedef struct
{
//
//! The vendor ID that this device is to present in the device descriptor.
//
unsigned short usVID;

//
//! The product ID that this device is to present in the device descriptor.
//
unsigned short usPID;

//
//! The maximum power consumption of the device, expressed in milliamps.
//
unsigned short usMaxPowermA;

//
//! Indicates whether the device is self- or bus-powered and whether or not
//! it supports remote wakeup. Valid values are USB_CONF_ATTR_SELF_PWR or
//! USB_CONF_ATTR_BUS_PWR, optionally ORed with USB_CONF_ATTR_RWAKE.
//
unsigned char ucPwrAttributes;

//
//! A pointer to the callback function which will be called to notify
//! the application of all asynchronous control events related to the
//! operation of the device.
//
tUSBCallback pfnControlCallback;

//
//! A client-supplied pointer which will be sent as the first
//! parameter in all calls made to the control channel callback,
//! pfnControlCallback.
//
void *pvControlCBData;

//
//! A pointer to the callback function which will be called to notify
//! the application of events related to the device's data receive channel.
//
tUSBCallback pfnRxCallback;

//
//! A client-supplied pointer which will be sent as the first
//! parameter in all calls made to the receive channel callback,
//! pfnRxCallback.
//
void *pvRxCBData;

//
//! A pointer to the callback function which will be called to notify
//! the application of events related to the device's data transmit
//! channel.
//
tUSBCallback pfnTxCallback;

//
//! A client-supplied pointer which will be sent as the first
//! parameter in all calls made to the transmit channel callback,
//! pfnTxCallback.
//
void *pvTxCBData;

//
//! A pointer to the string descriptor array for this device. This array
//! must contain the following string descriptor pointers in this order.
//! Language descriptor, Manufacturer name string (language 1), Product
//! name string (language 1), Serial number string (language 1),
//! Control interface description string (language 1), Configuration
//! description string (language 1).
//!
//! If supporting more than 1 language, the strings for indices 1 through 5
//! must be repeated for each of the other languages defined in the
//! language descriptor.
//
const unsigned char * const *ppStringDescriptors;

//
//! The number of descriptors provided in the ppStringDescriptors
//! array. This must be 1 + (5 * number of supported languages).
//
unsigned long ulNumStringDescriptors;

//
//! A pointer to the private instance data for this device. This memory
//! must remain accessible for as long as the CDC device is in use and must
//! not be modified by any code outside the CDC class driver.
//
tCDCSerInstance *psPrivateCDCSerData;
}
tUSBDCDCDevice;

而我对结构体填充的时候,我是这样做的

const tUSBDCDCDevice g_sCDCDevice =
{
USB_VID_STELLARIS, // 厂商ID -这个不能更改
USB_PID_SERIAL, // 产品ID
0, // 最大功耗 -本设备为自供电,所以设置为0
USB_CONF_ATTR_SELF_PWR, // 定义设备为自供电
USBControlHandler, // USB控制端口处理函数 -需自己实现
(void *)&g_sCDCDevice, // 回调函数需要传递的CDC类实例指针
USBBufferEventCallback, // 数据处理回调函数, -USB库实现
(void *)&g_sRxBuffer, // 接收数据缓冲区指针
USBBufferEventCallback, // 数据缓冲事件处理回调函数,-USB库实现
(void *)&g_sTxBuffer, // 数据发送缓冲区指针
g_pStringDescriptors, // 字符串描述符的列表
NUM_STRING_DESCRIPTORS, // 字符串描述符的个数
&g_sCDCInstance // CDC类的实例
};

我怎么判断哪些是我需要写的,哪里是可以调用的函数库里面的。

我需要怎么快时候的查到函数需要重新自己写???

问的比较乱,希望能得到您的回到。谢谢。

  • TI的USB底层驱动已经是模块化形式的,已经很完善了,基本不用你改,唯一要你改的就是USB通信过程中收发事件,即TxHandler和RxHandler,这个要根据你实际应用来修改,举个RxHandler里面接收的例子,比如当usb有接收到数据时,就会USB_EVENT_RX_AVAILABLE触发事件,然后你要进行一些数据的读取,用USBBufferRead函数来实现,发送的话就是直接用USBBufferDataWritten函数直接发送即可。

    至于你说的pv,ul,pfn等基本都是编程员的习惯缩写,一般p开头的都表示指针,比如pv就在你提到的变量表示以void类型的指针。关于TI的USB开发,可以交流一下,互相学习一下,因为这些USB内容本人已经在项目中一直执行并应用,希望能给你一些帮助

  • 谢谢你的回答,能否一起交流学习,我留下邮箱aizhixi@gmail.com 这个是我的邮箱。谢谢~~~

  • 您好。请问下TxHandler和RxHandler是如何调用的?多谢了。

  • usb中断触发,不用你调用,它属于usb收发的结构体里面,通过usb触发接收或者发送任务就能使能他们。源代码是:

    //*****************************************************************************
    //
    // Receive buffer (from the USB perspective).
    //
    //*****************************************************************************
    unsigned char g_pucUSBRxBuffer[BULK_BUFFER_SIZE];
    unsigned char g_pucRxBufferWorkspace[USB_BUFFER_WORKSPACE_SIZE];
    const tUSBBuffer g_sRxBuffer =
    {
    false, // This is a receive buffer.
    RxHandler, // pfnCallback
    (void *)&g_sBulkDevice, // Callback data is our device pointer.
    USBDBulkPacketRead, // pfnTransfer
    USBDBulkRxPacketAvailable, // pfnAvailable
    (void *)&g_sBulkDevice, // pvHandle
    g_pucUSBRxBuffer, // pcBuffer
    BULK_BUFFER_SIZE, // ulBufferSize
    g_pucRxBufferWorkspace // pvWorkspace
    };

    //*****************************************************************************
    //
    // Transmit buffer (from the USB perspective).
    //
    //*****************************************************************************
    unsigned char g_pucUSBTxBuffer[BULK_BUFFER_SIZE];
    unsigned char g_pucTxBufferWorkspace[USB_BUFFER_WORKSPACE_SIZE];
    const tUSBBuffer g_sTxBuffer =
    {
    true, // This is a transmit buffer.
    TxHandler, // pfnCallback
    (void *)&g_sBulkDevice, // Callback data is our device pointer.
    USBDBulkPacketWrite, // pfnTransfer
    USBDBulkTxPacketAvailable, // pfnAvailable
    (void *)&g_sBulkDevice, // pvHandle
    g_pucUSBTxBuffer, // pcBuffer
    BULK_BUFFER_SIZE, // ulBufferSize
    g_pucTxBufferWorkspace // pvWorkspace
    };

  • 多谢您了,可否问下系统是如何调用RxHandler和TxHandler的?我在usb中断服务程序里面并没有找到相关的内容啊。