]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC24_MPLAB/serial/serial.c
2745030cdc7dcb4f27fd1041aa306af922868e02
[freertos] / FreeRTOS / Demo / PIC24_MPLAB / serial / serial.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 \r
97 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. \r
98 \r
99 NOTE:  This driver is primarily to test the scheduler functionality.  It does\r
100 not effectively use the buffers or DMA and is therefore not intended to be\r
101 an example of an efficient driver. */\r
102 \r
103 /* Standard include file. */\r
104 #include <stdlib.h>\r
105 \r
106 /* Scheduler include files. */\r
107 #include "FreeRTOS.h"\r
108 #include "queue.h"\r
109 #include "task.h"\r
110 \r
111 /* Demo app include files. */\r
112 #include "serial.h"\r
113 \r
114 /* Hardware setup. */\r
115 #define serOUTPUT                                               0\r
116 #define serINPUT                                                1\r
117 #define serLOW_SPEED                                    0\r
118 #define serONE_STOP_BIT                                 0\r
119 #define serEIGHT_DATA_BITS_NO_PARITY    0\r
120 #define serNORMAL_IDLE_STATE                    0\r
121 #define serAUTO_BAUD_OFF                                0\r
122 #define serLOOPBACK_OFF                                 0\r
123 #define serWAKE_UP_DISABLE                              0\r
124 #define serNO_HARDWARE_FLOW_CONTROL             0\r
125 #define serSTANDARD_IO                                  0\r
126 #define serNO_IRDA                                              0\r
127 #define serCONTINUE_IN_IDLE_MODE                0\r
128 #define serUART_ENABLED                                 1\r
129 #define serINTERRUPT_ON_SINGLE_CHAR             0\r
130 #define serTX_ENABLE                                    1\r
131 #define serINTERRUPT_ENABLE                             1\r
132 #define serINTERRUPT_DISABLE                    0\r
133 #define serCLEAR_FLAG                                   0\r
134 #define serSET_FLAG                                             1\r
135 \r
136 \r
137 /* The queues used to communicate between tasks and ISR's. */\r
138 static QueueHandle_t xRxedChars; \r
139 static QueueHandle_t xCharsForTx; \r
140 \r
141 static portBASE_TYPE xTxHasEnded;\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
145 {\r
146 char cChar;\r
147 \r
148         /* Create the queues used by the com test task. */\r
149         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
150         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
151 \r
152         /* Setup the UART. */\r
153         U2MODEbits.BRGH         = serLOW_SPEED;\r
154         U2MODEbits.STSEL        = serONE_STOP_BIT;\r
155         U2MODEbits.PDSEL        = serEIGHT_DATA_BITS_NO_PARITY;\r
156         U2MODEbits.RXINV        = serNORMAL_IDLE_STATE;\r
157         U2MODEbits.ABAUD        = serAUTO_BAUD_OFF;\r
158         U2MODEbits.LPBACK       = serLOOPBACK_OFF;\r
159         U2MODEbits.WAKE         = serWAKE_UP_DISABLE;\r
160         U2MODEbits.UEN          = serNO_HARDWARE_FLOW_CONTROL;\r
161         U2MODEbits.IREN         = serNO_IRDA;\r
162         U2MODEbits.USIDL        = serCONTINUE_IN_IDLE_MODE;\r
163         U2MODEbits.UARTEN       = serUART_ENABLED;\r
164 \r
165         U2BRG = (unsigned short)(( (float)configCPU_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);\r
166 \r
167         U2STAbits.URXISEL       = serINTERRUPT_ON_SINGLE_CHAR;\r
168         U2STAbits.UTXEN         = serTX_ENABLE;\r
169         U2STAbits.UTXINV        = serNORMAL_IDLE_STATE;\r
170         U2STAbits.UTXISEL0      = serINTERRUPT_ON_SINGLE_CHAR;\r
171         U2STAbits.UTXISEL1      = serINTERRUPT_ON_SINGLE_CHAR;\r
172 \r
173         /* It is assumed that this function is called prior to the scheduler being\r
174         started.  Therefore interrupts must not be allowed to occur yet as they\r
175         may attempt to perform a context switch. */\r
176         portDISABLE_INTERRUPTS();\r
177 \r
178         IFS1bits.U2RXIF = serCLEAR_FLAG;\r
179         IFS1bits.U2TXIF = serCLEAR_FLAG;\r
180         IPC7bits.U2RXIP = configKERNEL_INTERRUPT_PRIORITY;\r
181         IPC7bits.U2TXIP = configKERNEL_INTERRUPT_PRIORITY;\r
182         IEC1bits.U2TXIE = serINTERRUPT_ENABLE;\r
183         IEC1bits.U2RXIE = serINTERRUPT_ENABLE;\r
184 \r
185         /* Clear the Rx buffer. */\r
186         while( U2STAbits.URXDA == serSET_FLAG )\r
187         {\r
188                 cChar = U2RXREG;\r
189         }\r
190 \r
191         xTxHasEnded = pdTRUE;\r
192 \r
193         return NULL;\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
198 {\r
199         /* Only one port is supported. */\r
200         ( void ) pxPort;\r
201 \r
202         /* Get the next character from the buffer.  Return false if no characters\r
203         are available or arrive before xBlockTime expires. */\r
204         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
205         {\r
206                 return pdTRUE;\r
207         }\r
208         else\r
209         {\r
210                 return pdFALSE;\r
211         }\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
216 {\r
217         /* Only one port is supported. */\r
218         ( void ) pxPort;\r
219 \r
220         /* Return false if after the block time there is no room on the Tx queue. */\r
221         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
222         {\r
223                 return pdFAIL;\r
224         }\r
225 \r
226         /* A critical section should not be required as xTxHasEnded will not be\r
227         written to by the ISR if it is already 0 (is this correct?). */\r
228         if( xTxHasEnded )\r
229         {\r
230                 xTxHasEnded = pdFALSE;\r
231                 IFS1bits.U2TXIF = serSET_FLAG;\r
232         }\r
233 \r
234         return pdPASS;\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 void vSerialClose( xComPortHandle xPort )\r
239 {\r
240         /* Note implemented. */\r
241         ( void ) xPort;\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 void __attribute__((__interrupt__, auto_psv)) _U2RXInterrupt( void )\r
246 {\r
247 char cChar;\r
248 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
249 \r
250         /* Get the character and post it on the queue of Rxed characters.\r
251         If the post causes a task to wake force a context switch as the woken task\r
252         may have a higher priority than the task we have interrupted. */\r
253         IFS1bits.U2RXIF = serCLEAR_FLAG;\r
254         while( U2STAbits.URXDA )\r
255         {\r
256                 cChar = U2RXREG;\r
257                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
258         }\r
259 \r
260         if( xHigherPriorityTaskWoken != pdFALSE )\r
261         {\r
262                 taskYIELD();\r
263         }\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 void __attribute__((__interrupt__, auto_psv)) _U2TXInterrupt( void )\r
268 {\r
269 signed char cChar;\r
270 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
271 \r
272         /* If the transmit buffer is full we cannot get the next character.\r
273         Another interrupt will occur the next time there is space so this does\r
274         not matter. */\r
275         IFS1bits.U2TXIF = serCLEAR_FLAG;\r
276         while( !( U2STAbits.UTXBF ) )\r
277         {\r
278                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
279                 {\r
280                         /* Send the next character queued for Tx. */\r
281                         U2TXREG = cChar;\r
282                 }\r
283                 else\r
284                 {\r
285                         /* Queue empty, nothing to send. */\r
286                         xTxHasEnded = pdTRUE;\r
287                         break;\r
288                 }\r
289         }\r
290 \r
291         if( xHigherPriorityTaskWoken != pdFALSE )\r
292         {\r
293                 taskYIELD();\r
294         }\r
295 }\r
296 \r
297 \r