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