]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Cygnal/serial/serial.c
Update version number to 8.1.1 for patch release that re-enables mutexes to be given...
[freertos] / FreeRTOS / Demo / Cygnal / serial / serial.c
1 /*\r
2     FreeRTOS V8.1.1 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\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
12      *                                                                       *\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
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \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
26 \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
31 \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
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \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
54 \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
58 \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
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 \r
67 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR DEMO PURPOSES */\r
68 #include <stdlib.h>\r
69 #include "FreeRTOS.h"\r
70 #include "queue.h"\r
71 #include "task.h"\r
72 #include "serial.h"\r
73 \r
74 /* Constants required to setup the serial control register. */\r
75 #define ser8_BIT_MODE                   ( ( unsigned char ) 0x40 )\r
76 #define serRX_ENABLE                    ( ( unsigned char ) 0x10 )\r
77 \r
78 /* Constants to setup the timer used to generate the baud rate. */\r
79 #define serCLOCK_DIV_48                 ( ( unsigned char ) 0x03 )\r
80 #define serUSE_PRESCALED_CLOCK  ( ( unsigned char ) 0x10 )\r
81 #define ser8BIT_WITH_RELOAD             ( ( unsigned char ) 0x20 )\r
82 #define serSMOD                                 ( ( unsigned char ) 0x10 )\r
83 \r
84 static QueueHandle_t xRxedChars; \r
85 static QueueHandle_t xCharsForTx; \r
86 \r
87 data static unsigned portBASE_TYPE uxTxEmpty;\r
88 \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
92 {\r
93 unsigned long ulReloadValue;\r
94 const portFLOAT fBaudConst = ( portFLOAT ) configCPU_CLOCK_HZ * ( portFLOAT ) 2.0;\r
95 unsigned char ucOriginalSFRPage;\r
96 \r
97         portENTER_CRITICAL();\r
98         {\r
99                 ucOriginalSFRPage = SFRPAGE;\r
100                 SFRPAGE = 0;\r
101 \r
102                 uxTxEmpty = pdTRUE;\r
103 \r
104                 /* Create the queues used by the com test task. */\r
105                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
106                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
107         \r
108                 /* Calculate the baud rate to use timer 1. */\r
109                 ulReloadValue = ( unsigned long ) ( ( ( portFLOAT ) 256 - ( fBaudConst / ( portFLOAT ) ( 32 * ulWantedBaud ) ) ) + ( portFLOAT ) 0.5 );\r
110 \r
111                 /* Set timer one for desired mode of operation. */\r
112                 TMOD &= 0x08;\r
113                 TMOD |= ser8BIT_WITH_RELOAD;\r
114                 SSTA0 |= serSMOD;\r
115 \r
116                 /* Set the reload and start values for the time. */\r
117                 TL1 = ( unsigned char ) ulReloadValue;\r
118                 TH1 = ( unsigned char ) ulReloadValue;\r
119 \r
120                 /* Setup the control register for standard n, 8, 1 - variable baud rate. */\r
121                 SCON = ser8_BIT_MODE | serRX_ENABLE;\r
122 \r
123                 /* Enable the serial port interrupts */\r
124                 ES = 1;\r
125 \r
126                 /* Start the timer. */\r
127                 TR1 = 1;\r
128 \r
129                 SFRPAGE = ucOriginalSFRPage;\r
130         }\r
131         portEXIT_CRITICAL();\r
132         \r
133         /* Unlike some ports, this serial code does not allow for more than one\r
134         com port.  We therefore don't return a pointer to a port structure and can\r
135         instead just return NULL. */\r
136         return NULL;\r
137 }\r
138 /*-----------------------------------------------------------*/\r
139 \r
140 void vSerialISR( void ) interrupt 4\r
141 {\r
142 char cChar;\r
143 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
144 \r
145         /* 8051 port interrupt routines MUST be placed within a critical section\r
146         if taskYIELD() is used within the ISR! */\r
147 \r
148         portENTER_CRITICAL();\r
149         {\r
150                 if( RI ) \r
151                 {\r
152                         /* Get the character and post it on the queue of Rxed characters.\r
153                         If the post causes a task to wake force a context switch if the woken task\r
154                         has a higher priority than the task we have interrupted. */\r
155                         cChar = SBUF;\r
156                         RI = 0;\r
157 \r
158                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
159                 }\r
160 \r
161                 if( TI ) \r
162                 {\r
163                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == ( portBASE_TYPE ) pdTRUE )\r
164                         {\r
165                                 /* Send the next character queued for Tx. */\r
166                                 SBUF = cChar;\r
167                         }\r
168                         else\r
169                         {\r
170                                 /* Queue empty, nothing to send. */\r
171                                 uxTxEmpty = pdTRUE;\r
172                         }\r
173 \r
174                         TI = 0;\r
175                 }\r
176         \r
177                 if( xHigherPriorityTaskWoken )\r
178                 {\r
179                         portYIELD();\r
180                 }\r
181         }\r
182         portEXIT_CRITICAL();\r
183 }\r
184 /*-----------------------------------------------------------*/\r
185 \r
186 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
187 {\r
188         /* There is only one port supported. */\r
189         ( void ) pxPort;\r
190 \r
191         /* Get the next character from the buffer.  Return false if no characters\r
192         are available, or arrive before xBlockTime expires. */\r
193         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
194         {\r
195                 return ( portBASE_TYPE ) pdTRUE;\r
196         }\r
197         else\r
198         {\r
199                 return ( portBASE_TYPE ) pdFALSE;\r
200         }\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
205 {\r
206 portBASE_TYPE xReturn;\r
207 \r
208         /* There is only one port supported. */\r
209         ( void ) pxPort;\r
210 \r
211         portENTER_CRITICAL();\r
212         {\r
213                 if( uxTxEmpty == pdTRUE )\r
214                 {\r
215                         SBUF = cOutChar;\r
216                         uxTxEmpty = pdFALSE;\r
217                         xReturn = ( portBASE_TYPE ) pdTRUE;\r
218                 }\r
219                 else\r
220                 {\r
221                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
222 \r
223                         if( xReturn == ( portBASE_TYPE ) pdFALSE )\r
224                         {\r
225                                 xReturn = ( portBASE_TYPE ) pdTRUE;\r
226                         }\r
227                 }\r
228         }\r
229         portEXIT_CRITICAL();\r
230 \r
231         return xReturn;\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 void vSerialClose( xComPortHandle xPort )\r
236 {\r
237         /* Not implemented in this port. */\r
238         ( void ) xPort;\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 \r
243 \r
244 \r
245 \r