]> git.sur5r.net Git - freertos/blob - Demo/NEC_V850ES_IAR/serial/serial.c
Remove unnecessary use of portLONG, portCHAR and portSHORT.
[freertos] / Demo / NEC_V850ES_IAR / serial / serial.c
1 /*\r
2         FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /*\r
49         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.\r
50 \r
51         *NOTE* - This file is designed to test some of the RTOS features - it is\r
52         not intended to represent an efficient implementation!\r
53 */\r
54 \r
55 /* Standard includes. */\r
56 #include <stdlib.h>\r
57 \r
58 /* Scheduler includes. */\r
59 #include "FreeRTOS.h"\r
60 #include "queue.h"\r
61 \r
62 /* Demo application includes. */\r
63 #include "serial.h"\r
64 \r
65 \r
66 /* Hardware specifics. */\r
67 #define serRX_DATA_PIN          ( 0x01 )\r
68 #define serTX_DATA_PIN          ( 0x02 )\r
69 #define serCLOCK_Fxx_DIV_8              0x03\r
70 #define serUPWR         ( 0x80 )\r
71 #define serUTXE         ( 0x40 )\r
72 #define serURXE         ( 0x20 )\r
73 #define serUCL          ( 0x02 )\r
74 #define serLSB          ( 0x10 )\r
75 \r
76 /* Misc. */\r
77 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
78 #define serHANDLE                                               ( ( xComPortHandle ) 1 )\r
79 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
80 \r
81 /*-----------------------------------------------------------*/\r
82 \r
83 /* Queues used to hold received characters, and characters waiting to be\r
84 transmitted. */\r
85 static xQueueHandle xRxedChars;\r
86 static xQueueHandle xCharsForTx;\r
87 \r
88 /*-----------------------------------------------------------*/\r
89 \r
90 /* Interrupt entry point written in the assembler file serialISR.s85. */\r
91 extern void vSerialISREntry( void );\r
92 \r
93 /* Flag to indicate whether or not there are characters already queued to send. */\r
94 static volatile unsigned long ulTxInProgress = pdFALSE;\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 /*\r
99  * See the serial2.h header file.\r
100  */\r
101 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
102 {\r
103 xComPortHandle xReturn = serHANDLE;\r
104 const unsigned portLONG ulFuclk = ( configCPU_CLOCK_HZ / 2 ) / 8UL;\r
105 \r
106         /* Create the queues used to hold Rx and Tx characters. */\r
107         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
108         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
109 \r
110         /* If the queues were created correctly then setup the serial port\r
111         hardware. */\r
112         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
113         {\r
114                 portENTER_CRITICAL();\r
115                 {\r
116                         /* Set the UART0 Rx and Tx pins to their alternative function. */\r
117                         PMC3 |= ( serRX_DATA_PIN | serTX_DATA_PIN );\r
118                         PM3 &= ~( serTX_DATA_PIN );\r
119 \r
120                         /* Setup clock for required baud. */                    \r
121                         UD0CTL1 = serCLOCK_Fxx_DIV_8;\r
122                         UD0CTL2 = ulFuclk / ( 2 * ulWantedBaud );\r
123 \r
124                         /* Enable, n81. */                      \r
125                         UD0CTL0 = ( serUPWR | serUTXE | serURXE | serUCL | serLSB );\r
126                         \r
127                         /* Enable interrupts for both Rx and Tx. */\r
128                         UD0TIC  = 0x07;\r
129                         UD0RIC  = 0x07;\r
130                         \r
131                         ulTxInProgress = pdFALSE;\r
132                 }\r
133                 portEXIT_CRITICAL();\r
134         }\r
135         else\r
136         {\r
137                 xReturn = ( xComPortHandle ) 0;\r
138         }\r
139 \r
140         /* This demo file only supports a single port but we have to return\r
141         something to comply with the standard demo header file. */\r
142         return xReturn;\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
147 {\r
148         /* The port handle is not required as this driver only supports one port. */\r
149         ( void ) pxPort;\r
150 \r
151         /* Get the next character from the buffer.  Return false if no characters\r
152         are available, or arrive before xBlockTime expires. */\r
153         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
154         {\r
155                 return pdTRUE;\r
156         }\r
157         else\r
158         {\r
159                 return pdFALSE;\r
160         }\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )\r
165 {\r
166 signed portCHAR *pxNext;\r
167 \r
168         /* A couple of parameters that this port does not use. */\r
169         ( void ) usStringLength;\r
170         ( void ) pxPort;\r
171 \r
172         /* NOTE: This implementation does not handle the queue being full as no\r
173         block time is used! */\r
174 \r
175         /* The port handle is not required as this driver only supports UART0. */\r
176         ( void ) pxPort;\r
177 \r
178         /* Send each character in the string, one at a time. */\r
179         pxNext = ( signed portCHAR * ) pcString;\r
180         while( *pxNext )\r
181         {\r
182                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
183                 pxNext++;\r
184         }\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
189 {\r
190 portBASE_TYPE xReturn = pdPASS;\r
191 \r
192         portENTER_CRITICAL();\r
193         {\r
194                 /* There are currently no characters queued up to send so write the\r
195                 character directly to the UART. */\r
196                 if( ulTxInProgress == pdFALSE )\r
197                 {\r
198                         UD0TX = cOutChar;\r
199                         ulTxInProgress = pdTRUE;\r
200                 }\r
201                 else\r
202                 {\r
203                         /* The UART is already busy so write the character to the Tx queue.\r
204                         The queue is drained from within the Tx interrupt. */\r
205                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
206                         {\r
207                                 xReturn = pdFAIL;\r
208                         }\r
209                 }\r
210         }\r
211         portEXIT_CRITICAL();\r
212         \r
213         return xReturn;\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 void vSerialClose( xComPortHandle xPort )\r
218 {\r
219         /* Not supported as not required by the demo application. */\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 /* Tx interrupt handler.  This is called from the asm file wrapper. */\r
224 void vUARTTxISRHandler( void )\r
225 {\r
226 char cChar;\r
227 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
228 \r
229         /* Are there any more characters queue to transmit? */\r
230         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
231         {\r
232                 /* Send the next character. */\r
233                 UD0TX = cChar;\r
234         }\r
235         else\r
236         {\r
237                 /* The UART is no longer active. */\r
238                 ulTxInProgress = pdFALSE;\r
239         }\r
240         \r
241         /* If reading a character from the Rx queue caused a task to unblock, and\r
242         the unblocked task has a priority higher than the currently running task,\r
243         then xHigherPriorityTaskWoken will have been set to true and a context\r
244         switch should occur now. */\r
245         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 /* Rx interrupt handler.  This is called from the asm file wrapper. */\r
250 void vUARTRxISRHandler( void )\r
251 {\r
252 portCHAR cChar;\r
253 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
254 \r
255         /* Send the received character to the Rx queue. */\r
256         cChar = UD0RX;\r
257         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
258         \r
259         /* If sending a character to the Tx queue caused a task to unblock, and\r
260         the unblocked task has a priority higher than the currently running task,\r
261         then xHigherPriorityTaskWoken will have been set to true and a context\r
262         switch should occur now. */\r
263         portYIELD_FROM_ISR( xHigherPriorityTaskWoken ); \r
264 }\r
265 \r
266 \r
267 \r
268 \r
269         \r