]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MB91460_Softune/SRC/serial/serial.c
Add additional critical section to the default tickless implementations.
[freertos] / FreeRTOS / Demo / MB91460_Softune / SRC / serial / serial.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 \r
66 /* \r
67  * BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.   \r
68  * \r
69  * This file only supports UART 2\r
70  */\r
71 \r
72 /* Standard includes. */\r
73 #include <stdlib.h>\r
74 \r
75 /* Scheduler includes. */\r
76 #include "FreeRTOS.h"\r
77 #include "queue.h"\r
78 #include "task.h"\r
79 \r
80 /* Demo application includes. */\r
81 #include "serial.h"\r
82 \r
83 /* The queue used to hold received characters. */\r
84 static xQueueHandle xRxedChars; \r
85 \r
86 /* The queue used to hold characters waiting transmission. */\r
87 static xQueueHandle xCharsForTx; \r
88 \r
89 static volatile portSHORT sTHREEmpty;\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
94 {\r
95         portENTER_CRITICAL();\r
96         {\r
97                 /* Create the queues used by the com test task. */\r
98                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
99                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
100 \r
101                  /* Initialize UART asynchronous mode */\r
102                 BGR02 = configPER_CLOCK_HZ / ulWantedBaud;\r
103                   \r
104                 SCR02 = 0x17;   /* 8N1 */\r
105                 SMR02 = 0x0d;   /* enable SOT3, Reset, normal mode */\r
106                 SSR02 = 0x02;   /* LSB first, enable receive interrupts */\r
107 \r
108                 PFR20_D0 = 1;   /* enable UART */\r
109                 PFR20_D1 = 1;   /* enable UART */\r
110 \r
111                 EPFR20_D1 = 0;  /* enable UART */\r
112         }\r
113         portEXIT_CRITICAL();\r
114         \r
115         /* Unlike other ports, this serial code does not allow for more than one\r
116         com port.  We therefore don't return a pointer to a port structure and can\r
117         instead just return NULL. */\r
118         return NULL;\r
119 }\r
120 /*-----------------------------------------------------------*/\r
121 \r
122 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
123 {\r
124         /* Get the next character from the buffer.  Return false if no characters\r
125         are available, or arrive before xBlockTime expires. */\r
126         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
127         {\r
128                 return pdTRUE;\r
129         }\r
130         else\r
131         {\r
132                 return pdFALSE;\r
133         }\r
134 }\r
135 /*-----------------------------------------------------------*/\r
136 \r
137 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
138 {\r
139 signed portBASE_TYPE xReturn;\r
140 \r
141         /* Transmit a character. */\r
142         portENTER_CRITICAL();\r
143         {\r
144                 if( sTHREEmpty == pdTRUE )\r
145                 {\r
146                         /* If sTHREEmpty is true then the UART Tx ISR has indicated that \r
147                         there are no characters queued to be transmitted - so we can\r
148                         write the character directly to the shift Tx register. */\r
149                         sTHREEmpty = pdFALSE;\r
150                         TDR02 = cOutChar;\r
151                         xReturn = pdPASS;\r
152                 }\r
153                 else\r
154                 {\r
155                         /* sTHREEmpty is false, so there are still characters waiting to be\r
156                         transmitted.  We have to queue this character so it gets \r
157                         transmitted     in turn. */\r
158 \r
159                         /* Return false if after the block time there is no room on the Tx \r
160                         queue.  It is ok to block inside a critical section as each task\r
161                         maintains it's own critical section status. */\r
162                         if (xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdTRUE)\r
163                         {\r
164                                 xReturn = pdPASS;\r
165                         }\r
166                         else\r
167                         {\r
168                                 xReturn = pdFAIL;\r
169                         }\r
170                 }\r
171                 \r
172                 if (pdPASS == xReturn)\r
173                 {\r
174                         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
175                         queue and send it.   This does not need to be in a critical section as\r
176                         if the interrupt has already removed the character the next interrupt\r
177                         will simply turn off the Tx interrupt again. */\r
178                         SSR02_TIE = 1;\r
179                 }\r
180                 \r
181         }\r
182         portEXIT_CRITICAL();\r
183 \r
184         return pdPASS;\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 /*\r
189  * UART RX interrupt service routine.\r
190  */\r
191  __interrupt void UART2_RxISR (void)\r
192 {\r
193         signed portCHAR cChar;\r
194         portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
195 \r
196         /* Get the character from the UART and post it on the queue of Rxed \r
197         characters. */\r
198         cChar = RDR02;\r
199 \r
200         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
201 \r
202         if( xHigherPriorityTaskWoken )\r
203         {\r
204                 /*If the post causes a task to wake force a context switch \r
205                 as the woken task may have a higher priority than the task we have \r
206                 interrupted. */\r
207                 portYIELD_FROM_ISR();\r
208         }\r
209 }\r
210 \r
211 /*-----------------------------------------------------------*/\r
212 \r
213 /*\r
214  * UART Tx interrupt service routine.\r
215  */\r
216 __interrupt void UART2_TxISR (void)\r
217 {\r
218         signed portCHAR cChar;\r
219         signed portBASE_TYPE xTaskWoken = pdFALSE;\r
220 \r
221         /* The previous character has been transmitted.  See if there are any\r
222         further characters waiting transmission. */\r
223         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
224         {\r
225                 /* There was another character queued - transmit it now. */\r
226                 TDR02 = cChar;\r
227         }\r
228         else\r
229         {\r
230                 /* There were no other characters to transmit. */\r
231                 sTHREEmpty = pdTRUE;\r
232                 \r
233                 /* Disable transmit interrupts */\r
234                 SSR02_TIE = 0;\r
235         }\r
236 }\r
237 \r