2 FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER for port 1.
\r
69 Note that this driver is written to test the RTOS port and is not intended
\r
70 to represent an optimised solution. In particular no use is made of the DMA
\r
73 /* Standard include files. */
\r
76 /* Scheduler include files. */
\r
77 #include "FreeRTOS.h"
\r
81 /* Demo application include files. */
\r
84 /* The queues used to communicate between the task code and the interrupt
\r
85 service routines. */
\r
86 static QueueHandle_t xRxedChars;
\r
87 static QueueHandle_t xCharsForTx;
\r
89 /* Hardware specific constants. */
\r
90 #define serTX_INTERRUPT ( ( unsigned char ) 0x80 )
\r
91 #define serRX_INTERRUPT ( ( unsigned char ) 0x40 )
\r
92 #define serTX_ENABLE ( ( unsigned char ) 0x20 )
\r
93 #define serRX_ENABLE ( ( unsigned char ) 0x10 )
\r
95 /* Macros to turn on and off the serial port THRE interrupt while leaving the
\r
96 other register bits in their correct state. The Rx interrupt is always
\r
98 #define serTX_INTERRUPT_ON() SCR1 = serTX_INTERRUPT | serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;
\r
99 #define serTX_INTERRUPT_OFF() SCR1 = serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;
\r
101 /* Bit used to switch on the channel 1 serial port in the module stop
\r
103 #define serMSTP6 ( ( unsigned short ) 0x0040 )
\r
105 /* Interrupt service routines. Note that the Rx and Tx service routines can
\r
106 cause a context switch and are therefore defined with the saveall attribute in
\r
107 addition to the interrupt_handler attribute. See the FreeRTOS.org WEB site
\r
108 documentation for a full explanation.*/
\r
109 void vCOM_1_Rx_ISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
110 void vCOM_1_Tx_ISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
\r
111 void vCOM_1_Error_ISR( void ) __attribute__ ( ( interrupt_handler ) );
\r
113 /*-----------------------------------------------------------*/
\r
116 * Initialise port 1 for interrupt driven communications.
\r
118 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
120 /* Create the queues used to communicate between the tasks and the
\r
121 interrupt service routines. */
\r
122 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
123 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
125 /* No parity, 8 data bits and 1 stop bit is the default so does not require
\r
126 configuration - setup the remains of the hardware. */
\r
127 portENTER_CRITICAL();
\r
129 /* Turn channel 1 on. */
\r
130 MSTPCR &= ~serMSTP6;
\r
132 /* Enable the channels and the Rx interrupt. The Tx interrupt is only
\r
133 enabled when data is being transmitted. */
\r
134 SCR1 = serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;
\r
136 /* Bit rate settings for 22.1184MHz clock only!. */
\r
137 switch( ulWantedBaud )
\r
139 case 4800 : BRR1 = 143;
\r
141 case 9600 : BRR1 = 71;
\r
143 case 19200 : BRR1 = 35;
\r
145 case 38400 : BRR1 = 17;
\r
147 case 57600 : BRR1 = 11;
\r
149 case 115200 : BRR1 = 5;
\r
151 default : BRR1 = 5;
\r
155 portEXIT_CRITICAL();
\r
157 /* Unlike some ports, this driver code does not allow for more than one
\r
158 com port. We therefore don't return a pointer to a port structure and can
\r
159 instead just return NULL. */
\r
162 /*-----------------------------------------------------------*/
\r
164 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
166 /* Get the next character from the buffer queue. Return false if no characters
\r
167 are available, or arrive before xBlockTime expires. */
\r
168 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
177 /*-----------------------------------------------------------*/
\r
179 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
181 signed portBASE_TYPE xReturn = pdPASS;
\r
183 /* Return false if after the block time there is no room on the Tx queue. */
\r
184 portENTER_CRITICAL();
\r
186 /* Send a character to the queue of characters waiting transmission.
\r
187 The queue is serviced by the Tx ISR. */
\r
188 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
190 /* Could not post onto the queue. */
\r
195 /* The message was posted onto the queue so we turn on the Tx
\r
196 interrupt to allow the Tx ISR to remove the character from the
\r
198 serTX_INTERRUPT_ON();
\r
201 portEXIT_CRITICAL();
\r
205 /*-----------------------------------------------------------*/
\r
207 void vSerialClose( xComPortHandle xPort )
\r
209 /* Not supported. */
\r
212 /*-----------------------------------------------------------*/
\r
214 void vCOM_1_Rx_ISR( void )
\r
216 /* This can cause a context switch so this macro must be the first line
\r
217 in the function. */
\r
218 portENTER_SWITCHING_ISR();
\r
220 /* As this is a switching ISR the local variables must be declared as
\r
222 static char cRxByte;
\r
223 static portBASE_TYPE xHigherPriorityTaskWoken;
\r
225 xHigherPriorityTaskWoken = pdFALSE;
\r
227 /* Get the character. */
\r
230 /* Post the character onto the queue of received characters - noting
\r
231 whether or not this wakes a task. */
\r
232 xQueueSendFromISR( xRxedChars, &cRxByte, &xHigherPriorityTaskWoken );
\r
234 /* Clear the interrupt. */
\r
235 SSR1 &= ~serRX_INTERRUPT;
\r
237 /* This must be the last line in the function. We pass cTaskWokenByPost so
\r
238 a context switch will occur if the received character woke a task that has
\r
239 a priority higher than the task we interrupted. */
\r
240 portEXIT_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
242 /*-----------------------------------------------------------*/
\r
244 void vCOM_1_Tx_ISR( void )
\r
246 /* This can cause a context switch so this macro must be the first line
\r
247 in the function. */
\r
248 portENTER_SWITCHING_ISR();
\r
250 /* As this is a switching ISR the local variables must be declared as
\r
252 static char cTxByte;
\r
253 static signed portBASE_TYPE xTaskWokenByTx;
\r
255 /* This variable is static so must be explicitly reinitialised each
\r
256 time the function executes. */
\r
257 xTaskWokenByTx = pdFALSE;
\r
259 /* The interrupt was caused by the THR becoming empty. Are there any
\r
260 more characters to transmit? Note whether or not the Tx interrupt has
\r
262 if( xQueueReceiveFromISR( xCharsForTx, &cTxByte, &xTaskWokenByTx ) == pdTRUE )
\r
264 /* A character was retrieved from the queue so can be sent to the
\r
268 /* Clear the interrupt. */
\r
269 SSR1 &= ~serTX_INTERRUPT;
\r
273 /* Queue empty, nothing to send so turn off the Tx interrupt. */
\r
274 serTX_INTERRUPT_OFF();
\r
277 /* This must be the last line in the function. We pass cTaskWokenByTx so
\r
278 a context switch will occur if the Tx'ed character woke a task that has
\r
279 a priority higher than the task we interrupted. */
\r
280 portEXIT_SWITCHING_ISR( xTaskWokenByTx );
\r
282 /*-----------------------------------------------------------*/
\r
285 * This ISR cannot cause a context switch so requires no special
\r
288 void vCOM_1_Error_ISR( void )
\r
290 volatile unsigned char ucIn;
\r