]> git.sur5r.net Git - freertos/blob - Demo/dsPIC_MPLAB/serial/serial.c
Update to V4.7.2.
[freertos] / Demo / dsPIC_MPLAB / serial / serial.c
1 /*\r
2         FreeRTOS.org V4.7.2 - 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         Please ensure to read the configuration and relevant port sections of the \r
29         online documentation.\r
30 \r
31         +++ http://www.FreeRTOS.org +++\r
32         Documentation, latest information, license and contact details.  \r
33 \r
34         +++ http://www.SafeRTOS.com +++\r
35         A version that is certified for use in safety critical systems.\r
36 \r
37         +++ http://www.OpenRTOS.com +++\r
38         Commercial support, development, porting, licensing and training services.\r
39 \r
40         ***************************************************************************\r
41 */\r
42 \r
43 \r
44 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. \r
45 \r
46 NOTE:  This driver is primarily to test the scheduler functionality.  It does\r
47 not effectively use the buffers or DMA and is therefore not intended to be\r
48 an example of an efficient driver. */\r
49 \r
50 /* Standard include file. */\r
51 #include <stdlib.h>\r
52 \r
53 /* Scheduler include files. */\r
54 #include "FreeRTOS.h"\r
55 #include "queue.h"\r
56 #include "task.h"\r
57 \r
58 /* Demo app include files. */\r
59 #include "serial.h"\r
60 \r
61 /* Hardware setup. */\r
62 #define serOUTPUT                                               0\r
63 #define serINPUT                                                1\r
64 #define serLOW_SPEED                                    0\r
65 #define serONE_STOP_BIT                                 0\r
66 #define serEIGHT_DATA_BITS_NO_PARITY    0\r
67 #define serNORMAL_IDLE_STATE                    0\r
68 #define serAUTO_BAUD_OFF                                0\r
69 #define serLOOPBACK_OFF                                 0\r
70 #define serWAKE_UP_DISABLE                              0\r
71 #define serNO_HARDWARE_FLOW_CONTROL             0\r
72 #define serSTANDARD_IO                                  0\r
73 #define serNO_IRDA                                              0\r
74 #define serCONTINUE_IN_IDLE_MODE                0\r
75 #define serUART_ENABLED                                 1\r
76 #define serINTERRUPT_ON_SINGLE_CHAR             0\r
77 #define serTX_ENABLE                                    1\r
78 #define serINTERRUPT_ENABLE                             1\r
79 #define serINTERRUPT_DISABLE                    0\r
80 #define serCLEAR_FLAG                                   0\r
81 #define serSET_FLAG                                             1\r
82 \r
83 \r
84 /* The queues used to communicate between tasks and ISR's. */\r
85 static xQueueHandle xRxedChars; \r
86 static xQueueHandle xCharsForTx; \r
87 \r
88 static portBASE_TYPE xTxHasEnded;\r
89 /*-----------------------------------------------------------*/\r
90 \r
91 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
92 {\r
93 portCHAR cChar;\r
94 \r
95         /* Create the queues used by the com test task. */\r
96         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
97         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
98 \r
99         /* Setup the UART. */\r
100         U2MODEbits.BRGH         = serLOW_SPEED;\r
101         U2MODEbits.STSEL        = serONE_STOP_BIT;\r
102         U2MODEbits.PDSEL        = serEIGHT_DATA_BITS_NO_PARITY;\r
103         U2MODEbits.ABAUD        = serAUTO_BAUD_OFF;\r
104         U2MODEbits.LPBACK       = serLOOPBACK_OFF;\r
105         U2MODEbits.WAKE         = serWAKE_UP_DISABLE;\r
106         U2MODEbits.UEN          = serNO_HARDWARE_FLOW_CONTROL;\r
107         U2MODEbits.IREN         = serNO_IRDA;\r
108         U2MODEbits.USIDL        = serCONTINUE_IN_IDLE_MODE;\r
109         U2MODEbits.UARTEN       = serUART_ENABLED;\r
110 \r
111         U2BRG = (unsigned portSHORT)(( (float)configCPU_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);\r
112 \r
113         U2STAbits.URXISEL       = serINTERRUPT_ON_SINGLE_CHAR;\r
114         U2STAbits.UTXEN         = serTX_ENABLE;\r
115         U2STAbits.UTXINV        = serNORMAL_IDLE_STATE;\r
116         U2STAbits.UTXISEL0      = serINTERRUPT_ON_SINGLE_CHAR;\r
117         U2STAbits.UTXISEL1      = serINTERRUPT_ON_SINGLE_CHAR;\r
118 \r
119         /* It is assumed that this function is called prior to the scheduler being\r
120         started.  Therefore interrupts must not be allowed to occur yet as they\r
121         may attempt to perform a context switch. */\r
122         portDISABLE_INTERRUPTS();\r
123 \r
124         IFS1bits.U2RXIF = serCLEAR_FLAG;\r
125         IFS1bits.U2TXIF = serCLEAR_FLAG;\r
126         IPC7bits.U2RXIP = configKERNEL_INTERRUPT_PRIORITY;\r
127         IPC7bits.U2TXIP = configKERNEL_INTERRUPT_PRIORITY;\r
128         IEC1bits.U2TXIE = serINTERRUPT_ENABLE;\r
129         IEC1bits.U2RXIE = serINTERRUPT_ENABLE;\r
130 \r
131         /* Clear the Rx buffer. */\r
132         while( U2STAbits.URXDA == serSET_FLAG )\r
133         {\r
134                 cChar = U2RXREG;\r
135         }\r
136 \r
137         xTxHasEnded = pdTRUE;\r
138 \r
139         return NULL;\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
144 {\r
145         /* Only one port is supported. */\r
146         ( void ) pxPort;\r
147 \r
148         /* Get the next character from the buffer.  Return false if no characters\r
149         are available or arrive before xBlockTime expires. */\r
150         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
151         {\r
152                 return pdTRUE;\r
153         }\r
154         else\r
155         {\r
156                 return pdFALSE;\r
157         }\r
158 }\r
159 /*-----------------------------------------------------------*/\r
160 \r
161 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
162 {\r
163         /* Only one port is supported. */\r
164         ( void ) pxPort;\r
165 \r
166         /* Return false if after the block time there is no room on the Tx queue. */\r
167         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
168         {\r
169                 return pdFAIL;\r
170         }\r
171 \r
172         /* A critical section should not be required as xTxHasEnded will not be\r
173         written to by the ISR if it is already 0 (is this correct?). */\r
174         if( xTxHasEnded )\r
175         {\r
176                 xTxHasEnded = pdFALSE;\r
177                 IFS1bits.U2TXIF = serSET_FLAG;\r
178         }\r
179 \r
180         return pdPASS;\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 void vSerialClose( xComPortHandle xPort )\r
185 {\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 void __attribute__((__interrupt__, auto_psv)) _U2RXInterrupt( void )\r
190 {\r
191 portCHAR cChar;\r
192 portBASE_TYPE xYieldRequired = pdFALSE;\r
193 \r
194         /* Get the character and post it on the queue of Rxed characters.\r
195         If the post causes a task to wake force a context switch as the woken task\r
196         may have a higher priority than the task we have interrupted. */\r
197         IFS1bits.U2RXIF = serCLEAR_FLAG;\r
198         while( U2STAbits.URXDA )\r
199         {\r
200                 cChar = U2RXREG;\r
201                 xYieldRequired = xQueueSendFromISR( xRxedChars, &cChar, xYieldRequired );\r
202         }\r
203 \r
204         if( xYieldRequired != pdFALSE )\r
205         {\r
206                 taskYIELD();\r
207         }\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 void __attribute__((__interrupt__, auto_psv)) _U2TXInterrupt( void )\r
212 {\r
213 signed portCHAR cChar;\r
214 portBASE_TYPE xTaskWoken = pdFALSE;\r
215 \r
216         /* If the transmit buffer is full we cannot get the next character.\r
217         Another interrupt will occur the next time there is space so this does\r
218         not matter. */\r
219         IFS1bits.U2TXIF = serCLEAR_FLAG;\r
220         while( !( U2STAbits.UTXBF ) )\r
221         {\r
222                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
223                 {\r
224                         /* Send the next character queued for Tx. */\r
225                         U2TXREG = cChar;\r
226                 }\r
227                 else\r
228                 {\r
229                         /* Queue empty, nothing to send. */\r
230                         xTxHasEnded = pdTRUE;\r
231                         break;\r
232                 }\r
233         }\r
234 \r
235         if( xTaskWoken != pdFALSE )\r
236         {\r
237                 taskYIELD();\r
238         }\r
239 }\r
240 \r
241 \r