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