]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R4_RM48_TMS570_CCS5/serial.c
Complete RM4x and TMS570 demo.
[freertos] / FreeRTOS / Demo / CORTEX_R4_RM48_TMS570_CCS5 / serial.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*\r
70         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.\r
71         \r
72         ***NOTE*** \r
73         The implementation provided in this file is intended to demonstrate using\r
74         queues to pass data into and out of interrupts, and to demonstrate context \r
75         switching from inside an interrupt service routine.  It is *not* intended to \r
76         represent an efficient implementation.  Real implementations should not pass \r
77         individual characters on queues, but instead use RAM buffers, DMA and/or \r
78         FIFO features as appropriate.  Semaphores can be used to signal a task that \r
79         data is available to be processed.\r
80 */\r
81 \r
82 /* Scheduler includes. */\r
83 #include "FreeRTOS.h"\r
84 #include "queue.h"\r
85 #include "semphr.h"\r
86 \r
87 /* Demo application includes. */\r
88 #include "serial.h"\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 /* Registers required to configure the SCI. */\r
93 #define serialSCI_GCR0_REG              ( * ( ( volatile unsigned long * ) 0xFFF7E400 ) )\r
94 #define serialSCI_GCR1_REG              ( * ( ( volatile unsigned long * ) 0xFFF7E404 ) )\r
95 #define serialSCI_GCR2_REG              ( * ( ( volatile unsigned long * ) 0xFFF7E408 ) )\r
96 #define serialSCI_SETINT_REG            ( * ( ( volatile unsigned long * ) 0xFFF7E40C ) )\r
97 #define serialSCI_CLRINT_REG            ( * ( ( volatile unsigned long * ) 0xFFF7E410 ) )\r
98 #define serialSCI_SETINTLVL_REG         ( * ( ( volatile unsigned long * ) 0xFFF7E414 ) )\r
99 #define serialSCI_CLRINTLVL_REG         ( * ( ( volatile unsigned long * ) 0xFFF7E418 ) )\r
100 #define serialSCI_FLR_REG               ( * ( ( volatile unsigned long * ) 0xFFF7E41C ) )\r
101 #define serialSCI_INTVEC0_REG           ( * ( ( volatile unsigned long * ) 0xFFF7E420 ) )\r
102 #define serialSCI_INTVEC1_REG           ( * ( ( volatile unsigned long * ) 0xFFF7E424 ) )\r
103 #define serialSCI_LENGTH_REG            ( * ( ( volatile unsigned long * ) 0xFFF7E428 ) )\r
104 #define serialSCI_BAUD_REG              ( * ( ( volatile unsigned long * ) 0xFFF7E42C ) )\r
105 #define serialSCI_RD_REG                ( * ( ( volatile unsigned long * ) 0xFFF7E434 ) )\r
106 #define serialSCI_TD_REG                ( * ( ( volatile unsigned long * ) 0xFFF7E438 ) )\r
107 #define serialSCI_FUN_REG               ( * ( ( volatile unsigned long * ) 0xFFF7E43C ) )\r
108 #define serialSCI_DIR_REG               ( * ( ( volatile unsigned long * ) 0xFFF7E440 ) )\r
109 #define serialSCI_DIN_REG               ( * ( ( volatile unsigned long * ) 0xFFF7E444 ) )\r
110 #define serialSCI_DOUT_REG              ( * ( ( volatile unsigned long * ) 0xFFF7E448 ) )\r
111 #define serialSCI_DSET_REG              ( * ( ( volatile unsigned long * ) 0xFFF7E44C ) )\r
112 #define serialSCI_DCLR_REG              ( * ( ( volatile unsigned long * ) 0xFFF7E450 ) )\r
113 \r
114 /* SCI constants */\r
115 #define serialSCI_FE_INT    ( 0x04000000 )  /* framming error */\r
116 #define serialSCI_OE_INT    ( 0x02000000 )  /* overrun error */\r
117 #define serialSCI_PE_INT    ( 0x01000000 )  /* parity error */\r
118 #define serialSCI_RX_INT    ( 0x00000200 )  /* receive buffer ready */\r
119 #define serialSCI_TX_INT    ( 0x00000100 )  /* transmit buffer ready */\r
120 #define serialSCI_WAKE_INT  ( 0x00000002 )  /* wakeup */\r
121 #define serialSCI_BREAK_INT ( 0x00000001 )  /* break detect */\r
122 #define serialSCI_IDLE_FLG  ( 0x00000004 )  /* IDLE flasg */\r
123 \r
124 /* Registers required to configure the VIM. */\r
125 #define serialVIM_REQMASKSET0_REG       ( * ( ( volatile unsigned long * ) 0xFFFFFE30 ) )\r
126 #define serialVIM_SCIHINT_RAM       ( * ( ( void (**)(void) ) 0xFFF82038 ) )\r
127 \r
128 \r
129 /*-----------------------------------------------------------*/\r
130 \r
131 /* Misc defines. */\r
132 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
133 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
134 #define serTX_BLOCK_TIME                                ( 40 / portTICK_RATE_MS )\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 /* The queue used to hold received characters. */\r
139 static xQueueHandle xRxedChars = NULL;\r
140 static xQueueHandle xCharsForTx = NULL;\r
141 \r
142 /*-----------------------------------------------------------*/\r
143 \r
144 /* UART interrupt handler. */\r
145 __interrupt void vSCIInterruptHandler( void );\r
146 \r
147 /*-----------------------------------------------------------*/\r
148 \r
149 /*\r
150  * See the serial2.h header file.\r
151  */\r
152 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
153 {\r
154 xComPortHandle xReturn = ( xComPortHandle ) 0;\r
155 \r
156         /* unused parameters, demo has a fixed baud rate (19200) */\r
157         ( void ) ulWantedBaud;\r
158 \r
159         /* Create the queues used to hold Rx/Tx characters. */\r
160         xRxedChars  = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
161         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
162         \r
163         /* If the queue/semaphore was created correctly then setup the serial port\r
164         hardware. */\r
165         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
166         {\r
167                 /* Initalise SCI1 */\r
168             /* Bring SCI out of reset */\r
169                 serialSCI_GCR0_REG = 0x00000001UL;\r
170             /* Disable all interrupts */\r
171                 serialSCI_CLRINT_REG = 0xFFFFFFFFUL;\r
172                 /* All Interrupt to SCI High Level */\r
173                 serialSCI_CLRINTLVL_REG = 0xFFFFFFFFUL;\r
174             /* Global control 1 */\r
175                 serialSCI_GCR1_REG = 0x03010032UL;\r
176             /* Baudrate */\r
177                 serialSCI_BAUD_REG = 292;\r
178             /* Transmission length (8-bit) */\r
179                 serialSCI_LENGTH_REG = 8 - 1;\r
180             /* Set SCI pins functional mode */\r
181                 serialSCI_FUN_REG = 0x00000006UL;\r
182             /* Enable RX interrupt */\r
183             serialSCI_SETINT_REG = 0x00000200UL;\r
184             /* Finally start SCI1 */\r
185             serialSCI_GCR1_REG |= 0x00000080UL;\r
186 \r
187                 /* Setup interrupt routine address in VIM table */\r
188             serialVIM_SCIHINT_RAM = &vSCIInterruptHandler;\r
189                 /* Enable SCI interrupt in VIM */\r
190             serialVIM_REQMASKSET0_REG = 0x00002000UL;\r
191         }\r
192 \r
193         /* This demo file only supports a single port but we have to return\r
194         something to comply with the standard demo header file. */\r
195         return xReturn;\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
200 {\r
201         /* The port handle is not required as this driver only supports one port. */\r
202         ( void ) pxPort;\r
203 \r
204         /* Get the next character from the buffer.  Return false if no characters\r
205         are available, or arrive before xBlockTime expires. */\r
206         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
207         {\r
208                 return pdTRUE;\r
209         }\r
210         else\r
211         {\r
212                 return pdFALSE;\r
213         }\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned portSHORT usStringLength )\r
218 {\r
219 signed char *pxNext;\r
220 \r
221         /* A couple of parameters that this port does not use. */\r
222         ( void ) usStringLength;\r
223 \r
224         /* NOTE: This implementation does not handle the queue being full as no\r
225         block time is used! */\r
226 \r
227         /* Send each character in the string, one at a time. */\r
228         pxNext = ( signed char * ) pcString;\r
229         while( *pxNext )\r
230         {\r
231                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
232                 pxNext++;\r
233         }\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
238 {\r
239 signed portBASE_TYPE xReturn;\r
240 \r
241         /* check if we are already transmitting */\r
242         if ( (serialSCI_SETINT_REG & serialSCI_TX_INT) == 0)\r
243         {\r
244                 /* First byte */\r
245 \r
246                 /* Wait until IDLE idle period is finished */\r
247                 while ( (serialSCI_FLR_REG & serialSCI_IDLE_FLG) != 0 ) \r
248                 { \r
249                         /* wait */ \r
250                 };\r
251                 \r
252                 /* Need to send first byte before interrupts flags are set. */\r
253                 serialSCI_TD_REG = cOutChar;\r
254                 \r
255                 /* Enable the TX interrupt. */\r
256                 serialSCI_SETINT_REG = serialSCI_TX_INT;\r
257 \r
258                 xReturn = pdPASS;\r
259         }\r
260         else if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )\r
261         {\r
262                 xReturn = pdPASS;\r
263         }\r
264         else\r
265         {\r
266                 xReturn = pdFAIL;\r
267         }\r
268 \r
269         return xReturn;\r
270 }\r
271 /*-----------------------------------------------------------*/\r
272 \r
273 void vSerialClose( xComPortHandle xPort )\r
274 {\r
275         /* Not supported as not required by the demo application. */\r
276 }\r
277 /*-----------------------------------------------------------*/\r
278 \r
279 __interrupt void vSCIInterruptHandler( void )\r
280 {\r
281 /* xHigherPriorityTaskWoken must be initialised to pdFALSE. */\r
282 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
283 char cChar;\r
284 portBASE_TYPE xVectorValue = serialSCI_INTVEC0_REG;\r
285 \r
286         switch( xVectorValue )\r
287         {\r
288                 case 11:\r
289                         /* Receive buffer full interrupt, send received char to queue */\r
290                         cChar = serialSCI_RD_REG;\r
291                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
292                         break;\r
293 \r
294                 case 12:\r
295                         /* Transmit buffer empty interrupt received */\r
296                         /* Are there any more characters to transmit? */\r
297                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
298                         {\r
299                                 /* A character was retrieved from the queue so can be sent to\r
300                                 the TD now. */\r
301                                 serialSCI_TD_REG = cChar;\r
302                         }\r
303                         else\r
304                         {\r
305                                 /* no more bytes, clear the TX interrupt */\r
306                                 serialSCI_CLRINT_REG = serialSCI_TX_INT;\r
307                         }\r
308                         break;\r
309 \r
310                 default:\r
311                         /* unused interrupt, clear flags */\r
312                         serialSCI_FLR_REG = 0x07000003;\r
313         }\r
314 \r
315         /* If calling xQueueSendFromISR() above caused a task to leave the blocked\r
316         state, and the task that left the blocked state has a priority above the\r
317         task that this interrupt interrupted, then xHighPriorityTaskWoken will have\r
318         been set to pdTRUE.  If xHigherPriorityTaskWoken equals true then calling\r
319         portYIELD_FROM_ISR() will result in this interrupt returning directly to the\r
320         unblocked task. */\r
321         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
322 }\r
323 \r
324 \r
325 \r
326 \r
327 \r
328         \r