]> git.sur5r.net Git - freertos/blob - Demo/PIC32MX_MPLAB/serial/serial.c
Raise the priority of the serial interrupt.
[freertos] / Demo / PIC32MX_MPLAB / serial / serial.c
1 /*\r
2         FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26     ***************************************************************************\r
27     ***************************************************************************\r
28     *                                                                         *\r
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and \r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety \r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting, \r
47         licensing and training services.\r
48 */\r
49 \r
50 \r
51 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. \r
52 \r
53 NOTE:  This driver is primarily to test the scheduler functionality.  It does\r
54 not effectively use the buffers or DMA and is therefore not intended to be\r
55 an example of an efficient driver. */\r
56 \r
57 /* Standard include file. */\r
58 #include <stdlib.h>\r
59 #include <plib.h>\r
60 \r
61 /* Scheduler include files. */\r
62 #include "FreeRTOS.h"\r
63 #include "queue.h"\r
64 #include "task.h"\r
65 \r
66 /* Demo app include files. */\r
67 #include "serial.h"\r
68 \r
69 /* Hardware setup. */\r
70 #define serSET_FLAG                                             ( 1 )\r
71 \r
72 /* The queues used to communicate between tasks and ISR's. */\r
73 static xQueueHandle xRxedChars; \r
74 static xQueueHandle xCharsForTx; \r
75 \r
76 /* Flag used to indicate the tx status. */\r
77 static portBASE_TYPE xTxHasEnded;\r
78 \r
79 /*-----------------------------------------------------------*/\r
80 \r
81 /* The UART interrupt handler. */\r
82 void __attribute__( (interrupt(ipl1), vector(_UART2_VECTOR))) vU2InterruptWrapper( void );\r
83 \r
84 /*-----------------------------------------------------------*/\r
85 \r
86 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
87 {\r
88 unsigned portSHORT usBRG;\r
89 \r
90         /* Create the queues used by the com test task. */\r
91         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
92         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
93 \r
94         /* Configure the UART and interrupts. */\r
95         usBRG = (unsigned portSHORT)(( (float)configPERIPHERAL_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);\r
96         OpenUART2( UART_EN, UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR, usBRG );\r
97         ConfigIntUART2( ( configKERNEL_INTERRUPT_PRIORITY + 1 ) | UART_INT_SUB_PR0 | UART_TX_INT_EN | UART_RX_INT_EN );\r
98 \r
99         xTxHasEnded = pdTRUE;\r
100 \r
101         /* Only a single port is implemented so we don't need to return anything. */\r
102         return NULL;\r
103 }\r
104 /*-----------------------------------------------------------*/\r
105 \r
106 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
107 {\r
108         /* Only one port is supported. */\r
109         ( void ) pxPort;\r
110 \r
111         /* Get the next character from the buffer.  Return false if no characters\r
112         are available or arrive before xBlockTime expires. */\r
113         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
114         {\r
115                 return pdTRUE;\r
116         }\r
117         else\r
118         {\r
119                 return pdFALSE;\r
120         }\r
121 }\r
122 /*-----------------------------------------------------------*/\r
123 \r
124 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
125 {\r
126         /* Only one port is supported. */\r
127         ( void ) pxPort;\r
128 \r
129         /* Return false if after the block time there is no room on the Tx queue. */\r
130         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
131         {\r
132                 return pdFAIL;\r
133         }\r
134 \r
135         /* A critical section should not be required as xTxHasEnded will not be\r
136         written to by the ISR if it is already 0 (is this correct?). */\r
137         if( xTxHasEnded )\r
138         {\r
139                 xTxHasEnded = pdFALSE;\r
140                 IFS1bits.U2TXIF = serSET_FLAG;\r
141         }\r
142 \r
143         return pdPASS;\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 void vSerialClose( xComPortHandle xPort )\r
148 {\r
149 }\r
150 /*-----------------------------------------------------------*/\r
151 \r
152 void vU2InterruptHandler( void )\r
153 {\r
154 /* Declared static to minimise stack use. */\r
155 static portCHAR cChar;\r
156 static portBASE_TYPE xHigherPriorityTaskWoken;\r
157 \r
158         xHigherPriorityTaskWoken = pdFALSE;\r
159 \r
160         /* Are any Rx interrupts pending? */\r
161         if( mU2RXGetIntFlag() )\r
162         {\r
163                 while( U2STAbits.URXDA )\r
164                 {\r
165                         /* Retrieve the received character and place it in the queue of\r
166                         received characters. */\r
167                         cChar = U2RXREG;\r
168                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
169                 }\r
170                 mU2RXClearIntFlag();\r
171         }\r
172 \r
173         /* Are any Tx interrupts pending? */\r
174         if( mU2TXGetIntFlag() )\r
175         {\r
176                 while( !( U2STAbits.UTXBF ) )\r
177                 {\r
178                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
179                         {\r
180                                 /* Send the next character queued for Tx. */\r
181                                 U2TXREG = cChar;\r
182                         }\r
183                         else\r
184                         {\r
185                                 /* Queue empty, nothing to send. */\r
186                                 xTxHasEnded = pdTRUE;\r
187                                 break;\r
188                         }\r
189                 }\r
190 \r
191                 mU2TXClearIntFlag();\r
192         }\r
193 \r
194         /* If sending or receiving necessitates a context switch, then switch now. */\r
195         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
196 }\r
197 \r
198 \r
199 \r
200 \r
201 \r
202 \r
203 \r