]> git.sur5r.net Git - freertos/blob - Demo/Cygnal/serial/serial.c
Update Cortex M3 ports to ensure 8 byte alignment.
[freertos] / Demo / Cygnal / serial / serial.c
1 /*\r
2     FreeRTOS V6.0.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 \r
55 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR DEMO PURPOSES */\r
56 #include <stdlib.h>\r
57 #include "FreeRTOS.h"\r
58 #include "queue.h"\r
59 #include "task.h"\r
60 #include "serial.h"\r
61 \r
62 /* Constants required to setup the serial control register. */\r
63 #define ser8_BIT_MODE                   ( ( unsigned char ) 0x40 )\r
64 #define serRX_ENABLE                    ( ( unsigned char ) 0x10 )\r
65 \r
66 /* Constants to setup the timer used to generate the baud rate. */\r
67 #define serCLOCK_DIV_48                 ( ( unsigned char ) 0x03 )\r
68 #define serUSE_PRESCALED_CLOCK  ( ( unsigned char ) 0x10 )\r
69 #define ser8BIT_WITH_RELOAD             ( ( unsigned char ) 0x20 )\r
70 #define serSMOD                                 ( ( unsigned char ) 0x10 )\r
71 \r
72 static xQueueHandle xRxedChars; \r
73 static xQueueHandle xCharsForTx; \r
74 \r
75 data static unsigned portBASE_TYPE uxTxEmpty;\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
80 {\r
81 unsigned long ulReloadValue;\r
82 const portFLOAT fBaudConst = ( portFLOAT ) configCPU_CLOCK_HZ * ( portFLOAT ) 2.0;\r
83 unsigned char ucOriginalSFRPage;\r
84 \r
85         portENTER_CRITICAL();\r
86         {\r
87                 ucOriginalSFRPage = SFRPAGE;\r
88                 SFRPAGE = 0;\r
89 \r
90                 uxTxEmpty = pdTRUE;\r
91 \r
92                 /* Create the queues used by the com test task. */\r
93                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
94                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
95         \r
96                 /* Calculate the baud rate to use timer 1. */\r
97                 ulReloadValue = ( unsigned long ) ( ( ( portFLOAT ) 256 - ( fBaudConst / ( portFLOAT ) ( 32 * ulWantedBaud ) ) ) + ( portFLOAT ) 0.5 );\r
98 \r
99                 /* Set timer one for desired mode of operation. */\r
100                 TMOD &= 0x08;\r
101                 TMOD |= ser8BIT_WITH_RELOAD;\r
102                 SSTA0 |= serSMOD;\r
103 \r
104                 /* Set the reload and start values for the time. */\r
105                 TL1 = ( unsigned char ) ulReloadValue;\r
106                 TH1 = ( unsigned char ) ulReloadValue;\r
107 \r
108                 /* Setup the control register for standard n, 8, 1 - variable baud rate. */\r
109                 SCON = ser8_BIT_MODE | serRX_ENABLE;\r
110 \r
111                 /* Enable the serial port interrupts */\r
112                 ES = 1;\r
113 \r
114                 /* Start the timer. */\r
115                 TR1 = 1;\r
116 \r
117                 SFRPAGE = ucOriginalSFRPage;\r
118         }\r
119         portEXIT_CRITICAL();\r
120         \r
121         /* Unlike some ports, this serial code does not allow for more than one\r
122         com port.  We therefore don't return a pointer to a port structure and can\r
123         instead just return NULL. */\r
124         return NULL;\r
125 }\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 void vSerialISR( void ) interrupt 4\r
129 {\r
130 char cChar;\r
131 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
132 \r
133         /* 8051 port interrupt routines MUST be placed within a critical section\r
134         if taskYIELD() is used within the ISR! */\r
135 \r
136         portENTER_CRITICAL();\r
137         {\r
138                 if( RI ) \r
139                 {\r
140                         /* Get the character and post it on the queue of Rxed characters.\r
141                         If the post causes a task to wake force a context switch if the woken task\r
142                         has a higher priority than the task we have interrupted. */\r
143                         cChar = SBUF;\r
144                         RI = 0;\r
145 \r
146                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
147                 }\r
148 \r
149                 if( TI ) \r
150                 {\r
151                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == ( portBASE_TYPE ) pdTRUE )\r
152                         {\r
153                                 /* Send the next character queued for Tx. */\r
154                                 SBUF = cChar;\r
155                         }\r
156                         else\r
157                         {\r
158                                 /* Queue empty, nothing to send. */\r
159                                 uxTxEmpty = pdTRUE;\r
160                         }\r
161 \r
162                         TI = 0;\r
163                 }\r
164         \r
165                 if( xHigherPriorityTaskWoken )\r
166                 {\r
167                         portYIELD();\r
168                 }\r
169         }\r
170         portEXIT_CRITICAL();\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
175 {\r
176         /* There is only one port supported. */\r
177         ( void ) pxPort;\r
178 \r
179         /* Get the next character from the buffer.  Return false if no characters\r
180         are available, or arrive before xBlockTime expires. */\r
181         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
182         {\r
183                 return ( portBASE_TYPE ) pdTRUE;\r
184         }\r
185         else\r
186         {\r
187                 return ( portBASE_TYPE ) pdFALSE;\r
188         }\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
193 {\r
194 portBASE_TYPE xReturn;\r
195 \r
196         /* There is only one port supported. */\r
197         ( void ) pxPort;\r
198 \r
199         portENTER_CRITICAL();\r
200         {\r
201                 if( uxTxEmpty == pdTRUE )\r
202                 {\r
203                         SBUF = cOutChar;\r
204                         uxTxEmpty = pdFALSE;\r
205                         xReturn = ( portBASE_TYPE ) pdTRUE;\r
206                 }\r
207                 else\r
208                 {\r
209                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
210 \r
211                         if( xReturn == ( portBASE_TYPE ) pdFALSE )\r
212                         {\r
213                                 xReturn = ( portBASE_TYPE ) pdTRUE;\r
214                         }\r
215                 }\r
216         }\r
217         portEXIT_CRITICAL();\r
218 \r
219         return xReturn;\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 void vSerialClose( xComPortHandle xPort )\r
224 {\r
225         /* Not implemented in this port. */\r
226         ( void ) xPort;\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 \r
231 \r
232 \r
233 \r