?? chap_9.c
字號:
/****************************************Copyright (c)**************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: Chap_9.c
** Last modified Date: 2005-8-6
** Last Version: V1.0
** Descriptions: chap_9.c, 實現USB1.1協議
** chap_9.c, realize USB1.1 protocol
**------------------------------------------------------------------------------------------------------
** Created by: 鄭明遠 MingYuan Zheng
** Created date: 2005-8-6
** Version: V1.0
** Descriptions: 初始版本 The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include "config.h"
#include "USBConfig.h"
#include "USBCI.h"
#include "Descriptor.h"
#include "Chap_9.h"
#include "USBDriver.h"
/* define control transfer structure variable */
CONTROL_XFER ControlData; /* 定義傳輸控制結構變量 */
/*************************************************************************
** 函數名稱: USB 標準設備請求入口地址指針表
** Function: Entry address of USB standard device request Function
**************************************************************************/
void (*StandardDeviceRequest[])(void) =
{
get_status,
clear_feature,
reserved,
set_feature,
reserved,
set_address,
get_descriptor,
reserved,
get_configuration,
set_configuration,
get_interface,
set_interface,
reserved,
reserved,
reserved,
reserved
};
//*************************************************************************
// USB 協議層函數 USB Protocal Function
//*************************************************************************
/*************************************************************************************************
** 函數名稱: stall_ep0() Name: stall_ep0()
** 功能描述: 使控制端點處于停止狀態 Function: stall logical endpoint 0
*************************************************************************************************/
void stall_ep0(void)
{
USB_SetEndpointStatus(0, 1);
USB_SetEndpointStatus(1, 1);
}
/*************************************************************************************************
** 函數名稱: reserved() Name: reserved()
** 功能描述: 保留子程序 Function: reserved function
*************************************************************************************************/
void reserved(void)
{
stall_ep0();
}
/*************************************************************************************************
** 函數名稱: init_unconfig() Name: init_unconfig()
** 功能描述: 進入地址狀態,禁止 0 除外的所有端點 Function: disable all endpoints except for control endpoint 0
*************************************************************************************************/
void init_unconfig(void)
{
USB_SetEndpointEnable(0);
}
/*************************************************************************************************
** 函數名稱: init_config() Name: init_config()
** 功能描述: 配置處理,允許端點收發 Function: enable all endpoints
*************************************************************************************************/
void init_config(void)
{
USB_SetEndpointEnable(1);
}
/*************************************************************************************************
** 函數名稱: single_transmit() Name: single_transmit()
** 功能描述: 通過物理端點 1 發送數據 Function: send data by physical endpoint 1
** 輸 入: INT8U * buf: 發送數據指針 Input: INT8U * buf: send buffer
INT8U len: 發送數據長度 INT8U len: send data length
** 輸 出: 無 Output: NULL
*************************************************************************************************/
void single_transmit(INT8U * buf, INT8U len)
{
/* the len must below the maxpacket size of physical endpoint 1 */
if( len <= EP0_PACKET_SIZE)
{ /* len 必須小于端點的最大包信息長度 */
USB_WriteEndpoint(1, len, buf);
}
}
/*************************************************************************************************
** 函數名稱: code_transmit() Name: code_transmit()
** 功能描述: 通過物理端點 1 發送數據 Function: send data by physical endpoint 1
** 輸 入: INT8U *pRomData: 發送數據指針 Inptut: INT8U * buf: send buffer
INT16U len: 發送數據長度 INT8U len: send data length(the value can above
EP0_PACKET_SIZE)
** 輸 出: 無 Output: NULL
*************************************************************************************************/
void code_transmit(INT8U * pRomData, INT16U len)
{
ControlData.wCount = 0;
if(ControlData.wLength > len)
ControlData.wLength = len; /* 長度要小于len the wLength can't above len */
ControlData.pData = pRomData;
if( ControlData.wLength >= EP0_PACKET_SIZE)
{ /* wLength above MaxPacketSize of physical endpoint 1 */
USB_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData);
ControlData.wCount += EP0_PACKET_SIZE; /* 計數器累加 count number have been sent */
DISABLE();
bEPPflags.bits.control_state = USB_TRANSMIT; /* 標識發送狀態 flag transmit state */
ENABLE();
}
else
{
USB_WriteEndpoint(1, ControlData.wLength, pRomData); /* 將全部數據寫入端點 write all data into endpoint */
ControlData.wCount += ControlData.wLength; /* 計數器累加 count number have been sent */
DISABLE();
bEPPflags.bits.control_state = USB_IDLE; /* 標識空閑狀態 flag IDLE state */
ENABLE();
}
}
//*************************************************************************
// USB 標準設備請求服務程序 USB standard device request service program
//*************************************************************************
/***************************************************************************************************************
** 函數名稱: get_status() Name: get_status()
** 功能描述: 主機要求獲取狀態,設備返回16位的 Function: the USB host request get the status of USB device,
狀態描述符給主機 USB device will response 16-bit descriptor
****************************************************************************************************************/
void get_status(void)
{
INT8U endp, txdat[2], c;
INT8U bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
/* 獲取請求類型 get the Request type */
if (bRecipient == USB_RECIPIENT_DEVICE)
{ /* request device */
if(bEPPflags.bits.remote_wakeup == 1)
txdat[0] = 3; /* 支持遠程喚醒和自供電 support reamote wakeup and power itself */
else
txdat[0] = 1; /* 不支持以上兩個功能 not support two function above */
txdat[1]=0; /* 高 8 位清 0 upper 8-bit clear */
single_transmit(txdat, 2); /* 發送16ibt到USB主機 transmit 16-bit to USB host */
}
else if (bRecipient == USB_RECIPIENT_INTERFACE)
{ /* request interface */
txdat[0]=0;
txdat[1]=0;
single_transmit(txdat, 2); /* 發送16ibt到USB主機 transmit 16-bit to USB host */
}
else if (bRecipient == USB_RECIPIENT_ENDPOINT)
{ /* request endpoint */
endp = (INT8U)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
if (ControlData.DeviceRequest.wIndex & (INT8U)USB_ENDPOINT_DIRECTION_MASK)
c = USB_SelectEndpoint(endp * 2 + 1); /* 讀 IN 端點狀態 read status of the IN endpoint */
else
c = USB_SelectEndpoint(endp * 2); /* 讀 OUT 端點狀態 read status of the OUT endpoint */
if(c & USB_STALL)
txdat[0] = 1; /* 端點已被禁止 the endpoint is stalled */
else
txdat[0] = 0; /* 端點已解禁 the endpoint is unstalled */
txdat[1] = 0;
single_transmit(txdat, 2); /* 發送16ibt到USB主機 transmit 16-bit to USB host */
}
else
stall_ep0(); /* 非標準請求,禁止邏輯端點0 */
/* not standard request, stall logical endpoint 0 */
}
/***************************************************************************************************************
** 函數名稱: clear_feature() Name: clear_feature()
** 功能描述: 清除特性 Function: clear feature
****************************************************************************************************************/
void clear_feature(void)
{
INT8U endp;
INT8U bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
/* 獲取請求類型 get request type */
if (bRecipient == USB_RECIPIENT_DEVICE
&& ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP)
{ /* request device */
DISABLE();
bEPPflags.bits.remote_wakeup = 0; /* 清除遠程喚醒特性 clear reamote wakeup feature */
ENABLE();
single_transmit(0, 0); /* 返回一個空包 return an empty packet */
}
else if (bRecipient == USB_RECIPIENT_ENDPOINT
&& ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL)
{
/* request endpoint */
endp = (INT8U)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
if (ControlData.DeviceRequest.wIndex & (INT8U)USB_ENDPOINT_DIRECTION_MASK)
USB_SetEndpointStatus(endp * 2 + 1, 0); /* 解禁 IN 端點 the IN endpoint is unstalled */
else
USB_SetEndpointStatus(endp * 2, 0); /* 解禁 OUT 端點 the OUT endpoint is unstalled */
single_transmit(0, 0); /* 發送空包表示執行成功 return an empty packet indicate perform sucessfully */
} else
stall_ep0(); /* 不成功,禁止邏輯端點0 not the request, stall logical endpoint 0 */
}
/***************************************************************************************************************
** 函數名稱: set_feature() Name: set_feature()
** 功能描述: 設置特性 Function: set feature
****************************************************************************************************************/
void set_feature(void)
{
INT8U endp;
INT8U bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
/* 獲取請求類型 get request type */
if (bRecipient == USB_RECIPIENT_DEVICE /* 請求設備 request device */
&& ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP)
{
DISABLE();
bEPPflags.bits.remote_wakeup = 1; /* 設置遠程喚醒標志 set reamote wakeup flag */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -