]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ColdFire_MCF5282_Eclipse/RTOSDemo/serial/serial.c
e28151165586266f0ceda0f1a94e045c881a864d
[freertos] / FreeRTOS / Demo / ColdFire_MCF5282_Eclipse / RTOSDemo / serial / serial.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 \r
66 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.\r
67 \r
68 NOTE:  This driver is primarily to test the scheduler functionality.  It does\r
69 not effectively use the buffers or DMA and is therefore not intended to be\r
70 an example of an efficient driver. */\r
71 \r
72 /* Standard include file. */\r
73 #include <stdlib.h>\r
74 \r
75 /* Scheduler include files. */\r
76 #include "FreeRTOS.h"\r
77 #include "queue.h"\r
78 #include "task.h"\r
79 \r
80 /* Demo app include files. */\r
81 #include "serial.h"\r
82 \r
83 /* Hardware definitions. */\r
84 #define serNO_PARITY            ( ( unsigned portCHAR ) 0x02 << 3 )\r
85 #define ser8DATA_BITS           ( ( unsigned portCHAR ) 0x03 )\r
86 #define ser1STOP_BIT            ( ( unsigned portCHAR ) 0x07 )\r
87 #define serSYSTEM_CLOCK         ( ( unsigned portCHAR ) 0xdd )\r
88 #define serTX_OUTPUT            ( ( unsigned portCHAR ) 0x04 )\r
89 #define serRX_INPUT                     ( ( unsigned portCHAR ) 0x08 )\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 /* The UART interrupt handler. */\r
106 void __attribute__( ( interrupt ) ) __cs3_isr_interrupt_78( void );\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
111 {\r
112 const unsigned portLONG ulBaudRateDivisor = ( configCPU_CLOCK_HZ / ( 32UL * ulWantedBaud ) );\r
113 \r
114         /* Create the queues used by the com test task. */\r
115         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
116         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
117 \r
118         xTxHasEnded = pdTRUE;\r
119 \r
120         /* Set the pins to UART mode. */\r
121         MCF_PAD_PUAPAR |= ( serTX_OUTPUT | serRX_INPUT );\r
122 \r
123         /* Reset the peripheral. */\r
124         MCF_UART1_UCR = MCF_UART_UCR_RESET_RX;\r
125         MCF_UART1_UCR = MCF_UART_UCR_RESET_TX;\r
126         MCF_UART1_UCR = MCF_UART_UCR_RESET_ERROR;\r
127         MCF_UART1_UCR = MCF_UART_UCR_RESET_BKCHGINT;\r
128         MCF_UART1_UCR = MCF_UART_UCR_RESET_MR | MCF_UART_UCR_RX_DISABLED | MCF_UART_UCR_TX_DISABLED;\r
129 \r
130         /* Configure the UART. */\r
131         MCF_UART1_UMR1 = serNO_PARITY | ser8DATA_BITS;\r
132         MCF_UART1_UMR2 = ser1STOP_BIT;\r
133         MCF_UART1_UCSR = serSYSTEM_CLOCK;\r
134 \r
135         MCF_UART1_UBG1 = ( unsigned portCHAR ) ( ( ulBaudRateDivisor >> 8UL ) & 0xffUL );\r
136         MCF_UART1_UBG2 = ( unsigned portCHAR ) ( ulBaudRateDivisor & 0xffUL );\r
137 \r
138         /* Turn it on. */\r
139         MCF_UART1_UCR = serTX_ENABLE | serRX_ENABLE;\r
140 \r
141         /* Configure the interrupt controller.  Run the UARTs above the kernel\r
142         interrupt priority for demo purposes. */\r
143     MCF_INTC0_ICR14 = ( ( configMAX_SYSCALL_INTERRUPT_PRIORITY - 2  ) << 3 );\r
144     MCF_INTC0_IMRL &= ~( MCF_INTC_IMRL_INT_MASK14 | 0x01 );\r
145 \r
146         /* The Tx interrupt is not enabled until there is data to send. */\r
147         MCF_UART1_UIMR = serRX_INT;\r
148 \r
149         /* Only a single port is implemented so we don't need to return anything. */\r
150         return NULL;\r
151 }\r
152 /*-----------------------------------------------------------*/\r
153 \r
154 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
155 {\r
156         /* Only one port is supported. */\r
157         ( void ) pxPort;\r
158 \r
159         /* Get the next character from the buffer.  Return false if no characters\r
160         are available or arrive before xBlockTime expires. */\r
161         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
162         {\r
163                 return pdTRUE;\r
164         }\r
165         else\r
166         {\r
167                 return pdFALSE;\r
168         }\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
173 {\r
174         /* Only one port is supported. */\r
175         ( void ) pxPort;\r
176 \r
177         /* Return false if after the block time there is no room on the Tx queue. */\r
178         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
179         {\r
180                 return pdFAIL;\r
181         }\r
182 \r
183         /* A critical section should not be required as xTxHasEnded will not be\r
184         written to by the ISR if it is already 0 (is this correct?). */\r
185         if( xTxHasEnded != pdFALSE )\r
186         {\r
187                 xTxHasEnded = pdFALSE;\r
188                 MCF_UART1_UIMR = serRX_INT | serTX_INT;\r
189         }\r
190 \r
191         return pdPASS;\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void vSerialClose( xComPortHandle xPort )\r
196 {\r
197         ( void ) xPort;\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 void __cs3_isr_interrupt_78( void )\r
202 {\r
203 unsigned portCHAR ucChar;\r
204 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xDoneSomething = pdTRUE;\r
205 \r
206         while( xDoneSomething != pdFALSE )\r
207         {\r
208                 xDoneSomething = pdFALSE;\r
209 \r
210                 /* Does the tx buffer contain space? */\r
211                 if( ( MCF_UART1_USR & MCF_UART_USR_TXRDY ) != 0x00 )\r
212                 {\r
213                         /* Are there any characters queued to be sent? */\r
214                         if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
215                         {\r
216                                 /* Send the next char. */\r
217                                 MCF_UART1_UTB = ucChar;\r
218                                 xDoneSomething = pdTRUE;\r
219                         }\r
220                         else\r
221                         {\r
222                                 /* Turn off the Tx interrupt until such time as another character\r
223                                 is being transmitted. */\r
224                                 MCF_UART1_UIMR = serRX_INT;\r
225                                 xTxHasEnded = pdTRUE;\r
226                         }\r
227                 }\r
228 \r
229                 if( MCF_UART1_USR & MCF_UART_USR_RXRDY )\r
230                 {\r
231                         ucChar = MCF_UART1_URB;\r
232                         xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );\r
233                         xDoneSomething = pdTRUE;\r
234                 }\r
235         }\r
236 \r
237     portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
238 }\r
239 \r
240 \r
241 \r
242 \r
243 \r
244 \r
245 \r