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