]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ColdFire_MCF52221_CodeWarrior/sources/serial/serial.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / ColdFire_MCF52221_CodeWarrior / sources / serial / serial.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\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 modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or 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     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 \r
68 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.\r
69 \r
70 NOTE:  This driver is primarily to test the scheduler functionality.  It does\r
71 not effectively use the buffers or DMA and is therefore not intended to be\r
72 an example of an efficient driver. */\r
73 \r
74 /* Standard include file. */\r
75 #include <stdlib.h>\r
76 \r
77 /* Scheduler include files. */\r
78 #include "FreeRTOS.h"\r
79 #include "queue.h"\r
80 #include "task.h"\r
81 \r
82 /* Demo app include files. */\r
83 #include "serial.h"\r
84 \r
85 /* Hardware definitions. */\r
86 #define serNO_PARITY            ( ( unsigned portCHAR ) 0x02 << 3 )\r
87 #define ser8DATA_BITS           ( ( unsigned portCHAR ) 0x03 )\r
88 #define ser1STOP_BIT            ( ( unsigned portCHAR ) 0x07 )\r
89 #define serSYSTEM_CLOCK         ( ( unsigned portCHAR ) 0xdd )\r
90 #define serTX_ENABLE            ( ( unsigned portCHAR ) 0x04 )\r
91 #define serRX_ENABLE            ( ( unsigned portCHAR ) 0x01 )\r
92 #define serTX_INT                       ( ( unsigned portCHAR ) 0x01 )\r
93 #define serRX_INT                       ( ( unsigned portCHAR ) 0x02 )\r
94 \r
95 \r
96 /* The queues used to communicate between tasks and ISR's. */\r
97 static xQueueHandle xRxedChars;\r
98 static xQueueHandle xCharsForTx;\r
99 \r
100 /* Flag used to indicate the tx status. */\r
101 static portBASE_TYPE xTxHasEnded = pdTRUE;\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
106 {\r
107 const unsigned portLONG ulBaudRateDivisor = ( configCPU_CLOCK_HZ / ( 32UL * ulWantedBaud ) );\r
108 \r
109         /* Create the queues used by the com test task. */\r
110         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
111         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
112 \r
113         xTxHasEnded = pdTRUE;\r
114 \r
115         /* Set the pins to UART mode. */\r
116         MCF_GPIO_PUAPAR |= MCF_GPIO_PUAPAR_UTXD0_UTXD0;\r
117         MCF_GPIO_PUAPAR |= MCF_GPIO_PUAPAR_URXD0_URXD0;\r
118 \r
119         /* Reset the peripheral. */\r
120         MCF_UART0_UCR = MCF_UART_UCR_RESET_RX;\r
121         MCF_UART0_UCR = MCF_UART_UCR_RESET_TX;\r
122         MCF_UART0_UCR = MCF_UART_UCR_RESET_ERROR;\r
123         MCF_UART0_UCR = MCF_UART_UCR_RESET_BKCHGINT;\r
124         MCF_UART0_UCR = MCF_UART_UCR_RESET_MR | MCF_UART_UCR_RX_DISABLED | MCF_UART_UCR_TX_DISABLED;\r
125 \r
126         /* Configure the UART. */\r
127         MCF_UART0_UMR1 = serNO_PARITY | ser8DATA_BITS;\r
128         MCF_UART0_UMR2 = ser1STOP_BIT;\r
129         MCF_UART0_UCSR = serSYSTEM_CLOCK;\r
130 \r
131         MCF_UART0_UBG1 = ( unsigned portCHAR ) ( ( ulBaudRateDivisor >> 8UL ) & 0xffUL );\r
132         MCF_UART0_UBG2 = ( unsigned portCHAR ) ( ulBaudRateDivisor & 0xffUL );\r
133 \r
134         /* Turn it on. */\r
135         MCF_UART0_UCR = serTX_ENABLE | serRX_ENABLE;\r
136 \r
137         /* Configure the interrupt controller.  Run the UARTs above the kernel\r
138         interrupt priority for demo purposes. */\r
139     MCF_INTC0_ICR13 = ( ( configMAX_SYSCALL_INTERRUPT_PRIORITY - 1  ) << 3 );\r
140     MCF_INTC0_IMRL &= ~( MCF_INTC_IMRL_INT_MASK13 | 0x01 );\r
141 \r
142         /* The Tx interrupt is not enabled until there is data to send. */\r
143         MCF_UART0_UIMR = serRX_INT;\r
144 \r
145         /* Only a single port is implemented so we don't need to return anything. */\r
146         return NULL;\r
147 }\r
148 /*-----------------------------------------------------------*/\r
149 \r
150 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
151 {\r
152         /* Only one port is supported. */\r
153         ( void ) pxPort;\r
154 \r
155         /* Get the next character from the buffer.  Return false if no characters\r
156         are available or arrive before xBlockTime expires. */\r
157         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
158         {\r
159                 return pdTRUE;\r
160         }\r
161         else\r
162         {\r
163                 return pdFALSE;\r
164         }\r
165 }\r
166 /*-----------------------------------------------------------*/\r
167 \r
168 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
169 {\r
170         /* Only one port is supported. */\r
171         ( void ) pxPort;\r
172 \r
173         /* Return false if after the block time there is no room on the Tx queue. */\r
174         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
175         {\r
176                 return pdFAIL;\r
177         }\r
178 \r
179         /* A critical section should not be required as xTxHasEnded will not be\r
180         written to by the ISR if it is already 0 (is this correct?). */\r
181         if( xTxHasEnded != pdFALSE )\r
182         {\r
183                 xTxHasEnded = pdFALSE;\r
184                 MCF_UART0_UIMR = serRX_INT | serTX_INT;\r
185         }\r
186 \r
187         return pdPASS;\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 void vSerialClose( xComPortHandle xPort )\r
192 {\r
193         ( void ) xPort;\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 __declspec(interrupt:0) void vUART0InterruptHandler( void )\r
198 {\r
199 unsigned portCHAR ucChar;\r
200 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xDoneSomething = pdTRUE;\r
201 \r
202         while( xDoneSomething != pdFALSE )\r
203         {\r
204                 xDoneSomething = pdFALSE;\r
205 \r
206                 /* Does the tx buffer contain space? */\r
207                 if( ( MCF_UART0_USR & MCF_UART_USR_TXRDY ) != 0x00 )\r
208                 {\r
209                         /* Are there any characters queued to be sent? */\r
210                         if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
211                         {\r
212                                 /* Send the next char. */\r
213                                 MCF_UART0_UTB = ucChar;\r
214                                 xDoneSomething = pdTRUE;\r
215                         }\r
216                         else\r
217                         {\r
218                                 /* Turn off the Tx interrupt until such time as another character\r
219                                 is being transmitted. */\r
220                                 MCF_UART0_UIMR = serRX_INT;\r
221                                 xTxHasEnded = pdTRUE;\r
222                         }\r
223                 }\r
224 \r
225                 if( MCF_UART0_USR & MCF_UART_USR_RXRDY )\r
226                 {\r
227                         ucChar = MCF_UART0_URB;\r
228                         xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );\r
229                         xDoneSomething = pdTRUE;\r
230                 }\r
231         }\r
232 \r
233     portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
234 }\r
235 \r
236 \r
237 \r
238 \r
239 \r
240 \r
241 \r