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