2 * FreeRTOS Kernel V10.1.1
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
30 BASIC INTERRUPT DRIVEN DRIVER FOR USB.
32 This file contains all the usb components that must be compiled
33 to ARM mode. The components that can be compiled to either ARM or THUMB
34 mode are contained in USB-CDC.c.
38 /* Scheduler includes. */
43 /* Demo application includes. */
48 #define usbINT_CLEAR_MASK (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )
49 /*-----------------------------------------------------------*/
51 /* Messages and queue used to communicate between the ISR and the USB task. */
52 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];
53 extern QueueHandle_t xUSBInterruptQueue;
54 /*-----------------------------------------------------------*/
56 /* The ISR can cause a context switch so is declared naked. */
57 void vUSB_ISR_Wrapper( void ) __attribute__ ((naked));
59 /* The function that actually performs the ISR work. This must be separate
60 from the wrapper function to ensure the correct stack frame gets set up. */
61 void vUSB_ISR_Handler( void );
62 /*-----------------------------------------------------------*/
64 void vUSB_ISR_Handler( void )
66 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
67 static volatile unsigned long ulNextMessage = 0;
68 xISRStatus *pxMessage;
69 unsigned long ulRxBytes;
70 unsigned char ucFifoIndex;
72 /* Use the next message from the array. */
73 pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );
76 /* Save UDP ISR state for task-level processing. */
77 pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;
78 pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
80 /* Clear interrupts from ICR. */
81 AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;
84 /* Process incoming FIFO data. Must set DIR (if needed) and clear RXSETUP
87 /* Read CSR and get incoming byte count. */
88 ulRxBytes = ( pxMessage->ulCSR0 >> 16 ) & usbRX_COUNT_MASK;
90 /* Receive control transfers on endpoint 0. */
91 if( pxMessage->ulCSR0 & ( AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 ) )
93 /* Save FIFO data buffer for either a SETUP or DATA stage */
94 for( ucFifoIndex = 0; ucFifoIndex < ulRxBytes; ucFifoIndex++ )
96 pxMessage->ucFifoData[ ucFifoIndex ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];
99 /* Set direction for data stage. Must be done before RXSETUP is
101 if( ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RXSETUP ) )
103 if( ulRxBytes && ( pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ] & 0x80 ) )
105 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] |= AT91C_UDP_DIR;
107 /* Might not be wise in an ISR! */
108 while( !(AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_DIR) );
112 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~AT91C_UDP_RXSETUP;
114 /* Might not be wise in an ISR! */
115 while ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RXSETUP );
119 /* Clear RX_DATA_BK0 */
120 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~AT91C_UDP_RX_DATA_BK0;
122 /* Might not be wise in an ISR! */
123 while ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RX_DATA_BK0 );
127 /* If we received data on endpoint 1, disable its interrupts until it is
128 processed in the main loop */
129 if( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & ( AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 ) )
131 AT91C_BASE_UDP->UDP_IDR = AT91C_UDP_EPINT1;
134 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~( AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT );
136 /* Clear interrupts for the other endpoints, retain data flags for endpoint
138 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] &= ~( AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP );
139 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_2 ] &= ~usbINT_CLEAR_MASK;
140 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_3 ] &= ~usbINT_CLEAR_MASK;
142 /* Post ISR data to queue for task-level processing */
143 xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );
145 /* Clear AIC to complete ISR processing */
146 AT91C_BASE_AIC->AIC_EOICR = 0;
148 /* Do a task switch if needed */
149 if( xHigherPriorityTaskWoken )
151 /* This call will ensure that the unblocked task will be executed
152 immediately upon completion of the ISR if it has a priority higher
153 than the interrupted task. */
154 portYIELD_FROM_ISR();
157 /*-----------------------------------------------------------*/
159 void vUSB_ISR_Wrapper( void )
161 /* Save the context of the interrupted task. */
164 /* Call the handler to do the work. This must be a separate
165 function to ensure the stack frame is set up correctly. */
168 /* Restore the context of whichever task will execute next. */
169 portRESTORE_CONTEXT();