]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ColdFire_MCF5282_Eclipse/RTOSDemo/serial/serial.c
506e736b2e1167b3ee9e97700cef4c62cf0ae9b2
[freertos] / FreeRTOS / Demo / ColdFire_MCF5282_Eclipse / RTOSDemo / serial / serial.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 \r
97 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.\r
98 \r
99 NOTE:  This driver is primarily to test the scheduler functionality.  It does\r
100 not effectively use the buffers or DMA and is therefore not intended to be\r
101 an example of an efficient driver. */\r
102 \r
103 /* Standard include file. */\r
104 #include <stdlib.h>\r
105 \r
106 /* Scheduler include files. */\r
107 #include "FreeRTOS.h"\r
108 #include "queue.h"\r
109 #include "task.h"\r
110 \r
111 /* Demo app include files. */\r
112 #include "serial.h"\r
113 \r
114 /* Hardware definitions. */\r
115 #define serNO_PARITY            ( ( unsigned char ) 0x02 << 3 )\r
116 #define ser8DATA_BITS           ( ( unsigned char ) 0x03 )\r
117 #define ser1STOP_BIT            ( ( unsigned char ) 0x07 )\r
118 #define serSYSTEM_CLOCK         ( ( unsigned char ) 0xdd )\r
119 #define serTX_OUTPUT            ( ( unsigned char ) 0x04 )\r
120 #define serRX_INPUT                     ( ( unsigned char ) 0x08 )\r
121 #define serTX_ENABLE            ( ( unsigned char ) 0x04 )\r
122 #define serRX_ENABLE            ( ( unsigned char ) 0x01 )\r
123 #define serTX_INT                       ( ( unsigned char ) 0x01 )\r
124 #define serRX_INT                       ( ( unsigned char ) 0x02 )\r
125 \r
126 \r
127 /* The queues used to communicate between tasks and ISR's. */\r
128 static QueueHandle_t xRxedChars;\r
129 static QueueHandle_t xCharsForTx;\r
130 \r
131 /* Flag used to indicate the tx status. */\r
132 static portBASE_TYPE xTxHasEnded = pdTRUE;\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 /* The UART interrupt handler. */\r
137 void __attribute__( ( interrupt ) ) __cs3_isr_interrupt_78( void );\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
142 {\r
143 const unsigned long ulBaudRateDivisor = ( configCPU_CLOCK_HZ / ( 32UL * ulWantedBaud ) );\r
144 \r
145         /* Create the queues used by the com test task. */\r
146         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
147         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
148 \r
149         xTxHasEnded = pdTRUE;\r
150 \r
151         /* Set the pins to UART mode. */\r
152         MCF_PAD_PUAPAR |= ( serTX_OUTPUT | serRX_INPUT );\r
153 \r
154         /* Reset the peripheral. */\r
155         MCF_UART1_UCR = MCF_UART_UCR_RESET_RX;\r
156         MCF_UART1_UCR = MCF_UART_UCR_RESET_TX;\r
157         MCF_UART1_UCR = MCF_UART_UCR_RESET_ERROR;\r
158         MCF_UART1_UCR = MCF_UART_UCR_RESET_BKCHGINT;\r
159         MCF_UART1_UCR = MCF_UART_UCR_RESET_MR | MCF_UART_UCR_RX_DISABLED | MCF_UART_UCR_TX_DISABLED;\r
160 \r
161         /* Configure the UART. */\r
162         MCF_UART1_UMR1 = serNO_PARITY | ser8DATA_BITS;\r
163         MCF_UART1_UMR2 = ser1STOP_BIT;\r
164         MCF_UART1_UCSR = serSYSTEM_CLOCK;\r
165 \r
166         MCF_UART1_UBG1 = ( unsigned char ) ( ( ulBaudRateDivisor >> 8UL ) & 0xffUL );\r
167         MCF_UART1_UBG2 = ( unsigned char ) ( ulBaudRateDivisor & 0xffUL );\r
168 \r
169         /* Turn it on. */\r
170         MCF_UART1_UCR = serTX_ENABLE | serRX_ENABLE;\r
171 \r
172         /* Configure the interrupt controller.  Run the UARTs above the kernel\r
173         interrupt priority for demo purposes. */\r
174     MCF_INTC0_ICR14 = ( ( configMAX_SYSCALL_INTERRUPT_PRIORITY - 2  ) << 3 );\r
175     MCF_INTC0_IMRL &= ~( MCF_INTC_IMRL_INT_MASK14 | 0x01 );\r
176 \r
177         /* The Tx interrupt is not enabled until there is data to send. */\r
178         MCF_UART1_UIMR = serRX_INT;\r
179 \r
180         /* Only a single port is implemented so we don't need to return anything. */\r
181         return NULL;\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
186 {\r
187         /* Only one port is supported. */\r
188         ( void ) pxPort;\r
189 \r
190         /* Get the next character from the buffer.  Return false if no characters\r
191         are available or arrive before xBlockTime expires. */\r
192         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
193         {\r
194                 return pdTRUE;\r
195         }\r
196         else\r
197         {\r
198                 return pdFALSE;\r
199         }\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
204 {\r
205         /* Only one port is supported. */\r
206         ( void ) pxPort;\r
207 \r
208         /* Return false if after the block time there is no room on the Tx queue. */\r
209         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
210         {\r
211                 return pdFAIL;\r
212         }\r
213 \r
214         /* A critical section should not be required as xTxHasEnded will not be\r
215         written to by the ISR if it is already 0 (is this correct?). */\r
216         if( xTxHasEnded != pdFALSE )\r
217         {\r
218                 xTxHasEnded = pdFALSE;\r
219                 MCF_UART1_UIMR = serRX_INT | serTX_INT;\r
220         }\r
221 \r
222         return pdPASS;\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 void vSerialClose( xComPortHandle xPort )\r
227 {\r
228         ( void ) xPort;\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 void __cs3_isr_interrupt_78( void )\r
233 {\r
234 unsigned char ucChar;\r
235 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xDoneSomething = pdTRUE;\r
236 \r
237         while( xDoneSomething != pdFALSE )\r
238         {\r
239                 xDoneSomething = pdFALSE;\r
240 \r
241                 /* Does the tx buffer contain space? */\r
242                 if( ( MCF_UART1_USR & MCF_UART_USR_TXRDY ) != 0x00 )\r
243                 {\r
244                         /* Are there any characters queued to be sent? */\r
245                         if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
246                         {\r
247                                 /* Send the next char. */\r
248                                 MCF_UART1_UTB = ucChar;\r
249                                 xDoneSomething = pdTRUE;\r
250                         }\r
251                         else\r
252                         {\r
253                                 /* Turn off the Tx interrupt until such time as another character\r
254                                 is being transmitted. */\r
255                                 MCF_UART1_UIMR = serRX_INT;\r
256                                 xTxHasEnded = pdTRUE;\r
257                         }\r
258                 }\r
259 \r
260                 if( MCF_UART1_USR & MCF_UART_USR_RXRDY )\r
261                 {\r
262                         ucChar = MCF_UART1_URB;\r
263                         xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );\r
264                         xDoneSomething = pdTRUE;\r
265                 }\r
266         }\r
267 \r
268     portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
269 }\r
270 \r
271 \r
272 \r
273 \r
274 \r
275 \r
276 \r