2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
31 BASIC INTERRUPT DRIVEN DRIVER FOR USB.
\r
33 This file contains all the usb components that must be compiled
\r
34 to ARM mode. The components that can be compiled to either ARM or THUMB
\r
35 mode are contained in USB-CDC.c.
\r
39 /* Scheduler includes. */
\r
40 #include "FreeRTOS.h"
\r
44 /* Demo application includes. */
\r
47 #include "USB-CDC.h"
\r
49 #define usbINT_CLEAR_MASK (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )
\r
50 /*-----------------------------------------------------------*/
\r
52 /* Messages and queue used to communicate between the ISR and the USB task. */
\r
53 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];
\r
54 extern QueueHandle_t xUSBInterruptQueue;
\r
55 /*-----------------------------------------------------------*/
\r
57 /* The ISR can cause a context switch so is declared naked. */
\r
58 void vUSB_ISR_Wrapper( void ) __attribute__ ((naked));
\r
60 /* The function that actually performs the ISR work. This must be separate
\r
61 from the wrapper function to ensure the correct stack frame gets set up. */
\r
62 void vUSB_ISR_Handler( void );
\r
63 /*-----------------------------------------------------------*/
\r
65 void vUSB_ISR_Handler( void )
\r
67 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
68 static volatile unsigned long ulNextMessage = 0;
\r
69 xISRStatus *pxMessage;
\r
70 unsigned long ulRxBytes;
\r
71 unsigned char ucFifoIndex;
\r
73 /* Use the next message from the array. */
\r
74 pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );
\r
77 /* Save UDP ISR state for task-level processing. */
\r
78 pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;
\r
79 pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
81 /* Clear interrupts from ICR. */
\r
82 AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;
\r
85 /* Process incoming FIFO data. Must set DIR (if needed) and clear RXSETUP
\r
88 /* Read CSR and get incoming byte count. */
\r
89 ulRxBytes = ( pxMessage->ulCSR0 >> 16 ) & usbRX_COUNT_MASK;
\r
91 /* Receive control transfers on endpoint 0. */
\r
92 if( pxMessage->ulCSR0 & ( AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 ) )
\r
94 /* Save FIFO data buffer for either a SETUP or DATA stage */
\r
95 for( ucFifoIndex = 0; ucFifoIndex < ulRxBytes; ucFifoIndex++ )
\r
97 pxMessage->ucFifoData[ ucFifoIndex ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];
\r
100 /* Set direction for data stage. Must be done before RXSETUP is
\r
102 if( ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RXSETUP ) )
\r
104 if( ulRxBytes && ( pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ] & 0x80 ) )
\r
106 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] |= AT91C_UDP_DIR;
\r
108 /* Might not be wise in an ISR! */
\r
109 while( !(AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_DIR) );
\r
112 /* Clear RXSETUP */
\r
113 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~AT91C_UDP_RXSETUP;
\r
115 /* Might not be wise in an ISR! */
\r
116 while ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RXSETUP );
\r
120 /* Clear RX_DATA_BK0 */
\r
121 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~AT91C_UDP_RX_DATA_BK0;
\r
123 /* Might not be wise in an ISR! */
\r
124 while ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RX_DATA_BK0 );
\r
128 /* If we received data on endpoint 1, disable its interrupts until it is
\r
129 processed in the main loop */
\r
130 if( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & ( AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 ) )
\r
132 AT91C_BASE_UDP->UDP_IDR = AT91C_UDP_EPINT1;
\r
135 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~( AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT );
\r
137 /* Clear interrupts for the other endpoints, retain data flags for endpoint
\r
139 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] &= ~( AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP );
\r
140 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_2 ] &= ~usbINT_CLEAR_MASK;
\r
141 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_3 ] &= ~usbINT_CLEAR_MASK;
\r
143 /* Post ISR data to queue for task-level processing */
\r
144 xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );
\r
146 /* Clear AIC to complete ISR processing */
\r
147 AT91C_BASE_AIC->AIC_EOICR = 0;
\r
149 /* Do a task switch if needed */
\r
150 if( xHigherPriorityTaskWoken )
\r
152 /* This call will ensure that the unblocked task will be executed
\r
153 immediately upon completion of the ISR if it has a priority higher
\r
154 than the interrupted task. */
\r
155 portYIELD_FROM_ISR();
\r
158 /*-----------------------------------------------------------*/
\r
160 void vUSB_ISR_Wrapper( void )
\r
162 /* Save the context of the interrupted task. */
\r
163 portSAVE_CONTEXT();
\r
165 /* Call the handler to do the work. This must be a separate
\r
166 function to ensure the stack frame is set up correctly. */
\r
167 vUSB_ISR_Handler();
\r
169 /* Restore the context of whichever task will execute next. */
\r
170 portRESTORE_CONTEXT();
\r