📄 cdc.c
字号:
/*********************************************************************
* FileName: cdc.c
********************************************************************/
#include <p18cxxx.h>
#include "system\typedefs.h"
#include "system\usb\usb.h"
#pragma udata
byte cdc_rx_len; // total rx length
byte cdc_trf_state; // States are defined cdc.h
POINTER pCDCSrc; // Dedicated source pointer
POINTER pCDCDst; // Dedicated destination pointer
byte cdc_tx_len; // total tx length
byte cdc_mem_type; // _ROM, _RAM
LINE_CODING line_coding; // Buffer to store line coding information
CONTROL_SIGNAL_BITMAP control_signal_bitmap;
#define dummy_length 0x08
byte dummy_encapsulated_cmd_response[dummy_length];
#pragma code
/******************************************************************************
* Function: void USBCheckCDCRequest(void)
* PreCondition: None
* Input: None
* Output: None
* Side Effects: None
* Overview: This routine checks the setup data packet to see if it
* knows how to handle it
*****************************************************************************/
void USBCheckCDCRequest(void)
{
if(SetupPkt.Recipient != RCPT_INTF) return;
if(SetupPkt.RequestType != CLASS) return;
if((SetupPkt.bIntfID != CDC_COMM_INTF_ID)&&
(SetupPkt.bIntfID != CDC_DATA_INTF_ID)) return;
switch(SetupPkt.bRequest)
{
case SEND_ENCAPSULATED_COMMAND:
ctrl_trf_session_owner = MUID_CDC;
pSrc.bRam = (byte*)&dummy_encapsulated_cmd_response;
usb_stat.ctrl_trf_mem = _RAM;
LSB(wCount) = dummy_length;
break;
case GET_ENCAPSULATED_RESPONSE:
ctrl_trf_session_owner = MUID_CDC;
pDst.bRam = (byte*)&dummy_encapsulated_cmd_response;
break;
case SET_COMM_FEATURE: // Optional
break;
case GET_COMM_FEATURE: // Optional
break;
case CLEAR_COMM_FEATURE: // Optional
break;
case SET_LINE_CODING:
ctrl_trf_session_owner = MUID_CDC;
pDst.bRam = (byte*)&line_coding; // Set destination
break;
case GET_LINE_CODING:
ctrl_trf_session_owner = MUID_CDC;
pSrc.bRam = (byte*)&line_coding; // Set source
usb_stat.ctrl_trf_mem = _RAM; // Set memory type
LSB(wCount) = LINE_CODING_LENGTH; // Set data count
break;
case SET_CONTROL_LINE_STATE:
ctrl_trf_session_owner = MUID_CDC;
control_signal_bitmap._byte = LSB(SetupPkt.W_Value);
break;
case SEND_BREAK: // Optional
break;
default:
break;
}
}
/******************************************************************************
* Function: void CDCInitEP(void)
* PreCondition: None
* Input: None
* Output: None
* Side Effects: None
* Overview: CDCInitEP initializes CDC endpoints, buffer descriptors,
* internal state-machine, and variables.
* It should be called after the USB host has sent out a
* SET_CONFIGURATION request.
* See USBStdSetCfgHandler() in usb9.c for examples.
*****************************************************************************/
void CDCInitEP(void)
{
line_coding.dwDTERate._dword = 115200; // baud rate
line_coding.bCharFormat = 0x00; // 1 stop bit
line_coding.bParityType = 0x00; // None
line_coding.bDataBits = 0x08; // 5,6,7,8, or 16
cdc_trf_state = CDC_TX_READY;
cdc_rx_len = 0;
CDC_COMM_UEP = EP_IN|HSHK_EN; // Enable 1 Comm pipe
CDC_DATA_UEP = EP_OUT_IN|HSHK_EN; // Enable 2 data pipes
CDC_INT_BD_IN.ADR = (byte*)&cdc_notice; // Set buffer address
CDC_INT_BD_IN.Stat._byte = _UCPU|_DAT1; // Set status
CDC_BULK_BD_OUT.Cnt = sizeof(cdc_data_rx); // Set buffer size
CDC_BULK_BD_OUT.ADR = (byte*)&cdc_data_rx; // Set buffer address
CDC_BULK_BD_OUT.Stat._byte = _USIE|_DAT0|_DTSEN;// Set status
CDC_BULK_BD_IN.ADR = (byte*)&cdc_data_tx; // Set buffer size
CDC_BULK_BD_IN.Stat._byte = _UCPU|_DAT1; // Set buffer address
}
/******************************************************************************
* Function: byte getsUSBUSART(char *buffer,
* byte len)
* PreCondition: Value of input argument 'len' should be smaller than the
* maximum endpoint size responsible for receiving bulk
* data from USB host for CDC class.
* Input argument 'buffer' should point to a buffer area that
* is bigger or equal to the size specified by 'len'.
* Input: buffer : Pointer to where received bytes are to be stored
* len : The number of bytes expected.
* Output: The number of bytes copied to buffer.
*****************************************************************************/
byte getsUSBUSART(char *buffer, byte len)
{
cdc_rx_len = 0;
if(!mCDCUsartRxIsBusy())
{
if(len > CDC_BULK_BD_OUT.Cnt)
len = CDC_BULK_BD_OUT.Cnt;
for(cdc_rx_len = 0; cdc_rx_len < len; cdc_rx_len++)
buffer[cdc_rx_len] = cdc_data_rx[cdc_rx_len];
CDC_BULK_BD_OUT.Cnt = sizeof(cdc_data_rx);
mUSBBufferReady(CDC_BULK_BD_OUT);
}
return cdc_rx_len;
}
/******************************************************************************
* Function: void putsUSBUSART(char *data)
* PreCondition: cdc_trf_state must be in the CDC_TX_READY state.
*
* The string of characters pointed to by 'data' must equal
* to or smaller than 255 bytes.
* Input: data : Pointer to a null-terminated string of data.
* If a null character is not found, 255 bytes
* of data will be transferred to the host.
* Output: None
*****************************************************************************/
void putsUSBUSART(char *data)
{
byte len;
if(cdc_trf_state != CDC_TX_READY) return;
len = 0;
do
{
len++;
if(len == 255) break; // Break loop once max len is reached.
}while(*data++);
data-=len;
mUSBUSARTTxRam((byte*)data,len); // See cdc.h
}
/******************************************************************************
* Function: void putrsUSBUSART(const rom char *data)
* PreCondition: cdc_trf_state must be in the CDC_TX_READY state.
* The string of characters pointed to by 'data' must equal
* to or smaller than 255 bytes.
* Input: data : Pointer to a null-terminated string of data.
* If a null character is not found, 255 bytes
* of data will be transferred to the host.
* Output: None
*****************************************************************************/
void putrsUSBUSART(const rom char *data)
{
byte len;
if(cdc_trf_state != CDC_TX_READY) return;
len = 0;
do
{
len++;
if(len == 255) break; // Break loop once max len is reached.
}while(*data++);
data-=len;
mUSBUSARTTxRom((rom byte*)data,len); // See cdc.h
}
/******************************************************************************
* Function: void CDCTxService(void)
* PreCondition: None
* Input: None
* Output: None
* Side Effects: None
*****************************************************************************/
void CDCTxService(void)
{
byte byte_to_send;
if(mCDCUsartTxIsBusy()) return;
if(cdc_trf_state == CDC_TX_COMPLETING)
cdc_trf_state = CDC_TX_READY;
if(cdc_trf_state == CDC_TX_READY) return;
if(cdc_trf_state == CDC_TX_BUSY_ZLP)
{
CDC_BULK_BD_IN.Cnt = 0;
cdc_trf_state = CDC_TX_COMPLETING;
}
else if(cdc_trf_state == CDC_TX_BUSY)
{
if(cdc_tx_len > sizeof(cdc_data_tx))
byte_to_send = sizeof(cdc_data_tx);
else
byte_to_send = cdc_tx_len;
CDC_BULK_BD_IN.Cnt = byte_to_send;
cdc_tx_len = cdc_tx_len - byte_to_send;
pCDCDst.bRam = (byte*)&cdc_data_tx; // Set destination pointer
if(cdc_mem_type == _ROM) // Determine type of memory source
{
while(byte_to_send)
{
*pCDCDst.bRam = *pCDCSrc.bRom;
pCDCDst.bRam++;
pCDCSrc.bRom++;
byte_to_send--;
}
}
else
{
while(byte_to_send)
{
*pCDCDst.bRam = *pCDCSrc.bRam;
pCDCDst.bRam++;
pCDCSrc.bRam++;
byte_to_send--;
}
}
if(cdc_tx_len == 0)
{
if(CDC_BULK_BD_IN.Cnt == sizeof(cdc_data_tx))
cdc_trf_state = CDC_TX_BUSY_ZLP;
else
cdc_trf_state = CDC_TX_COMPLETING;
}
}
mUSBBufferReady(CDC_BULK_BD_IN);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -