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