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