2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 #include "FreeRTOS.h"
\r
34 /*---------------------------------------------------------------------------*/
\r
36 #define serialSTRING_DELAY_TICKS ( portMAX_DELAY )
\r
37 /*---------------------------------------------------------------------------*/
\r
39 CY_ISR_PROTO( vUartRxISR );
\r
40 CY_ISR_PROTO( vUartTxISR );
\r
41 /*---------------------------------------------------------------------------*/
\r
43 static QueueHandle_t xSerialTxQueue = NULL;
\r
44 static QueueHandle_t xSerialRxQueue = NULL;
\r
45 /*---------------------------------------------------------------------------*/
\r
47 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
50 xSerialRxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );
\r
51 isr_UART1_RX_BYTE_RECEIVED_ClearPending();
\r
52 isr_UART1_RX_BYTE_RECEIVED_StartEx(vUartRxISR);
\r
55 xSerialTxQueue = xQueueCreate( uxQueueLength, sizeof( signed char ) );
\r
56 isr_UART1_TX_BYTE_COMPLETE_ClearPending() ;
\r
57 isr_UART1_TX_BYTE_COMPLETE_StartEx(vUartTxISR);
\r
59 /* Clear the interrupt modes for the Tx for the time being. */
\r
60 UART_1_SetTxInterruptMode( 0 );
\r
62 /* Both configured successfully. */
\r
63 return ( xComPortHandle )( xSerialTxQueue && xSerialRxQueue );
\r
65 /*---------------------------------------------------------------------------*/
\r
67 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
69 unsigned short usIndex = 0;
\r
71 for( usIndex = 0; usIndex < usStringLength; usIndex++ )
\r
73 /* Check for pre-mature end of line. */
\r
74 if( '\0' == pcString[ usIndex ] )
\r
79 /* Send out, one character at a time. */
\r
80 if( pdTRUE != xSerialPutChar( NULL, pcString[ usIndex ], serialSTRING_DELAY_TICKS ) )
\r
82 /* Failed to send, this will be picked up in the receive comtest task. */
\r
86 /*---------------------------------------------------------------------------*/
\r
88 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
90 portBASE_TYPE xReturn = pdFALSE;
\r
92 if( pdTRUE == xQueueReceive( xSerialRxQueue, pcRxedChar, xBlockTime ) )
\r
94 /* Picked up a character. */
\r
99 /*---------------------------------------------------------------------------*/
\r
101 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
103 portBASE_TYPE xReturn = pdFALSE;
\r
105 /* The ISR is processing characters is so just add to the end of the queue. */
\r
106 if( pdTRUE == xQueueSend( xSerialTxQueue, &cOutChar, xBlockTime ) )
\r
112 /* The queue is probably full. */
\r
116 /* Make sure that the interrupt will fire in the case where:
\r
117 Currently sending so the Tx Complete will fire.
\r
118 Not sending so the Empty will fire. */
\r
119 taskENTER_CRITICAL();
\r
120 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE | UART_1_TX_STS_FIFO_EMPTY );
\r
121 taskEXIT_CRITICAL();
\r
125 /*---------------------------------------------------------------------------*/
\r
129 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
130 volatile unsigned char ucStatus = 0;
\r
131 signed char cInChar = 0;
\r
132 unsigned long ulMask = 0;
\r
134 /* Read the status to acknowledge. */
\r
135 ucStatus = UART_1_ReadRxStatus();
\r
137 /* Only interested in a character being received. */
\r
138 if( 0 != ( ucStatus & UART_1_RX_STS_FIFO_NOTEMPTY ) )
\r
140 /* Get the character. */
\r
141 cInChar = UART_1_GetChar();
\r
143 /* Mask off the other RTOS interrupts to interact with the queue. */
\r
144 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();
\r
146 /* Try to deliver the character. */
\r
147 if( pdTRUE != xQueueSendFromISR( xSerialRxQueue, &cInChar, &xHigherPriorityTaskWoken ) )
\r
149 /* Run out of space. */
\r
152 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );
\r
155 /* If we delivered the character then a context switch might be required.
\r
156 xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry. If calling
\r
157 xQueueSendFromISR() caused a task to unblock, and the unblocked task has
\r
158 a priority equal to or higher than the currently running task (the task this
\r
159 ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and
\r
160 portEND_SWITCHING_ISR() will request a context switch to the newly unblocked
\r
162 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
164 /*---------------------------------------------------------------------------*/
\r
168 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
169 volatile unsigned char ucStatus = 0;
\r
170 signed char cOutChar = 0;
\r
171 unsigned long ulMask = 0;
\r
173 /* Read the status to acknowledge. */
\r
174 ucStatus = UART_1_ReadTxStatus();
\r
176 /* Check to see whether this is a genuine interrupt. */
\r
177 if( ( 0 != ( ucStatus & UART_1_TX_STS_COMPLETE ) ) || ( 0 != ( ucStatus & UART_1_TX_STS_FIFO_EMPTY ) ) )
\r
179 /* Mask off the other RTOS interrupts to interact with the queue. */
\r
180 ulMask = portSET_INTERRUPT_MASK_FROM_ISR();
\r
182 if( pdTRUE == xQueueReceiveFromISR( xSerialTxQueue, &cOutChar, &xHigherPriorityTaskWoken ) )
\r
184 /* Send the next character. */
\r
185 UART_1_PutChar( cOutChar );
\r
187 /* If we are firing, then the only interrupt we are interested in
\r
188 is the Complete. The application code will add the Empty interrupt
\r
189 when there is something else to be done. */
\r
190 UART_1_SetTxInterruptMode( UART_1_TX_STS_COMPLETE );
\r
194 /* There is no work left so disable the interrupt until the application
\r
195 puts more into the queue. */
\r
196 UART_1_SetTxInterruptMode( 0 );
\r
199 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulMask );
\r
202 /* If we delivered the character then a context switch might be required.
\r
203 xHigherPriorityTaskWoken was set to pdFALSE on interrupt entry. If calling
\r
204 xQueueSendFromISR() caused a task to unblock, and the unblocked task has
\r
205 a priority equal to or higher than the currently running task (the task this
\r
206 ISR interrupted), then xHigherPriorityTaskWoken will have been set to pdTRUE and
\r
207 portEND_SWITCHING_ISR() will request a context switch to the newly unblocked
\r
209 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
211 /*---------------------------------------------------------------------------*/
\r