]> 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 V5.4.2 - 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     * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29     * See http://www.FreeRTOS.org/Documentation for details                   *\r
30     *                                                                         *\r
31     ***************************************************************************\r
32 \r
33     1 tab == 4 spaces!\r
34 \r
35     Please ensure to read the configuration and relevant port sections of the\r
36     online documentation.\r
37 \r
38     http://www.FreeRTOS.org - Documentation, latest information, license and\r
39     contact details.\r
40 \r
41     http://www.SafeRTOS.com - A version that is certified for use in safety\r
42     critical systems.\r
43 \r
44     http://www.OpenRTOS.com - Commercial support, development, porting,\r
45     licensing and training services.\r
46 */\r
47 \r
48 /* NOTE:  This is just a test file and not intended to be a generic \r
49 COM driver. */\r
50 \r
51 #include "altera_avalon_uart.h"\r
52 #include "altera_avalon_uart_regs.h"\r
53 #include "sys/alt_irq.h"\r
54 \r
55 #include "FreeRTOS.h"\r
56 #include "queue.h"\r
57 #include "system.h"\r
58 #include "Serial.h"\r
59 /*---------------------------------------------------------------------------*/\r
60 \r
61 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
62 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
63 /*---------------------------------------------------------------------------*/\r
64 \r
65 static xQueueHandle xRxedChars; \r
66 static xQueueHandle xCharsForTx; \r
67 \r
68 alt_u32 uartControl;\r
69 /*---------------------------------------------------------------------------*/\r
70 \r
71 static void vUARTInterruptHandler( void* context, alt_u32 id );\r
72 static void vUARTReceiveHandler( alt_u32 status );\r
73 static void vUARTTransmitHandler( alt_u32 status );\r
74 /*---------------------------------------------------------------------------*/\r
75 \r
76 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
77 {\r
78         /* Create the queues used to hold Rx and Tx characters. */\r
79         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
80         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
81 \r
82         /* If the queues were created correctly then setup the serial port hardware. */\r
83         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
84         {\r
85                 portENTER_CRITICAL();\r
86                 {\r
87                         uartControl = ALTERA_AVALON_UART_CONTROL_RTS_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_DCTS_MSK;\r
88                         IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl ); \r
89                   \r
90                     /* register the interrupt handler */\r
91                         alt_irq_register ( UART_IRQ, NULL, vUARTInterruptHandler );\r
92                 }\r
93                 portEXIT_CRITICAL();\r
94         }\r
95         else\r
96         {\r
97                 return ( xComPortHandle ) 0;\r
98         }\r
99     return ( xComPortHandle ) 1;\r
100 }\r
101 /*---------------------------------------------------------------------------*/\r
102 \r
103 void vSerialClose( xComPortHandle xPort )\r
104 {\r
105     /* Never used. */\r
106 }\r
107 /*---------------------------------------------------------------------------*/\r
108 \r
109 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
110 {\r
111         /* The port handle is not required as this driver only supports one port. */\r
112         ( void ) pxPort;\r
113 \r
114 \r
115         /* Get the next character from the buffer.  Return false if no characters\r
116         are available, or arrive before xBlockTime expires. */\r
117         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
118         {\r
119                 return pdTRUE;\r
120         }\r
121         else\r
122         {\r
123                 uartControl |= ALTERA_AVALON_UART_CONTROL_RRDY_MSK;\r
124                 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
125                 return pdFALSE;\r
126         }\r
127 }\r
128 /*---------------------------------------------------------------------------*/\r
129 \r
130 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
131 {\r
132 signed portBASE_TYPE lReturn = pdPASS;\r
133 \r
134         /* Place the character in the queue of characters to be transmitted. */\r
135         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )\r
136         {\r
137         /*Triggers an interrupt on every character or (down) when queue is full. */\r
138         uartControl |= ALTERA_AVALON_UART_CONTROL_TRDY_MSK; \r
139         IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
140         lReturn = pdPASS;\r
141     }\r
142     else\r
143     {   \r
144                 lReturn = pdFAIL;\r
145         }\r
146         return lReturn;\r
147 }\r
148 /*---------------------------------------------------------------------------*/\r
149 \r
150 void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )\r
151 {\r
152 signed portCHAR *pxNext;\r
153 \r
154         /* A couple of parameters that this port does not use. */\r
155         ( void ) usStringLength;\r
156         ( void ) pxPort;\r
157 \r
158         /* NOTE: This implementation does not handle the queue being full as no block time is used! */\r
159 \r
160         /* The port handle is not required as this driver only supports UART0. */\r
161         ( void ) pxPort;\r
162 \r
163         /* Send each character in the string, one at a time. */\r
164         pxNext = ( signed portCHAR * ) pcString;\r
165         while( *pxNext )\r
166         {\r
167                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
168                 pxNext++;\r
169         }\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 static void vUARTInterruptHandler( void* context, alt_u32 id )\r
174 {\r
175         alt_u32 status;\r
176 \r
177         /* Read the status register in order to determine the cause of the \r
178     interrupt. */\r
179         status = IORD_ALTERA_AVALON_UART_STATUS( UART_BASE );\r
180         \r
181         /* Clear any error flags set at the device */\r
182         IOWR_ALTERA_AVALON_UART_STATUS( UART_BASE, 0 );\r
183         \r
184         /* process a read irq */\r
185         if ( status & ALTERA_AVALON_UART_STATUS_RRDY_MSK )\r
186         {\r
187                 vUARTReceiveHandler( status );\r
188         }\r
189         \r
190         /* process a write irq */\r
191         if ( status & ( ALTERA_AVALON_UART_STATUS_TRDY_MSK  ) )\r
192         {\r
193                 vUARTTransmitHandler( status );\r
194         }\r
195 }\r
196 /*---------------------------------------------------------------------------*/\r
197 \r
198 static void vUARTReceiveHandler( alt_u32 status )\r
199 {\r
200 signed portCHAR cChar;\r
201 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
202 \r
203         /* If there was an error, discard the data */\r
204         if ( status & ( ALTERA_AVALON_UART_STATUS_PE_MSK | ALTERA_AVALON_UART_STATUS_FE_MSK ) )\r
205         {\r
206         asm("break");\r
207                 return;\r
208         }\r
209 \r
210         /* Transfer data from the device to the circular buffer */\r
211         cChar = IORD_ALTERA_AVALON_UART_RXDATA( UART_BASE );\r
212         if ( pdTRUE != xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken ) )\r
213         {\r
214                 /* If the circular buffer was full, disable interrupts. Interrupts will \r
215         be re-enabled when data is removed from the buffer. */\r
216                 uartControl &= ~ALTERA_AVALON_UART_CONTROL_RRDY_MSK;\r
217                 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
218         }\r
219     \r
220         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
221 }\r
222 /*---------------------------------------------------------------------------*/\r
223 \r
224 static void vUARTTransmitHandler( alt_u32 status )\r
225 {\r
226 signed portCHAR cChar;\r
227 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
228         /* Transfer data if there is some ready to be transferred */\r
229         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
230         {\r
231                 IOWR_ALTERA_AVALON_UART_TXDATA( UART_BASE, cChar );\r
232     }\r
233     else\r
234     {\r
235                 uartControl &= ~ALTERA_AVALON_UART_CONTROL_TRDY_MSK;\r
236     }\r
237         \r
238         IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
239     portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );    \r
240 }    \r
241 /*---------------------------------------------------------------------------*/\r