]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTUS_APS3_GCC/Demo/serial.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / CORTUS_APS3_GCC / Demo / serial.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /*  Basic interrupt driven serial port driver for uart1.\r
68 */\r
69 \r
70 /* Standard includes. */\r
71 #include <stdlib.h>\r
72 \r
73 /* Kernel includes. */\r
74 #include "FreeRTOS.h"\r
75 #include "queue.h"\r
76 #include "task.h"\r
77 \r
78 /* Demo application includes. */\r
79 #include <machine/uart.h>\r
80 #include <machine/ic.h>\r
81 #include "serial.h"\r
82 /*-----------------------------------------------------------*/\r
83 \r
84 #define comBLOCK_RETRY_TIME                             10\r
85 /*-----------------------------------------------------------*/\r
86 \r
87 /* The interrupt handlers are naked functions that call C handlers.  The C\r
88 handlers are marked as noinline to ensure they work correctly when the\r
89 optimiser is on. */\r
90 void interrupt5_handler( void ) __attribute__((naked));\r
91 static void prvTxHandler( void ) __attribute__((noinline));\r
92 void interrupt6_handler( void ) __attribute__((naked));\r
93 static void prvRxHandler( void ) __attribute__((noinline));\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 /* Queues used to hold received characters, and characters waiting to be\r
98 transmitted. */\r
99 static xQueueHandle xRxedChars;\r
100 static xQueueHandle xCharsForTx;\r
101 extern unsigned portBASE_TYPE *pxVectorTable;\r
102 /*-----------------------------------------------------------*/\r
103 \r
104 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
105 {\r
106         /* Create the queues used to hold Rx and Tx characters. */\r
107         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
108         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
109 \r
110         if( ( xRxedChars ) && ( xCharsForTx ) )\r
111         {\r
112                 /* Set up interrupts */\r
113                 /* tx interrupt will be enabled when we need to send something */\r
114                 uart1->tx_mask = 0;\r
115                 uart1->rx_mask = 1;\r
116                 irq[IRQ_UART1_TX].ien = 1;\r
117                 irq[IRQ_UART1_TX].ipl = portSYSTEM_INTERRUPT_PRIORITY_LEVEL;\r
118                 irq[IRQ_UART1_RX].ien = 1;\r
119                 irq[IRQ_UART1_RX].ipl = portSYSTEM_INTERRUPT_PRIORITY_LEVEL;\r
120         }\r
121 \r
122         return ( xComPortHandle ) 0;\r
123 }\r
124 /*-----------------------------------------------------------*/\r
125 \r
126 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
127 {\r
128         /* The port handle is not required as this driver only supports uart1. */\r
129         (void) pxPort;\r
130 \r
131         /* Get the next character from the buffer.      Return false if no characters\r
132            are available, or arrive before xBlockTime expires. */\r
133         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
134         {\r
135                 return pdTRUE;\r
136         }\r
137         else\r
138         {\r
139                 return pdFALSE;\r
140         }\r
141 }\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
145 {\r
146         int i;\r
147         signed char *pChNext;\r
148 \r
149         /* Send each character in the string, one at a time. */\r
150         pChNext = ( signed char * )pcString;\r
151         for( i = 0; i < usStringLength; i++ )\r
152         {\r
153                 /* Block until character has been transmitted. */\r
154                 while( xSerialPutChar( pxPort, *pChNext, comBLOCK_RETRY_TIME ) != pdTRUE ); pChNext++;\r
155         }\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
160 {\r
161         ( void ) pxPort;\r
162 \r
163         /* Place the character in the queue of characters to be transmitted. */\r
164         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
165         {\r
166                 return pdFAIL;\r
167         }\r
168 \r
169         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
170            queue and send it. This does not need to be in a critical section as\r
171            if the interrupt has already removed the character the next interrupt\r
172            will simply turn off the Tx interrupt again. */\r
173         uart1->tx_mask = 1;\r
174 \r
175         return pdPASS;\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 void vSerialClose( xComPortHandle xPort )\r
180 {\r
181         /* Not supported as not required by the demo application. */\r
182         ( void ) xPort;\r
183 }\r
184 /*-----------------------------------------------------------*/\r
185 \r
186 /* UART Tx interrupt handler. */\r
187 void interrupt5_handler( void )\r
188 {\r
189         /* This is a naked function. */\r
190         portSAVE_CONTEXT();\r
191         prvTxHandler();\r
192         portRESTORE_CONTEXT();\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static void prvTxHandler( void )\r
197 {\r
198 signed char cChar;\r
199 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
200 \r
201         /* The interrupt was caused by the transmit fifo having space for at least one\r
202         character. Are there any more characters to transmit? */\r
203         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
204         {\r
205                 /* A character was retrieved from the queue so can be sent to the uart now. */\r
206                 uart1->tx_data = cChar;\r
207         }\r
208         else\r
209         {\r
210                 /* Queue empty, nothing to send so turn off the Tx interrupt. */\r
211                 uart1->tx_mask = 0;\r
212         }\r
213 \r
214         /* If an event caused a task to unblock then we call "Yield from ISR" to\r
215         ensure that the unblocked task is the task that executes when the interrupt\r
216         completes if the unblocked task has a priority higher than the interrupted\r
217         task. */\r
218         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 /* UART Rx interrupt. */\r
223 void interrupt6_handler( void )\r
224 {\r
225         portSAVE_CONTEXT();\r
226         prvRxHandler();\r
227         portRESTORE_CONTEXT();\r
228 }\r
229 /*-----------------------------------------------------------*/\r
230 \r
231 static void prvRxHandler( void )\r
232 {\r
233 signed char cChar;\r
234 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
235 \r
236         /* The interrupt was caused by the receiver getting data. */\r
237         cChar = uart1->rx_data;\r
238 \r
239         xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
240 \r
241         /* If an event caused a task to unblock then we call "Yield from ISR" to\r
242         ensure that the unblocked task is the task that executes when the interrupt\r
243         completes if the unblocked task has a priority higher than the interrupted\r
244         task. */\r
245         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 \r