1 |
633c646a
|
henryk
|
/*
|
2 |
|
|
FreeRTOS.org V4.2.1 - Copyright (C) 2003-2007 Richard Barry.
|
3 |
|
|
|
4 |
|
|
This file is part of the FreeRTOS.org distribution.
|
5 |
|
|
|
6 |
|
|
FreeRTOS.org is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
9 |
|
|
(at your option) any later version.
|
10 |
|
|
|
11 |
|
|
FreeRTOS.org is distributed in the hope that it will be useful,
|
12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
|
|
GNU General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with FreeRTOS.org; if not, write to the Free Software
|
18 |
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 |
|
|
|
20 |
|
|
A special exception to the GPL can be applied should you wish to distribute
|
21 |
|
|
a combined work that includes FreeRTOS.org, without being obliged to provide
|
22 |
|
|
the source code for any proprietary components. See the licensing section
|
23 |
|
|
of http://www.FreeRTOS.org for full details of how and when the exception
|
24 |
|
|
can be applied.
|
25 |
|
|
|
26 |
|
|
***************************************************************************
|
27 |
|
|
See http://www.FreeRTOS.org for documentation, latest information, license
|
28 |
|
|
and contact details. Please ensure to read the configuration and relevant
|
29 |
|
|
port sections of the online documentation.
|
30 |
|
|
***************************************************************************
|
31 |
|
|
*/
|
32 |
|
|
|
33 |
|
|
|
34 |
|
|
/*
|
35 |
|
|
BASIC INTERRUPT DRIVEN DRIVER FOR USB.
|
36 |
|
|
|
37 |
|
|
This file contains all the usb components that must be compiled
|
38 |
|
|
to ARM mode. The components that can be compiled to either ARM or THUMB
|
39 |
|
|
mode are contained in USB-CDC.c.
|
40 |
|
|
|
41 |
|
|
*/
|
42 |
|
|
|
43 |
|
|
/* Scheduler includes. */
|
44 |
|
|
#include <FreeRTOS.h>
|
45 |
|
|
#include <task.h>
|
46 |
|
|
#include <queue.h>
|
47 |
|
|
|
48 |
|
|
/* Demo application includes. */
|
49 |
|
|
#include <board.h>
|
50 |
|
|
#include <usb.h>
|
51 |
|
|
#include <USB-CDC.h>
|
52 |
|
|
|
53 |
|
|
#define usbINT_CLEAR_MASK (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )
|
54 |
|
|
/*-----------------------------------------------------------*/
|
55 |
|
|
|
56 |
|
|
/* Messages and queue used to communicate between the ISR and the USB task. */
|
57 |
|
|
static xISRStatus xISRMessages[usbQUEUE_LENGTH + 1];
|
58 |
|
|
extern xQueueHandle xUSBInterruptQueue;
|
59 |
|
|
/*-----------------------------------------------------------*/
|
60 |
|
|
|
61 |
|
|
/* The ISR can cause a context switch so is declared naked. */
|
62 |
|
|
void vUSB_ISR (void) __attribute__ ((naked));
|
63 |
|
|
|
64 |
|
|
/*-----------------------------------------------------------*/
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
void
|
68 |
|
|
vUSB_ISR (void)
|
69 |
|
|
{
|
70 |
|
|
/* This ISR can cause a context switch. Therefore a call to the
|
71 |
|
|
portENTER_SWITCHING_ISR() macro is made. This must come BEFORE any
|
72 |
|
|
stack variable declarations. */
|
73 |
|
|
portENTER_SWITCHING_ISR ();
|
74 |
|
|
|
75 |
|
|
/* Now variables can be declared. */
|
76 |
|
|
portCHAR cTaskWokenByPost = pdFALSE;
|
77 |
|
|
static volatile unsigned portLONG ulNextMessage = 0;
|
78 |
|
|
xISRStatus *pxMessage;
|
79 |
|
|
unsigned portLONG ulRxBytes;
|
80 |
|
|
unsigned portCHAR ucFifoIndex;
|
81 |
|
|
|
82 |
|
|
/* Use the next message from the array. */
|
83 |
|
|
pxMessage = &(xISRMessages[(ulNextMessage & usbQUEUE_LENGTH)]);
|
84 |
|
|
ulNextMessage++;
|
85 |
|
|
|
86 |
|
|
/* Save UDP ISR state for task-level processing. */
|
87 |
|
|
pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;
|
88 |
|
|
pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[usbEND_POINT_0];
|
89 |
|
|
|
90 |
|
|
/* Clear interrupts from ICR. */
|
91 |
|
|
AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
/* Process incoming FIFO data. Must set DIR (if needed) and clear RXSETUP
|
95 |
|
|
before exit. */
|
96 |
|
|
|
97 |
|
|
/* Read CSR and get incoming byte count. */
|
98 |
|
|
ulRxBytes = (pxMessage->ulCSR0 >> 16) & usbRX_COUNT_MASK;
|
99 |
|
|
|
100 |
|
|
/* Receive control transfers on endpoint 0. */
|
101 |
|
|
if (pxMessage->ulCSR0 & (AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0))
|
102 |
|
|
{
|
103 |
|
|
/* Save FIFO data buffer for either a SETUP or DATA stage */
|
104 |
|
|
for (ucFifoIndex = 0; ucFifoIndex < ulRxBytes; ucFifoIndex++)
|
105 |
|
|
{
|
106 |
|
|
pxMessage->ucFifoData[ucFifoIndex] =
|
107 |
|
|
AT91C_BASE_UDP->UDP_FDR[usbEND_POINT_0];
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
/* Set direction for data stage. Must be done before RXSETUP is
|
111 |
|
|
cleared. */
|
112 |
|
|
if ((AT91C_BASE_UDP->UDP_CSR[usbEND_POINT_0] & AT91C_UDP_RXSETUP))
|
113 |
|
|
{
|
114 |
|
|
if (ulRxBytes
|
115 |
|
|
&& (pxMessage->ucFifoData[usbREQUEST_TYPE_INDEX] & 0x80))
|
116 |
|
|
{
|
117 |
|
|
AT91C_BASE_UDP->UDP_CSR[usbEND_POINT_0] |= AT91C_UDP_DIR;
|
118 |
|
|
|
119 |
|
|
/* Might not be wise in an ISR! */
|
120 |
|
|
while (!
|
121 |
|
|
(AT91C_BASE_UDP->
|
122 |
|
|
UDP_CSR[usbEND_POINT_0] & AT91C_UDP_DIR));
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
/* Clear RXSETUP */
|
126 |
|
|
AT91C_BASE_UDP->UDP_CSR[usbEND_POINT_0] &= ~AT91C_UDP_RXSETUP;
|
127 |
|
|
|
128 |
|
|
/* Might not be wise in an ISR! */
|
129 |
|
|
while (AT91C_BASE_UDP->UDP_CSR[usbEND_POINT_0] & AT91C_UDP_RXSETUP);
|
130 |
|
|
}
|
131 |
|
|
else
|
132 |
|
|
{
|
133 |
|
|
/* Clear RX_DATA_BK0 */
|
134 |
|
|
AT91C_BASE_UDP->UDP_CSR[usbEND_POINT_0] &= ~AT91C_UDP_RX_DATA_BK0;
|
135 |
|
|
|
136 |
|
|
/* Might not be wise in an ISR! */
|
137 |
|
|
while (AT91C_BASE_UDP->
|
138 |
|
|
UDP_CSR[usbEND_POINT_0] & AT91C_UDP_RX_DATA_BK0);
|
139 |
|
|
}
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/* If we received data on endpoint 1, disable its interrupts until it is
|
143 |
|
|
processed in the main loop */
|
144 |
|
|
if (AT91C_BASE_UDP->
|
145 |
|
|
UDP_CSR[usbEND_POINT_1] & (AT91C_UDP_RX_DATA_BK0 |
|
146 |
|
|
AT91C_UDP_RX_DATA_BK1))
|
147 |
|
|
{
|
148 |
|
|
AT91C_BASE_UDP->UDP_IDR = AT91C_UDP_EPINT1;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
AT91C_BASE_UDP->UDP_CSR[usbEND_POINT_0] &=
|
152 |
|
|
~(AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT);
|
153 |
|
|
|
154 |
|
|
/* Clear interrupts for the other endpoints, retain data flags for endpoint
|
155 |
|
|
1. */
|
156 |
|
|
AT91C_BASE_UDP->UDP_CSR[usbEND_POINT_1] &=
|
157 |
|
|
~(AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP);
|
158 |
|
|
AT91C_BASE_UDP->UDP_CSR[usbEND_POINT_2] &= ~usbINT_CLEAR_MASK;
|
159 |
|
|
AT91C_BASE_UDP->UDP_CSR[usbEND_POINT_3] &= ~usbINT_CLEAR_MASK;
|
160 |
|
|
|
161 |
|
|
/* Post ISR data to queue for task-level processing */
|
162 |
|
|
cTaskWokenByPost =
|
163 |
|
|
xQueueSendFromISR (xUSBInterruptQueue, &pxMessage, cTaskWokenByPost);
|
164 |
|
|
|
165 |
|
|
/* Clear AIC to complete ISR processing */
|
166 |
|
|
AT91C_BASE_AIC->AIC_EOICR = 0;
|
167 |
|
|
|
168 |
|
|
/* Do a task switch if needed */
|
169 |
|
|
portEXIT_SWITCHING_ISR (cTaskWokenByPost)}
|