]> git.sur5r.net Git - freertos/blob - Demo/NiosII_CycloneIII_DBC3C40_GCC/RTOSDemo/serial.c
Remove unnecessary use of portLONG, portCHAR and portSHORT.
[freertos] / Demo / NiosII_CycloneIII_DBC3C40_GCC / RTOSDemo / serial.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     This file is part of the FreeRTOS distribution.\r
5 \r
6     FreeRTOS is free software; you can redistribute it and/or modify it    under\r
7     the terms of the GNU General Public License (version 2) as published by the\r
8     Free Software Foundation and modified by the FreeRTOS exception.\r
9     **NOTE** The exception to the GPL is included to allow you to distribute a\r
10     combined work that includes FreeRTOS without being obliged to provide the\r
11     source code for proprietary components outside of the FreeRTOS kernel.\r
12     Alternative commercial license and support terms are also available upon\r
13     request.  See the licensing section of http://www.FreeRTOS.org for full\r
14     license details.\r
15 \r
16     FreeRTOS is distributed in the hope that it will be useful,    but WITHOUT\r
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19     more details.\r
20 \r
21     You should have received a copy of the GNU General Public License along\r
22     with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23     Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26     ***************************************************************************\r
27     *                                                                         *\r
28     * The FreeRTOS eBook and reference manual are available to purchase for a *\r
29     * small fee. Help yourself get started quickly while also helping the     *\r
30     * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *\r
31     *                                                                         *\r
32     ***************************************************************************\r
33 \r
34     1 tab == 4 spaces!\r
35 \r
36     Please ensure to read the configuration and relevant port sections of the\r
37     online documentation.\r
38 \r
39     http://www.FreeRTOS.org - Documentation, latest information, license and\r
40     contact details.\r
41 \r
42     http://www.SafeRTOS.com - A version that is certified for use in safety\r
43     critical systems.\r
44 \r
45     http://www.OpenRTOS.com - Commercial support, development, porting,\r
46     licensing and training services.\r
47 */\r
48 \r
49 /* NOTE:  This is just a test file and not intended to be a generic \r
50 COM driver. */\r
51 \r
52 #include "altera_avalon_uart.h"\r
53 #include "altera_avalon_uart_regs.h"\r
54 #include "sys/alt_irq.h"\r
55 \r
56 #include "FreeRTOS.h"\r
57 #include "queue.h"\r
58 #include "system.h"\r
59 #include "Serial.h"\r
60 /*---------------------------------------------------------------------------*/\r
61 \r
62 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
63 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
64 /*---------------------------------------------------------------------------*/\r
65 \r
66 static xQueueHandle xRxedChars; \r
67 static xQueueHandle xCharsForTx; \r
68 \r
69 alt_u32 uartControl;\r
70 /*---------------------------------------------------------------------------*/\r
71 \r
72 static void vUARTInterruptHandler( void* context, alt_u32 id );\r
73 static void vUARTReceiveHandler( alt_u32 status );\r
74 static void vUARTTransmitHandler( alt_u32 status );\r
75 /*---------------------------------------------------------------------------*/\r
76 \r
77 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
78 {\r
79         /* Create the queues used to hold Rx and Tx characters. */\r
80         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
81         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
82 \r
83         /* If the queues were created correctly then setup the serial port hardware. */\r
84         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
85         {\r
86                 portENTER_CRITICAL();\r
87                 {\r
88                         uartControl = ALTERA_AVALON_UART_CONTROL_RTS_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_DCTS_MSK;\r
89                         IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl ); \r
90                   \r
91                     /* register the interrupt handler */\r
92                         alt_irq_register ( UART_IRQ, NULL, vUARTInterruptHandler );\r
93                 }\r
94                 portEXIT_CRITICAL();\r
95         }\r
96         else\r
97         {\r
98                 return ( xComPortHandle ) 0;\r
99         }\r
100     return ( xComPortHandle ) 1;\r
101 }\r
102 /*---------------------------------------------------------------------------*/\r
103 \r
104 void vSerialClose( xComPortHandle xPort )\r
105 {\r
106     /* Never used. */\r
107 }\r
108 /*---------------------------------------------------------------------------*/\r
109 \r
110 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
111 {\r
112         /* The port handle is not required as this driver only supports one port. */\r
113         ( void ) pxPort;\r
114 \r
115 \r
116         /* Get the next character from the buffer.  Return false if no characters\r
117         are available, or arrive before xBlockTime expires. */\r
118         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
119         {\r
120                 return pdTRUE;\r
121         }\r
122         else\r
123         {\r
124                 uartControl |= ALTERA_AVALON_UART_CONTROL_RRDY_MSK;\r
125                 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
126                 return pdFALSE;\r
127         }\r
128 }\r
129 /*---------------------------------------------------------------------------*/\r
130 \r
131 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
132 {\r
133 signed portBASE_TYPE lReturn = pdPASS;\r
134 \r
135         /* Place the character in the queue of characters to be transmitted. */\r
136         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )\r
137         {\r
138         /*Triggers an interrupt on every character or (down) when queue is full. */\r
139         uartControl |= ALTERA_AVALON_UART_CONTROL_TRDY_MSK; \r
140         IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
141         lReturn = pdPASS;\r
142     }\r
143     else\r
144     {   \r
145                 lReturn = pdFAIL;\r
146         }\r
147         return lReturn;\r
148 }\r
149 /*---------------------------------------------------------------------------*/\r
150 \r
151 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
152 {\r
153 signed char *pxNext;\r
154 \r
155         /* A couple of parameters that this port does not use. */\r
156         ( void ) usStringLength;\r
157         ( void ) pxPort;\r
158 \r
159         /* NOTE: This implementation does not handle the queue being full as no block time is used! */\r
160 \r
161         /* The port handle is not required as this driver only supports UART0. */\r
162         ( void ) pxPort;\r
163 \r
164         /* Send each character in the string, one at a time. */\r
165         pxNext = ( signed char * ) pcString;\r
166         while( *pxNext )\r
167         {\r
168                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
169                 pxNext++;\r
170         }\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 static void vUARTInterruptHandler( void* context, alt_u32 id )\r
175 {\r
176         alt_u32 status;\r
177 \r
178         /* Read the status register in order to determine the cause of the \r
179     interrupt. */\r
180         status = IORD_ALTERA_AVALON_UART_STATUS( UART_BASE );\r
181         \r
182         /* Clear any error flags set at the device */\r
183         IOWR_ALTERA_AVALON_UART_STATUS( UART_BASE, 0 );\r
184         \r
185         /* process a read irq */\r
186         if ( status & ALTERA_AVALON_UART_STATUS_RRDY_MSK )\r
187         {\r
188                 vUARTReceiveHandler( status );\r
189         }\r
190         \r
191         /* process a write irq */\r
192         if ( status & ( ALTERA_AVALON_UART_STATUS_TRDY_MSK  ) )\r
193         {\r
194                 vUARTTransmitHandler( status );\r
195         }\r
196 }\r
197 /*---------------------------------------------------------------------------*/\r
198 \r
199 static void vUARTReceiveHandler( alt_u32 status )\r
200 {\r
201 signed char cChar;\r
202 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
203 \r
204         /* If there was an error, discard the data */\r
205         if ( status & ( ALTERA_AVALON_UART_STATUS_PE_MSK | ALTERA_AVALON_UART_STATUS_FE_MSK ) )\r
206         {\r
207         asm("break");\r
208                 return;\r
209         }\r
210 \r
211         /* Transfer data from the device to the circular buffer */\r
212         cChar = IORD_ALTERA_AVALON_UART_RXDATA( UART_BASE );\r
213         if ( pdTRUE != xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken ) )\r
214         {\r
215                 /* If the circular buffer was full, disable interrupts. Interrupts will \r
216         be re-enabled when data is removed from the buffer. */\r
217                 uartControl &= ~ALTERA_AVALON_UART_CONTROL_RRDY_MSK;\r
218                 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
219         }\r
220     \r
221         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
222 }\r
223 /*---------------------------------------------------------------------------*/\r
224 \r
225 static void vUARTTransmitHandler( alt_u32 status )\r
226 {\r
227 signed char cChar;\r
228 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
229         /* Transfer data if there is some ready to be transferred */\r
230         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
231         {\r
232                 IOWR_ALTERA_AVALON_UART_TXDATA( UART_BASE, cChar );\r
233     }\r
234     else\r
235     {\r
236                 uartControl &= ~ALTERA_AVALON_UART_CONTROL_TRDY_MSK;\r
237     }\r
238         \r
239         IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
240     portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );    \r
241 }    \r
242 /*---------------------------------------------------------------------------*/\r