]> git.sur5r.net Git - freertos/blob - Demo/Cygnal/serial/serial.c
c0634eef4a8f79cbf3aea2315ebc77acd38774ed
[freertos] / Demo / Cygnal / serial / serial.c
1 /*\r
2         FreeRTOS.org V4.4.0 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 \r
37 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR DEMO PURPOSES */\r
38 #include <stdlib.h>\r
39 #include "FreeRTOS.h"\r
40 #include "queue.h"\r
41 #include "task.h"\r
42 #include "serial.h"\r
43 \r
44 /* Constants required to setup the serial control register. */\r
45 #define ser8_BIT_MODE                   ( ( unsigned portCHAR ) 0x40 )\r
46 #define serRX_ENABLE                    ( ( unsigned portCHAR ) 0x10 )\r
47 \r
48 /* Constants to setup the timer used to generate the baud rate. */\r
49 #define serCLOCK_DIV_48                 ( ( unsigned portCHAR ) 0x03 )\r
50 #define serUSE_PRESCALED_CLOCK  ( ( unsigned portCHAR ) 0x10 )\r
51 #define ser8BIT_WITH_RELOAD             ( ( unsigned portCHAR ) 0x20 )\r
52 #define serSMOD                                 ( ( unsigned portCHAR ) 0x10 )\r
53 \r
54 static xQueueHandle xRxedChars; \r
55 static xQueueHandle xCharsForTx; \r
56 \r
57 data static unsigned portBASE_TYPE uxTxEmpty;\r
58 \r
59 /*-----------------------------------------------------------*/\r
60 \r
61 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
62 {\r
63 unsigned portLONG ulReloadValue;\r
64 const portFLOAT fBaudConst = ( portFLOAT ) configCPU_CLOCK_HZ * ( portFLOAT ) 2.0;\r
65 unsigned portCHAR ucOriginalSFRPage;\r
66 \r
67         portENTER_CRITICAL();\r
68         {\r
69                 ucOriginalSFRPage = SFRPAGE;\r
70                 SFRPAGE = 0;\r
71 \r
72                 uxTxEmpty = pdTRUE;\r
73 \r
74                 /* Create the queues used by the com test task. */\r
75                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );\r
76                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );\r
77         \r
78                 /* Calculate the baud rate to use timer 1. */\r
79                 ulReloadValue = ( unsigned portLONG ) ( ( ( portFLOAT ) 256 - ( fBaudConst / ( portFLOAT ) ( 32 * ulWantedBaud ) ) ) + ( portFLOAT ) 0.5 );\r
80 \r
81                 /* Set timer one for desired mode of operation. */\r
82                 TMOD &= 0x08;\r
83                 TMOD |= ser8BIT_WITH_RELOAD;\r
84                 SSTA0 |= serSMOD;\r
85 \r
86                 /* Set the reload and start values for the time. */\r
87                 TL1 = ( unsigned portCHAR ) ulReloadValue;\r
88                 TH1 = ( unsigned portCHAR ) ulReloadValue;\r
89 \r
90                 /* Setup the control register for standard n, 8, 1 - variable baud rate. */\r
91                 SCON = ser8_BIT_MODE | serRX_ENABLE;\r
92 \r
93                 /* Enable the serial port interrupts */\r
94                 ES = 1;\r
95 \r
96                 /* Start the timer. */\r
97                 TR1 = 1;\r
98 \r
99                 SFRPAGE = ucOriginalSFRPage;\r
100         }\r
101         portEXIT_CRITICAL();\r
102         \r
103         /* Unlike some ports, this serial code does not allow for more than one\r
104         com port.  We therefore don't return a pointer to a port structure and can\r
105         instead just return NULL. */\r
106         return NULL;\r
107 }\r
108 /*-----------------------------------------------------------*/\r
109 \r
110 void vSerialISR( void ) interrupt 4\r
111 {\r
112 portCHAR cChar;\r
113 portBASE_TYPE xTaskWokenByRx = pdFALSE, xTaskWokenByTx = pdFALSE;\r
114 \r
115         /* 8051 port interrupt routines MUST be placed within a critical section\r
116         if taskYIELD() is used within the ISR! */\r
117 \r
118         portENTER_CRITICAL();\r
119         {\r
120                 if( RI ) \r
121                 {\r
122                         /* Get the character and post it on the queue of Rxed characters.\r
123                         If the post causes a task to wake force a context switch as the woken task\r
124                         may have a higher priority than the task we have interrupted. */\r
125                         cChar = SBUF;\r
126                         RI = 0;\r
127 \r
128                         if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )\r
129                         {\r
130                                 xTaskWokenByRx = ( portBASE_TYPE ) pdTRUE;\r
131                         }\r
132                 }\r
133 \r
134                 if( TI ) \r
135                 {\r
136                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWokenByTx ) == ( portBASE_TYPE ) pdTRUE )\r
137                         {\r
138                                 /* Send the next character queued for Tx. */\r
139                                 SBUF = cChar;\r
140                         }\r
141                         else\r
142                         {\r
143                                 /* Queue empty, nothing to send. */\r
144                                 uxTxEmpty = pdTRUE;\r
145                         }\r
146 \r
147                         TI = 0;\r
148                 }\r
149         \r
150                 if( xTaskWokenByRx || xTaskWokenByTx )\r
151                 {\r
152                         portYIELD();\r
153                 }\r
154         }\r
155         portEXIT_CRITICAL();\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
160 {\r
161         /* There is only one port supported. */\r
162         ( void ) pxPort;\r
163 \r
164         /* Get the next character from the buffer.  Return false if no characters\r
165         are available, or arrive before xBlockTime expires. */\r
166         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
167         {\r
168                 return ( portBASE_TYPE ) pdTRUE;\r
169         }\r
170         else\r
171         {\r
172                 return ( portBASE_TYPE ) pdFALSE;\r
173         }\r
174 }\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
178 {\r
179 portBASE_TYPE xReturn;\r
180 \r
181         /* There is only one port supported. */\r
182         ( void ) pxPort;\r
183 \r
184         portENTER_CRITICAL();\r
185         {\r
186                 if( uxTxEmpty == pdTRUE )\r
187                 {\r
188                         SBUF = cOutChar;\r
189                         uxTxEmpty = pdFALSE;\r
190                         xReturn = ( portBASE_TYPE ) pdTRUE;\r
191                 }\r
192                 else\r
193                 {\r
194                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
195 \r
196                         if( xReturn == ( portBASE_TYPE ) pdFALSE )\r
197                         {\r
198                                 xReturn = ( portBASE_TYPE ) pdTRUE;\r
199                         }\r
200                 }\r
201         }\r
202         portEXIT_CRITICAL();\r
203 \r
204         return xReturn;\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 void vSerialClose( xComPortHandle xPort )\r
209 {\r
210         /* Not implemented in this port. */\r
211         ( void ) xPort;\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 \r
216 \r
217 \r
218 \r