]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/NiosII_CycloneIII_DBC3C40_GCC/RTOSDemo/serial.c
Update version number ready to release the FAT file system demo.
[freertos] / FreeRTOS / Demo / NiosII_CycloneIII_DBC3C40_GCC / RTOSDemo / serial.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 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 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /* NOTE:  This is just a test file and not intended to be a generic \r
76 COM driver. */\r
77 \r
78 #include "altera_avalon_uart.h"\r
79 #include "altera_avalon_uart_regs.h"\r
80 #include "sys/alt_irq.h"\r
81 \r
82 #include "FreeRTOS.h"\r
83 #include "queue.h"\r
84 #include "system.h"\r
85 #include "Serial.h"\r
86 /*---------------------------------------------------------------------------*/\r
87 \r
88 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
89 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
90 /*---------------------------------------------------------------------------*/\r
91 \r
92 static xQueueHandle xRxedChars; \r
93 static xQueueHandle xCharsForTx; \r
94 \r
95 alt_u32 uartControl;\r
96 /*---------------------------------------------------------------------------*/\r
97 \r
98 static void vUARTInterruptHandler( void* context, alt_u32 id );\r
99 static void vUARTReceiveHandler( alt_u32 status );\r
100 static void vUARTTransmitHandler( alt_u32 status );\r
101 /*---------------------------------------------------------------------------*/\r
102 \r
103 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
104 {\r
105         /* Create the queues used to hold Rx and Tx characters. */\r
106         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
107         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
108 \r
109         /* If the queues were created correctly then setup the serial port hardware. */\r
110         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
111         {\r
112                 portENTER_CRITICAL();\r
113                 {\r
114                         uartControl = ALTERA_AVALON_UART_CONTROL_RTS_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_DCTS_MSK;\r
115                         IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl ); \r
116                   \r
117                     /* register the interrupt handler */\r
118                         alt_irq_register ( UART_IRQ, NULL, vUARTInterruptHandler );\r
119                 }\r
120                 portEXIT_CRITICAL();\r
121         }\r
122         else\r
123         {\r
124                 return ( xComPortHandle ) 0;\r
125         }\r
126     return ( xComPortHandle ) 1;\r
127 }\r
128 /*---------------------------------------------------------------------------*/\r
129 \r
130 void vSerialClose( xComPortHandle xPort )\r
131 {\r
132     /* Never used. */\r
133 }\r
134 /*---------------------------------------------------------------------------*/\r
135 \r
136 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
137 {\r
138         /* The port handle is not required as this driver only supports one port. */\r
139         ( void ) pxPort;\r
140 \r
141 \r
142         /* Get the next character from the buffer.  Return false if no characters\r
143         are available, or arrive before xBlockTime expires. */\r
144         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
145         {\r
146                 return pdTRUE;\r
147         }\r
148         else\r
149         {\r
150                 uartControl |= ALTERA_AVALON_UART_CONTROL_RRDY_MSK;\r
151                 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
152                 return pdFALSE;\r
153         }\r
154 }\r
155 /*---------------------------------------------------------------------------*/\r
156 \r
157 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
158 {\r
159 signed portBASE_TYPE lReturn = pdPASS;\r
160 \r
161         /* Place the character in the queue of characters to be transmitted. */\r
162         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )\r
163         {\r
164         /*Triggers an interrupt on every character or (down) when queue is full. */\r
165         uartControl |= ALTERA_AVALON_UART_CONTROL_TRDY_MSK; \r
166         IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
167         lReturn = pdPASS;\r
168     }\r
169     else\r
170     {   \r
171                 lReturn = pdFAIL;\r
172         }\r
173         return lReturn;\r
174 }\r
175 /*---------------------------------------------------------------------------*/\r
176 \r
177 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
178 {\r
179 signed char *pxNext;\r
180 \r
181         /* A couple of parameters that this port does not use. */\r
182         ( void ) usStringLength;\r
183         ( void ) pxPort;\r
184 \r
185         /* NOTE: This implementation does not handle the queue being full as no block time is used! */\r
186 \r
187         /* The port handle is not required as this driver only supports UART0. */\r
188         ( void ) pxPort;\r
189 \r
190         /* Send each character in the string, one at a time. */\r
191         pxNext = ( signed char * ) pcString;\r
192         while( *pxNext )\r
193         {\r
194                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
195                 pxNext++;\r
196         }\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 static void vUARTInterruptHandler( void* context, alt_u32 id )\r
201 {\r
202         alt_u32 status;\r
203 \r
204         /* Read the status register in order to determine the cause of the \r
205     interrupt. */\r
206         status = IORD_ALTERA_AVALON_UART_STATUS( UART_BASE );\r
207         \r
208         /* Clear any error flags set at the device */\r
209         IOWR_ALTERA_AVALON_UART_STATUS( UART_BASE, 0 );\r
210         \r
211         /* process a read irq */\r
212         if ( status & ALTERA_AVALON_UART_STATUS_RRDY_MSK )\r
213         {\r
214                 vUARTReceiveHandler( status );\r
215         }\r
216         \r
217         /* process a write irq */\r
218         if ( status & ( ALTERA_AVALON_UART_STATUS_TRDY_MSK  ) )\r
219         {\r
220                 vUARTTransmitHandler( status );\r
221         }\r
222 }\r
223 /*---------------------------------------------------------------------------*/\r
224 \r
225 static void vUARTReceiveHandler( alt_u32 status )\r
226 {\r
227 signed char cChar;\r
228 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
229 \r
230         /* If there was an error, discard the data */\r
231         if ( status & ( ALTERA_AVALON_UART_STATUS_PE_MSK | ALTERA_AVALON_UART_STATUS_FE_MSK ) )\r
232         {\r
233         asm("break");\r
234                 return;\r
235         }\r
236 \r
237         /* Transfer data from the device to the circular buffer */\r
238         cChar = IORD_ALTERA_AVALON_UART_RXDATA( UART_BASE );\r
239         if ( pdTRUE != xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken ) )\r
240         {\r
241                 /* If the circular buffer was full, disable interrupts. Interrupts will \r
242         be re-enabled when data is removed from the buffer. */\r
243                 uartControl &= ~ALTERA_AVALON_UART_CONTROL_RRDY_MSK;\r
244                 IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
245         }\r
246     \r
247         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
248 }\r
249 /*---------------------------------------------------------------------------*/\r
250 \r
251 static void vUARTTransmitHandler( alt_u32 status )\r
252 {\r
253 signed char cChar;\r
254 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
255         /* Transfer data if there is some ready to be transferred */\r
256         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
257         {\r
258                 IOWR_ALTERA_AVALON_UART_TXDATA( UART_BASE, cChar );\r
259     }\r
260     else\r
261     {\r
262                 uartControl &= ~ALTERA_AVALON_UART_CONTROL_TRDY_MSK;\r
263     }\r
264         \r
265         IOWR_ALTERA_AVALON_UART_CONTROL( UART_BASE, uartControl );\r
266     portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );    \r
267 }    \r
268 /*---------------------------------------------------------------------------*/\r