]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Cygnal/serial/serial.c
9cee48d0765b47a6bda75d3f70305ff9a7d98fcf
[freertos] / FreeRTOS / Demo / Cygnal / serial / serial.c
1 /*\r
2     FreeRTOS V9.0.0 - Copyright (C) 2016 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     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR DEMO PURPOSES */\r
72 #include <stdlib.h>\r
73 #include "FreeRTOS.h"\r
74 #include "queue.h"\r
75 #include "task.h"\r
76 #include "serial.h"\r
77 \r
78 /* Constants required to setup the serial control register. */\r
79 #define ser8_BIT_MODE                   ( ( unsigned char ) 0x40 )\r
80 #define serRX_ENABLE                    ( ( unsigned char ) 0x10 )\r
81 \r
82 /* Constants to setup the timer used to generate the baud rate. */\r
83 #define serCLOCK_DIV_48                 ( ( unsigned char ) 0x03 )\r
84 #define serUSE_PRESCALED_CLOCK  ( ( unsigned char ) 0x10 )\r
85 #define ser8BIT_WITH_RELOAD             ( ( unsigned char ) 0x20 )\r
86 #define serSMOD                                 ( ( unsigned char ) 0x10 )\r
87 \r
88 static QueueHandle_t xRxedChars; \r
89 static QueueHandle_t xCharsForTx; \r
90 \r
91 data static unsigned portBASE_TYPE uxTxEmpty;\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
96 {\r
97 unsigned long ulReloadValue;\r
98 const portFLOAT fBaudConst = ( portFLOAT ) configCPU_CLOCK_HZ * ( portFLOAT ) 2.0;\r
99 unsigned char ucOriginalSFRPage;\r
100 \r
101         portENTER_CRITICAL();\r
102         {\r
103                 ucOriginalSFRPage = SFRPAGE;\r
104                 SFRPAGE = 0;\r
105 \r
106                 uxTxEmpty = pdTRUE;\r
107 \r
108                 /* Create the queues used by the com test task. */\r
109                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
110                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
111         \r
112                 /* Calculate the baud rate to use timer 1. */\r
113                 ulReloadValue = ( unsigned long ) ( ( ( portFLOAT ) 256 - ( fBaudConst / ( portFLOAT ) ( 32 * ulWantedBaud ) ) ) + ( portFLOAT ) 0.5 );\r
114 \r
115                 /* Set timer one for desired mode of operation. */\r
116                 TMOD &= 0x08;\r
117                 TMOD |= ser8BIT_WITH_RELOAD;\r
118                 SSTA0 |= serSMOD;\r
119 \r
120                 /* Set the reload and start values for the time. */\r
121                 TL1 = ( unsigned char ) ulReloadValue;\r
122                 TH1 = ( unsigned char ) ulReloadValue;\r
123 \r
124                 /* Setup the control register for standard n, 8, 1 - variable baud rate. */\r
125                 SCON = ser8_BIT_MODE | serRX_ENABLE;\r
126 \r
127                 /* Enable the serial port interrupts */\r
128                 ES = 1;\r
129 \r
130                 /* Start the timer. */\r
131                 TR1 = 1;\r
132 \r
133                 SFRPAGE = ucOriginalSFRPage;\r
134         }\r
135         portEXIT_CRITICAL();\r
136         \r
137         /* Unlike some ports, this serial code does not allow for more than one\r
138         com port.  We therefore don't return a pointer to a port structure and can\r
139         instead just return NULL. */\r
140         return NULL;\r
141 }\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 void vSerialISR( void ) interrupt 4\r
145 {\r
146 char cChar;\r
147 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
148 \r
149         /* 8051 port interrupt routines MUST be placed within a critical section\r
150         if taskYIELD() is used within the ISR! */\r
151 \r
152         portENTER_CRITICAL();\r
153         {\r
154                 if( RI ) \r
155                 {\r
156                         /* Get the character and post it on the queue of Rxed characters.\r
157                         If the post causes a task to wake force a context switch if the woken task\r
158                         has a higher priority than the task we have interrupted. */\r
159                         cChar = SBUF;\r
160                         RI = 0;\r
161 \r
162                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
163                 }\r
164 \r
165                 if( TI ) \r
166                 {\r
167                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == ( portBASE_TYPE ) pdTRUE )\r
168                         {\r
169                                 /* Send the next character queued for Tx. */\r
170                                 SBUF = cChar;\r
171                         }\r
172                         else\r
173                         {\r
174                                 /* Queue empty, nothing to send. */\r
175                                 uxTxEmpty = pdTRUE;\r
176                         }\r
177 \r
178                         TI = 0;\r
179                 }\r
180         \r
181                 if( xHigherPriorityTaskWoken )\r
182                 {\r
183                         portYIELD();\r
184                 }\r
185         }\r
186         portEXIT_CRITICAL();\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
191 {\r
192         /* There is only one port supported. */\r
193         ( void ) pxPort;\r
194 \r
195         /* Get the next character from the buffer.  Return false if no characters\r
196         are available, or arrive before xBlockTime expires. */\r
197         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
198         {\r
199                 return ( portBASE_TYPE ) pdTRUE;\r
200         }\r
201         else\r
202         {\r
203                 return ( portBASE_TYPE ) pdFALSE;\r
204         }\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
209 {\r
210 portBASE_TYPE xReturn;\r
211 \r
212         /* There is only one port supported. */\r
213         ( void ) pxPort;\r
214 \r
215         portENTER_CRITICAL();\r
216         {\r
217                 if( uxTxEmpty == pdTRUE )\r
218                 {\r
219                         SBUF = cOutChar;\r
220                         uxTxEmpty = pdFALSE;\r
221                         xReturn = ( portBASE_TYPE ) pdTRUE;\r
222                 }\r
223                 else\r
224                 {\r
225                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
226 \r
227                         if( xReturn == ( portBASE_TYPE ) pdFALSE )\r
228                         {\r
229                                 xReturn = ( portBASE_TYPE ) pdTRUE;\r
230                         }\r
231                 }\r
232         }\r
233         portEXIT_CRITICAL();\r
234 \r
235         return xReturn;\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 void vSerialClose( xComPortHandle xPort )\r
240 {\r
241         /* Not implemented in this port. */\r
242         ( void ) xPort;\r
243 }\r
244 /*-----------------------------------------------------------*/\r
245 \r
246 \r
247 \r
248 \r
249 \r