]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/TriCore_TC1782_TriBoard_GCC/RTOSDemo/serial.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / TriCore_TC1782_TriBoard_GCC / RTOSDemo / serial.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /* Hardware specific includes. */\r
68 #include <tc1782.h>\r
69 #include <machine/intrinsics.h>\r
70 #include <machine/cint.h>\r
71 #include <machine/wdtcon.h>\r
72 \r
73 /* Scheduler Includes. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 #include "queue.h"\r
77 \r
78 /* Demo Includes. */\r
79 #include "serial.h"\r
80 \r
81 /*****************************************************************************\r
82  * Please note!\r
83  *\r
84  * This file is here to provide a means of generating interrupts to test the\r
85  * FreeRTOS tricore port.  First - it configures the UART into loopback mode,\r
86  * and has only been used in loopback mode.  Therefore, the baud rate\r
87  * calculation used has never been verified for correctness.  Second -\r
88  * characters are passed into and out of the interrupt service routines using\r
89  * character queues.  This provides a good test of the interrupt interaction,\r
90  * context switching mechanism, and kernel loading, it is however highly\r
91  * inefficient if the UART is being used to handle even moderate amounts of\r
92  * data throughput.\r
93  */\r
94 \r
95 \r
96 /*---------------------------------------------------------------------------*/\r
97 \r
98 /*\r
99  * See if the Serial Transmit Interrupt is currently activated, meaning that\r
100  * the interrupt is working through the back log of bytes that it needs to\r
101  * send. If the ISR is not enabled, then it will be triggered to send the first\r
102  * byte, and it will be automatically re-triggered when that byte has been\r
103  * sent. When the queue is exhausted, the ISR disables itself.\r
104  */\r
105 static void prvCheckTransmit( void );\r
106 \r
107 /*\r
108  * The transmit and receive interrupt handlers.\r
109  */\r
110 static void prvTxBufferInterruptHandler( int iArg ) __attribute__( ( longcall ) );\r
111 static void prvRxInterruptHandler( int iArg )__attribute__( ( longcall ) );\r
112 \r
113 /*-----------------------------------------------------------*/\r
114 \r
115 /* Queues used to pass bytes into and out of the interrupt handlers.\r
116 NOTE:  This is not intended to be an example of an efficient interrupt handler,\r
117 but instead to load the kernel and interrupt mechanisms in order to test the\r
118 FreeRTOS port.  Using a FIFO, DMA, circular buffer, etc. architecture will\r
119 to improve efficiency. */\r
120 static xQueueHandle xSerialTransmitQueue = NULL;\r
121 static xQueueHandle xSerialReceiveQueue = NULL;\r
122 static volatile portBASE_TYPE xTransmitStatus = 0UL;\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
127 {\r
128 unsigned long ulReloadValue = 0UL;\r
129 \r
130         ulReloadValue = ( configPERIPHERAL_CLOCK_HZ / ( 48UL * ulWantedBaud ) ) - 1UL;\r
131 \r
132         if( NULL == xSerialTransmitQueue )\r
133         {\r
134                 xSerialTransmitQueue = xQueueCreate( uxQueueLength, sizeof( char ) );\r
135                 xSerialReceiveQueue = xQueueCreate( uxQueueLength, sizeof( char ) );\r
136         }\r
137 \r
138         /* Enable ASC0 Module. */\r
139         unlock_wdtcon();\r
140         {\r
141                 while ( 0 != ( WDT_CON0.reg & 0x1UL ) );\r
142                 ASC0_CLC.reg = 0x0200UL;\r
143         }\r
144         lock_wdtcon();\r
145 \r
146         /* Disable the Operation. */\r
147         ASC0_CON.reg &= 0xFFFF7FFF;\r
148 \r
149         /* Set-up the GPIO Ports. */\r
150         P3_IOCR0.reg = 0x00009000;      /* 3.0 ASC In, 3.1 Alt ASC Out */\r
151 \r
152         /* Write the baud rate. */\r
153         ASC0_BG.reg = ulReloadValue;\r
154 \r
155         /* Reconfigure and re-initialise the Operation. */\r
156         ASC0_PISEL.reg = 0UL;\r
157         ASC0_CON.reg = 0UL;\r
158         ASC0_CON.bits.M = 0x01; /* 8bit async. */\r
159         ASC0_CON.bits.REN = 0x01; /* Receiver enabled. */\r
160         ASC0_CON.bits.FDE = 0x01; /* Fractional divider enabled. */\r
161         ASC0_CON.bits.BRS = 0x01; /* Divide by three. */\r
162         ASC0_CON.bits.LB = 0x01; /* Loopback enabled. */\r
163         ASC0_CON.bits.R = 0x01; /* Enable the baud rate generator. */\r
164 \r
165         /* Install the Tx interrupt. */\r
166         if( 0 != _install_int_handler( configINTERRUPT_PRIORITY_TX, prvTxBufferInterruptHandler, 0 ) )\r
167         {\r
168                 ASC0_TBSRC.reg = configINTERRUPT_PRIORITY_TX | 0x5000UL;\r
169                 xTransmitStatus = 0UL;\r
170         }\r
171 \r
172         /* Install the Rx interrupt. */\r
173         if( 0 != _install_int_handler( configINTERRUPT_PRIORITY_RX, prvRxInterruptHandler, 0 ) )\r
174         {\r
175                 ASC0_RSRC.reg = configINTERRUPT_PRIORITY_RX | 0x5000UL;\r
176         }\r
177 \r
178         /* COM Handle is never used by demo code. */\r
179         return (xComPortHandle) pdPASS;\r
180 }\r
181 /*---------------------------------------------------------------------------*/\r
182 \r
183 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
184 {\r
185 unsigned short usChar;\r
186 \r
187         for( usChar = 0; usChar < usStringLength; usChar++ )\r
188         {\r
189                 xSerialPutChar( pxPort, pcString[ usChar ], portMAX_DELAY );\r
190         }\r
191 }\r
192 /*---------------------------------------------------------------------------*/\r
193 \r
194 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
195 {\r
196         /* Just to remove compiler warnings about unused parameters. */\r
197         ( void )pxPort;\r
198 \r
199         return xQueueReceive( xSerialReceiveQueue, pcRxedChar, xBlockTime );\r
200 }\r
201 /*---------------------------------------------------------------------------*/\r
202 \r
203 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
204 {\r
205 portBASE_TYPE xReturn = pdPASS;\r
206 \r
207         /* Just to remove compiler warnings about unused parameters. */\r
208         ( void )pxPort;\r
209 \r
210         /* Send the character to the interrupt handler. */\r
211         xReturn = xQueueSend( xSerialTransmitQueue, &cOutChar, xBlockTime );\r
212 \r
213         /* Start the transmission of bytes if necessary. */\r
214         prvCheckTransmit();\r
215 \r
216         return xReturn;\r
217 }\r
218 /*---------------------------------------------------------------------------*/\r
219 \r
220 static void prvTxBufferInterruptHandler( int iArg )\r
221 {\r
222 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
223 unsigned char ucTx;\r
224 \r
225         /* Just to remove compiler warnings about unused parameters. */\r
226         ( void ) iArg;\r
227 \r
228         /* ACK. */\r
229         ASC0_TBSRC.reg |= 0x4000UL;\r
230         xTransmitStatus = 1UL;\r
231 \r
232         /* TBUF Can be refilled. */\r
233         if( pdPASS == xQueueReceiveFromISR( xSerialTransmitQueue, &ucTx, &xHigherPriorityTaskWoken ) )\r
234         {\r
235                 ASC0_TBUF.reg = ucTx;\r
236         }\r
237         else\r
238         {\r
239                 /* Failed to get a character out of the Queue. No longer busy. */\r
240                 xTransmitStatus = 0UL;\r
241         }\r
242 \r
243         /* Finally end ISR and switch Task if necessary. */\r
244         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
245 }\r
246 /*---------------------------------------------------------------------------*/\r
247 \r
248 static void prvRxInterruptHandler( int iArg )\r
249 {\r
250 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
251 unsigned char ucRx;\r
252 \r
253         /* Just to remove compiler warnings about unused parameters. */\r
254         ( void ) iArg;\r
255 \r
256         /* Grab the character as early as possible. */\r
257         ucRx = ( unsigned char ) ASC0_RBUF.reg;\r
258 \r
259         /* ACK. */\r
260         ASC0_RSRC.reg |= 0x4000UL;\r
261 \r
262         /* Frame available in RBUF. */\r
263         if( pdPASS != xQueueSendFromISR( xSerialReceiveQueue, &ucRx, &xHigherPriorityTaskWoken ) )\r
264         {\r
265                 /* Error handling code can go here. */\r
266         }\r
267 \r
268         /* Finally end ISR and switch Task if necessary. */\r
269         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
270 }\r
271 /*---------------------------------------------------------------------------*/\r
272 \r
273 void prvCheckTransmit( void )\r
274 {\r
275         /* Check to see if the interrupt handler is working its way through the\r
276         buffer. */\r
277         if( 0 == xTransmitStatus )\r
278         {\r
279                 /* Not currently operational so kick off the first byte. */\r
280                 ASC0_TBSRC.reg |= 0x8000UL;\r
281         }\r
282 }\r
283 /*---------------------------------------------------------------------------*/\r