]> git.sur5r.net Git - freertos/blob - Demo/Cygnal/serial/serial.c
First version under SVN is V4.0.1
[freertos] / Demo / Cygnal / serial / serial.c
1 /*\r
2         FreeRTOS V4.0.1 - Copyright (C) 2003-2006 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS 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 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; 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, 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 */\r
32 \r
33 \r
34 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR DEMO PURPOSES */\r
35 #include <stdlib.h>\r
36 #include "FreeRTOS.h"\r
37 #include "queue.h"\r
38 #include "task.h"\r
39 #include "serial.h"\r
40 \r
41 /* Constants required to setup the serial control register. */\r
42 #define ser8_BIT_MODE                   ( ( unsigned portCHAR ) 0x40 )\r
43 #define serRX_ENABLE                    ( ( unsigned portCHAR ) 0x10 )\r
44 \r
45 /* Constants to setup the timer used to generate the baud rate. */\r
46 #define serCLOCK_DIV_48                 ( ( unsigned portCHAR ) 0x03 )\r
47 #define serUSE_PRESCALED_CLOCK  ( ( unsigned portCHAR ) 0x10 )\r
48 #define ser8BIT_WITH_RELOAD             ( ( unsigned portCHAR ) 0x20 )\r
49 #define serSMOD                                 ( ( unsigned portCHAR ) 0x10 )\r
50 \r
51 static xQueueHandle xRxedChars; \r
52 static xQueueHandle xCharsForTx; \r
53 \r
54 data static unsigned portBASE_TYPE uxTxEmpty;\r
55 \r
56 /*-----------------------------------------------------------*/\r
57 \r
58 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
59 {\r
60 unsigned portLONG ulReloadValue;\r
61 const portFLOAT fBaudConst = ( portFLOAT ) configCPU_CLOCK_HZ * ( portFLOAT ) 2.0;\r
62 unsigned portCHAR ucOriginalSFRPage;\r
63 \r
64         portENTER_CRITICAL();\r
65         {\r
66                 ucOriginalSFRPage = SFRPAGE;\r
67                 SFRPAGE = 0;\r
68 \r
69                 uxTxEmpty = pdTRUE;\r
70 \r
71                 /* Create the queues used by the com test task. */\r
72                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );\r
73                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );\r
74         \r
75                 /* Calculate the baud rate to use timer 1. */\r
76                 ulReloadValue = ( unsigned portLONG ) ( ( ( portFLOAT ) 256 - ( fBaudConst / ( portFLOAT ) ( 32 * ulWantedBaud ) ) ) + ( portFLOAT ) 0.5 );\r
77 \r
78                 /* Set timer one for desired mode of operation. */\r
79                 TMOD &= 0x08;\r
80                 TMOD |= ser8BIT_WITH_RELOAD;\r
81                 SSTA0 |= serSMOD;\r
82 \r
83                 /* Set the reload and start values for the time. */\r
84                 TL1 = ( unsigned portCHAR ) ulReloadValue;\r
85                 TH1 = ( unsigned portCHAR ) ulReloadValue;\r
86 \r
87                 /* Setup the control register for standard n, 8, 1 - variable baud rate. */\r
88                 SCON = ser8_BIT_MODE | serRX_ENABLE;\r
89 \r
90                 /* Enable the serial port interrupts */\r
91                 ES = 1;\r
92 \r
93                 /* Start the timer. */\r
94                 TR1 = 1;\r
95 \r
96                 SFRPAGE = ucOriginalSFRPage;\r
97         }\r
98         portEXIT_CRITICAL();\r
99         \r
100         /* Unlike some ports, this serial code does not allow for more than one\r
101         com port.  We therefore don't return a pointer to a port structure and can\r
102         instead just return NULL. */\r
103         return NULL;\r
104 }\r
105 /*-----------------------------------------------------------*/\r
106 \r
107 void vSerialISR( void ) interrupt 4\r
108 {\r
109 portCHAR cChar;\r
110 portBASE_TYPE xTaskWokenByRx = pdFALSE, xTaskWokenByTx = pdFALSE;\r
111 \r
112         /* 8051 port interrupt routines MUST be placed within a critical section\r
113         if taskYIELD() is used within the ISR! */\r
114 \r
115         portENTER_CRITICAL();\r
116         {\r
117                 if( RI ) \r
118                 {\r
119                         /* Get the character and post it on the queue of Rxed characters.\r
120                         If the post causes a task to wake force a context switch as the woken task\r
121                         may have a higher priority than the task we have interrupted. */\r
122                         cChar = SBUF;\r
123                         RI = 0;\r
124 \r
125                         if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )\r
126                         {\r
127                                 xTaskWokenByRx = ( portBASE_TYPE ) pdTRUE;\r
128                         }\r
129                 }\r
130 \r
131                 if( TI ) \r
132                 {\r
133                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWokenByTx ) == ( portBASE_TYPE ) pdTRUE )\r
134                         {\r
135                                 /* Send the next character queued for Tx. */\r
136                                 SBUF = cChar;\r
137                         }\r
138                         else\r
139                         {\r
140                                 /* Queue empty, nothing to send. */\r
141                                 uxTxEmpty = pdTRUE;\r
142                         }\r
143 \r
144                         TI = 0;\r
145                 }\r
146         \r
147                 if( xTaskWokenByRx || xTaskWokenByTx )\r
148                 {\r
149                         portYIELD();\r
150                 }\r
151         }\r
152         portEXIT_CRITICAL();\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
157 {\r
158         /* There is only one port supported. */\r
159         ( void ) pxPort;\r
160 \r
161         /* Get the next character from the buffer.  Return false if no characters\r
162         are available, or arrive before xBlockTime expires. */\r
163         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
164         {\r
165                 return ( portBASE_TYPE ) pdTRUE;\r
166         }\r
167         else\r
168         {\r
169                 return ( portBASE_TYPE ) pdFALSE;\r
170         }\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
175 {\r
176 portBASE_TYPE xReturn;\r
177 \r
178         /* There is only one port supported. */\r
179         ( void ) pxPort;\r
180 \r
181         portENTER_CRITICAL();\r
182         {\r
183                 if( uxTxEmpty == pdTRUE )\r
184                 {\r
185                         SBUF = cOutChar;\r
186                         uxTxEmpty = pdFALSE;\r
187                         xReturn = ( portBASE_TYPE ) pdTRUE;\r
188                 }\r
189                 else\r
190                 {\r
191                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
192 \r
193                         if( xReturn == ( portBASE_TYPE ) pdFALSE )\r
194                         {\r
195                                 xReturn = ( portBASE_TYPE ) pdTRUE;\r
196                         }\r
197                 }\r
198         }\r
199         portEXIT_CRITICAL();\r
200 \r
201         return xReturn;\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 void vSerialClose( xComPortHandle xPort )\r
206 {\r
207         /* Not implemented in this port. */\r
208         ( void ) xPort;\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 \r
213 \r
214 \r
215 \r