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