changing circuitry to disable RTC, update initialization to match
[fw/openalt] / uart / uartISR.c
1 //
2 //  Standard includes
3 //
4 #include <stdlib.h>
5
6 //
7 //  Scheduler includes
8 //
9 #include "FreeRTOS.h"
10 #include "queue.h"
11 #include "task.h"
12
13 //
14 //  Demo application includes
15 //
16 #include "uart.h"
17 #include "uartISR.h"
18
19 //
20 //  Constants to determine the ISR source
21 //
22 #define serSOURCE_THRE                                    ((unsigned portCHAR) 0x02)
23 #define serSOURCE_RX_TIMEOUT                    ((unsigned portCHAR) 0x0c)
24 #define serSOURCE_ERROR                                   ((unsigned portCHAR) 0x06)
25 #define serSOURCE_RX                                        ((unsigned portCHAR) 0x04)
26 #define serINTERRUPT_SOURCE_MASK        ((unsigned portCHAR) 0x0f)
27
28 //
29 //  Queues used to hold received characters, and characters waiting to be transmitted
30 //
31 static xQueueHandle xRX0Queue; 
32 static xQueueHandle xTX0Queue; 
33 static volatile portLONG lTHREEmpty0;
34 static xQueueHandle xRX1Queue; 
35 static xQueueHandle xTX1Queue; 
36 static volatile portLONG lTHREEmpty1;
37
38 //
39 //
40 //
41 void uartISRCreateQueues (portCHAR pxPort, unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRX0Queue, xQueueHandle *pxTX0Queue, portLONG volatile **pplTHREEmptyFlag)
42 {
43   switch (pxPort)
44   {
45     case 0:    
46       {
47         //
48         //  Create the queues used to hold Rx and Tx characters
49         //
50         *pxRX0Queue = xRX0Queue = xQueueCreate (uxQueueLength, (unsigned portBASE_TYPE) sizeof (signed portCHAR));
51         *pxTX0Queue = xTX0Queue = xQueueCreate (uxQueueLength + 1, (unsigned portBASE_TYPE) sizeof (signed portCHAR));
52
53         //
54         //  Initialise the THRE empty flag - and pass back a reference
55         //
56         lTHREEmpty0 = (portLONG) pdTRUE;
57         *pplTHREEmptyFlag = &lTHREEmpty0;
58       }
59       break;
60
61     case 1:
62       {
63         //
64         //  Create the queues used to hold Rx and Tx characters
65         //
66         *pxRX0Queue = xRX1Queue = xQueueCreate (uxQueueLength, (unsigned portCHAR) sizeof (signed portCHAR));
67         *pxTX0Queue = xTX1Queue = xQueueCreate (uxQueueLength + 1, (unsigned portCHAR) sizeof (signed portCHAR));
68
69         //
70         //  Initialise the THRE empty flag - and pass back a reference
71         //
72         lTHREEmpty1 = (portLONG) pdTRUE;
73         *pplTHREEmptyFlag = &lTHREEmpty1;       
74       }
75       break;
76   }
77 }
78
79 //
80 //
81 //
82 void uartISR0 (void) __attribute__ ((naked));
83 void uartISR0 (void)
84 {
85   portENTER_SWITCHING_ISR ();
86
87   signed portCHAR cChar;
88   portBASE_TYPE xTaskWokenByTx = pdFALSE;
89   portBASE_TYPE xTaskWokenByRx = pdFALSE;
90
91   switch (UART0_IIR & serINTERRUPT_SOURCE_MASK)
92   {
93     //
94     //  Not handling this, but clear the interrupt
95     //
96     case serSOURCE_ERROR :      
97       {
98         cChar = UART0_LSR;
99       }
100       break;
101
102     //
103     //  The THRE is empty.  If there is another character in the Tx queue, send it now,
104     //  otherwise, no more characters, so indicate THRE is available
105     //
106     case serSOURCE_THRE :       
107       {
108         if (xQueueReceiveFromISR (xTX0Queue, &cChar, &xTaskWokenByTx) == pdTRUE)
109           UART0_THR = cChar;
110         else
111           lTHREEmpty0 = pdTRUE;
112       }
113       break;
114
115     //
116     //  A character was received.  Place it in the queue of received characters
117     //
118     case serSOURCE_RX_TIMEOUT :
119     case serSOURCE_RX   :       
120       {
121         cChar = UART0_RBR;
122
123         if (xQueueSendFromISR (xRX0Queue, &cChar, (portBASE_TYPE) pdFALSE)) 
124           xTaskWokenByRx = pdTRUE;
125       }
126       break;
127
128     default     :
129       break;
130   }
131
132   VIC_VectAddr = (unsigned portLONG) 0;
133
134   portEXIT_SWITCHING_ISR ((xTaskWokenByTx || xTaskWokenByRx));
135 }
136
137 //
138 //
139 //
140 void uartISR1 (void) __attribute__ ((naked));
141 void uartISR1 (void)
142 {
143   portENTER_SWITCHING_ISR ();
144
145   signed portCHAR cChar;
146   portBASE_TYPE xTaskWokenByTx = pdFALSE;
147   portBASE_TYPE xTaskWokenByRx = pdFALSE;
148
149   switch (UART1_IIR & serINTERRUPT_SOURCE_MASK)
150   {
151     //
152     //  Not handling this, but clear the interrupt
153     //
154     case serSOURCE_ERROR :
155       {
156         cChar = UART1_LSR;
157       }
158       break;
159
160     //
161     //  The THRE is empty.  If there is another character in the Tx queue, send it now,
162     //  otherwise, no more characters, so indicate THRE is available
163     //
164     case serSOURCE_THRE :
165       {
166         if (xQueueReceiveFromISR (xTX1Queue, &cChar, &xTaskWokenByTx) == pdTRUE)
167           UART1_THR = cChar;
168         else
169           lTHREEmpty1 = pdTRUE;
170       }
171       break;
172
173     //
174     //  A character was received.  Place it in the queue of received characters
175     //
176     case serSOURCE_RX_TIMEOUT :
177     case serSOURCE_RX   :
178       {
179         cChar = UART1_RBR;
180
181         if (xQueueSendFromISR (xRX1Queue, &cChar, (portBASE_TYPE) pdFALSE)) 
182           xTaskWokenByRx = pdTRUE;
183       }
184       break;
185
186     default     :
187       break;
188   }
189
190   VIC_VectAddr = (unsigned portLONG) 0;
191
192   portEXIT_SWITCHING_ISR ((xTaskWokenByTx || xTaskWokenByRx));
193 }