]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/dsPIC_MPLAB/serial/serial.c
6323b716deaaeae106e02a48f0009fa427978e6c
[freertos] / FreeRTOS / Demo / dsPIC_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.ABAUD        = serAUTO_BAUD_OFF;\r
157         U2MODEbits.LPBACK       = serLOOPBACK_OFF;\r
158         U2MODEbits.WAKE         = serWAKE_UP_DISABLE;\r
159         U2MODEbits.UEN          = serNO_HARDWARE_FLOW_CONTROL;\r
160         U2MODEbits.IREN         = serNO_IRDA;\r
161         U2MODEbits.USIDL        = serCONTINUE_IN_IDLE_MODE;\r
162         U2MODEbits.UARTEN       = serUART_ENABLED;\r
163 \r
164         U2BRG = (unsigned short)(( (float)configCPU_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);\r
165 \r
166         U2STAbits.URXISEL       = serINTERRUPT_ON_SINGLE_CHAR;\r
167         U2STAbits.UTXEN         = serTX_ENABLE;\r
168         U2STAbits.UTXINV        = serNORMAL_IDLE_STATE;\r
169         U2STAbits.UTXISEL0      = serINTERRUPT_ON_SINGLE_CHAR;\r
170         U2STAbits.UTXISEL1      = serINTERRUPT_ON_SINGLE_CHAR;\r
171 \r
172         /* It is assumed that this function is called prior to the scheduler being\r
173         started.  Therefore interrupts must not be allowed to occur yet as they\r
174         may attempt to perform a context switch. */\r
175         portDISABLE_INTERRUPTS();\r
176 \r
177         IFS1bits.U2RXIF = serCLEAR_FLAG;\r
178         IFS1bits.U2TXIF = serCLEAR_FLAG;\r
179         IPC7bits.U2RXIP = configKERNEL_INTERRUPT_PRIORITY;\r
180         IPC7bits.U2TXIP = configKERNEL_INTERRUPT_PRIORITY;\r
181         IEC1bits.U2TXIE = serINTERRUPT_ENABLE;\r
182         IEC1bits.U2RXIE = serINTERRUPT_ENABLE;\r
183 \r
184         /* Clear the Rx buffer. */\r
185         while( U2STAbits.URXDA == serSET_FLAG )\r
186         {\r
187                 cChar = U2RXREG;\r
188         }\r
189 \r
190         xTxHasEnded = pdTRUE;\r
191 \r
192         return NULL;\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
197 {\r
198         /* Only one port is supported. */\r
199         ( void ) pxPort;\r
200 \r
201         /* Get the next character from the buffer.  Return false if no characters\r
202         are available or arrive before xBlockTime expires. */\r
203         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
204         {\r
205                 return pdTRUE;\r
206         }\r
207         else\r
208         {\r
209                 return pdFALSE;\r
210         }\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
215 {\r
216         /* Only one port is supported. */\r
217         ( void ) pxPort;\r
218 \r
219         /* Return false if after the block time there is no room on the Tx queue. */\r
220         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
221         {\r
222                 return pdFAIL;\r
223         }\r
224 \r
225         /* A critical section should not be required as xTxHasEnded will not be\r
226         written to by the ISR if it is already 0 (is this correct?). */\r
227         if( xTxHasEnded )\r
228         {\r
229                 xTxHasEnded = pdFALSE;\r
230                 IFS1bits.U2TXIF = serSET_FLAG;\r
231         }\r
232 \r
233         return pdPASS;\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 void vSerialClose( xComPortHandle xPort )\r
238 {\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 void __attribute__((__interrupt__, auto_psv)) _U2RXInterrupt( void )\r
243 {\r
244 char cChar;\r
245 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
246 \r
247         /* Get the character and post it on the queue of Rxed characters.\r
248         If the post causes a task to wake force a context switch as the woken task\r
249         may have a higher priority than the task we have interrupted. */\r
250         IFS1bits.U2RXIF = serCLEAR_FLAG;\r
251         while( U2STAbits.URXDA )\r
252         {\r
253                 cChar = U2RXREG;\r
254                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
255         }\r
256 \r
257         if( xHigherPriorityTaskWoken != pdFALSE )\r
258         {\r
259                 taskYIELD();\r
260         }\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 void __attribute__((__interrupt__, auto_psv)) _U2TXInterrupt( void )\r
265 {\r
266 signed char cChar;\r
267 portBASE_TYPE xTaskWoken = pdFALSE;\r
268 \r
269         /* If the transmit buffer is full we cannot get the next character.\r
270         Another interrupt will occur the next time there is space so this does\r
271         not matter. */\r
272         IFS1bits.U2TXIF = serCLEAR_FLAG;\r
273         while( !( U2STAbits.UTXBF ) )\r
274         {\r
275                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
276                 {\r
277                         /* Send the next character queued for Tx. */\r
278                         U2TXREG = cChar;\r
279                 }\r
280                 else\r
281                 {\r
282                         /* Queue empty, nothing to send. */\r
283                         xTxHasEnded = pdTRUE;\r
284                         break;\r
285                 }\r
286         }\r
287 \r
288         if( xTaskWoken != pdFALSE )\r
289         {\r
290                 taskYIELD();\r
291         }\r
292 }\r
293 \r
294 \r