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