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