2 FreeRTOS V9.0.1 - Copyright (C) 2017 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR DEMO PURPOSES */
\r
73 #include "FreeRTOS.h"
\r
78 /* Constants required to setup the serial control register. */
\r
79 #define ser8_BIT_MODE ( ( unsigned char ) 0x40 )
\r
80 #define serRX_ENABLE ( ( unsigned char ) 0x10 )
\r
82 /* Constants to setup the timer used to generate the baud rate. */
\r
83 #define serCLOCK_DIV_48 ( ( unsigned char ) 0x03 )
\r
84 #define serUSE_PRESCALED_CLOCK ( ( unsigned char ) 0x10 )
\r
85 #define ser8BIT_WITH_RELOAD ( ( unsigned char ) 0x20 )
\r
86 #define serSMOD ( ( unsigned char ) 0x10 )
\r
88 static QueueHandle_t xRxedChars;
\r
89 static QueueHandle_t xCharsForTx;
\r
91 data static unsigned portBASE_TYPE uxTxEmpty;
\r
93 /*-----------------------------------------------------------*/
\r
95 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
97 unsigned long ulReloadValue;
\r
98 const portFLOAT fBaudConst = ( portFLOAT ) configCPU_CLOCK_HZ * ( portFLOAT ) 2.0;
\r
99 unsigned char ucOriginalSFRPage;
\r
101 portENTER_CRITICAL();
\r
103 ucOriginalSFRPage = SFRPAGE;
\r
106 uxTxEmpty = pdTRUE;
\r
108 /* Create the queues used by the com test task. */
\r
109 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
110 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
112 /* Calculate the baud rate to use timer 1. */
\r
113 ulReloadValue = ( unsigned long ) ( ( ( portFLOAT ) 256 - ( fBaudConst / ( portFLOAT ) ( 32 * ulWantedBaud ) ) ) + ( portFLOAT ) 0.5 );
\r
115 /* Set timer one for desired mode of operation. */
\r
117 TMOD |= ser8BIT_WITH_RELOAD;
\r
120 /* Set the reload and start values for the time. */
\r
121 TL1 = ( unsigned char ) ulReloadValue;
\r
122 TH1 = ( unsigned char ) ulReloadValue;
\r
124 /* Setup the control register for standard n, 8, 1 - variable baud rate. */
\r
125 SCON = ser8_BIT_MODE | serRX_ENABLE;
\r
127 /* Enable the serial port interrupts */
\r
130 /* Start the timer. */
\r
133 SFRPAGE = ucOriginalSFRPage;
\r
135 portEXIT_CRITICAL();
\r
137 /* Unlike some ports, this serial code does not allow for more than one
\r
138 com port. We therefore don't return a pointer to a port structure and can
\r
139 instead just return NULL. */
\r
142 /*-----------------------------------------------------------*/
\r
144 void vSerialISR( void ) interrupt 4
\r
147 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
149 /* 8051 port interrupt routines MUST be placed within a critical section
\r
150 if taskYIELD() is used within the ISR! */
\r
152 portENTER_CRITICAL();
\r
156 /* Get the character and post it on the queue of Rxed characters.
\r
157 If the post causes a task to wake force a context switch if the woken task
\r
158 has a higher priority than the task we have interrupted. */
\r
162 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
167 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == ( portBASE_TYPE ) pdTRUE )
\r
169 /* Send the next character queued for Tx. */
\r
174 /* Queue empty, nothing to send. */
\r
175 uxTxEmpty = pdTRUE;
\r
181 if( xHigherPriorityTaskWoken )
\r
186 portEXIT_CRITICAL();
\r
188 /*-----------------------------------------------------------*/
\r
190 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
192 /* There is only one port supported. */
\r
195 /* Get the next character from the buffer. Return false if no characters
\r
196 are available, or arrive before xBlockTime expires. */
\r
197 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
199 return ( portBASE_TYPE ) pdTRUE;
\r
203 return ( portBASE_TYPE ) pdFALSE;
\r
206 /*-----------------------------------------------------------*/
\r
208 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
210 portBASE_TYPE xReturn;
\r
212 /* There is only one port supported. */
\r
215 portENTER_CRITICAL();
\r
217 if( uxTxEmpty == pdTRUE )
\r
220 uxTxEmpty = pdFALSE;
\r
221 xReturn = ( portBASE_TYPE ) pdTRUE;
\r
225 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
227 if( xReturn == ( portBASE_TYPE ) pdFALSE )
\r
229 xReturn = ( portBASE_TYPE ) pdTRUE;
\r
233 portEXIT_CRITICAL();
\r
237 /*-----------------------------------------------------------*/
\r
239 void vSerialClose( xComPortHandle xPort )
\r
241 /* Not implemented in this port. */
\r
244 /*-----------------------------------------------------------*/
\r