]> begriffs open source - cmsis-freertos/blob - Demo/PIC32MX_MPLAB/serial/serial.c
Updated to FreeRTOS V10.0.1
[cmsis-freertos] / Demo / PIC32MX_MPLAB / serial / serial.c
1 /*
2  * FreeRTOS Kernel V10.0.1
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28
29 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. 
30
31 NOTE:  This driver is primarily to test the scheduler functionality.  It does
32 not effectively use the buffers or DMA and is therefore not intended to be
33 an example of an efficient driver. */
34
35 /* Standard include file. */
36 #include <stdlib.h>
37 #include <plib.h>
38
39 /* Scheduler include files. */
40 #include "FreeRTOS.h"
41 #include "queue.h"
42 #include "task.h"
43
44 /* Demo app include files. */
45 #include "serial.h"
46
47 /* Hardware setup. */
48 #define serSET_FLAG                                             ( 1 )
49
50 /* The queues used to communicate between tasks and ISR's. */
51 static QueueHandle_t xRxedChars; 
52 static QueueHandle_t xCharsForTx; 
53
54 /* Flag used to indicate the tx status. */
55 static volatile portBASE_TYPE xTxHasEnded;
56
57 /*-----------------------------------------------------------*/
58
59 /* The UART interrupt handler.  As this uses the FreeRTOS assembly interrupt
60 entry point the IPL setting in the following prototype has no effect.  The
61 interrupt priority is set by the call to  ConfigIntUART2() in 
62 xSerialPortInitMinimal(). */
63 void __attribute__( (interrupt(IPL0AUTO), vector(_UART2_VECTOR))) vU2InterruptWrapper( void );
64
65 /*-----------------------------------------------------------*/
66
67 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
68 {
69 unsigned short usBRG;
70
71         /* Create the queues used by the com test task. */
72         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
73         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
74
75         /* Configure the UART and interrupts. */
76         usBRG = (unsigned short)(( (float)configPERIPHERAL_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);
77         OpenUART2( UART_EN, UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR, usBRG );
78         ConfigIntUART2( ( configKERNEL_INTERRUPT_PRIORITY + 1 ) | UART_INT_SUB_PR0 | UART_TX_INT_EN | UART_RX_INT_EN );
79
80         xTxHasEnded = pdTRUE;
81
82         /* Only a single port is implemented so we don't need to return anything. */
83         return NULL;
84 }
85 /*-----------------------------------------------------------*/
86
87 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
88 {
89         /* Only one port is supported. */
90         ( void ) pxPort;
91
92         /* Get the next character from the buffer.  Return false if no characters
93         are available or arrive before xBlockTime expires. */
94         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
95         {
96                 return pdTRUE;
97         }
98         else
99         {
100                 return pdFALSE;
101         }
102 }
103 /*-----------------------------------------------------------*/
104
105 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
106 {
107 signed portBASE_TYPE xReturn;
108
109         /* Only one port is supported. */
110         ( void ) pxPort;
111
112         /* Return false if after the block time there is no room on the Tx queue. */
113         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
114         {
115                 xReturn = pdFAIL;
116         }
117         else
118         {
119                 xReturn = pdPASS;
120         }
121
122         if( xReturn != pdFAIL )
123         {
124                 /* A critical section should not be required as xTxHasEnded will not be
125                 written to by the ISR if it is already 0. */
126                 if(  xTxHasEnded == pdTRUE )
127                 {
128                         xTxHasEnded = pdFALSE;
129                         IFS1SET = _IFS1_U2TXIF_MASK;
130                 }
131         }
132
133         return pdPASS;
134 }
135 /*-----------------------------------------------------------*/
136
137 void vSerialClose( xComPortHandle xPort )
138 {
139 }
140 /*-----------------------------------------------------------*/
141
142 void vU2InterruptHandler( void )
143 {
144 /* Declared static to minimise stack use. */
145 static char cChar;
146 static portBASE_TYPE xHigherPriorityTaskWoken;
147
148         xHigherPriorityTaskWoken = pdFALSE;
149
150         /* Are any Rx interrupts pending? */
151         if( IFS1bits.U2RXIF == 1)
152         {
153                 while( U2STAbits.URXDA )
154                 {
155                         /* Retrieve the received character and place it in the queue of
156                         received characters. */
157                         cChar = U2RXREG;
158                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
159                 }
160                 IFS1CLR = _IFS1_U2RXIF_MASK;
161         }
162
163         /* Are any Tx interrupts pending? */
164         if( IFS1bits.U2TXIF == 1 )
165         {
166                 while( ( U2STAbits.UTXBF ) == 0 )
167                 {
168                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
169                         {
170                                 /* Send the next character queued for Tx. */
171                                 U2TXREG = cChar;
172                         }
173                         else
174                         {
175                                 /* Queue empty, nothing to send. */
176                                 xTxHasEnded = pdTRUE;
177                                 break;
178                         }
179                 }
180
181                 IFS1CLR = _IFS1_U2TXIF_MASK;
182         }
183
184         /* If sending or receiving necessitates a context switch, then switch now. */
185         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
186 }
187
188
189
190
191
192
193