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