📄 freescale
字号:
/*************************************************************************
* DISCLAIMER *
* Services performed by FREESCALE in this matter are performed *
* AS IS and without any warranty. CUSTOMER retains the final decision *
* relative to the total design and functionality of the end product. *
* FREESCALE neither guarantees nor will be held liable by CUSTOMER *
* for the success of this project. FREESCALE disclaims all warranties, *
* express, implied or statutory including, but not limited to, *
* implied warranty of merchantability or fitness for a particular *
* purpose on any hardware, software ore advise supplied to the project *
* by FREESCALE, and or any product resulting from FREESCALE services. *
* In no event shall FREESCALE be liable for incidental or consequential *
* damages arising out of this agreement. CUSTOMER agrees to hold *
* FREESCALE harmless against any and all claims demands or actions *
* by anyone on account of any damage, or injury, whether commercial, *
* contractual, or tortuous, rising directly or indirectly as a result *
* of the advise or assistance supplied CUSTOMER in connection with *
* product, services or goods supplied under this Agreement. *
*************************************************************************/
/*************************************************************************************************
* File name : Usb_Setup_Pkt.c
*
* Description : This file deal all events on endpoint 0 in enumeration process
*
*
* History :
* 04/01/2007 : Initial Development
*
*************************************************************************************************/
#include <stddef.h>
#include <MC9S08JM60.h>
#include "Usb_Bdt.h"
#include "Usb_Drv.h"
#include "Usb_Descriptor.h"
#include "Usr_Ep_Handler.h"
/*Local variable definition */
byte Ctrl_Trf_State; /* Control Transfer State*/
/* Global variable definition */
byte Ctrl_Trf_Session_Owner; /* Current transfer session owner*/
byte *pSrc; /* Data source pointer*/
byte *pObj; /* Data destination pointer*/
int Transfer_Cnt; /* Data counter*/
/* Local functions definition */
void USB_CtrlEP_SetupStage_Complete(void);
void USB_CtrlTrf_RxData(void);
void USB_CtrlTrf_TxData(void);
void USB_StdReq_Handler(void);
void USB_ClassReq_Handler(void);
void USB_StdGetDsc_Handler(void);
void USB_StdSetCfg_Handler(void);
void USB_StdGetStatus_Handler(void);
void USB_StdFeatureReq_Handler(void);
/* Global functions definition */
void USB_CtrlTrf_SetupStage_Processing(void);
void USB_CtrlTrf_OUT_Handler(void);
void USB_CtrlTrf_IN_Handler(void);
void USB_Prepare_Next_Trf(void);
/******************************************************************************
* Function: void USB_CtrlTrf_OUT_Handler(void)
* Input: None
* Output: None
* Overview: This function handles an OUT transaction according to
* which control transfer state is currently active.
*****************************************************************************/
void USB_CtrlTrf_OUT_Handler(void)
{
if(Ctrl_Trf_State == CTL_TRF_DATA_RX)
USB_CtrlTrf_RxData(); /*Data stage*/
else
USB_Prepare_Next_Trf(); /*Current transfer completes*/
return;
}
/******************************************************************************
* Function: void USB_CtrlTrf_RxData(void)
* Input: None
*
* Output: None
* Overview:
* Note: None
*****************************************************************************/
void USB_CtrlTrf_RxData(void)
{
int Byte_Num;
Byte_Num = (int)Bdtmap.ep0Bo.Cnt;
Transfer_Cnt = Transfer_Cnt + Byte_Num;
pSrc = (byte*)&CtrlTrf_Data;
while(Byte_Num)
{
*pObj = *pSrc; /*Please init the pObj when receiving data in enumeration process*/
pObj++;
pSrc++;
Byte_Num--;
}
if(Bdtmap.ep0Bo.Stat.McuCtlBit.DTS == 0)
Bdtmap.ep0Bo.Stat._byte = _SIE|_DATA1|_DTS;
else
Bdtmap.ep0Bo.Stat._byte = _SIE|_DATA0|_DTS;
return;
}
/******************************************************************************
* Function: void USB_CtrlTrf_IN_Handler(void)
* Input: None
* Output: None
* Overview: This function handles an IN transaction according to
* which controls transfer state is currently active.
* It will check if it is the set-address transaction
*
*****************************************************************************/
void USB_CtrlTrf_IN_Handler(void)
{
if(Usb_Device_State == ADDR_PENDING_STATE)
{
ADDR = Setup_Pkt.CtlAdd.bDevADR._byte;
if(ADDR > 0x00)
Usb_Device_State = ADDRESS_STATE;
else
Usb_Device_State = DEFAULT_STATE;
}
if(Ctrl_Trf_State == CTL_TRF_DATA_TX)
USB_CtrlTrf_TxData();
else
USB_Prepare_Next_Trf();
return;
}
/******************************************************************************
* Function: void USB_CtrlTrf_TxData(void)
* Input: None
* Output: None
* Overview:
*
*****************************************************************************/
void USB_CtrlTrf_TxData(void)
{
int Byte_To_Send;
if(Transfer_Cnt < EP0_BUFF_SIZE)
Byte_To_Send = Transfer_Cnt;
else
Byte_To_Send = EP0_BUFF_SIZE;
Bdtmap.ep0Bi.Cnt = (byte)(Byte_To_Send);
Transfer_Cnt = Transfer_Cnt - Byte_To_Send;
pObj = (byte*)&CtrlTrf_Data;
while(Byte_To_Send)
{
*pObj = *pSrc;
pObj++;
pSrc++;
Byte_To_Send--;
}
if(Ctrl_Trf_State == CTL_TRF_DATA_TX)
{
if(Bdtmap.ep0Bi.Stat.McuCtlBit.DATA == 0)
Bdtmap.ep0Bi.Stat._byte = _SIE|_DATA1|_DTS;
else
Bdtmap.ep0Bi.Stat._byte = _SIE|_DATA0|_DTS;
}
return;
}
/******************************************************************************
* Function: void USB_CtrlTrf_SetupStage_Processing(void)
*
* Input: None
*
* Output: None
*
* Overview: This function has 3 steps
* 1. init the control transfer state machine.
* 2. calls on each of the module that may know how to
* service the Setup Request from the host.
* 3. Once each of the modules has a chance to check if
* it is responsible for servicing the request
* then checks direction of the transfer to determine how
* to prepare EP0 for control transfer.
*
*****************************************************************************/
void USB_CtrlTrf_SetupStage_Processing(void)
{
Transfer_Cnt = 0;
Ctrl_Trf_State = WAIT_SETUP;
Ctrl_Trf_Session_Owner = CLASS_NULL;
USB_StdReq_Handler();
if(Ctrl_Trf_Session_Owner == CLASS_NULL) /*if it is not USB standard request*/
USB_ClassReq_Handler(); /*verify if it is class request*/
USB_CtrlEP_SetupStage_Complete();
return;
}
/******************************************************************************
* Function: void USB_CtrlEP_SetupStage_Complete(void)
* Input: None
*
* Output: None
* Overview: This function wrap up the ramaining tasks in servicing
* a Setup Request. Its main task is to set the endpoint
* controls appropriately for a given situation.
*****************************************************************************/
void USB_CtrlEP_SetupStage_Complete(void)
{
int Tmp;
if(Ctrl_Trf_Session_Owner == CLASS_NULL)
{
Bdtmap.ep0Bo.Cnt = EP0_BUFF_SIZE;
Bdtmap.ep0Bo.Addr = 0x0C; /*can calculate the address according the offset in Bdtmap*/
/*We can set the EPSTALL bit in EPCTL or the STALL bit in BDT register*/
/*EPCTL0_EPSTALL = 1;*/
Bdtmap.ep0Bo.Stat._byte = _SIE|_BDTSTALL;
Bdtmap.ep0Bi.Stat._byte = _SIE|_BDTSTALL;
}
else
{
if(Setup_Pkt.CtlReqT.DataDir == DEV_TO_HOST)
{
Tmp = ((LSB(Setup_Pkt.CtlPara.W_Length)<<8) | (MSB(Setup_Pkt.CtlPara.W_Length))); //adjust endian
if(Tmp < Transfer_Cnt)
Transfer_Cnt = Tmp;
USB_CtrlTrf_TxData();
Ctrl_Trf_State = CTL_TRF_DATA_TX;
Bdtmap.ep0Bo.Cnt = EP0_BUFF_SIZE;
Bdtmap.ep0Bo.Addr = 0x0C; /*can calculate the addree according the offset in Bdtmap*/
Bdtmap.ep0Bo.Stat._byte = _SIE;
Bdtmap.ep0Bi.Addr = 0x08; /*can calculate the addree according the offset in Bdtmap*/
Bdtmap.ep0Bi.Stat._byte = _SIE|_DATA1|_DTS;
}
else
{
Ctrl_Trf_State = CTL_TRF_DATA_RX;
Bdtmap.ep0Bi.Cnt = 0;
Bdtmap.ep0Bi.Addr = 0x08; /*can calculate the addree according the offset in Bdtmap*/
Bdtmap.ep0Bi.Stat._byte = _SIE|_DATA1|_DTS;
Bdtmap.ep0Bo.Cnt = EP0_BUFF_SIZE;
Bdtmap.ep0Bo.Addr = 0x0C; /*can calculate the addree according the offset in Bdtmap*/
Bdtmap.ep0Bo.Stat._byte = _SIE|_DATA1|_DTS;
}
}
CTL_TSUSPEND = 0;
return;
}
/******************************************************************************
* Function: void USB_Prepare_Next_Trf(void)
* Input: None
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -