]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R4_RM48_TMS570_CCS5/serial.c
Slow configured clock in the RM48 and TMS570 demos to 50MHz.
[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 /* Baudrate */\r
129 #define serialBAURATE           19200.0                 /* Baudrate in Hz */\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /* Misc defines. */\r
134 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
135 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
136 #define serTX_BLOCK_TIME                                ( 40 / portTICK_RATE_MS )\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 \r
140 /* The queue used to hold received characters. */\r
141 static xQueueHandle xRxedChars = NULL;\r
142 static xQueueHandle xCharsForTx = NULL;\r
143 \r
144 /*-----------------------------------------------------------*/\r
145 \r
146 /* UART interrupt handler. */\r
147 __interrupt void vSCIInterruptHandler( void );\r
148 \r
149 /*-----------------------------------------------------------*/\r
150 \r
151 /*\r
152  * See the serial2.h header file.\r
153  */\r
154 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
155 {\r
156 xComPortHandle xReturn = ( xComPortHandle ) 0;\r
157 \r
158         /* unused parameters, demo has a fixed baud rate (19200) */\r
159         ( void ) ulWantedBaud;\r
160 \r
161         /* Create the queues used to hold Rx/Tx characters. */\r
162         xRxedChars  = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
163         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
164         \r
165         /* If the queue/semaphore was created correctly then setup the serial port\r
166         hardware. */\r
167         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
168         {\r
169                 /* Initalise SCI1 */\r
170             /* Bring SCI out of reset */\r
171                 serialSCI_GCR0_REG = 0x00000001UL;\r
172             /* Disable all interrupts */\r
173                 serialSCI_CLRINT_REG = 0xFFFFFFFFUL;\r
174                 /* All Interrupt to SCI High Level */\r
175                 serialSCI_CLRINTLVL_REG = 0xFFFFFFFFUL;\r
176             /* Global control 1 */\r
177                 serialSCI_GCR1_REG = 0x03010032UL;\r
178             /* Baudrate */\r
179                 serialSCI_BAUD_REG = ((unsigned long)((configCPU_CLOCK_HZ / (16.0 * serialBAURATE) + 0.5)) - 1) & 0x00FFFFFF;\r
180             /* Transmission length (8-bit) */\r
181                 serialSCI_LENGTH_REG = 8 - 1;\r
182             /* Set SCI pins functional mode */\r
183                 serialSCI_FUN_REG = 0x00000006UL;\r
184             /* Enable RX interrupt */\r
185             serialSCI_SETINT_REG = 0x00000200UL;\r
186             /* Finally start SCI1 */\r
187             serialSCI_GCR1_REG |= 0x00000080UL;\r
188 \r
189                 /* Setup interrupt routine address in VIM table */\r
190             serialVIM_SCIHINT_RAM = &vSCIInterruptHandler;\r
191                 /* Enable SCI interrupt in VIM */\r
192             serialVIM_REQMASKSET0_REG = 0x00002000UL;\r
193         }\r
194 \r
195         /* This demo file only supports a single port but we have to return\r
196         something to comply with the standard demo header file. */\r
197         return xReturn;\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
202 {\r
203         /* The port handle is not required as this driver only supports one port. */\r
204         ( void ) pxPort;\r
205 \r
206         /* Get the next character from the buffer.  Return false if no characters\r
207         are available, or arrive before xBlockTime expires. */\r
208         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
209         {\r
210                 return pdTRUE;\r
211         }\r
212         else\r
213         {\r
214                 return pdFALSE;\r
215         }\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned portSHORT usStringLength )\r
220 {\r
221 signed char *pxNext;\r
222 \r
223         /* A couple of parameters that this port does not use. */\r
224         ( void ) usStringLength;\r
225 \r
226         /* NOTE: This implementation does not handle the queue being full as no\r
227         block time is used! */\r
228 \r
229         /* Send each character in the string, one at a time. */\r
230         pxNext = ( signed char * ) pcString;\r
231         while( *pxNext )\r
232         {\r
233                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
234                 pxNext++;\r
235         }\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
240 {\r
241 signed portBASE_TYPE xReturn;\r
242 \r
243         /* check if we are already transmitting */\r
244         if ( (serialSCI_SETINT_REG & serialSCI_TX_INT) == 0)\r
245         {\r
246                 /* First byte */\r
247 \r
248                 /* Wait until IDLE idle period is finished */\r
249                 while ( (serialSCI_FLR_REG & serialSCI_IDLE_FLG) != 0 ) \r
250                 { \r
251                         /* wait */ \r
252                 };\r
253                 \r
254                 /* Need to send first byte before interrupts flags are set. */\r
255                 serialSCI_TD_REG = cOutChar;\r
256                 \r
257                 /* Enable the TX interrupt. */\r
258                 serialSCI_SETINT_REG = serialSCI_TX_INT;\r
259 \r
260                 xReturn = pdPASS;\r
261         }\r
262         else if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )\r
263         {\r
264                 xReturn = pdPASS;\r
265         }\r
266         else\r
267         {\r
268                 xReturn = pdFAIL;\r
269         }\r
270 \r
271         return xReturn;\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r
275 void vSerialClose( xComPortHandle xPort )\r
276 {\r
277         /* Not supported as not required by the demo application. */\r
278 }\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 __interrupt void vSCIInterruptHandler( void )\r
282 {\r
283 /* xHigherPriorityTaskWoken must be initialised to pdFALSE. */\r
284 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
285 char cChar;\r
286 portBASE_TYPE xVectorValue = serialSCI_INTVEC0_REG;\r
287 \r
288         switch( xVectorValue )\r
289         {\r
290                 case 11:\r
291                         /* Receive buffer full interrupt, send received char to queue */\r
292                         cChar = serialSCI_RD_REG;\r
293                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
294                         break;\r
295 \r
296                 case 12:\r
297                         /* Transmit buffer empty interrupt received */\r
298                         /* Are there any more characters to transmit? */\r
299                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
300                         {\r
301                                 /* A character was retrieved from the queue so can be sent to\r
302                                 the TD now. */\r
303                                 serialSCI_TD_REG = cChar;\r
304                         }\r
305                         else\r
306                         {\r
307                                 /* no more bytes, clear the TX interrupt */\r
308                                 serialSCI_CLRINT_REG = serialSCI_TX_INT;\r
309                         }\r
310                         break;\r
311 \r
312                 default:\r
313                         /* unused interrupt, clear flags */\r
314                         serialSCI_FLR_REG = 0x07000003;\r
315         }\r
316 \r
317         /* If calling xQueueSendFromISR() above caused a task to leave the blocked\r
318         state, and the task that left the blocked state has a priority above the\r
319         task that this interrupt interrupted, then xHighPriorityTaskWoken will have\r
320         been set to pdTRUE.  If xHigherPriorityTaskWoken equals true then calling\r
321         portYIELD_FROM_ISR() will result in this interrupt returning directly to the\r
322         unblocked task. */\r
323         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
324 }\r
325 \r
326 \r
327 \r
328 \r
329 \r
330         \r