?? usbsample.c
字號:
/*
FreeRTOS.org V4.2.1 - Copyright (C) 2003-2007 Richard Barry.
This file is part of the FreeRTOS.org distribution.
FreeRTOS.org is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FreeRTOS.org is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FreeRTOS.org; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
A special exception to the GPL can be applied should you wish to distribute
a combined work that includes FreeRTOS.org, without being obliged to provide
the source code for any proprietary components. See the licensing section
of http://www.FreeRTOS.org for full details of how and when the exception
can be applied.
***************************************************************************
See http://www.FreeRTOS.org for documentation, latest information, license
and contact details. Please ensure to read the configuration and relevant
port sections of the online documentation.
Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
with commercial development and support options.
***************************************************************************
*/
/*
Sample interrupt driven USB device driver. This is a minimal implementation
for demonstration only. Although functional, it is not a full and compliant
implementation.
The USB device enumerates as a simple 3 axis joystick, and once configured
transmits 3 axis of data which can be viewed from the USB host machine.
This file implements the USB interrupt service routine, and a demo FreeRTOS
task. The interrupt service routine handles the USB hardware - taking a
snapshot of the USB status at the point of the interrupt. The task receives
the status information from the interrupt for processing at the task level.
See the FreeRTOS.org WEB documentation for more information.
*/
/*
Changes from V2.5.5
+ Descriptors that have a length that is an exact multiple of usbFIFO_LENGTH
can now be transmitted. To this end an extra parameter has been
added to the prvSendControlData() function, and the state
eSENDING_EVEN_DESCRIPTOR has been introduced. Thanks to Scott Miller for
assisting with this contribution.
Changes from V2.6.0
+ Replaced the duplicated RX_DATA_BK0 in the interrupt mask with the
RX_DATA_BK1.
*/
/* Standard includes. */
#include <string.h>
/* Demo board includes. */
#include "board.h"
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
/* Descriptor type definitions. */
#define usbDESCRIPTOR_TYPE_DEVICE ( 0x01 )
#define usbDESCRIPTOR_TYPE_CONFIGURATION ( 0x02 )
#define usbDESCRIPTOR_TYPE_STRING ( 0x03 )
/* USB request type definitions. */
#define usbGET_REPORT_REQUEST ( 0x01 )
#define usbGET_IDLE_REQUEST ( 0x02 )
#define usbGET_PROTOCOL_REQUEST ( 0x03 )
#define usbSET_REPORT_REQUEST ( 0x09 )
#define usbSET_IDLE_REQUEST ( 0x0A )
#define usbSET_PROTOCOL_REQUEST ( 0x0B )
#define usbGET_CONFIGURATION_REQUEST ( 0x08 )
#define usbGET_STATUS_REQUEST ( 0x00 )
#define usbCLEAR_FEATURE_REQUEST ( 0x01 )
#define usbSET_FEATURE_REQUEST ( 0x03 )
#define usbSET_ADDRESS_REQUEST ( 0x05 )
#define usbGET_DESCRIPTOR_REQUEST ( 0x06 )
#define usbSET_CONFIGURATION_REQUEST ( 0x09 )
#define usbGET_INTERFACE_REQUEST ( 0x0A )
#define usbSET_INTERFACE_REQUEST ( 0x0B )
/* Misc USB definitions. */
#define usbDEVICE_CLASS_VENDOR_SPECIFIC ( 0xFF )
#define usbBUS_POWERED ( 0x80 )
#define usbHID_REPORT_DESCRIPTOR ( 0x22 )
#define AT91C_UDP_TRANSCEIVER_ENABLE ( *( ( unsigned long * ) 0xfffb0074 ) )
/* Index to the various string. */
#define usbLANGUAGE_STRING ( 0 )
#define usbMANUFACTURER_STRING ( 1 )
#define usbPRODUCT_STRING ( 2 )
#define usbCONFIGURATION_STRING ( 3 )
#define usbINTERFACE_STRING ( 4 )
/* Data indexes for reading the request from the xISRStatus.ucFifoData[]
into xUSB_REQUEST. The data order is designed for speed - so looks a
little odd. */
#define usbREQUEST_TYPE_INDEX ( 7 )
#define usbREQUEST_INDEX ( 6 )
#define usbVALUE_HIGH_BYTE ( 4 )
#define usbVALUE_LOW_BYTE ( 5 )
#define usbINDEX_HIGH_BYTE ( 2 )
#define usbINDEX_LOW_BYTE ( 3 )
#define usbLENGTH_HIGH_BYTE ( 0 )
#define usbLENGTH_LOW_BYTE ( 1 )
/* Misc application definitions. */
#define usbINTERRUPT_PRIORITY ( 3 )
#define usbQUEUE_LENGTH ( 0x3 ) /* Must have all bits set! */
#define usbFIFO_LENGTH ( ( unsigned portLONG ) 8 )
#define usbEND_POINT_0 ( 0 )
#define usbEND_POINT_1 ( 1 )
#define usbXUP ( 1 )
#define usbXDOWN ( 2 )
#define usbYUP ( 3 )
#define usbYDOWN ( 4 )
#define usbMAX_COORD ( 120 )
#define usbMAX_TX_MESSAGE_SIZE ( 128 )
#define usbRX_COUNT_MASK ( ( unsigned portLONG ) 0x7ff )
#define AT91C_UDP_STALLSENT AT91C_UDP_ISOERROR
#define usbSHORTEST_DELAY ( ( portTickType ) 1 )
#define usbINIT_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
#define usbSHORT_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
#define usbEND_POINT_RESET_MASK ( ( unsigned portLONG ) 0x0f )
#define usbDATA_INC ( ( portCHAR ) 5 )
#define usbEXPECTED_NUMBER_OF_BYTES ( ( unsigned portLONG ) 8 )
/* Control request types. */
#define usbSTANDARD_DEVICE_REQUEST ( 0 )
#define usbSTANDARD_INTERFACE_REQUEST ( 1 )
#define usbSTANDARD_END_POINT_REQUEST ( 2 )
#define usbCLASS_INTERFACE_REQUEST ( 5 )
/*-----------------------------------------------------------*/
/* Structure used to take a snapshot of the USB status from within the ISR. */
typedef struct X_ISR_STATUS
{
unsigned portLONG ulISR;
unsigned portLONG ulCSR0;
unsigned portCHAR ucFifoData[ 8 ];
} xISRStatus;
/* Structure used to hold the received requests. */
typedef struct
{
unsigned portCHAR ucReqType;
unsigned portCHAR ucRequest;
unsigned portSHORT usValue;
unsigned portSHORT usIndex;
unsigned portSHORT usLength;
} xUSB_REQUEST;
typedef enum
{
eNOTHING,
eJUST_RESET,
eJUST_GOT_CONFIG,
eJUST_GOT_ADDRESS,
eSENDING_EVEN_DESCRIPTOR,
eREADY_TO_SEND
} eDRIVER_STATE;
/* Structure used to control the data being sent to the host. */
typedef struct
{
unsigned portCHAR ucTxBuffer[ usbMAX_TX_MESSAGE_SIZE ];
unsigned portLONG ulNextCharIndex;
unsigned portLONG ulTotalDataLength;
} xTX_MESSAGE;
/*-----------------------------------------------------------*/
/*
* The USB interrupt service routine. This takes a snapshot of the USB
* device at the time of the interrupt, clears the interrupts, and posts
* the data to the USB processing task.
*/
__arm void vUSB_ISR( void );
/*
* Called after the bus reset interrupt - this function readies all the
* end points for communication.
*/
static void prvResetEndPoints( void );
/*
* Setup the USB hardware, install the interrupt service routine and
* initialise all the state variables.
*/
static void vInitUSBInterface( void );
/*
* Decode and act upon an interrupt generated by the control end point.
*/
static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage );
/*
* For simplicity requests are separated into device, interface, class
* interface and end point requests.
*
* Decode and handle standard device requests originating on the control
* end point.
*/
static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest );
/*
* For simplicity requests are separated into device, interface, class
* interface and end point requests.
*
* Decode and handle standard interface requests originating on the control
* end point.
*/
static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest );
/*
* For simplicity requests are separated into device, interface, class
* interface and end point requests.
*
* Decode and handle standard end point requests originating on the control
* end point.
*/
static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest );
/*
* For simplicity requests are separated into device, interface, class
* interface and end point requests.
*
* Decode and handle the class interface requests.
*/
static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest );
/*
* Setup the Tx buffer to send data in response to a control request.
*
* The data to be transmitted is buffered, the state variables are updated,
* then prvSendNextSegment() is called to start the transmission off. Once
* the first segment has been sent the remaining segments are transmitted
* in response to TXCOMP interrupts until the entire buffer has been
* sent.
*/
static void prvSendControlData( unsigned portCHAR *pucData, unsigned portSHORT usRequestedLength, unsigned portLONG ulLengthLeftToSend, portLONG lSendingDescriptor );
/*
* Examine the Tx buffer to see if there is any more data to be transmitted.
*
* If there is data to be transmitted then send the next segment. A segment
* can have a maximum of 8 bytes (this is defined as the maximum for the end
* point by the descriptor). The final segment may be less than 8 bytes if
* the total data length was not an exact multiple of 8.
*/
static void prvSendNextSegment( void );
/*
* A stall condition is forced each time the host makes a request that is not
* supported by this minimal implementation.
*
* A stall is forced by setting the appropriate bit in the end points control
* and status register.
*/
static void prvSendStall( void );
/*
* A NULL (or zero length packet) is transmitted in acknowledge the reception
* of certain events from the host.
*/
static void prvUSBTransmitNull( void );
/*
* When the host requests a descriptor this function is called to determine
* which descriptor is being requested and start its transmission.
*/
static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest );
/*
* This demo USB device enumerates as a simple 3 axis joystick. Once
* configured this function is periodically called to generate some sample
* joystick data.
*
* The x and y axis are made to move in a square. The z axis is made to
* repeatedly increment up to its maximum.
*/
static void prvTransmitSampleValues( void );
/*
* The created task to handle the USB demo functionality.
*/
void vUSBDemoTask( void *pvParameters );
/*-----------------------------------------------------------*/
/*
- DESCRIPTOR DEFINITIONS -
*/
/* String descriptors used during the enumeration process.
These take the form:
{
Length of descriptor,
Descriptor type,
Data
}
*/
const portCHAR pxLanguageStringDescriptor[] =
{
4,
usbDESCRIPTOR_TYPE_STRING,
0x09, 0x04
};
const portCHAR pxManufacturerStringDescriptor[] =
{
18,
usbDESCRIPTOR_TYPE_STRING,
'F', 0x00,
'r', 0x00,
'e', 0x00,
'e', 0x00,
'R', 0x00,
'T', 0x00,
'O', 0x00,
'S', 0x00
};
const portCHAR pxProductStringDescriptor[] =
{
44,
usbDESCRIPTOR_TYPE_STRING,
'F', 0x00,
'r', 0x00,
'e', 0x00,
'e', 0x00,
'R', 0x00,
'T', 0x00,
'O', 0x00,
'S', 0x00,
'.', 0x00,
'o', 0x00,
'r', 0x00,
'g', 0x00,
' ', 0x00,
'J', 0x00,
'o', 0x00,
'y', 0x00,
's', 0x00,
't', 0x00,
'i', 0x00,
'c', 0x00,
'k', 0x00
};
const portCHAR pxConfigurationStringDescriptor[] =
{
38,
usbDESCRIPTOR_TYPE_STRING,
'C', 0x00,
'o', 0x00,
'n', 0x00,
'f', 0x00,
'i', 0x00,
'g', 0x00,
'u', 0x00,
'r', 0x00,
'a', 0x00,
't', 0x00,
'i', 0x00,
'o', 0x00,
'n', 0x00,
' ', 0x00,
'N', 0x00,
'a', 0x00,
'm', 0x00,
'e', 0x00
};
const portCHAR pxInterfaceStringDescriptor[] =
{
30,
usbDESCRIPTOR_TYPE_STRING,
'I', 0x00,
'n', 0x00,
't', 0x00,
'e', 0x00,
'r', 0x00,
'f', 0x00,
'a', 0x00,
'c', 0x00,
'e', 0x00,
' ', 0x00,
'N', 0x00,
'a', 0x00,
'm', 0x00,
'e', 0x00
};
/* Enumeration descriptors. */
const portCHAR pxReportDescriptor[] =
{
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
0x09, 0x04, /* USAGE (Joystick) */
0xa1, 0x01, /* COLLECTION (Application) */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -