]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MX_MPLAB/serial/serial.c
Update version number to V8.0.0 (without the release candidate number).
[freertos] / FreeRTOS / Demo / PIC32MX_MPLAB / serial / serial.c
1 /*\r
2     FreeRTOS V8.0.0 - 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 \r
67 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. \r
68 \r
69 NOTE:  This driver is primarily to test the scheduler functionality.  It does\r
70 not effectively use the buffers or DMA and is therefore not intended to be\r
71 an example of an efficient driver. */\r
72 \r
73 /* Standard include file. */\r
74 #include <stdlib.h>\r
75 #include <plib.h>\r
76 \r
77 /* Scheduler include files. */\r
78 #include "FreeRTOS.h"\r
79 #include "queue.h"\r
80 #include "task.h"\r
81 \r
82 /* Demo app include files. */\r
83 #include "serial.h"\r
84 \r
85 /* Hardware setup. */\r
86 #define serSET_FLAG                                             ( 1 )\r
87 \r
88 /* The queues used to communicate between tasks and ISR's. */\r
89 static QueueHandle_t xRxedChars; \r
90 static QueueHandle_t xCharsForTx; \r
91 \r
92 /* Flag used to indicate the tx status. */\r
93 static volatile portBASE_TYPE xTxHasEnded;\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 /* The UART interrupt handler.  As this uses the FreeRTOS assembly interrupt\r
98 entry point the IPL setting in the following prototype has no effect.  The\r
99 interrupt priority is set by the call to  ConfigIntUART2() in \r
100 xSerialPortInitMinimal(). */\r
101 void __attribute__( (interrupt(ipl0), vector(_UART2_VECTOR))) vU2InterruptWrapper( void );\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
106 {\r
107 unsigned short usBRG;\r
108 \r
109         /* Create the queues used by the com test task. */\r
110         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
111         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
112 \r
113         /* Configure the UART and interrupts. */\r
114         usBRG = (unsigned short)(( (float)configPERIPHERAL_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);\r
115         OpenUART2( UART_EN, UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR, usBRG );\r
116         ConfigIntUART2( ( configKERNEL_INTERRUPT_PRIORITY + 1 ) | UART_INT_SUB_PR0 | UART_TX_INT_EN | UART_RX_INT_EN );\r
117 \r
118         xTxHasEnded = pdTRUE;\r
119 \r
120         /* Only a single port is implemented so we don't need to return anything. */\r
121         return NULL;\r
122 }\r
123 /*-----------------------------------------------------------*/\r
124 \r
125 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
126 {\r
127         /* Only one port is supported. */\r
128         ( void ) pxPort;\r
129 \r
130         /* Get the next character from the buffer.  Return false if no characters\r
131         are available or arrive before xBlockTime expires. */\r
132         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
133         {\r
134                 return pdTRUE;\r
135         }\r
136         else\r
137         {\r
138                 return pdFALSE;\r
139         }\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
144 {\r
145 signed portBASE_TYPE xReturn;\r
146 \r
147         /* Only one port is supported. */\r
148         ( void ) pxPort;\r
149 \r
150         /* Return false if after the block time there is no room on the Tx queue. */\r
151         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
152         {\r
153                 xReturn = pdFAIL;\r
154         }\r
155         else\r
156         {\r
157                 xReturn = pdPASS;\r
158         }\r
159 \r
160         if( xReturn != pdFAIL )\r
161         {\r
162                 /* A critical section should not be required as xTxHasEnded will not be\r
163                 written to by the ISR if it is already 0. */\r
164                 if(  xTxHasEnded == pdTRUE )\r
165                 {\r
166                         xTxHasEnded = pdFALSE;\r
167                         IFS1SET = _IFS1_U2TXIF_MASK;\r
168                 }\r
169         }\r
170 \r
171         return pdPASS;\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 void vSerialClose( xComPortHandle xPort )\r
176 {\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 void vU2InterruptHandler( void )\r
181 {\r
182 /* Declared static to minimise stack use. */\r
183 static char cChar;\r
184 static portBASE_TYPE xHigherPriorityTaskWoken;\r
185 \r
186         xHigherPriorityTaskWoken = pdFALSE;\r
187 \r
188         /* Are any Rx interrupts pending? */\r
189         if( IFS1bits.U2RXIF == 1)\r
190         {\r
191                 while( U2STAbits.URXDA )\r
192                 {\r
193                         /* Retrieve the received character and place it in the queue of\r
194                         received characters. */\r
195                         cChar = U2RXREG;\r
196                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
197                 }\r
198                 IFS1CLR = _IFS1_U2RXIF_MASK;\r
199         }\r
200 \r
201         /* Are any Tx interrupts pending? */\r
202         if( IFS1bits.U2TXIF == 1 )\r
203         {\r
204                 while( ( U2STAbits.UTXBF ) == 0 )\r
205                 {\r
206                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
207                         {\r
208                                 /* Send the next character queued for Tx. */\r
209                                 U2TXREG = cChar;\r
210                         }\r
211                         else\r
212                         {\r
213                                 /* Queue empty, nothing to send. */\r
214                                 xTxHasEnded = pdTRUE;\r
215                                 break;\r
216                         }\r
217                 }\r
218 \r
219                 IFS1CLR = _IFS1_U2TXIF_MASK;\r
220         }\r
221 \r
222         /* If sending or receiving necessitates a context switch, then switch now. */\r
223         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
224 }\r
225 \r
226 \r
227 \r
228 \r
229 \r
230 \r
231 \r