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