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