]> git.sur5r.net Git - freertos/blob - Demo/CORTUS_APS3_GCC/Demo/serial.c
New MicroBlaze port: Added a FreeRTOS exception handler, and installed it in each...
[freertos] / Demo / CORTUS_APS3_GCC / Demo / serial.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*  Basic interrupt driven serial port driver for uart1.\r
55 */\r
56 \r
57 /* Standard includes. */\r
58 #include <stdlib.h>\r
59 \r
60 /* Kernel includes. */\r
61 #include "FreeRTOS.h"\r
62 #include "queue.h"\r
63 #include "task.h"\r
64 \r
65 /* Demo application includes. */\r
66 #include <machine/uart.h>\r
67 #include <machine/ic.h>\r
68 #include "serial.h"\r
69 /*-----------------------------------------------------------*/\r
70 \r
71 #define comBLOCK_RETRY_TIME                             10\r
72 /*-----------------------------------------------------------*/\r
73 \r
74 /* The interrupt handlers are naked functions that call C handlers.  The C\r
75 handlers are marked as noinline to ensure they work correctly when the\r
76 optimiser is on. */\r
77 void interrupt5_handler( void ) __attribute__((naked));\r
78 static void prvTxHandler( void ) __attribute__((noinline));\r
79 void interrupt6_handler( void ) __attribute__((naked));\r
80 static void prvRxHandler( void ) __attribute__((noinline));\r
81 \r
82 /*-----------------------------------------------------------*/\r
83 \r
84 /* Queues used to hold received characters, and characters waiting to be\r
85 transmitted. */\r
86 static xQueueHandle xRxedChars;\r
87 static xQueueHandle xCharsForTx;\r
88 extern unsigned portBASE_TYPE *pxVectorTable;\r
89 /*-----------------------------------------------------------*/\r
90 \r
91 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
92 {\r
93         /* Create the queues used to hold Rx and Tx characters. */\r
94         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
95         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
96 \r
97         if( ( xRxedChars ) && ( xCharsForTx ) )\r
98         {\r
99                 /* Set up interrupts */\r
100                 /* tx interrupt will be enabled when we need to send something */\r
101                 uart1->tx_mask = 0;\r
102                 uart1->rx_mask = 1;\r
103                 irq[IRQ_UART1_TX].ien = 1;\r
104                 irq[IRQ_UART1_TX].ipl = portSYSTEM_INTERRUPT_PRIORITY_LEVEL;\r
105                 irq[IRQ_UART1_RX].ien = 1;\r
106                 irq[IRQ_UART1_RX].ipl = portSYSTEM_INTERRUPT_PRIORITY_LEVEL;\r
107         }\r
108 \r
109         return ( xComPortHandle ) 0;\r
110 }\r
111 /*-----------------------------------------------------------*/\r
112 \r
113 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
114 {\r
115         /* The port handle is not required as this driver only supports uart1. */\r
116         (void) pxPort;\r
117 \r
118         /* Get the next character from the buffer.      Return false if no characters\r
119            are available, or arrive before xBlockTime expires. */\r
120         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
121         {\r
122                 return pdTRUE;\r
123         }\r
124         else\r
125         {\r
126                 return pdFALSE;\r
127         }\r
128 }\r
129 /*-----------------------------------------------------------*/\r
130 \r
131 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
132 {\r
133         int i;\r
134         signed char *pChNext;\r
135 \r
136         /* Send each character in the string, one at a time. */\r
137         pChNext = ( signed char * )pcString;\r
138         for( i = 0; i < usStringLength; i++ )\r
139         {\r
140                 /* Block until character has been transmitted. */\r
141                 while( xSerialPutChar( pxPort, *pChNext, comBLOCK_RETRY_TIME ) != pdTRUE ); pChNext++;\r
142         }\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
147 {\r
148         ( void ) pxPort;\r
149 \r
150         /* Place the character in the queue of characters to be transmitted. */\r
151         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
152         {\r
153                 return pdFAIL;\r
154         }\r
155 \r
156         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
157            queue and send it. This does not need to be in a critical section as\r
158            if the interrupt has already removed the character the next interrupt\r
159            will simply turn off the Tx interrupt again. */\r
160         uart1->tx_mask = 1;\r
161 \r
162         return pdPASS;\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 void vSerialClose( xComPortHandle xPort )\r
167 {\r
168         /* Not supported as not required by the demo application. */\r
169         ( void ) xPort;\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 /* UART Tx interrupt handler. */\r
174 void interrupt5_handler( void )\r
175 {\r
176         /* This is a naked function. */\r
177         portSAVE_CONTEXT();\r
178         prvTxHandler();\r
179         portRESTORE_CONTEXT();\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 static void prvTxHandler( void )\r
184 {\r
185 signed char cChar;\r
186 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
187 \r
188         /* The interrupt was caused by the transmit fifo having space for at least one\r
189         character. Are there any more characters to transmit? */\r
190         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
191         {\r
192                 /* A character was retrieved from the queue so can be sent to the uart now. */\r
193                 uart1->tx_data = cChar;\r
194         }\r
195         else\r
196         {\r
197                 /* Queue empty, nothing to send so turn off the Tx interrupt. */\r
198                 uart1->tx_mask = 0;\r
199         }\r
200 \r
201         /* If an event caused a task to unblock then we call "Yield from ISR" to\r
202         ensure that the unblocked task is the task that executes when the interrupt\r
203         completes if the unblocked task has a priority higher than the interrupted\r
204         task. */\r
205         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 /* UART Rx interrupt. */\r
210 void interrupt6_handler( void )\r
211 {\r
212         portSAVE_CONTEXT();\r
213         prvRxHandler();\r
214         portRESTORE_CONTEXT();\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 static void prvRxHandler( void )\r
219 {\r
220 signed char cChar;\r
221 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
222 \r
223         /* The interrupt was caused by the receiver getting data. */\r
224         cChar = uart1->rx_data;\r
225 \r
226         xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
227 \r
228         /* If an event caused a task to unblock then we call "Yield from ISR" to\r
229         ensure that the unblocked task is the task that executes when the interrupt\r
230         completes if the unblocked task has a priority higher than the interrupted\r
231         task. */\r
232         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 \r