]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/dsPIC_MPLAB/serial/serial.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / dsPIC_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 \r
38 /* Scheduler include files. */\r
39 #include "FreeRTOS.h"\r
40 #include "queue.h"\r
41 #include "task.h"\r
42 \r
43 /* Demo app include files. */\r
44 #include "serial.h"\r
45 \r
46 /* Hardware setup. */\r
47 #define serOUTPUT                                               0\r
48 #define serINPUT                                                1\r
49 #define serLOW_SPEED                                    0\r
50 #define serONE_STOP_BIT                                 0\r
51 #define serEIGHT_DATA_BITS_NO_PARITY    0\r
52 #define serNORMAL_IDLE_STATE                    0\r
53 #define serAUTO_BAUD_OFF                                0\r
54 #define serLOOPBACK_OFF                                 0\r
55 #define serWAKE_UP_DISABLE                              0\r
56 #define serNO_HARDWARE_FLOW_CONTROL             0\r
57 #define serSTANDARD_IO                                  0\r
58 #define serNO_IRDA                                              0\r
59 #define serCONTINUE_IN_IDLE_MODE                0\r
60 #define serUART_ENABLED                                 1\r
61 #define serINTERRUPT_ON_SINGLE_CHAR             0\r
62 #define serTX_ENABLE                                    1\r
63 #define serINTERRUPT_ENABLE                             1\r
64 #define serINTERRUPT_DISABLE                    0\r
65 #define serCLEAR_FLAG                                   0\r
66 #define serSET_FLAG                                             1\r
67 \r
68 \r
69 /* The queues used to communicate between tasks and ISR's. */\r
70 static QueueHandle_t xRxedChars; \r
71 static QueueHandle_t xCharsForTx; \r
72 \r
73 static portBASE_TYPE xTxHasEnded;\r
74 /*-----------------------------------------------------------*/\r
75 \r
76 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
77 {\r
78 char cChar;\r
79 \r
80         /* Create the queues used by the com test task. */\r
81         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
82         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
83 \r
84         /* Setup the UART. */\r
85         U2MODEbits.BRGH         = serLOW_SPEED;\r
86         U2MODEbits.STSEL        = serONE_STOP_BIT;\r
87         U2MODEbits.PDSEL        = serEIGHT_DATA_BITS_NO_PARITY;\r
88         U2MODEbits.ABAUD        = serAUTO_BAUD_OFF;\r
89         U2MODEbits.LPBACK       = serLOOPBACK_OFF;\r
90         U2MODEbits.WAKE         = serWAKE_UP_DISABLE;\r
91         U2MODEbits.UEN          = serNO_HARDWARE_FLOW_CONTROL;\r
92         U2MODEbits.IREN         = serNO_IRDA;\r
93         U2MODEbits.USIDL        = serCONTINUE_IN_IDLE_MODE;\r
94         U2MODEbits.UARTEN       = serUART_ENABLED;\r
95 \r
96         U2BRG = (unsigned short)(( (float)configCPU_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);\r
97 \r
98         U2STAbits.URXISEL       = serINTERRUPT_ON_SINGLE_CHAR;\r
99         U2STAbits.UTXEN         = serTX_ENABLE;\r
100         U2STAbits.UTXINV        = serNORMAL_IDLE_STATE;\r
101         U2STAbits.UTXISEL0      = serINTERRUPT_ON_SINGLE_CHAR;\r
102         U2STAbits.UTXISEL1      = serINTERRUPT_ON_SINGLE_CHAR;\r
103 \r
104         /* It is assumed that this function is called prior to the scheduler being\r
105         started.  Therefore interrupts must not be allowed to occur yet as they\r
106         may attempt to perform a context switch. */\r
107         portDISABLE_INTERRUPTS();\r
108 \r
109         IFS1bits.U2RXIF = serCLEAR_FLAG;\r
110         IFS1bits.U2TXIF = serCLEAR_FLAG;\r
111         IPC7bits.U2RXIP = configKERNEL_INTERRUPT_PRIORITY;\r
112         IPC7bits.U2TXIP = configKERNEL_INTERRUPT_PRIORITY;\r
113         IEC1bits.U2TXIE = serINTERRUPT_ENABLE;\r
114         IEC1bits.U2RXIE = serINTERRUPT_ENABLE;\r
115 \r
116         /* Clear the Rx buffer. */\r
117         while( U2STAbits.URXDA == serSET_FLAG )\r
118         {\r
119                 cChar = U2RXREG;\r
120         }\r
121 \r
122         xTxHasEnded = pdTRUE;\r
123 \r
124         return NULL;\r
125 }\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
129 {\r
130         /* Only one port is supported. */\r
131         ( void ) pxPort;\r
132 \r
133         /* Get the next character from the buffer.  Return false if no characters\r
134         are available or arrive before xBlockTime expires. */\r
135         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
136         {\r
137                 return pdTRUE;\r
138         }\r
139         else\r
140         {\r
141                 return pdFALSE;\r
142         }\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
147 {\r
148         /* Only one port is supported. */\r
149         ( void ) pxPort;\r
150 \r
151         /* Return false if after the block time there is no room on the Tx queue. */\r
152         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
153         {\r
154                 return pdFAIL;\r
155         }\r
156 \r
157         /* A critical section should not be required as xTxHasEnded will not be\r
158         written to by the ISR if it is already 0 (is this correct?). */\r
159         if( xTxHasEnded )\r
160         {\r
161                 xTxHasEnded = pdFALSE;\r
162                 IFS1bits.U2TXIF = serSET_FLAG;\r
163         }\r
164 \r
165         return pdPASS;\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 void vSerialClose( xComPortHandle xPort )\r
170 {\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 void __attribute__((__interrupt__, auto_psv)) _U2RXInterrupt( void )\r
175 {\r
176 char cChar;\r
177 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
178 \r
179         /* Get the character and post it on the queue of Rxed characters.\r
180         If the post causes a task to wake force a context switch as the woken task\r
181         may have a higher priority than the task we have interrupted. */\r
182         IFS1bits.U2RXIF = serCLEAR_FLAG;\r
183         while( U2STAbits.URXDA )\r
184         {\r
185                 cChar = U2RXREG;\r
186                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
187         }\r
188 \r
189         if( xHigherPriorityTaskWoken != pdFALSE )\r
190         {\r
191                 taskYIELD();\r
192         }\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 void __attribute__((__interrupt__, auto_psv)) _U2TXInterrupt( void )\r
197 {\r
198 signed char cChar;\r
199 portBASE_TYPE xTaskWoken = pdFALSE;\r
200 \r
201         /* If the transmit buffer is full we cannot get the next character.\r
202         Another interrupt will occur the next time there is space so this does\r
203         not matter. */\r
204         IFS1bits.U2TXIF = serCLEAR_FLAG;\r
205         while( !( U2STAbits.UTXBF ) )\r
206         {\r
207                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
208                 {\r
209                         /* Send the next character queued for Tx. */\r
210                         U2TXREG = cChar;\r
211                 }\r
212                 else\r
213                 {\r
214                         /* Queue empty, nothing to send. */\r
215                         xTxHasEnded = pdTRUE;\r
216                         break;\r
217                 }\r
218         }\r
219 \r
220         if( xTaskWoken != pdFALSE )\r
221         {\r
222                 taskYIELD();\r
223         }\r
224 }\r
225 \r
226 \r