]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_AT91SAM7X256_Eclipse/RTOSDemo/USB/USB_ISR.c
Update version number ready for V8.2.1 release.
[freertos] / FreeRTOS / Demo / ARM7_AT91SAM7X256_Eclipse / RTOSDemo / USB / USB_ISR.c
1 /*\r
2     FreeRTOS V8.2.1 - Copyright (C) 2015 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /* Scheduler includes. */\r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 #include "queue.h"\r
74 \r
75 /* Demo app includes. */\r
76 #include "USBSample.h"\r
77 \r
78 #define usbINT_CLEAR_MASK       (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )\r
79 \r
80 #define usbCSR_CLEAR_BIT( pulValueNow, ulBit )                                                                                  \\r
81 {                                                                                                                                                                               \\r
82         /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */                                                                                         \\r
83         /* STALLSENT and RX_DATA_BK1 to 1 so the */                                                                                     \\r
84         /* write has no effect. */                                                                                                                      \\r
85         ( * ( ( unsigned long * ) pulValueNow ) ) |= ( unsigned long ) 0x4f;            \\r
86                                                                                                                                                                                 \\r
87         /* Clear the FORCE_STALL and TXPKTRDY bits */                                                                           \\r
88         /* so the write has no effect. */                                                                                                       \\r
89         ( * ( ( unsigned long * ) pulValueNow ) ) &= ( unsigned long ) 0xffffffcf;      \\r
90                                                                                                                                                                                 \\r
91         /* Clear whichever bit we want clear. */                                                                                        \\r
92         ( * ( ( unsigned long * ) pulValueNow ) ) &= ( ~ulBit );                                                \\r
93 }\r
94 \r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 /*\r
99  * ISR entry point.\r
100  */\r
101 \r
102 void vUSB_ISR_Wrapper( void ) __attribute__((naked));\r
103 \r
104 /*\r
105  * Actual ISR handler.  This must be separate from the entry point as the stack\r
106  * is used.\r
107  */\r
108 void vUSB_ISR_Handler( void ) __attribute__((noinline));\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /* Array in which the USB interrupt status is passed between the ISR and task. */\r
113 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];\r
114 \r
115 /* Queue used to pass messages between the ISR and the task. */\r
116 extern QueueHandle_t xUSBInterruptQueue; \r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 void vUSB_ISR_Handler( void )\r
121 {\r
122 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE; \r
123 static volatile unsigned long ulNextMessage = 0;\r
124 xISRStatus *pxMessage;\r
125 unsigned long ulTemp, ulRxBytes;\r
126 \r
127         /* To reduce the amount of time spent in this interrupt it would be \r
128         possible to defer the majority of this processing to an 'interrupt task',\r
129         that is a task that runs at a higher priority than any of the application\r
130         tasks. */\r
131 \r
132         /* Take the next message from the queue.  Note that usbQUEUE_LENGTH *must*\r
133         be all 1's, as in 0x01, 0x03, 0x07, etc. */\r
134         pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );\r
135         ulNextMessage++;\r
136 \r
137         /* Take a snapshot of the current USB state for processing at the task\r
138         level. */\r
139         pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;\r
140         pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
141 \r
142         /* Clear the interrupts from the ICR register.  The bus end interrupt is\r
143         cleared separately as it does not appear in the mask register. */\r
144         AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;\r
145         \r
146         /* If there are bytes in the FIFO then we have to retrieve them here.  \r
147         Ideally this would be done at the task level.  However we need to clear the\r
148         RXSETUP interrupt before leaving the ISR, and this may cause the data in\r
149         the FIFO to be overwritten.  Also the DIR bit has to be changed before the\r
150         RXSETUP bit is cleared (as per the SAM7 manual). */\r
151         ulTemp = pxMessage->ulCSR0;\r
152         \r
153         /* Are there any bytes in the FIFO? */\r
154         ulRxBytes = ulTemp >> 16;\r
155         ulRxBytes &= usbRX_COUNT_MASK;\r
156         \r
157         /* With this minimal implementation we are only interested in receiving \r
158         setup bytes on the control end point. */\r
159         if( ( ulRxBytes > 0 ) && ( ulTemp & AT91C_UDP_RXSETUP ) )\r
160         {\r
161                 /* Take off 1 for a zero based index. */\r
162                 while( ulRxBytes > 0 )\r
163                 {\r
164                         ulRxBytes--;\r
165                         pxMessage->ucFifoData[ ulRxBytes ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];                 \r
166                 }\r
167                 \r
168                 /* The direction must be changed first. */\r
169                 usbCSR_SET_BIT( &ulTemp, ( AT91C_UDP_DIR ) );\r
170                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;\r
171         }\r
172         \r
173         /* Must write zero's to TXCOMP, STALLSENT, RXSETUP, and the RX DATA\r
174         registers to clear the interrupts in the CSR register. */\r
175         usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );\r
176         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;\r
177 \r
178         /* Also clear the interrupts in the CSR1 register. */\r
179         ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];\r
180         usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK ); \r
181         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;\r
182 \r
183         /* The message now contains the entire state and optional data from\r
184         the USB interrupt.  This can now be posted on the Rx queue ready for\r
185         processing at the task level. */\r
186         xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );\r
187 \r
188         /* We may want to switch to the USB task, if this message has made\r
189         it the highest priority task that is ready to execute. */\r
190         if( xHigherPriorityTaskWoken )\r
191         {\r
192                 portYIELD_FROM_ISR();\r
193         }\r
194 \r
195         /* Clear the AIC ready for the next interrupt. */               \r
196         AT91C_BASE_AIC->AIC_EOICR = 0;\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 void vUSB_ISR_Wrapper( void )\r
201 {\r
202         /* Save the context of the interrupted task. */\r
203         portSAVE_CONTEXT();\r
204 \r
205         /* Call the handler itself.  This must be a separate function as it uses\r
206         the stack. */\r
207         __asm volatile ("bl vUSB_ISR_Handler");\r
208 \r
209         /* Restore the context of the task that is going to \r
210         execute next. This might not be the same as the originally \r
211         interrupted task.*/\r
212         portRESTORE_CONTEXT();\r
213 }\r