]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/TriCore_TC1782_TriBoard_GCC/RTOSDemo/serial.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / TriCore_TC1782_TriBoard_GCC / RTOSDemo / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* Hardware specific includes. */\r
30 #include <tc1782.h>\r
31 #include <machine/intrinsics.h>\r
32 #include <machine/cint.h>\r
33 #include <machine/wdtcon.h>\r
34 \r
35 /* Scheduler Includes. */\r
36 #include "FreeRTOS.h"\r
37 #include "task.h"\r
38 #include "queue.h"\r
39 \r
40 /* Demo Includes. */\r
41 #include "serial.h"\r
42 \r
43 /*****************************************************************************\r
44  * Please note!\r
45  *\r
46  * This file is here to provide a means of generating interrupts to test the\r
47  * FreeRTOS tricore port.  First - it configures the UART into loopback mode,\r
48  * and has only been used in loopback mode.  Therefore, the baud rate\r
49  * calculation used has never been verified for correctness.  Second -\r
50  * characters are passed into and out of the interrupt service routines using\r
51  * character queues.  This provides a good test of the interrupt interaction,\r
52  * context switching mechanism, and kernel loading, it is however highly\r
53  * inefficient if the UART is being used to handle even moderate amounts of\r
54  * data throughput.\r
55  */\r
56 \r
57 \r
58 /*---------------------------------------------------------------------------*/\r
59 \r
60 /*\r
61  * See if the Serial Transmit Interrupt is currently activated, meaning that\r
62  * the interrupt is working through the back log of bytes that it needs to\r
63  * send. If the ISR is not enabled, then it will be triggered to send the first\r
64  * byte, and it will be automatically re-triggered when that byte has been\r
65  * sent. When the queue is exhausted, the ISR disables itself.\r
66  */\r
67 static void prvCheckTransmit( void );\r
68 \r
69 /*\r
70  * The transmit and receive interrupt handlers.\r
71  */\r
72 static void prvTxBufferInterruptHandler( int iArg ) __attribute__( ( longcall ) );\r
73 static void prvRxInterruptHandler( int iArg )__attribute__( ( longcall ) );\r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77 /* Queues used to pass bytes into and out of the interrupt handlers.\r
78 NOTE:  This is not intended to be an example of an efficient interrupt handler,\r
79 but instead to load the kernel and interrupt mechanisms in order to test the\r
80 FreeRTOS port.  Using a FIFO, DMA, circular buffer, etc. architecture will\r
81 to improve efficiency. */\r
82 static QueueHandle_t xSerialTransmitQueue = NULL;\r
83 static QueueHandle_t xSerialReceiveQueue = NULL;\r
84 static volatile portBASE_TYPE xTransmitStatus = 0UL;\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
89 {\r
90 unsigned long ulReloadValue = 0UL;\r
91 \r
92         ulReloadValue = ( configPERIPHERAL_CLOCK_HZ / ( 48UL * ulWantedBaud ) ) - 1UL;\r
93 \r
94         if( NULL == xSerialTransmitQueue )\r
95         {\r
96                 xSerialTransmitQueue = xQueueCreate( uxQueueLength, sizeof( char ) );\r
97                 xSerialReceiveQueue = xQueueCreate( uxQueueLength, sizeof( char ) );\r
98         }\r
99 \r
100         /* Enable ASC0 Module. */\r
101         unlock_wdtcon();\r
102         {\r
103                 while ( 0 != ( WDT_CON0.reg & 0x1UL ) );\r
104                 ASC0_CLC.reg = 0x0200UL;\r
105         }\r
106         lock_wdtcon();\r
107 \r
108         /* Disable the Operation. */\r
109         ASC0_CON.reg &= 0xFFFF7FFF;\r
110 \r
111         /* Set-up the GPIO Ports. */\r
112         P3_IOCR0.reg = 0x00009000;      /* 3.0 ASC In, 3.1 Alt ASC Out */\r
113 \r
114         /* Write the baud rate. */\r
115         ASC0_BG.reg = ulReloadValue;\r
116 \r
117         /* Reconfigure and re-initialise the Operation. */\r
118         ASC0_PISEL.reg = 0UL;\r
119         ASC0_CON.reg = 0UL;\r
120         ASC0_CON.bits.M = 0x01; /* 8bit async. */\r
121         ASC0_CON.bits.REN = 0x01; /* Receiver enabled. */\r
122         ASC0_CON.bits.FDE = 0x01; /* Fractional divider enabled. */\r
123         ASC0_CON.bits.BRS = 0x01; /* Divide by three. */\r
124         ASC0_CON.bits.LB = 0x01; /* Loopback enabled. */\r
125         ASC0_CON.bits.R = 0x01; /* Enable the baud rate generator. */\r
126 \r
127         /* Install the Tx interrupt. */\r
128         if( 0 != _install_int_handler( configINTERRUPT_PRIORITY_TX, prvTxBufferInterruptHandler, 0 ) )\r
129         {\r
130                 ASC0_TBSRC.reg = configINTERRUPT_PRIORITY_TX | 0x5000UL;\r
131                 xTransmitStatus = 0UL;\r
132         }\r
133 \r
134         /* Install the Rx interrupt. */\r
135         if( 0 != _install_int_handler( configINTERRUPT_PRIORITY_RX, prvRxInterruptHandler, 0 ) )\r
136         {\r
137                 ASC0_RSRC.reg = configINTERRUPT_PRIORITY_RX | 0x5000UL;\r
138         }\r
139 \r
140         /* COM Handle is never used by demo code. */\r
141         return (xComPortHandle) pdPASS;\r
142 }\r
143 /*---------------------------------------------------------------------------*/\r
144 \r
145 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
146 {\r
147 unsigned short usChar;\r
148 \r
149         for( usChar = 0; usChar < usStringLength; usChar++ )\r
150         {\r
151                 xSerialPutChar( pxPort, pcString[ usChar ], portMAX_DELAY );\r
152         }\r
153 }\r
154 /*---------------------------------------------------------------------------*/\r
155 \r
156 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
157 {\r
158         /* Just to remove compiler warnings about unused parameters. */\r
159         ( void )pxPort;\r
160 \r
161         return xQueueReceive( xSerialReceiveQueue, pcRxedChar, xBlockTime );\r
162 }\r
163 /*---------------------------------------------------------------------------*/\r
164 \r
165 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
166 {\r
167 portBASE_TYPE xReturn = pdPASS;\r
168 \r
169         /* Just to remove compiler warnings about unused parameters. */\r
170         ( void )pxPort;\r
171 \r
172         /* Send the character to the interrupt handler. */\r
173         xReturn = xQueueSend( xSerialTransmitQueue, &cOutChar, xBlockTime );\r
174 \r
175         /* Start the transmission of bytes if necessary. */\r
176         prvCheckTransmit();\r
177 \r
178         return xReturn;\r
179 }\r
180 /*---------------------------------------------------------------------------*/\r
181 \r
182 static void prvTxBufferInterruptHandler( int iArg )\r
183 {\r
184 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
185 unsigned char ucTx;\r
186 \r
187         /* Just to remove compiler warnings about unused parameters. */\r
188         ( void ) iArg;\r
189 \r
190         /* ACK. */\r
191         ASC0_TBSRC.reg |= 0x4000UL;\r
192         xTransmitStatus = 1UL;\r
193 \r
194         /* TBUF Can be refilled. */\r
195         if( pdPASS == xQueueReceiveFromISR( xSerialTransmitQueue, &ucTx, &xHigherPriorityTaskWoken ) )\r
196         {\r
197                 ASC0_TBUF.reg = ucTx;\r
198         }\r
199         else\r
200         {\r
201                 /* Failed to get a character out of the Queue. No longer busy. */\r
202                 xTransmitStatus = 0UL;\r
203         }\r
204 \r
205         /* Finally end ISR and switch Task if necessary. */\r
206         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
207 }\r
208 /*---------------------------------------------------------------------------*/\r
209 \r
210 static void prvRxInterruptHandler( int iArg )\r
211 {\r
212 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
213 unsigned char ucRx;\r
214 \r
215         /* Just to remove compiler warnings about unused parameters. */\r
216         ( void ) iArg;\r
217 \r
218         /* Grab the character as early as possible. */\r
219         ucRx = ( unsigned char ) ASC0_RBUF.reg;\r
220 \r
221         /* ACK. */\r
222         ASC0_RSRC.reg |= 0x4000UL;\r
223 \r
224         /* Frame available in RBUF. */\r
225         if( pdPASS != xQueueSendFromISR( xSerialReceiveQueue, &ucRx, &xHigherPriorityTaskWoken ) )\r
226         {\r
227                 /* Error handling code can go here. */\r
228         }\r
229 \r
230         /* Finally end ISR and switch Task if necessary. */\r
231         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
232 }\r
233 /*---------------------------------------------------------------------------*/\r
234 \r
235 void prvCheckTransmit( void )\r
236 {\r
237         /* Check to see if the interrupt handler is working its way through the\r
238         buffer. */\r
239         if( 0 == xTransmitStatus )\r
240         {\r
241                 /* Not currently operational so kick off the first byte. */\r
242                 ASC0_TBSRC.reg |= 0x8000UL;\r
243         }\r
244 }\r
245 /*---------------------------------------------------------------------------*/\r