]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MX_MPLAB/serial/serial.c
Update license information text files for the CLI, TCP and UDP products to be correct...
[freertos] / FreeRTOS / Demo / PIC32MX_MPLAB / serial / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 \r
30 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. \r
31 \r
32 NOTE:  This driver is primarily to test the scheduler functionality.  It does\r
33 not effectively use the buffers or DMA and is therefore not intended to be\r
34 an example of an efficient driver. */\r
35 \r
36 /* Standard include file. */\r
37 #include <stdlib.h>\r
38 #include <plib.h>\r
39 \r
40 /* Scheduler include files. */\r
41 #include "FreeRTOS.h"\r
42 #include "queue.h"\r
43 #include "task.h"\r
44 \r
45 /* Demo app include files. */\r
46 #include "serial.h"\r
47 \r
48 /* Hardware setup. */\r
49 #define serSET_FLAG                                             ( 1 )\r
50 \r
51 /* The queues used to communicate between tasks and ISR's. */\r
52 static QueueHandle_t xRxedChars; \r
53 static QueueHandle_t xCharsForTx; \r
54 \r
55 /* Flag used to indicate the tx status. */\r
56 static volatile portBASE_TYPE xTxHasEnded;\r
57 \r
58 /*-----------------------------------------------------------*/\r
59 \r
60 /* The UART interrupt handler.  As this uses the FreeRTOS assembly interrupt\r
61 entry point the IPL setting in the following prototype has no effect.  The\r
62 interrupt priority is set by the call to  ConfigIntUART2() in \r
63 xSerialPortInitMinimal(). */\r
64 void __attribute__( (interrupt(IPL0AUTO), vector(_UART2_VECTOR))) vU2InterruptWrapper( void );\r
65 \r
66 /*-----------------------------------------------------------*/\r
67 \r
68 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
69 {\r
70 unsigned short usBRG;\r
71 \r
72         /* Create the queues used by the com test task. */\r
73         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
74         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
75 \r
76         /* Configure the UART and interrupts. */\r
77         usBRG = (unsigned short)(( (float)configPERIPHERAL_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);\r
78         OpenUART2( UART_EN, UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR, usBRG );\r
79         ConfigIntUART2( ( configKERNEL_INTERRUPT_PRIORITY + 1 ) | UART_INT_SUB_PR0 | UART_TX_INT_EN | UART_RX_INT_EN );\r
80 \r
81         xTxHasEnded = pdTRUE;\r
82 \r
83         /* Only a single port is implemented so we don't need to return anything. */\r
84         return NULL;\r
85 }\r
86 /*-----------------------------------------------------------*/\r
87 \r
88 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
89 {\r
90         /* Only one port is supported. */\r
91         ( void ) pxPort;\r
92 \r
93         /* Get the next character from the buffer.  Return false if no characters\r
94         are available or arrive before xBlockTime expires. */\r
95         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
96         {\r
97                 return pdTRUE;\r
98         }\r
99         else\r
100         {\r
101                 return pdFALSE;\r
102         }\r
103 }\r
104 /*-----------------------------------------------------------*/\r
105 \r
106 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
107 {\r
108 signed portBASE_TYPE xReturn;\r
109 \r
110         /* Only one port is supported. */\r
111         ( void ) pxPort;\r
112 \r
113         /* Return false if after the block time there is no room on the Tx queue. */\r
114         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
115         {\r
116                 xReturn = pdFAIL;\r
117         }\r
118         else\r
119         {\r
120                 xReturn = pdPASS;\r
121         }\r
122 \r
123         if( xReturn != pdFAIL )\r
124         {\r
125                 /* A critical section should not be required as xTxHasEnded will not be\r
126                 written to by the ISR if it is already 0. */\r
127                 if(  xTxHasEnded == pdTRUE )\r
128                 {\r
129                         xTxHasEnded = pdFALSE;\r
130                         IFS1SET = _IFS1_U2TXIF_MASK;\r
131                 }\r
132         }\r
133 \r
134         return pdPASS;\r
135 }\r
136 /*-----------------------------------------------------------*/\r
137 \r
138 void vSerialClose( xComPortHandle xPort )\r
139 {\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 void vU2InterruptHandler( void )\r
144 {\r
145 /* Declared static to minimise stack use. */\r
146 static char cChar;\r
147 static portBASE_TYPE xHigherPriorityTaskWoken;\r
148 \r
149         xHigherPriorityTaskWoken = pdFALSE;\r
150 \r
151         /* Are any Rx interrupts pending? */\r
152         if( IFS1bits.U2RXIF == 1)\r
153         {\r
154                 while( U2STAbits.URXDA )\r
155                 {\r
156                         /* Retrieve the received character and place it in the queue of\r
157                         received characters. */\r
158                         cChar = U2RXREG;\r
159                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
160                 }\r
161                 IFS1CLR = _IFS1_U2RXIF_MASK;\r
162         }\r
163 \r
164         /* Are any Tx interrupts pending? */\r
165         if( IFS1bits.U2TXIF == 1 )\r
166         {\r
167                 while( ( U2STAbits.UTXBF ) == 0 )\r
168                 {\r
169                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
170                         {\r
171                                 /* Send the next character queued for Tx. */\r
172                                 U2TXREG = cChar;\r
173                         }\r
174                         else\r
175                         {\r
176                                 /* Queue empty, nothing to send. */\r
177                                 xTxHasEnded = pdTRUE;\r
178                                 break;\r
179                         }\r
180                 }\r
181 \r
182                 IFS1CLR = _IFS1_U2TXIF_MASK;\r
183         }\r
184 \r
185         /* If sending or receiving necessitates a context switch, then switch now. */\r
186         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
187 }\r
188 \r
189 \r
190 \r
191 \r
192 \r
193 \r
194 \r