/**
* @brief TMC_InterfaceDeInit
* The function DeInit the Host Channels used for the CDC class.
* @param pdev: Selected device
* @param hdev: Selected device property
* @retval None
*/
void TMC_InterfaceDeInit ( USB_OTG_CORE_HANDLE *pdev,
void *phost)
{
if ( TMC_Machine.TMC_CommItf.hc_num_in)
{
USB_OTG_HC_Halt(pdev, TMC_Machine.TMC_CommItf.hc_num_in);
USBH_Free_Channel (pdev,TMC_Machine.TMC_CommItf.hc_num_in);
TMC_Machine.TMC_CommItf.hc_num_in = 0; /* Reset the Channel as Free */
}
if ( TMC_Machine.TMC_DataItf.hc_num_out)
{
USB_OTG_HC_Halt(pdev, TMC_Machine.TMC_DataItf.hc_num_out);
USBH_Free_Channel (pdev,TMC_Machine.TMC_DataItf.hc_num_out);
TMC_Machine.TMC_DataItf.hc_num_out = 0; /* Reset the Channel as Free */
}
if ( TMC_Machine.TMC_DataItf.hc_num_in)
{
USB_OTG_HC_Halt(pdev, TMC_Machine.TMC_DataItf.hc_num_in);
USBH_Free_Channel (pdev,TMC_Machine.TMC_DataItf.hc_num_in);
TMC_Machine.TMC_DataItf.hc_num_in = 0; /* Reset the Channel as Free */
}
// return USBH_OK;
}
// REN control
// CTL a1 a0 01 00 00 00 01 00 REN CONTROL 418us 140.1.0 2022/09/23 13:36:30.907 ausbtmc
uint8_t cabitles[64];
uint8_t ren[4];
//a1 07 00 00 00 00 18 00 GET CAPABILITIES
USBH_Status TMC_GET_Capbilites(USB_OTG_CORE_HANDLE *pdev , USBH_HOST *phost)
{
phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_TYPE_CLASS | \
USB_REQ_RECIPIENT_INTERFACE;
phost->Control.setup.b.bRequest = 0x7;// TMC_GET_LINE_CODING;
phost->Control.setup.b.wValue.w = 0;
phost->Control.setup.b.wIndex.w =0;// TMC_Desc.TMC_UnionFuncDesc.bMasterInterface; /*At to be checked*/
phost->Control.setup.b.wLength.w = 0x18;//LINE_CODING_STRUCTURE_SIZE;
return USBH_CtlReq(pdev, phost, cabitles, 0x18);
}
USBH_Status TMC_REN(USB_OTG_CORE_HANDLE *pdev , USBH_HOST *phost)
{
phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_TYPE_CLASS | \
USB_REQ_RECIPIENT_INTERFACE;
phost->Control.setup.b.bRequest = USB_TMC_REQ_REN_CONTROL;// TMC_GET_LINE_CODING;
phost->Control.setup.b.wValue.w = 0x01;
phost->Control.setup.b.wIndex.w =0;//
phost->Control.setup.b.wLength.w = 0x1;
return USBH_CtlReq(pdev, phost, ren, 1);
}
USBH_Status TMC_initclear(USB_OTG_CORE_HANDLE *pdev , USBH_HOST *phost)
{
phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_TYPE_CLASS | \
USB_REQ_RECIPIENT_INTERFACE;
phost->Control.setup.b.bRequest = 0x5;// TMC_GET_LINE_CODING;
phost->Control.setup.b.wValue.w = 0x00;
phost->Control.setup.b.wIndex.w =0;//
phost->Control.setup.b.wLength.w = 0x1;
return USBH_CtlReq(pdev, phost, ren, 1);
}
USBH_Status TMC_Checkclear(USB_OTG_CORE_HANDLE *pdev , USBH_HOST *phost)
{
phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_TYPE_CLASS | \
USB_REQ_RECIPIENT_INTERFACE;
phost->Control.setup.b.bRequest = 06;// ;
phost->Control.setup.b.wValue.w = 0x00;
phost->Control.setup.b.wIndex.w =0;//
phost->Control.setup.b.wLength.w = 0x2;
return USBH_CtlReq(pdev, phost, ren, 2);
}
USBH_Status TMC_LOCK_OUT(USB_OTG_CORE_HANDLE *pdev , USBH_HOST *phost)
{
phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_TYPE_CLASS | \
USB_REQ_RECIPIENT_INTERFACE;
phost->Control.setup.b.bRequest = USB_TMC_REQ_LOCAL_LOCKOUT;// ;
phost->Control.setup.b.wValue.w = 0x00;
phost->Control.setup.b.wIndex.w =0;//
phost->Control.setup.b.wLength.w = 0x1;
return USBH_CtlReq(pdev, phost, cabitles, 1);
}
USBH_Status TMC_READ_Stb(USB_OTG_CORE_HANDLE *pdev , USBH_HOST *phost)
{
phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_TYPE_CLASS | \
USB_REQ_RECIPIENT_INTERFACE;
phost->Control.setup.b.bRequest = USB_TMC_REQ_LOCAL_LOCKOUT;// ;
phost->Control.setup.b.wValue.w = 0x00;
phost->Control.setup.b.wIndex.w =0;//
phost->Control.setup.b.wLength.w = 0x1;
return USBH_CtlReq(pdev, phost, cabitles, 1);
}
//a2 03 08 00 81 00 02 00 ABORT BULK IN
USBH_Status TMC__AbortBulkIn(USB_OTG_CORE_HANDLE *pdev, USBH_HOST *phost)
{
phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_TYPE_CLASS | \
USB_REQ_RECIPIENT_ENDPOINT;
phost->Control.setup.b.bRequest = 0;//TMC_SET_LINE_CODING;
phost->Control.setup.b.wValue.w = 0;
phost->Control.setup.b.wIndex.w = 0;//TMC_Desc.TMC_UnionFuncDesc.bMasterInterface;
phost->Control.setup.b.wLength.w =8;// LINE_CODING_STRUCTURE_SIZE;
return USBH_CtlReq(pdev, phost, 0 , 0 );
}
USBH_Status TMC__clear_out_ep(void)
{
USBH_Status status = USBH_BUSY;
status = USBH_ClrFeature(&USB_OTG_Core_fs,&USB_Host, TMC_Machine.TMC_DataItf.cdcOutEp, TMC_Machine.TMC_DataItf.hc_num_out);
return status;
}
USBH_Status TMC__clear_in_ep(void)
{
return USBH_ClrFeature(&USB_OTG_Core_fs,&USB_Host, TMC_Machine.TMC_DataItf.cdcInEp, TMC_Machine.TMC_DataItf.hc_num_in);
}
/**
//r:a1,80,0d00,0000,0003
//r:a1,80,0e00,0000,0003
//r:a1,05,0000,0000,0001
//r:a1,06,0000,0000,0002
//set in
//r:a1,05,0000,0000,0001
//r:a1,06,0000,0000,0002
a2 03 08 00 81 00 02 00 ABORT BULK IN
a1 07 00 00 00 00 18 00 GET CAPABILITIES
* @brief TMC_ClassRequest
* The function is responsible for handling CDC Class requests
* for CDC class.
* @param pdev: Selected device
* @param hdev: Selected device property
* @retval USBH_Status :Response for USB Set Protocol request
*/
static USBH_Status TMC_ClassRequest(USB_OTG_CORE_HANDLE *pdev ,
void *phost)
{
USBH_HOST *pphost = phost;
USBH_Status status = USBH_BUSY;
USBH_Status ClassReqStatus = USBH_BUSY;
switch(TMC_ReqState)
{
case TMC_GET_CAPABILITIES:
/*Issue the get line coding request*/
ClassReqStatus = TMC_GET_Capbilites(pdev, phost);
if( ClassReqStatus == USBH_OK )
{ /*Change the state */
TMC_ReqState = TMC_INTIT_CLEAR ;
}
break;
case TMC_REN_CTRL:
/*Issue the get line coding request*/
// ClassReqStatus = TMC_GET_Capbilites(pdev, phost);
// if( ClassReqStatus == USBH_OK )
// { /*Change the state */
// ClassReqStatus = TMC_initclear
// }
break;
case TMC_INTIT_CLEAR:
/*Issue the get line coding request*/
ClassReqStatus = TMC_initclear (pdev, phost);
if( ClassReqStatus == USBH_OK )
{ /*Change the state */
TMC_ReqState = TMC_CHECK_CLEAR;
}
break;
case TMC_CHECK_CLEAR:
/*Issue the get line coding request*/
ClassReqStatus = TMC_Checkclear(pdev, phost);
if( ClassReqStatus == USBH_OK )
{ /*Change the state */
status = USBH_OK;
//TMC_ReqState = TMC_ERROR_STATE;
}
break;
case TMC_ERROR_STATE:
//ClassReqStatus = TMC__clear_out_ep();
ClassReqStatus = USBH_ClrFeature(pdev, phost, 0x00, pphost->Control.hc_num_out);
if(ClassReqStatus == USBH_OK )
{
/*Change the state to waiting*/
//TMC_ReqState = TMC_GET_CAPABILITIES ;
status = USBH_OK;
}
break;
default:;
break;
}
return status;
}
/**
* @brief TMC_Handle
* The function is for managing state machine for CDC data transfers
* @param pdev: Selected device
* @param hdev: Selected device property
* @retval USBH_Status
*/
static USBH_Status TMC_Handle(USB_OTG_CORE_HANDLE *pdev ,
void *phost)
{
USBH_Status status = USBH_OK;
USBH_HOST *pphost = phost;
/* Call Application process */
pphost->usr_cb->UserApplication();
/*Handle the transmission */
TMC_ProcessTransmission(pdev, pphost);
/*Always send in packet to device*/
TMC_ProcessReception(pdev, pphost);
return status;
}
/**
* @brief The function is responsible for sending data to the device
* @param pdev: Selected device
* @retval None
*/
void TMC_ProcessTransmission(USB_OTG_CORE_HANDLE *pdev, USBH_HOST *phost)
{
static uint32_t len ;
URB_STATE URB_StatusTx = URB_IDLE;
URB_StatusTx = HCD_GetURB_State(pdev , TMC_Machine.TMC_DataItf.hc_num_out);
switch(TMC_TxParam.State)
{
case TMC_IDLE:
break;
case TMC_SEND_DATA:
if(( URB_StatusTx == URB_DONE ) || (URB_StatusTx == URB_IDLE))
{
/*Check the data length is more then the TMC_Machine.TMC_DataItf.TMC_DataItf.length */
if(TMC_TxParam.DataLength > TMC_Machine.TMC_DataItf.length)
{
len = TMC_Machine.TMC_DataItf.length ;
/*Send the data */
USBH_BulkSendData (pdev,
TMC_TxParam.pRxTxBuff,
len ,
TMC_Machine.TMC_DataItf.hc_num_out);
}
else
{
len = TMC_TxParam.DataLength ;
/*Send the remaining data */
USBH_BulkSendData (pdev,
TMC_TxParam.pRxTxBuff,
len,
TMC_Machine.TMC_DataItf.hc_num_out);
}
TMC_TxParam.State = TMC_DATA_SENT;
}
break;
case TMC_DATA_SENT:
/*Check the status done for transmssion*/
if(URB_StatusTx == URB_DONE )
{
/*Point to next chunc of data*/
TMC_TxParam.pRxTxBuff += len ;
/*decrease the data length*/
TMC_TxParam.DataLength -= len;
if(TMC_TxParam.DataLength == 0)
{
TMC_TxParam.State = TMC_IDLE;
}
else
{
TMC_TxParam.State = TMC_SEND_DATA;
}
}
else if( URB_StatusTx == URB_NOTREADY )
{
/*Send the same data */
USBH_BulkSendData (pdev,
(TMC_TxParam.pRxTxBuff),
len,
TMC_Machine.TMC_DataItf.hc_num_out);
}
break;
default:
break;
}
}
/**
* @brief This function responsible for reception of data from the device
* @param pdev: Selected device
* @retval None
*/
static void TMC_ProcessReception(USB_OTG_CORE_HANDLE *pdev, USBH_HOST *phost)
{
if(RX_Enabled == 1)
{
URB_STATE URB_StatusRx = HCD_GetURB_State(pdev , TMC_Machine.TMC_DataItf.hc_num_in);
switch(TMC_RxParam.State)
{
case TMC_IDLE:
/*check the received length lesser then the remaining space available in the
buffer */
if(TMC_RxParam.DataLength < (TMC_RxParam.BufferLen - TMC_Machine.TMC_DataItf.length))
{
/*Receive the data */
USBH_BulkReceiveData(pdev,
TMC_RxParam.pFillBuff,
TMC_Machine.TMC_DataItf.length,
TMC_Machine.TMC_DataItf.hc_num_in);
/*change the cdc state to USBH_TMC_GET_DATA*/
TMC_RxParam.State = TMC_GET_DATA;
}
break;
case TMC_GET_DATA:
/*Check the last state of the device is URB_DONE */
if(URB_StatusRx == URB_DONE)
{
/* Move the pointer as well as datalength */
TMC_RxParam.DataLength += pdev->host.hc[TMC_Machine.TMC_DataItf.hc_num_in].xfer_count ;
TMC_RxParam.pFillBuff += pdev->host.hc[TMC_Machine.TMC_DataItf.hc_num_in].xfer_count ;
/* Process the recived data */
TMC_ReceiveData(&TMC_RxParam);
/*change the state od the CDC state*/
TMC_RxParam.State = TMC_IDLE;
}
break;
default:
break;
}
}
}
/**
* @brief Initialize the transmit and receive buffer and its parameter
* @param None
* @retval None
*/
static void TMC_InitTxRxParam(void)
{
/*Initialize the Transmit buffer and its parameter*/
TMC_TxParam.State = TMC_IDLE;
TMC_TxParam.DataLength = 0;
TMC_TxParam.pRxTxBuff = TxBuf;
/*Initialize the Receive buffer and its parameter*/
TMC_RxParam.State = TMC_IDLE;
TMC_RxParam.DataLength = 0;
TMC_RxParam.pFillBuff = RxBuf;
TMC_RxParam.pEmptyBuff = RxBuf;
TMC_RxParam.BufferLen = sizeof(RxBuf);
}
/**
* @brief This is a call back function from cdc core layer to redirect the
* received data on the user out put system
* @param TMC_Data: type of USBH_CDCXfer_TypeDef
* @retval None
*/
static void TMC_ReceiveData(TMC_Xfer_TypeDef *TMC_Data)
{
uint8_t *ptr;
if(TMC_Data->pEmptyBuff < TMC_Data->pFillBuff)
{
ptr = TMC_Data->pFillBuff;
*ptr = 0x00;
/* redirect the received data on the user out put system */
UserCb.Receive(TMC_Data->pEmptyBuff, TMC_Data->DataLength);
TMC_Data->pFillBuff = TMC_Data->pEmptyBuff ;
TMC_Data->DataLength = 0; /*Reset the data length to zero*/
}
}
/**
* @brief This function send data to the device.
* @param fileName : name of the file
* @retval the filestate will be returned
* 0 - ok, 1 error
*/
int TMC_SendData(uint8_t *data, uint16_t length)
{
if(TMC_TxParam.State == TMC_IDLE)
{
TMC_TxParam.pRxTxBuff = data;
TMC_TxParam.DataLength = length; //CDC_SEND_DATA;
TMC_TxParam.State = TMC_SEND_DATA;
// UART_Send(data,length);
return 0;
}
return 1;
}
int GetTmcRxState(void)
{
return TMC_TxParam.State;
}
void TMC_StartReception( USB_OTG_CORE_HANDLE *pdev)
{
RX_Enabled = 1;
}
void TMC_StopReception( USB_OTG_CORE_HANDLE *pdev)
{
RX_Enabled = 0;
USB_OTG_HC_Halt(pdev, TMC_Machine.TMC_DataItf.hc_num_in);
USBH_Free_Channel(pdev,TMC_Machine.TMC_DataItf.hc_num_in);
}
成功驱动过示波器安捷伦万用表相关设备。