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