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