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