2 FreeRTOS.org V4.3.1 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
37 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER for port 1.
\r
39 Note that this driver is written to test the RTOS port and is not intended
\r
40 to represent an optimised solution. In particular no use is made of the DMA
\r
43 /* Standard include files. */
\r
46 /* Scheduler include files. */
\r
47 #include "FreeRTOS.h"
\r
51 /* Demo application include files. */
\r
54 /* The queues used to communicate between the task code and the interrupt
\r
55 service routines. */
\r
56 static xQueueHandle xRxedChars;
\r
57 static xQueueHandle xCharsForTx;
\r
59 /* Hardware specific constants. */
\r
60 #define serTX_INTERRUPT ( ( unsigned portCHAR ) 0x80 )
\r
61 #define serRX_INTERRUPT ( ( unsigned portCHAR ) 0x40 )
\r
62 #define serTX_ENABLE ( ( unsigned portCHAR ) 0x20 )
\r
63 #define serRX_ENABLE ( ( unsigned portCHAR ) 0x10 )
\r
65 /* Macros to turn on and off the serial port THRE interrupt while leaving the
\r
66 other register bits in their correct state. The Rx interrupt is always
\r
68 #define serTX_INTERRUPT_ON() SCR1 = serTX_INTERRUPT | serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;
\r
69 #define serTX_INTERRUPT_OFF() SCR1 = serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;
\r
71 /* Bit used to switch on the channel 1 serial port in the module stop
\r
73 #define serMSTP6 ( ( unsigned portSHORT ) 0x0040 )
\r
75 /* Interrupt service routines. Note that the Rx and Tx service routines can
\r
76 cause a context switch and are therefore defined with the saveall attribute in
\r
77 addition to the interrupt_handler attribute. See the FreeRTOS.org WEB site
\r
78 documentation for a full explanation.*/
\r
79 void vCOM_1_Rx_ISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
80 void vCOM_1_Tx_ISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
81 void vCOM_1_Error_ISR( void ) __attribute__ ( ( interrupt_handler ) );
\r
83 /*-----------------------------------------------------------*/
\r
86 * Initialise port 1 for interrupt driven communications.
\r
88 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
90 /* Create the queues used to communicate between the tasks and the
\r
91 interrupt service routines. */
\r
92 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
93 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
95 /* No parity, 8 data bits and 1 stop bit is the default so does not require
\r
96 configuration - setup the remains of the hardware. */
\r
97 portENTER_CRITICAL();
\r
99 /* Turn channel 1 on. */
\r
100 MSTPCR &= ~serMSTP6;
\r
102 /* Enable the channels and the Rx interrupt. The Tx interrupt is only
\r
103 enabled when data is being transmitted. */
\r
104 SCR1 = serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;
\r
106 /* Bit rate settings for 22.1184MHz clock only!. */
\r
107 switch( ulWantedBaud )
\r
109 case 4800 : BRR1 = 143;
\r
111 case 9600 : BRR1 = 71;
\r
113 case 19200 : BRR1 = 35;
\r
115 case 38400 : BRR1 = 17;
\r
117 case 57600 : BRR1 = 11;
\r
119 case 115200 : BRR1 = 5;
\r
121 default : BRR1 = 5;
\r
125 portEXIT_CRITICAL();
\r
127 /* Unlike some ports, this driver code does not allow for more than one
\r
128 com port. We therefore don't return a pointer to a port structure and can
\r
129 instead just return NULL. */
\r
132 /*-----------------------------------------------------------*/
\r
134 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
136 /* Get the next character from the buffer queue. Return false if no characters
\r
137 are available, or arrive before xBlockTime expires. */
\r
138 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
147 /*-----------------------------------------------------------*/
\r
149 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
151 signed portBASE_TYPE xReturn = pdPASS;
\r
153 /* Return false if after the block time there is no room on the Tx queue. */
\r
154 portENTER_CRITICAL();
\r
156 /* Send a character to the queue of characters waiting transmission.
\r
157 The queue is serviced by the Tx ISR. */
\r
158 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
160 /* Could not post onto the queue. */
\r
165 /* The message was posted onto the queue so we turn on the Tx
\r
166 interrupt to allow the Tx ISR to remove the character from the
\r
168 serTX_INTERRUPT_ON();
\r
171 portEXIT_CRITICAL();
\r
175 /*-----------------------------------------------------------*/
\r
177 void vSerialClose( xComPortHandle xPort )
\r
179 /* Not supported. */
\r
182 /*-----------------------------------------------------------*/
\r
184 void vCOM_1_Rx_ISR( void )
\r
186 /* This can cause a context switch so this macro must be the first line
\r
187 in the function. */
\r
188 portENTER_SWITCHING_ISR();
\r
190 /* As this is a switching ISR the local variables must be declared as
\r
192 static portCHAR cRxByte;
\r
193 static portBASE_TYPE xTaskWokenByPost;
\r
195 /* Get the character. */
\r
198 /* Post the character onto the queue of received characters - noting
\r
199 whether or not this wakes a task. */
\r
200 xTaskWokenByPost = xQueueSendFromISR( xRxedChars, &cRxByte, pdFALSE );
\r
202 /* Clear the interrupt. */
\r
203 SSR1 &= ~serRX_INTERRUPT;
\r
205 /* This must be the last line in the function. We pass cTaskWokenByPost so
\r
206 a context switch will occur if the received character woke a task that has
\r
207 a priority higher than the task we interrupted. */
\r
208 portEXIT_SWITCHING_ISR( xTaskWokenByPost );
\r
210 /*-----------------------------------------------------------*/
\r
212 void vCOM_1_Tx_ISR( void )
\r
214 /* This can cause a context switch so this macro must be the first line
\r
215 in the function. */
\r
216 portENTER_SWITCHING_ISR();
\r
218 /* As this is a switching ISR the local variables must be declared as
\r
220 static portCHAR cTxByte;
\r
221 static signed portBASE_TYPE xTaskWokenByTx;
\r
223 /* This variable is static so must be explicitly reinitialised each
\r
224 time the function executes. */
\r
225 xTaskWokenByTx = pdFALSE;
\r
227 /* The interrupt was caused by the THR becoming empty. Are there any
\r
228 more characters to transmit? Note whether or not the Tx interrupt has
\r
230 if( xQueueReceiveFromISR( xCharsForTx, &cTxByte, &xTaskWokenByTx ) == pdTRUE )
\r
232 /* A character was retrieved from the queue so can be sent to the
\r
236 /* Clear the interrupt. */
\r
237 SSR1 &= ~serTX_INTERRUPT;
\r
241 /* Queue empty, nothing to send so turn off the Tx interrupt. */
\r
242 serTX_INTERRUPT_OFF();
\r
245 /* This must be the last line in the function. We pass cTaskWokenByTx so
\r
246 a context switch will occur if the Tx'ed character woke a task that has
\r
247 a priority higher than the task we interrupted. */
\r
248 portEXIT_SWITCHING_ISR( xTaskWokenByTx );
\r
250 /*-----------------------------------------------------------*/
\r
253 * This ISR cannot cause a context switch so requires no special
\r
256 void vCOM_1_Error_ISR( void )
\r
258 volatile unsigned portCHAR ucIn;
\r