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