📄 usbsample.c
字号:
/*
FreeRTOS.org V5.0.2 - Copyright (C) 2003-2008 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.
***************************************************************************
***************************************************************************
* *
* SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
* and even write all or part of your application on your behalf. *
* See http://www.OpenRTOS.com for details of the services we provide to *
* expedite your project. *
* *
***************************************************************************
***************************************************************************
Please ensure to read the configuration and relevant port sections of the
online documentation.
http://www.FreeRTOS.org - Documentation, latest information, license and
contact details.
http://www.SafeRTOS.com - A version that is certified for use in safety
critical systems.
http://www.OpenRTOS.com - Commercial support, development, porting,
licensing and training services.
*/
/*
Sample interrupt driven mouse device driver. This is a minimal implementation
for demonstration only. Although functional, it may not be a fully and
compliant implementation. The small joystick on the SAM7X EK can be used to
move the mouse cursor, pressing the joystick transmits mouse clicks. Note
that it might be necessary to run the demo stand along (without the
debugger) in order for the USB device to be recognised by the host computer.
The interrupt handler itself is contained within USB_ISR.c.
See the FreeRTOS.org online documentation for more information.
*/
/* Standard includes. */
#include <string.h>
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
/* Demo application includes. */
#include "USBSample.h"
/* Joystick inputs used to move the 'mouse' cursor. */
#define usbSW1 ( 1 << 21 ) /* PA21 */
#define usbSW2 ( 1 << 22 ) /* PA22 */
#define usbSW3 ( 1 << 23 ) /* PA23 */
#define usbSW4 ( 1 << 24 ) /* PA24 */
#define usbSW_CLICK ( 1 << 25 ) /* PA25 */
/* 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 usbFIFO_LENGTH ( ( unsigned portLONG ) 8 )
#define usbXUP ( 1 )
#define usbXDOWN ( 2 )
#define usbYUP ( 3 )
#define usbYDOWN ( 4 )
#define usbMAX_COORD ( 120 )
#define usbMAX_TX_MESSAGE_SIZE ( 128 )
#define usbSHORTEST_DELAY ( ( portTickType ) 1 )
#define usbINIT_DELAY ( ( portTickType ) 1000 / 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 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. This is implemented in USB_ISR.c.
*/
extern void vUSB_ISR_Wrapper( 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 );
/*
* Transmit movement and clicks on the EK joystick as mouse inputs.
*/
static void prvTransmitSampleValues( void );
/*
* The created task to handle the USB demo functionality.
*/
static void vUSBDemoTask( void *pvParameters );
/*
* Simple algorithm to ramp up the mouse cursor speed to make it easier to
* use.
*/
static void prvControlCursorSpeed( signed portCHAR *cVal, unsigned portLONG ulInput, unsigned portLONG ulSwitch1, unsigned portLONG ulSwitch2 );
/*-----------------------------------------------------------*/
/*
- 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[] =
{
38,
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,
'M', 0x00,
'o', 0x00,
'u', 0x00,
's', 0x00,
'e', 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,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -