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