]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/TriCore_TC1782_TriBoard_GCC/RTOSDemo/serial.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / TriCore_TC1782_TriBoard_GCC / RTOSDemo / serial.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - 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     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     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /* Hardware specific includes. */\r
97 #include <tc1782.h>\r
98 #include <machine/intrinsics.h>\r
99 #include <machine/cint.h>\r
100 #include <machine/wdtcon.h>\r
101 \r
102 /* Scheduler Includes. */\r
103 #include "FreeRTOS.h"\r
104 #include "task.h"\r
105 #include "queue.h"\r
106 \r
107 /* Demo Includes. */\r
108 #include "serial.h"\r
109 \r
110 /*****************************************************************************\r
111  * Please note!\r
112  *\r
113  * This file is here to provide a means of generating interrupts to test the\r
114  * FreeRTOS tricore port.  First - it configures the UART into loopback mode,\r
115  * and has only been used in loopback mode.  Therefore, the baud rate\r
116  * calculation used has never been verified for correctness.  Second -\r
117  * characters are passed into and out of the interrupt service routines using\r
118  * character queues.  This provides a good test of the interrupt interaction,\r
119  * context switching mechanism, and kernel loading, it is however highly\r
120  * inefficient if the UART is being used to handle even moderate amounts of\r
121  * data throughput.\r
122  */\r
123 \r
124 \r
125 /*---------------------------------------------------------------------------*/\r
126 \r
127 /*\r
128  * See if the Serial Transmit Interrupt is currently activated, meaning that\r
129  * the interrupt is working through the back log of bytes that it needs to\r
130  * send. If the ISR is not enabled, then it will be triggered to send the first\r
131  * byte, and it will be automatically re-triggered when that byte has been\r
132  * sent. When the queue is exhausted, the ISR disables itself.\r
133  */\r
134 static void prvCheckTransmit( void );\r
135 \r
136 /*\r
137  * The transmit and receive interrupt handlers.\r
138  */\r
139 static void prvTxBufferInterruptHandler( int iArg ) __attribute__( ( longcall ) );\r
140 static void prvRxInterruptHandler( int iArg )__attribute__( ( longcall ) );\r
141 \r
142 /*-----------------------------------------------------------*/\r
143 \r
144 /* Queues used to pass bytes into and out of the interrupt handlers.\r
145 NOTE:  This is not intended to be an example of an efficient interrupt handler,\r
146 but instead to load the kernel and interrupt mechanisms in order to test the\r
147 FreeRTOS port.  Using a FIFO, DMA, circular buffer, etc. architecture will\r
148 to improve efficiency. */\r
149 static QueueHandle_t xSerialTransmitQueue = NULL;\r
150 static QueueHandle_t xSerialReceiveQueue = NULL;\r
151 static volatile portBASE_TYPE xTransmitStatus = 0UL;\r
152 \r
153 /*-----------------------------------------------------------*/\r
154 \r
155 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
156 {\r
157 unsigned long ulReloadValue = 0UL;\r
158 \r
159         ulReloadValue = ( configPERIPHERAL_CLOCK_HZ / ( 48UL * ulWantedBaud ) ) - 1UL;\r
160 \r
161         if( NULL == xSerialTransmitQueue )\r
162         {\r
163                 xSerialTransmitQueue = xQueueCreate( uxQueueLength, sizeof( char ) );\r
164                 xSerialReceiveQueue = xQueueCreate( uxQueueLength, sizeof( char ) );\r
165         }\r
166 \r
167         /* Enable ASC0 Module. */\r
168         unlock_wdtcon();\r
169         {\r
170                 while ( 0 != ( WDT_CON0.reg & 0x1UL ) );\r
171                 ASC0_CLC.reg = 0x0200UL;\r
172         }\r
173         lock_wdtcon();\r
174 \r
175         /* Disable the Operation. */\r
176         ASC0_CON.reg &= 0xFFFF7FFF;\r
177 \r
178         /* Set-up the GPIO Ports. */\r
179         P3_IOCR0.reg = 0x00009000;      /* 3.0 ASC In, 3.1 Alt ASC Out */\r
180 \r
181         /* Write the baud rate. */\r
182         ASC0_BG.reg = ulReloadValue;\r
183 \r
184         /* Reconfigure and re-initialise the Operation. */\r
185         ASC0_PISEL.reg = 0UL;\r
186         ASC0_CON.reg = 0UL;\r
187         ASC0_CON.bits.M = 0x01; /* 8bit async. */\r
188         ASC0_CON.bits.REN = 0x01; /* Receiver enabled. */\r
189         ASC0_CON.bits.FDE = 0x01; /* Fractional divider enabled. */\r
190         ASC0_CON.bits.BRS = 0x01; /* Divide by three. */\r
191         ASC0_CON.bits.LB = 0x01; /* Loopback enabled. */\r
192         ASC0_CON.bits.R = 0x01; /* Enable the baud rate generator. */\r
193 \r
194         /* Install the Tx interrupt. */\r
195         if( 0 != _install_int_handler( configINTERRUPT_PRIORITY_TX, prvTxBufferInterruptHandler, 0 ) )\r
196         {\r
197                 ASC0_TBSRC.reg = configINTERRUPT_PRIORITY_TX | 0x5000UL;\r
198                 xTransmitStatus = 0UL;\r
199         }\r
200 \r
201         /* Install the Rx interrupt. */\r
202         if( 0 != _install_int_handler( configINTERRUPT_PRIORITY_RX, prvRxInterruptHandler, 0 ) )\r
203         {\r
204                 ASC0_RSRC.reg = configINTERRUPT_PRIORITY_RX | 0x5000UL;\r
205         }\r
206 \r
207         /* COM Handle is never used by demo code. */\r
208         return (xComPortHandle) pdPASS;\r
209 }\r
210 /*---------------------------------------------------------------------------*/\r
211 \r
212 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
213 {\r
214 unsigned short usChar;\r
215 \r
216         for( usChar = 0; usChar < usStringLength; usChar++ )\r
217         {\r
218                 xSerialPutChar( pxPort, pcString[ usChar ], portMAX_DELAY );\r
219         }\r
220 }\r
221 /*---------------------------------------------------------------------------*/\r
222 \r
223 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
224 {\r
225         /* Just to remove compiler warnings about unused parameters. */\r
226         ( void )pxPort;\r
227 \r
228         return xQueueReceive( xSerialReceiveQueue, pcRxedChar, xBlockTime );\r
229 }\r
230 /*---------------------------------------------------------------------------*/\r
231 \r
232 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
233 {\r
234 portBASE_TYPE xReturn = pdPASS;\r
235 \r
236         /* Just to remove compiler warnings about unused parameters. */\r
237         ( void )pxPort;\r
238 \r
239         /* Send the character to the interrupt handler. */\r
240         xReturn = xQueueSend( xSerialTransmitQueue, &cOutChar, xBlockTime );\r
241 \r
242         /* Start the transmission of bytes if necessary. */\r
243         prvCheckTransmit();\r
244 \r
245         return xReturn;\r
246 }\r
247 /*---------------------------------------------------------------------------*/\r
248 \r
249 static void prvTxBufferInterruptHandler( int iArg )\r
250 {\r
251 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
252 unsigned char ucTx;\r
253 \r
254         /* Just to remove compiler warnings about unused parameters. */\r
255         ( void ) iArg;\r
256 \r
257         /* ACK. */\r
258         ASC0_TBSRC.reg |= 0x4000UL;\r
259         xTransmitStatus = 1UL;\r
260 \r
261         /* TBUF Can be refilled. */\r
262         if( pdPASS == xQueueReceiveFromISR( xSerialTransmitQueue, &ucTx, &xHigherPriorityTaskWoken ) )\r
263         {\r
264                 ASC0_TBUF.reg = ucTx;\r
265         }\r
266         else\r
267         {\r
268                 /* Failed to get a character out of the Queue. No longer busy. */\r
269                 xTransmitStatus = 0UL;\r
270         }\r
271 \r
272         /* Finally end ISR and switch Task if necessary. */\r
273         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
274 }\r
275 /*---------------------------------------------------------------------------*/\r
276 \r
277 static void prvRxInterruptHandler( int iArg )\r
278 {\r
279 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
280 unsigned char ucRx;\r
281 \r
282         /* Just to remove compiler warnings about unused parameters. */\r
283         ( void ) iArg;\r
284 \r
285         /* Grab the character as early as possible. */\r
286         ucRx = ( unsigned char ) ASC0_RBUF.reg;\r
287 \r
288         /* ACK. */\r
289         ASC0_RSRC.reg |= 0x4000UL;\r
290 \r
291         /* Frame available in RBUF. */\r
292         if( pdPASS != xQueueSendFromISR( xSerialReceiveQueue, &ucRx, &xHigherPriorityTaskWoken ) )\r
293         {\r
294                 /* Error handling code can go here. */\r
295         }\r
296 \r
297         /* Finally end ISR and switch Task if necessary. */\r
298         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
299 }\r
300 /*---------------------------------------------------------------------------*/\r
301 \r
302 void prvCheckTransmit( void )\r
303 {\r
304         /* Check to see if the interrupt handler is working its way through the\r
305         buffer. */\r
306         if( 0 == xTransmitStatus )\r
307         {\r
308                 /* Not currently operational so kick off the first byte. */\r
309                 ASC0_TBSRC.reg |= 0x8000UL;\r
310         }\r
311 }\r
312 /*---------------------------------------------------------------------------*/\r