]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/TriCore_TC1782_TriBoard_GCC/RTOSDemo/serial.c
Change version number in common files within the FreeRTOS-plus directory and check...
[freertos] / FreeRTOS / Demo / TriCore_TC1782_TriBoard_GCC / RTOSDemo / 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 /* Hardware specific includes. */\r
76 #include <tc1782.h>\r
77 #include <machine/intrinsics.h>\r
78 #include <machine/cint.h>\r
79 #include <machine/wdtcon.h>\r
80 \r
81 /* Scheduler Includes. */\r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 #include "queue.h"\r
85 \r
86 /* Demo Includes. */\r
87 #include "serial.h"\r
88 \r
89 /*****************************************************************************\r
90  * Please note!\r
91  *\r
92  * This file is here to provide a means of generating interrupts to test the\r
93  * FreeRTOS tricore port.  First - it configures the UART into loopback mode,\r
94  * and has only been used in loopback mode.  Therefore, the baud rate\r
95  * calculation used has never been verified for correctness.  Second -\r
96  * characters are passed into and out of the interrupt service routines using\r
97  * character queues.  This provides a good test of the interrupt interaction,\r
98  * context switching mechanism, and kernel loading, it is however highly\r
99  * inefficient if the UART is being used to handle even moderate amounts of\r
100  * data throughput.\r
101  */\r
102 \r
103 \r
104 /*---------------------------------------------------------------------------*/\r
105 \r
106 /*\r
107  * See if the Serial Transmit Interrupt is currently activated, meaning that\r
108  * the interrupt is working through the back log of bytes that it needs to\r
109  * send. If the ISR is not enabled, then it will be triggered to send the first\r
110  * byte, and it will be automatically re-triggered when that byte has been\r
111  * sent. When the queue is exhausted, the ISR disables itself.\r
112  */\r
113 static void prvCheckTransmit( void );\r
114 \r
115 /*\r
116  * The transmit and receive interrupt handlers.\r
117  */\r
118 static void prvTxBufferInterruptHandler( int iArg ) __attribute__( ( longcall ) );\r
119 static void prvRxInterruptHandler( int iArg )__attribute__( ( longcall ) );\r
120 \r
121 /*-----------------------------------------------------------*/\r
122 \r
123 /* Queues used to pass bytes into and out of the interrupt handlers.\r
124 NOTE:  This is not intended to be an example of an efficient interrupt handler,\r
125 but instead to load the kernel and interrupt mechanisms in order to test the\r
126 FreeRTOS port.  Using a FIFO, DMA, circular buffer, etc. architecture will\r
127 to improve efficiency. */\r
128 static xQueueHandle xSerialTransmitQueue = NULL;\r
129 static xQueueHandle xSerialReceiveQueue = NULL;\r
130 static volatile portBASE_TYPE xTransmitStatus = 0UL;\r
131 \r
132 /*-----------------------------------------------------------*/\r
133 \r
134 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
135 {\r
136 unsigned long ulReloadValue = 0UL;\r
137 \r
138         ulReloadValue = ( configPERIPHERAL_CLOCK_HZ / ( 48UL * ulWantedBaud ) ) - 1UL;\r
139 \r
140         if( NULL == xSerialTransmitQueue )\r
141         {\r
142                 xSerialTransmitQueue = xQueueCreate( uxQueueLength, sizeof( char ) );\r
143                 xSerialReceiveQueue = xQueueCreate( uxQueueLength, sizeof( char ) );\r
144         }\r
145 \r
146         /* Enable ASC0 Module. */\r
147         unlock_wdtcon();\r
148         {\r
149                 while ( 0 != ( WDT_CON0.reg & 0x1UL ) );\r
150                 ASC0_CLC.reg = 0x0200UL;\r
151         }\r
152         lock_wdtcon();\r
153 \r
154         /* Disable the Operation. */\r
155         ASC0_CON.reg &= 0xFFFF7FFF;\r
156 \r
157         /* Set-up the GPIO Ports. */\r
158         P3_IOCR0.reg = 0x00009000;      /* 3.0 ASC In, 3.1 Alt ASC Out */\r
159 \r
160         /* Write the baud rate. */\r
161         ASC0_BG.reg = ulReloadValue;\r
162 \r
163         /* Reconfigure and re-initialise the Operation. */\r
164         ASC0_PISEL.reg = 0UL;\r
165         ASC0_CON.reg = 0UL;\r
166         ASC0_CON.bits.M = 0x01; /* 8bit async. */\r
167         ASC0_CON.bits.REN = 0x01; /* Receiver enabled. */\r
168         ASC0_CON.bits.FDE = 0x01; /* Fractional divider enabled. */\r
169         ASC0_CON.bits.BRS = 0x01; /* Divide by three. */\r
170         ASC0_CON.bits.LB = 0x01; /* Loopback enabled. */\r
171         ASC0_CON.bits.R = 0x01; /* Enable the baud rate generator. */\r
172 \r
173         /* Install the Tx interrupt. */\r
174         if( 0 != _install_int_handler( configINTERRUPT_PRIORITY_TX, prvTxBufferInterruptHandler, 0 ) )\r
175         {\r
176                 ASC0_TBSRC.reg = configINTERRUPT_PRIORITY_TX | 0x5000UL;\r
177                 xTransmitStatus = 0UL;\r
178         }\r
179 \r
180         /* Install the Rx interrupt. */\r
181         if( 0 != _install_int_handler( configINTERRUPT_PRIORITY_RX, prvRxInterruptHandler, 0 ) )\r
182         {\r
183                 ASC0_RSRC.reg = configINTERRUPT_PRIORITY_RX | 0x5000UL;\r
184         }\r
185 \r
186         /* COM Handle is never used by demo code. */\r
187         return (xComPortHandle) pdPASS;\r
188 }\r
189 /*---------------------------------------------------------------------------*/\r
190 \r
191 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
192 {\r
193 unsigned short usChar;\r
194 \r
195         for( usChar = 0; usChar < usStringLength; usChar++ )\r
196         {\r
197                 xSerialPutChar( pxPort, pcString[ usChar ], portMAX_DELAY );\r
198         }\r
199 }\r
200 /*---------------------------------------------------------------------------*/\r
201 \r
202 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
203 {\r
204         /* Just to remove compiler warnings about unused parameters. */\r
205         ( void )pxPort;\r
206 \r
207         return xQueueReceive( xSerialReceiveQueue, pcRxedChar, xBlockTime );\r
208 }\r
209 /*---------------------------------------------------------------------------*/\r
210 \r
211 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
212 {\r
213 portBASE_TYPE xReturn = pdPASS;\r
214 \r
215         /* Just to remove compiler warnings about unused parameters. */\r
216         ( void )pxPort;\r
217 \r
218         /* Send the character to the interrupt handler. */\r
219         xReturn = xQueueSend( xSerialTransmitQueue, &cOutChar, xBlockTime );\r
220 \r
221         /* Start the transmission of bytes if necessary. */\r
222         prvCheckTransmit();\r
223 \r
224         return xReturn;\r
225 }\r
226 /*---------------------------------------------------------------------------*/\r
227 \r
228 static void prvTxBufferInterruptHandler( int iArg )\r
229 {\r
230 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
231 unsigned char ucTx;\r
232 \r
233         /* Just to remove compiler warnings about unused parameters. */\r
234         ( void ) iArg;\r
235 \r
236         /* ACK. */\r
237         ASC0_TBSRC.reg |= 0x4000UL;\r
238         xTransmitStatus = 1UL;\r
239 \r
240         /* TBUF Can be refilled. */\r
241         if( pdPASS == xQueueReceiveFromISR( xSerialTransmitQueue, &ucTx, &xHigherPriorityTaskWoken ) )\r
242         {\r
243                 ASC0_TBUF.reg = ucTx;\r
244         }\r
245         else\r
246         {\r
247                 /* Failed to get a character out of the Queue. No longer busy. */\r
248                 xTransmitStatus = 0UL;\r
249         }\r
250 \r
251         /* Finally end ISR and switch Task if necessary. */\r
252         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
253 }\r
254 /*---------------------------------------------------------------------------*/\r
255 \r
256 static void prvRxInterruptHandler( int iArg )\r
257 {\r
258 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
259 unsigned char ucRx;\r
260 \r
261         /* Just to remove compiler warnings about unused parameters. */\r
262         ( void ) iArg;\r
263 \r
264         /* Grab the character as early as possible. */\r
265         ucRx = ( unsigned char ) ASC0_RBUF.reg;\r
266 \r
267         /* ACK. */\r
268         ASC0_RSRC.reg |= 0x4000UL;\r
269 \r
270         /* Frame available in RBUF. */\r
271         if( pdPASS != xQueueSendFromISR( xSerialReceiveQueue, &ucRx, &xHigherPriorityTaskWoken ) )\r
272         {\r
273                 /* Error handling code can go here. */\r
274         }\r
275 \r
276         /* Finally end ISR and switch Task if necessary. */\r
277         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
278 }\r
279 /*---------------------------------------------------------------------------*/\r
280 \r
281 void prvCheckTransmit( void )\r
282 {\r
283         /* Check to see if the interrupt handler is working its way through the\r
284         buffer. */\r
285         if( 0 == xTransmitStatus )\r
286         {\r
287                 /* Not currently operational so kick off the first byte. */\r
288                 ASC0_TBSRC.reg |= 0x8000UL;\r
289         }\r
290 }\r
291 /*---------------------------------------------------------------------------*/\r