]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/dsPIC_MPLAB/serial/serial.c
30d6b1dcde72bdfe94205a8a24ab5c81c50f07f4
[freertos] / FreeRTOS / Demo / dsPIC_MPLAB / serial / serial.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 \r
76 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. \r
77 \r
78 NOTE:  This driver is primarily to test the scheduler functionality.  It does\r
79 not effectively use the buffers or DMA and is therefore not intended to be\r
80 an example of an efficient driver. */\r
81 \r
82 /* Standard include file. */\r
83 #include <stdlib.h>\r
84 \r
85 /* Scheduler include files. */\r
86 #include "FreeRTOS.h"\r
87 #include "queue.h"\r
88 #include "task.h"\r
89 \r
90 /* Demo app include files. */\r
91 #include "serial.h"\r
92 \r
93 /* Hardware setup. */\r
94 #define serOUTPUT                                               0\r
95 #define serINPUT                                                1\r
96 #define serLOW_SPEED                                    0\r
97 #define serONE_STOP_BIT                                 0\r
98 #define serEIGHT_DATA_BITS_NO_PARITY    0\r
99 #define serNORMAL_IDLE_STATE                    0\r
100 #define serAUTO_BAUD_OFF                                0\r
101 #define serLOOPBACK_OFF                                 0\r
102 #define serWAKE_UP_DISABLE                              0\r
103 #define serNO_HARDWARE_FLOW_CONTROL             0\r
104 #define serSTANDARD_IO                                  0\r
105 #define serNO_IRDA                                              0\r
106 #define serCONTINUE_IN_IDLE_MODE                0\r
107 #define serUART_ENABLED                                 1\r
108 #define serINTERRUPT_ON_SINGLE_CHAR             0\r
109 #define serTX_ENABLE                                    1\r
110 #define serINTERRUPT_ENABLE                             1\r
111 #define serINTERRUPT_DISABLE                    0\r
112 #define serCLEAR_FLAG                                   0\r
113 #define serSET_FLAG                                             1\r
114 \r
115 \r
116 /* The queues used to communicate between tasks and ISR's. */\r
117 static xQueueHandle xRxedChars; \r
118 static xQueueHandle xCharsForTx; \r
119 \r
120 static portBASE_TYPE xTxHasEnded;\r
121 /*-----------------------------------------------------------*/\r
122 \r
123 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
124 {\r
125 char cChar;\r
126 \r
127         /* Create the queues used by the com test task. */\r
128         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
129         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
130 \r
131         /* Setup the UART. */\r
132         U2MODEbits.BRGH         = serLOW_SPEED;\r
133         U2MODEbits.STSEL        = serONE_STOP_BIT;\r
134         U2MODEbits.PDSEL        = serEIGHT_DATA_BITS_NO_PARITY;\r
135         U2MODEbits.ABAUD        = serAUTO_BAUD_OFF;\r
136         U2MODEbits.LPBACK       = serLOOPBACK_OFF;\r
137         U2MODEbits.WAKE         = serWAKE_UP_DISABLE;\r
138         U2MODEbits.UEN          = serNO_HARDWARE_FLOW_CONTROL;\r
139         U2MODEbits.IREN         = serNO_IRDA;\r
140         U2MODEbits.USIDL        = serCONTINUE_IN_IDLE_MODE;\r
141         U2MODEbits.UARTEN       = serUART_ENABLED;\r
142 \r
143         U2BRG = (unsigned short)(( (float)configCPU_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);\r
144 \r
145         U2STAbits.URXISEL       = serINTERRUPT_ON_SINGLE_CHAR;\r
146         U2STAbits.UTXEN         = serTX_ENABLE;\r
147         U2STAbits.UTXINV        = serNORMAL_IDLE_STATE;\r
148         U2STAbits.UTXISEL0      = serINTERRUPT_ON_SINGLE_CHAR;\r
149         U2STAbits.UTXISEL1      = serINTERRUPT_ON_SINGLE_CHAR;\r
150 \r
151         /* It is assumed that this function is called prior to the scheduler being\r
152         started.  Therefore interrupts must not be allowed to occur yet as they\r
153         may attempt to perform a context switch. */\r
154         portDISABLE_INTERRUPTS();\r
155 \r
156         IFS1bits.U2RXIF = serCLEAR_FLAG;\r
157         IFS1bits.U2TXIF = serCLEAR_FLAG;\r
158         IPC7bits.U2RXIP = configKERNEL_INTERRUPT_PRIORITY;\r
159         IPC7bits.U2TXIP = configKERNEL_INTERRUPT_PRIORITY;\r
160         IEC1bits.U2TXIE = serINTERRUPT_ENABLE;\r
161         IEC1bits.U2RXIE = serINTERRUPT_ENABLE;\r
162 \r
163         /* Clear the Rx buffer. */\r
164         while( U2STAbits.URXDA == serSET_FLAG )\r
165         {\r
166                 cChar = U2RXREG;\r
167         }\r
168 \r
169         xTxHasEnded = pdTRUE;\r
170 \r
171         return NULL;\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
176 {\r
177         /* Only one port is supported. */\r
178         ( void ) pxPort;\r
179 \r
180         /* Get the next character from the buffer.  Return false if no characters\r
181         are available or arrive before xBlockTime expires. */\r
182         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
183         {\r
184                 return pdTRUE;\r
185         }\r
186         else\r
187         {\r
188                 return pdFALSE;\r
189         }\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
194 {\r
195         /* Only one port is supported. */\r
196         ( void ) pxPort;\r
197 \r
198         /* Return false if after the block time there is no room on the Tx queue. */\r
199         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
200         {\r
201                 return pdFAIL;\r
202         }\r
203 \r
204         /* A critical section should not be required as xTxHasEnded will not be\r
205         written to by the ISR if it is already 0 (is this correct?). */\r
206         if( xTxHasEnded )\r
207         {\r
208                 xTxHasEnded = pdFALSE;\r
209                 IFS1bits.U2TXIF = serSET_FLAG;\r
210         }\r
211 \r
212         return pdPASS;\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 void vSerialClose( xComPortHandle xPort )\r
217 {\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 void __attribute__((__interrupt__, auto_psv)) _U2RXInterrupt( void )\r
222 {\r
223 char cChar;\r
224 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
225 \r
226         /* Get the character and post it on the queue of Rxed characters.\r
227         If the post causes a task to wake force a context switch as the woken task\r
228         may have a higher priority than the task we have interrupted. */\r
229         IFS1bits.U2RXIF = serCLEAR_FLAG;\r
230         while( U2STAbits.URXDA )\r
231         {\r
232                 cChar = U2RXREG;\r
233                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
234         }\r
235 \r
236         if( xHigherPriorityTaskWoken != pdFALSE )\r
237         {\r
238                 taskYIELD();\r
239         }\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 void __attribute__((__interrupt__, auto_psv)) _U2TXInterrupt( void )\r
244 {\r
245 signed char cChar;\r
246 portBASE_TYPE xTaskWoken = pdFALSE;\r
247 \r
248         /* If the transmit buffer is full we cannot get the next character.\r
249         Another interrupt will occur the next time there is space so this does\r
250         not matter. */\r
251         IFS1bits.U2TXIF = serCLEAR_FLAG;\r
252         while( !( U2STAbits.UTXBF ) )\r
253         {\r
254                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
255                 {\r
256                         /* Send the next character queued for Tx. */\r
257                         U2TXREG = cChar;\r
258                 }\r
259                 else\r
260                 {\r
261                         /* Queue empty, nothing to send. */\r
262                         xTxHasEnded = pdTRUE;\r
263                         break;\r
264                 }\r
265         }\r
266 \r
267         if( xTaskWoken != pdFALSE )\r
268         {\r
269                 taskYIELD();\r
270         }\r
271 }\r
272 \r
273 \r