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