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