2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER for port 1.
\r
31 Note that this driver is written to test the RTOS port and is not intended
\r
32 to represent an optimised solution. In particular no use is made of the DMA
\r
35 /* Standard include files. */
\r
38 /* Scheduler include files. */
\r
39 #include "FreeRTOS.h"
\r
43 /* Demo application include files. */
\r
46 /* The queues used to communicate between the task code and the interrupt
\r
47 service routines. */
\r
48 static QueueHandle_t xRxedChars;
\r
49 static QueueHandle_t xCharsForTx;
\r
51 /* Hardware specific constants. */
\r
52 #define serTX_INTERRUPT ( ( unsigned char ) 0x80 )
\r
53 #define serRX_INTERRUPT ( ( unsigned char ) 0x40 )
\r
54 #define serTX_ENABLE ( ( unsigned char ) 0x20 )
\r
55 #define serRX_ENABLE ( ( unsigned char ) 0x10 )
\r
57 /* Macros to turn on and off the serial port THRE interrupt while leaving the
\r
58 other register bits in their correct state. The Rx interrupt is always
\r
60 #define serTX_INTERRUPT_ON() SCR1 = serTX_INTERRUPT | serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;
\r
61 #define serTX_INTERRUPT_OFF() SCR1 = serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;
\r
63 /* Bit used to switch on the channel 1 serial port in the module stop
\r
65 #define serMSTP6 ( ( unsigned short ) 0x0040 )
\r
67 /* Interrupt service routines. Note that the Rx and Tx service routines can
\r
68 cause a context switch and are therefore defined with the saveall attribute in
\r
69 addition to the interrupt_handler attribute. See the FreeRTOS.org WEB site
\r
70 documentation for a full explanation.*/
\r
71 void vCOM_1_Rx_ISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
72 void vCOM_1_Tx_ISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
73 void vCOM_1_Error_ISR( void ) __attribute__ ( ( interrupt_handler ) );
\r
75 /*-----------------------------------------------------------*/
\r
78 * Initialise port 1 for interrupt driven communications.
\r
80 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
82 /* Create the queues used to communicate between the tasks and the
\r
83 interrupt service routines. */
\r
84 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
85 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
87 /* No parity, 8 data bits and 1 stop bit is the default so does not require
\r
88 configuration - setup the remains of the hardware. */
\r
89 portENTER_CRITICAL();
\r
91 /* Turn channel 1 on. */
\r
92 MSTPCR &= ~serMSTP6;
\r
94 /* Enable the channels and the Rx interrupt. The Tx interrupt is only
\r
95 enabled when data is being transmitted. */
\r
96 SCR1 = serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;
\r
98 /* Bit rate settings for 22.1184MHz clock only!. */
\r
99 switch( ulWantedBaud )
\r
101 case 4800 : BRR1 = 143;
\r
103 case 9600 : BRR1 = 71;
\r
105 case 19200 : BRR1 = 35;
\r
107 case 38400 : BRR1 = 17;
\r
109 case 57600 : BRR1 = 11;
\r
111 case 115200 : BRR1 = 5;
\r
113 default : BRR1 = 5;
\r
117 portEXIT_CRITICAL();
\r
119 /* Unlike some ports, this driver code does not allow for more than one
\r
120 com port. We therefore don't return a pointer to a port structure and can
\r
121 instead just return NULL. */
\r
124 /*-----------------------------------------------------------*/
\r
126 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
128 /* Get the next character from the buffer queue. Return false if no characters
\r
129 are available, or arrive before xBlockTime expires. */
\r
130 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
139 /*-----------------------------------------------------------*/
\r
141 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
143 signed portBASE_TYPE xReturn = pdPASS;
\r
145 /* Return false if after the block time there is no room on the Tx queue. */
\r
146 portENTER_CRITICAL();
\r
148 /* Send a character to the queue of characters waiting transmission.
\r
149 The queue is serviced by the Tx ISR. */
\r
150 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
152 /* Could not post onto the queue. */
\r
157 /* The message was posted onto the queue so we turn on the Tx
\r
158 interrupt to allow the Tx ISR to remove the character from the
\r
160 serTX_INTERRUPT_ON();
\r
163 portEXIT_CRITICAL();
\r
167 /*-----------------------------------------------------------*/
\r
169 void vSerialClose( xComPortHandle xPort )
\r
171 /* Not supported. */
\r
174 /*-----------------------------------------------------------*/
\r
176 void vCOM_1_Rx_ISR( void )
\r
178 /* This can cause a context switch so this macro must be the first line
\r
179 in the function. */
\r
180 portENTER_SWITCHING_ISR();
\r
182 /* As this is a switching ISR the local variables must be declared as
\r
184 static char cRxByte;
\r
185 static portBASE_TYPE xHigherPriorityTaskWoken;
\r
187 xHigherPriorityTaskWoken = pdFALSE;
\r
189 /* Get the character. */
\r
192 /* Post the character onto the queue of received characters - noting
\r
193 whether or not this wakes a task. */
\r
194 xQueueSendFromISR( xRxedChars, &cRxByte, &xHigherPriorityTaskWoken );
\r
196 /* Clear the interrupt. */
\r
197 SSR1 &= ~serRX_INTERRUPT;
\r
199 /* This must be the last line in the function. We pass cTaskWokenByPost so
\r
200 a context switch will occur if the received character woke a task that has
\r
201 a priority higher than the task we interrupted. */
\r
202 portEXIT_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
204 /*-----------------------------------------------------------*/
\r
206 void vCOM_1_Tx_ISR( void )
\r
208 /* This can cause a context switch so this macro must be the first line
\r
209 in the function. */
\r
210 portENTER_SWITCHING_ISR();
\r
212 /* As this is a switching ISR the local variables must be declared as
\r
214 static char cTxByte;
\r
215 static signed portBASE_TYPE xTaskWokenByTx;
\r
217 /* This variable is static so must be explicitly reinitialised each
\r
218 time the function executes. */
\r
219 xTaskWokenByTx = pdFALSE;
\r
221 /* The interrupt was caused by the THR becoming empty. Are there any
\r
222 more characters to transmit? Note whether or not the Tx interrupt has
\r
224 if( xQueueReceiveFromISR( xCharsForTx, &cTxByte, &xTaskWokenByTx ) == pdTRUE )
\r
226 /* A character was retrieved from the queue so can be sent to the
\r
230 /* Clear the interrupt. */
\r
231 SSR1 &= ~serTX_INTERRUPT;
\r
235 /* Queue empty, nothing to send so turn off the Tx interrupt. */
\r
236 serTX_INTERRUPT_OFF();
\r
239 /* This must be the last line in the function. We pass cTaskWokenByTx so
\r
240 a context switch will occur if the Tx'ed character woke a task that has
\r
241 a priority higher than the task we interrupted. */
\r
242 portEXIT_SWITCHING_ISR( xTaskWokenByTx );
\r
244 /*-----------------------------------------------------------*/
\r
247 * This ISR cannot cause a context switch so requires no special
\r
250 void vCOM_1_Error_ISR( void )
\r
252 volatile unsigned char ucIn;
\r