2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER for port 1.
\r
68 Note that this driver is written to test the RTOS port and is not intended
\r
69 to represent an optimised solution. In particular no use is made of the DMA
\r
72 /* Standard include files. */
\r
75 /* Scheduler include files. */
\r
76 #include "FreeRTOS.h"
\r
80 /* Demo application include files. */
\r
83 /* The queues used to communicate between the task code and the interrupt
\r
84 service routines. */
\r
85 static xQueueHandle xRxedChars;
\r
86 static xQueueHandle xCharsForTx;
\r
88 /* Hardware specific constants. */
\r
89 #define serTX_INTERRUPT ( ( unsigned char ) 0x80 )
\r
90 #define serRX_INTERRUPT ( ( unsigned char ) 0x40 )
\r
91 #define serTX_ENABLE ( ( unsigned char ) 0x20 )
\r
92 #define serRX_ENABLE ( ( unsigned char ) 0x10 )
\r
94 /* Macros to turn on and off the serial port THRE interrupt while leaving the
\r
95 other register bits in their correct state. The Rx interrupt is always
\r
97 #define serTX_INTERRUPT_ON() SCR1 = serTX_INTERRUPT | serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;
\r
98 #define serTX_INTERRUPT_OFF() SCR1 = serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;
\r
100 /* Bit used to switch on the channel 1 serial port in the module stop
\r
102 #define serMSTP6 ( ( unsigned short ) 0x0040 )
\r
104 /* Interrupt service routines. Note that the Rx and Tx service routines can
\r
105 cause a context switch and are therefore defined with the saveall attribute in
\r
106 addition to the interrupt_handler attribute. See the FreeRTOS.org WEB site
\r
107 documentation for a full explanation.*/
\r
108 void vCOM_1_Rx_ISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
109 void vCOM_1_Tx_ISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
110 void vCOM_1_Error_ISR( void ) __attribute__ ( ( interrupt_handler ) );
\r
112 /*-----------------------------------------------------------*/
\r
115 * Initialise port 1 for interrupt driven communications.
\r
117 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
119 /* Create the queues used to communicate between the tasks and the
\r
120 interrupt service routines. */
\r
121 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
122 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
124 /* No parity, 8 data bits and 1 stop bit is the default so does not require
\r
125 configuration - setup the remains of the hardware. */
\r
126 portENTER_CRITICAL();
\r
128 /* Turn channel 1 on. */
\r
129 MSTPCR &= ~serMSTP6;
\r
131 /* Enable the channels and the Rx interrupt. The Tx interrupt is only
\r
132 enabled when data is being transmitted. */
\r
133 SCR1 = serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;
\r
135 /* Bit rate settings for 22.1184MHz clock only!. */
\r
136 switch( ulWantedBaud )
\r
138 case 4800 : BRR1 = 143;
\r
140 case 9600 : BRR1 = 71;
\r
142 case 19200 : BRR1 = 35;
\r
144 case 38400 : BRR1 = 17;
\r
146 case 57600 : BRR1 = 11;
\r
148 case 115200 : BRR1 = 5;
\r
150 default : BRR1 = 5;
\r
154 portEXIT_CRITICAL();
\r
156 /* Unlike some ports, this driver code does not allow for more than one
\r
157 com port. We therefore don't return a pointer to a port structure and can
\r
158 instead just return NULL. */
\r
161 /*-----------------------------------------------------------*/
\r
163 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
165 /* Get the next character from the buffer queue. Return false if no characters
\r
166 are available, or arrive before xBlockTime expires. */
\r
167 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
176 /*-----------------------------------------------------------*/
\r
178 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
180 signed portBASE_TYPE xReturn = pdPASS;
\r
182 /* Return false if after the block time there is no room on the Tx queue. */
\r
183 portENTER_CRITICAL();
\r
185 /* Send a character to the queue of characters waiting transmission.
\r
186 The queue is serviced by the Tx ISR. */
\r
187 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
189 /* Could not post onto the queue. */
\r
194 /* The message was posted onto the queue so we turn on the Tx
\r
195 interrupt to allow the Tx ISR to remove the character from the
\r
197 serTX_INTERRUPT_ON();
\r
200 portEXIT_CRITICAL();
\r
204 /*-----------------------------------------------------------*/
\r
206 void vSerialClose( xComPortHandle xPort )
\r
208 /* Not supported. */
\r
211 /*-----------------------------------------------------------*/
\r
213 void vCOM_1_Rx_ISR( void )
\r
215 /* This can cause a context switch so this macro must be the first line
\r
216 in the function. */
\r
217 portENTER_SWITCHING_ISR();
\r
219 /* As this is a switching ISR the local variables must be declared as
\r
221 static char cRxByte;
\r
222 static portBASE_TYPE xHigherPriorityTaskWoken;
\r
224 xHigherPriorityTaskWoken = pdFALSE;
\r
226 /* Get the character. */
\r
229 /* Post the character onto the queue of received characters - noting
\r
230 whether or not this wakes a task. */
\r
231 xQueueSendFromISR( xRxedChars, &cRxByte, &xHigherPriorityTaskWoken );
\r
233 /* Clear the interrupt. */
\r
234 SSR1 &= ~serRX_INTERRUPT;
\r
236 /* This must be the last line in the function. We pass cTaskWokenByPost so
\r
237 a context switch will occur if the received character woke a task that has
\r
238 a priority higher than the task we interrupted. */
\r
239 portEXIT_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
241 /*-----------------------------------------------------------*/
\r
243 void vCOM_1_Tx_ISR( void )
\r
245 /* This can cause a context switch so this macro must be the first line
\r
246 in the function. */
\r
247 portENTER_SWITCHING_ISR();
\r
249 /* As this is a switching ISR the local variables must be declared as
\r
251 static char cTxByte;
\r
252 static signed portBASE_TYPE xTaskWokenByTx;
\r
254 /* This variable is static so must be explicitly reinitialised each
\r
255 time the function executes. */
\r
256 xTaskWokenByTx = pdFALSE;
\r
258 /* The interrupt was caused by the THR becoming empty. Are there any
\r
259 more characters to transmit? Note whether or not the Tx interrupt has
\r
261 if( xQueueReceiveFromISR( xCharsForTx, &cTxByte, &xTaskWokenByTx ) == pdTRUE )
\r
263 /* A character was retrieved from the queue so can be sent to the
\r
267 /* Clear the interrupt. */
\r
268 SSR1 &= ~serTX_INTERRUPT;
\r
272 /* Queue empty, nothing to send so turn off the Tx interrupt. */
\r
273 serTX_INTERRUPT_OFF();
\r
276 /* This must be the last line in the function. We pass cTaskWokenByTx so
\r
277 a context switch will occur if the Tx'ed character woke a task that has
\r
278 a priority higher than the task we interrupted. */
\r
279 portEXIT_SWITCHING_ISR( xTaskWokenByTx );
\r
281 /*-----------------------------------------------------------*/
\r
284 * This ISR cannot cause a context switch so requires no special
\r
287 void vCOM_1_Error_ISR( void )
\r
289 volatile unsigned char ucIn;
\r