]> git.sur5r.net Git - freertos/blob - Demo/NiosII_CycloneIII_DBC3C40_GCC/RTOSDemo/serial.c
Update Cortex M3 ports to ensure 8 byte alignment.
[freertos] / Demo / NiosII_CycloneIII_DBC3C40_GCC / RTOSDemo / serial.c
1 /*\r
2     FreeRTOS V6.0.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\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 exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     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 /* NOTE:  This is just a test file and not intended to be a generic \r
55 COM driver. */\r
56 \r
57 #include "altera_avalon_uart.h"\r
58 #include "altera_avalon_uart_regs.h"\r
59 #include "sys/alt_irq.h"\r
60 \r
61 #include "FreeRTOS.h"\r
62 #include "queue.h"\r
63 #include "system.h"\r
64 #include "Serial.h"\r
65 /*---------------------------------------------------------------------------*/\r
66 \r
67 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
68 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
69 /*---------------------------------------------------------------------------*/\r
70 \r
71 static xQueueHandle xRxedChars; \r
72 static xQueueHandle xCharsForTx; \r
73 \r
74 alt_u32 uartControl;\r
75 /*---------------------------------------------------------------------------*/\r
76 \r
77 static void vUARTInterruptHandler( void* context, alt_u32 id );\r
78 static void vUARTReceiveHandler( alt_u32 status );\r
79 static void vUARTTransmitHandler( alt_u32 status );\r
80 /*---------------------------------------------------------------------------*/\r
81 \r
82 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
83 {\r
84         /* Create the queues used to hold Rx and Tx characters. */\r
85         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
86         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
87 \r
88         /* If the queues were created correctly then setup the serial port hardware. */\r
89         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
90         {\r
91                 portENTER_CRITICAL();\r
92                 {\r
93                         uartControl = ALTERA_AVALON_UART_CONTROL_RTS_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_DCTS_MSK;\r
94                         IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl ); \r
95                   \r
96                     /* register the interrupt handler */\r
97                         alt_irq_register ( UART_IRQ, NULL, vUARTInterruptHandler );\r
98                 }\r
99                 portEXIT_CRITICAL();\r
100         }\r
101         else\r
102         {\r
103                 return ( xComPortHandle ) 0;\r
104         }\r
105     return ( xComPortHandle ) 1;\r
106 }\r
107 /*---------------------------------------------------------------------------*/\r
108 \r
109 void vSerialClose( xComPortHandle xPort )\r
110 {\r
111     /* Never used. */\r
112 }\r
113 /*---------------------------------------------------------------------------*/\r
114 \r
115 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
116 {\r
117         /* The port handle is not required as this driver only supports one port. */\r
118         ( void ) pxPort;\r
119 \r
120 \r
121         /* Get the next character from the buffer.  Return false if no characters\r
122         are available, or arrive before xBlockTime expires. */\r
123         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
124         {\r
125                 return pdTRUE;\r
126         }\r
127         else\r
128         {\r
129                 uartControl |= ALTERA_AVALON_UART_CONTROL_RRDY_MSK;\r
130                 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
131                 return pdFALSE;\r
132         }\r
133 }\r
134 /*---------------------------------------------------------------------------*/\r
135 \r
136 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
137 {\r
138 signed portBASE_TYPE lReturn = pdPASS;\r
139 \r
140         /* Place the character in the queue of characters to be transmitted. */\r
141         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )\r
142         {\r
143         /*Triggers an interrupt on every character or (down) when queue is full. */\r
144         uartControl |= ALTERA_AVALON_UART_CONTROL_TRDY_MSK; \r
145         IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
146         lReturn = pdPASS;\r
147     }\r
148     else\r
149     {   \r
150                 lReturn = pdFAIL;\r
151         }\r
152         return lReturn;\r
153 }\r
154 /*---------------------------------------------------------------------------*/\r
155 \r
156 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
157 {\r
158 signed char *pxNext;\r
159 \r
160         /* A couple of parameters that this port does not use. */\r
161         ( void ) usStringLength;\r
162         ( void ) pxPort;\r
163 \r
164         /* NOTE: This implementation does not handle the queue being full as no block time is used! */\r
165 \r
166         /* The port handle is not required as this driver only supports UART0. */\r
167         ( void ) pxPort;\r
168 \r
169         /* Send each character in the string, one at a time. */\r
170         pxNext = ( signed char * ) pcString;\r
171         while( *pxNext )\r
172         {\r
173                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
174                 pxNext++;\r
175         }\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 static void vUARTInterruptHandler( void* context, alt_u32 id )\r
180 {\r
181         alt_u32 status;\r
182 \r
183         /* Read the status register in order to determine the cause of the \r
184     interrupt. */\r
185         status = IORD_ALTERA_AVALON_UART_STATUS( UART_BASE );\r
186         \r
187         /* Clear any error flags set at the device */\r
188         IOWR_ALTERA_AVALON_UART_STATUS( UART_BASE, 0 );\r
189         \r
190         /* process a read irq */\r
191         if ( status & ALTERA_AVALON_UART_STATUS_RRDY_MSK )\r
192         {\r
193                 vUARTReceiveHandler( status );\r
194         }\r
195         \r
196         /* process a write irq */\r
197         if ( status & ( ALTERA_AVALON_UART_STATUS_TRDY_MSK  ) )\r
198         {\r
199                 vUARTTransmitHandler( status );\r
200         }\r
201 }\r
202 /*---------------------------------------------------------------------------*/\r
203 \r
204 static void vUARTReceiveHandler( alt_u32 status )\r
205 {\r
206 signed char cChar;\r
207 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
208 \r
209         /* If there was an error, discard the data */\r
210         if ( status & ( ALTERA_AVALON_UART_STATUS_PE_MSK | ALTERA_AVALON_UART_STATUS_FE_MSK ) )\r
211         {\r
212         asm("break");\r
213                 return;\r
214         }\r
215 \r
216         /* Transfer data from the device to the circular buffer */\r
217         cChar = IORD_ALTERA_AVALON_UART_RXDATA( UART_BASE );\r
218         if ( pdTRUE != xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken ) )\r
219         {\r
220                 /* If the circular buffer was full, disable interrupts. Interrupts will \r
221         be re-enabled when data is removed from the buffer. */\r
222                 uartControl &= ~ALTERA_AVALON_UART_CONTROL_RRDY_MSK;\r
223                 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
224         }\r
225     \r
226         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
227 }\r
228 /*---------------------------------------------------------------------------*/\r
229 \r
230 static void vUARTTransmitHandler( alt_u32 status )\r
231 {\r
232 signed char cChar;\r
233 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
234         /* Transfer data if there is some ready to be transferred */\r
235         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
236         {\r
237                 IOWR_ALTERA_AVALON_UART_TXDATA( UART_BASE, cChar );\r
238     }\r
239     else\r
240     {\r
241                 uartControl &= ~ALTERA_AVALON_UART_CONTROL_TRDY_MSK;\r
242     }\r
243         \r
244         IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
245     portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );    \r
246 }    \r
247 /*---------------------------------------------------------------------------*/\r