1 |
580 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
#include <device.h>
|
55 |
|
|
#include "FreeRTOS.h"
|
56 |
|
|
#include "queue.h"
|
57 |
|
|
#include "task.h"
|
58 |
|
|
#include "serial.h"
|
59 |
|
|
/*---------------------------------------------------------------------------*/
|
60 |
|
|
|
61 |
|
|
#define serialSTRING_DELAY_TICKS ( portMAX_DELAY )
|
62 |
|
|
/*---------------------------------------------------------------------------*/
|
63 |
|
|
|
64 |
|
|
CY_ISR_PROTO( vUartRxISR );
|
65 |
|
|
CY_ISR_PROTO( vUartTxISR );
|
66 |
|
|
/*---------------------------------------------------------------------------*/
|
67 |
|
|
|
68 |
|
|
static xQueueHandle xSerialTxQueue = NULL;
|
69 |
|
|
static xQueueHandle xSerialRxQueue = NULL;
|
70 |
|
|
/*---------------------------------------------------------------------------*/
|
71 |
|
|
|
72 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
73 |
|
|
{
|
74 |
|
|
/* Configure Rx. */
|
75 |
|
|
xSerialRxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );
|
76 |
|
|
isr_UART1_RX_BYTE_RECEIVED_ClearPending();
|
77 |
|
|
isr_UART1_RX_BYTE_RECEIVED_StartEx(vUartRxISR);
|
78 |
|
|
|
79 |
|
|
/* Configure Tx */
|
80 |
|
|
xSerialTxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );
|
81 |
|
|
isr_UART1_TX_BYTE_COMPLETE_ClearPending() ;
|
82 |
|
|
isr_UART1_TX_BYTE_COMPLETE_StartEx(vUartTxISR);
|
83 |
|
|
|
84 |
|
|
/* Clear the interrupt modes for the Tx for the time being. */
|
85 |
|
|
UART_1_SetTxInterruptMode( 0 );
|
86 |
|
|
|
87 |
|
|
/* Both configured successfully. */
|
88 |
|
|
return ( xComPortHandle )( xSerialTxQueue && xSerialRxQueue );
|
89 |
|
|
}
|
90 |
|
|
/*---------------------------------------------------------------------------*/
|
91 |
|
|
|
92 |
|
|
void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
|
93 |
|
|
{
|
94 |
|
|
unsigned short usIndex = 0;
|
95 |
|
|
|
96 |
|
|
for( usIndex = 0; usIndex < usStringLength; usIndex++ )
|
97 |
|
|
{
|
98 |
|
|
/* Check for pre-mature end of line. */
|
99 |
|
|
if( '\0' == pcString[ usIndex ] )
|
100 |
|
|
{
|
101 |
|
|
break;
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
/* Send out, one character at a time. */
|
105 |
|
|
if( pdTRUE != xSerialPutChar( NULL, pcString[ usIndex ], serialSTRING_DELAY_TICKS ) )
|
106 |
|
|
{
|
107 |
|
|
/* Failed to send, this will be picked up in the receive comtest task. */
|
108 |
|
|
}
|
109 |
|
|
}
|
110 |
|
|
}
|
111 |
|
|
/*---------------------------------------------------------------------------*/
|
112 |
|
|
|
113 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
|
114 |
|
|
{
|
115 |
|
|
portBASE_TYPE xReturn = pdFALSE;
|
116 |
|
|
|
117 |
|
|
if( pdTRUE == xQueueReceive( xSerialRxQueue, pcRxedChar, xBlockTime ) )
|
118 |
|
|
{
|
119 |
|
|
/* Picked up a character. */
|
120 |
|
|
xReturn = pdTRUE;
|
121 |
|
|
}
|
122 |
|
|
return xReturn;
|
123 |
|
|
}
|
124 |
|
|
/*---------------------------------------------------------------------------*/
|
125 |
|
|
|
126 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
|
127 |
|
|
{
|
128 |
|
|
portBASE_TYPE xReturn = pdFALSE;
|
129 |
|
|
|
130 |
|
|
/* The ISR is processing characters is so just add to the end of the queue. */
|
131 |
|
|
if( pdTRUE == xQueueSend( xSerialTxQueue, &cOutChar, xBlockTime ) )
|
132 |
|
|
{
|
133 |
|
|
xReturn = pdTRUE;
|
134 |
|
|
}
|
135 |
|
|
else
|
136 |
|
|
{
|
137 |
|
|
/* The queue is probably full. */
|
138 |
|
|
xReturn = pdFALSE;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
/* Make sure that the interrupt will fire in the case where:
|
142 |
|
|
Currently sending so the Tx Complete will fire.
|
143 |
|
|
Not sending so the Empty will fire. */
|
144 |
|
|
taskENTER_CRITICAL();
|
145 |
|
|
UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE | UART_1_TX_STS_FIFO_EMPTY );
|
146 |
|
|
taskEXIT_CRITICAL();
|
147 |
|
|
|
148 |
|
|
return xReturn;
|
149 |
|
|
}
|
150 |
|
|
/*---------------------------------------------------------------------------*/
|
151 |
|
|
|
152 |
|
|
CY_ISR(vUartRxISR)
|
153 |
|
|
{
|
154 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
155 |
|
|
volatile unsigned char ucStatus = 0;
|
156 |
|
|
signed char cInChar = 0;
|
157 |
|
|
unsigned long ulMask = 0;
|
158 |
|
|
|
159 |
|
|
/* Read the status to acknowledge. */
|
160 |
|
|
ucStatus = UART_1_ReadRxStatus();
|
161 |
|
|
|
162 |
|
|
/* Only interested in a character being received. */
|
163 |
|
|
if( 0 != ( ucStatus & UART_1_RX_STS_FIFO_NOTEMPTY ) )
|
164 |
|
|
{
|
165 |
|
|
/* Get the character. */
|
166 |
|
|
cInChar = UART_1_GetChar();
|
167 |
|
|
|
168 |
|
|
/* Mask off the other RTOS interrupts to interact with the queue. */
|
169 |
|
|
ulMask = portSET_INTERRUPT_MASK_FROM_ISR();
|
170 |
|
|
{
|
171 |
|
|
/* Try to deliver the character. */
|
172 |
|
|
if( pdTRUE != xQueueSendFromISR( xSerialRxQueue, &cInChar, &xHigherPriorityTaskWoken ) )
|
173 |
|
|
{
|
174 |
|
|
/* Run out of space. */
|
175 |
|
|
}
|
176 |
|
|
}
|
177 |
|
|
portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
/* If we delivered the character then a context switch might be required.
|
181 |
|
|
xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry. If calling
|
182 |
|
|
xQueueSendFromISR() caused a task to unblock, and the unblocked task has
|
183 |
|
|
a priority equal to or higher than the currently running task (the task this
|
184 |
|
|
ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and
|
185 |
|
|
portEND_SWITCHING_ISR() will request a context switch to the newly unblocked
|
186 |
|
|
task. */
|
187 |
|
|
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
|
188 |
|
|
}
|
189 |
|
|
/*---------------------------------------------------------------------------*/
|
190 |
|
|
|
191 |
|
|
CY_ISR(vUartTxISR)
|
192 |
|
|
{
|
193 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
194 |
|
|
volatile unsigned char ucStatus = 0;
|
195 |
|
|
signed char cOutChar = 0;
|
196 |
|
|
unsigned long ulMask = 0;
|
197 |
|
|
|
198 |
|
|
/* Read the status to acknowledge. */
|
199 |
|
|
ucStatus = UART_1_ReadTxStatus();
|
200 |
|
|
|
201 |
|
|
/* Check to see whether this is a genuine interrupt. */
|
202 |
|
|
if( ( 0 != ( ucStatus & UART_1_TX_STS_COMPLETE ) ) || ( 0 != ( ucStatus & UART_1_TX_STS_FIFO_EMPTY ) ) )
|
203 |
|
|
{
|
204 |
|
|
/* Mask off the other RTOS interrupts to interact with the queue. */
|
205 |
|
|
ulMask = portSET_INTERRUPT_MASK_FROM_ISR();
|
206 |
|
|
{
|
207 |
|
|
if( pdTRUE == xQueueReceiveFromISR( xSerialTxQueue, &cOutChar, &xHigherPriorityTaskWoken ) )
|
208 |
|
|
{
|
209 |
|
|
/* Send the next character. */
|
210 |
|
|
UART_1_PutChar( cOutChar );
|
211 |
|
|
|
212 |
|
|
/* If we are firing, then the only interrupt we are interested in
|
213 |
|
|
is the Complete. The application code will add the Empty interrupt
|
214 |
|
|
when there is something else to be done. */
|
215 |
|
|
UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE );
|
216 |
|
|
}
|
217 |
|
|
else
|
218 |
|
|
{
|
219 |
|
|
/* There is no work left so disable the interrupt until the application
|
220 |
|
|
puts more into the queue. */
|
221 |
|
|
UART_1_SetTxInterruptMode( 0 );
|
222 |
|
|
}
|
223 |
|
|
}
|
224 |
|
|
portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
/* If we delivered the character then a context switch might be required.
|
228 |
|
|
xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry. If calling
|
229 |
|
|
xQueueSendFromISR() caused a task to unblock, and the unblocked task has
|
230 |
|
|
a priority equal to or higher than the currently running task (the task this
|
231 |
|
|
ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and
|
232 |
|
|
portEND_SWITCHING_ISR() will request a context switch to the newly unblocked
|
233 |
|
|
task. */
|
234 |
|
|
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
|
235 |
|
|
}
|
236 |
|
|
/*---------------------------------------------------------------------------*/
|