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