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