]> git.sur5r.net Git - freertos/blob - Demo/MB91460_Softune/SRC/serial/serial.c
Ready for V5.2.0 release.
[freertos] / Demo / MB91460_Softune / SRC / serial / serial.c
1 /*\r
2         FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it \r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9 \r
10         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
11         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or \r
12         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for \r
13         more details.\r
14 \r
15         You should have received a copy of the GNU General Public License along \r
16         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59 \r
17         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
18 \r
19         A special exception to the GPL is included to allow you to distribute a \r
20         combined work that includes FreeRTOS.org without being obliged to provide\r
21         the source code for any proprietary components.  See the licensing section\r
22         of http://www.FreeRTOS.org for full details.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 \r
53 /* \r
54  * BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.   \r
55  * \r
56  * This file only supports UART 2\r
57  */\r
58 \r
59 /* Standard includes. */\r
60 #include <stdlib.h>\r
61 \r
62 /* Scheduler includes. */\r
63 #include "FreeRTOS.h"\r
64 #include "queue.h"\r
65 #include "task.h"\r
66 \r
67 /* Demo application includes. */\r
68 #include "serial.h"\r
69 \r
70 /* The queue used to hold received characters. */\r
71 static xQueueHandle xRxedChars; \r
72 \r
73 /* The queue used to hold characters waiting transmission. */\r
74 static xQueueHandle xCharsForTx; \r
75 \r
76 static volatile portSHORT sTHREEmpty;\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
81 {\r
82         portENTER_CRITICAL();\r
83         {\r
84                 /* Create the queues used by the com test task. */\r
85                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
86                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
87 \r
88                  /* Initialize UART asynchronous mode */\r
89                 BGR02 = configPER_CLOCK_HZ / ulWantedBaud;\r
90                   \r
91                 SCR02 = 0x17;   /* 8N1 */\r
92                 SMR02 = 0x0d;   /* enable SOT3, Reset, normal mode */\r
93                 SSR02 = 0x02;   /* LSB first, enable receive interrupts */\r
94 \r
95                 PFR20_D0 = 1;   /* enable UART */\r
96                 PFR20_D1 = 1;   /* enable UART */\r
97 \r
98                 EPFR20_D1 = 0;  /* enable UART */\r
99         }\r
100         portEXIT_CRITICAL();\r
101         \r
102         /* Unlike other ports, this serial code does not allow for more than one\r
103         com port.  We therefore don't return a pointer to a port structure and can\r
104         instead just return NULL. */\r
105         return NULL;\r
106 }\r
107 /*-----------------------------------------------------------*/\r
108 \r
109 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
110 {\r
111         /* Get the next character from the buffer.  Return false if no characters\r
112         are available, or arrive before xBlockTime expires. */\r
113         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
114         {\r
115                 return pdTRUE;\r
116         }\r
117         else\r
118         {\r
119                 return pdFALSE;\r
120         }\r
121 }\r
122 /*-----------------------------------------------------------*/\r
123 \r
124 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
125 {\r
126 signed portBASE_TYPE xReturn;\r
127 \r
128         /* Transmit a character. */\r
129         portENTER_CRITICAL();\r
130         {\r
131                 if( sTHREEmpty == pdTRUE )\r
132                 {\r
133                         /* If sTHREEmpty is true then the UART Tx ISR has indicated that \r
134                         there are no characters queued to be transmitted - so we can\r
135                         write the character directly to the shift Tx register. */\r
136                         sTHREEmpty = pdFALSE;\r
137                         TDR02 = cOutChar;\r
138                         xReturn = pdPASS;\r
139                 }\r
140                 else\r
141                 {\r
142                         /* sTHREEmpty is false, so there are still characters waiting to be\r
143                         transmitted.  We have to queue this character so it gets \r
144                         transmitted     in turn. */\r
145 \r
146                         /* Return false if after the block time there is no room on the Tx \r
147                         queue.  It is ok to block inside a critical section as each task\r
148                         maintains it's own critical section status. */\r
149                         if (xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdTRUE)\r
150                         {\r
151                                 xReturn = pdPASS;\r
152                         }\r
153                         else\r
154                         {\r
155                                 xReturn = pdFAIL;\r
156                         }\r
157                 }\r
158                 \r
159                 if (pdPASS == xReturn)\r
160                 {\r
161                         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
162                         queue and send it.   This does not need to be in a critical section as\r
163                         if the interrupt has already removed the character the next interrupt\r
164                         will simply turn off the Tx interrupt again. */\r
165                         SSR02_TIE = 1;\r
166                 }\r
167                 \r
168         }\r
169         portEXIT_CRITICAL();\r
170 \r
171         return pdPASS;\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 /*\r
176  * UART RX interrupt service routine.\r
177  */\r
178  __interrupt void UART2_RxISR (void)\r
179 {\r
180         signed portCHAR cChar;\r
181         portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
182 \r
183         /* Get the character from the UART and post it on the queue of Rxed \r
184         characters. */\r
185         cChar = RDR02;\r
186 \r
187         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
188 \r
189         if( xHigherPriorityTaskWoken )\r
190         {\r
191                 /*If the post causes a task to wake force a context switch \r
192                 as the woken task may have a higher priority than the task we have \r
193                 interrupted. */\r
194                 portYIELD_FROM_ISR();\r
195         }\r
196 }\r
197 \r
198 /*-----------------------------------------------------------*/\r
199 \r
200 /*\r
201  * UART Tx interrupt service routine.\r
202  */\r
203 __interrupt void UART2_TxISR (void)\r
204 {\r
205         signed portCHAR cChar;\r
206         signed portBASE_TYPE xTaskWoken = pdFALSE;\r
207 \r
208         /* The previous character has been transmitted.  See if there are any\r
209         further characters waiting transmission. */\r
210         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
211         {\r
212                 /* There was another character queued - transmit it now. */\r
213                 TDR02 = cChar;\r
214         }\r
215         else\r
216         {\r
217                 /* There were no other characters to transmit. */\r
218                 sTHREEmpty = pdTRUE;\r
219                 \r
220                 /* Disable transmit interrupts */\r
221                 SSR02_TIE = 0;\r
222         }\r
223 }\r
224 \r