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