]> git.sur5r.net Git - freertos/blob - Demo/ColdFire_MCF5282_Eclipse/RTOSDemo/serial/serial.c
Add STM32F0 demo.
[freertos] / Demo / ColdFire_MCF5282_Eclipse / RTOSDemo / serial / serial.c
1 /*\r
2     FreeRTOS V7.1.0 - Copyright (C) 2011 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     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.\r
56 \r
57 NOTE:  This driver is primarily to test the scheduler functionality.  It does\r
58 not effectively use the buffers or DMA and is therefore not intended to be\r
59 an example of an efficient driver. */\r
60 \r
61 /* Standard include file. */\r
62 #include <stdlib.h>\r
63 \r
64 /* Scheduler include files. */\r
65 #include "FreeRTOS.h"\r
66 #include "queue.h"\r
67 #include "task.h"\r
68 \r
69 /* Demo app include files. */\r
70 #include "serial.h"\r
71 \r
72 /* Hardware definitions. */\r
73 #define serNO_PARITY            ( ( unsigned portCHAR ) 0x02 << 3 )\r
74 #define ser8DATA_BITS           ( ( unsigned portCHAR ) 0x03 )\r
75 #define ser1STOP_BIT            ( ( unsigned portCHAR ) 0x07 )\r
76 #define serSYSTEM_CLOCK         ( ( unsigned portCHAR ) 0xdd )\r
77 #define serTX_OUTPUT            ( ( unsigned portCHAR ) 0x04 )\r
78 #define serRX_INPUT                     ( ( unsigned portCHAR ) 0x08 )\r
79 #define serTX_ENABLE            ( ( unsigned portCHAR ) 0x04 )\r
80 #define serRX_ENABLE            ( ( unsigned portCHAR ) 0x01 )\r
81 #define serTX_INT                       ( ( unsigned portCHAR ) 0x01 )\r
82 #define serRX_INT                       ( ( unsigned portCHAR ) 0x02 )\r
83 \r
84 \r
85 /* The queues used to communicate between tasks and ISR's. */\r
86 static xQueueHandle xRxedChars;\r
87 static xQueueHandle xCharsForTx;\r
88 \r
89 /* Flag used to indicate the tx status. */\r
90 static portBASE_TYPE xTxHasEnded = pdTRUE;\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /* The UART interrupt handler. */\r
95 void __attribute__( ( interrupt ) ) __cs3_isr_interrupt_78( void );\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
100 {\r
101 const unsigned portLONG ulBaudRateDivisor = ( configCPU_CLOCK_HZ / ( 32UL * ulWantedBaud ) );\r
102 \r
103         /* Create the queues used by the com test task. */\r
104         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
105         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
106 \r
107         xTxHasEnded = pdTRUE;\r
108 \r
109         /* Set the pins to UART mode. */\r
110         MCF_PAD_PUAPAR |= ( serTX_OUTPUT | serRX_INPUT );\r
111 \r
112         /* Reset the peripheral. */\r
113         MCF_UART1_UCR = MCF_UART_UCR_RESET_RX;\r
114         MCF_UART1_UCR = MCF_UART_UCR_RESET_TX;\r
115         MCF_UART1_UCR = MCF_UART_UCR_RESET_ERROR;\r
116         MCF_UART1_UCR = MCF_UART_UCR_RESET_BKCHGINT;\r
117         MCF_UART1_UCR = MCF_UART_UCR_RESET_MR | MCF_UART_UCR_RX_DISABLED | MCF_UART_UCR_TX_DISABLED;\r
118 \r
119         /* Configure the UART. */\r
120         MCF_UART1_UMR1 = serNO_PARITY | ser8DATA_BITS;\r
121         MCF_UART1_UMR2 = ser1STOP_BIT;\r
122         MCF_UART1_UCSR = serSYSTEM_CLOCK;\r
123 \r
124         MCF_UART1_UBG1 = ( unsigned portCHAR ) ( ( ulBaudRateDivisor >> 8UL ) & 0xffUL );\r
125         MCF_UART1_UBG2 = ( unsigned portCHAR ) ( ulBaudRateDivisor & 0xffUL );\r
126 \r
127         /* Turn it on. */\r
128         MCF_UART1_UCR = serTX_ENABLE | serRX_ENABLE;\r
129 \r
130         /* Configure the interrupt controller.  Run the UARTs above the kernel\r
131         interrupt priority for demo purposes. */\r
132     MCF_INTC0_ICR14 = ( ( configMAX_SYSCALL_INTERRUPT_PRIORITY - 2  ) << 3 );\r
133     MCF_INTC0_IMRL &= ~( MCF_INTC_IMRL_INT_MASK14 | 0x01 );\r
134 \r
135         /* The Tx interrupt is not enabled until there is data to send. */\r
136         MCF_UART1_UIMR = serRX_INT;\r
137 \r
138         /* Only a single port is implemented so we don't need to return anything. */\r
139         return NULL;\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
144 {\r
145         /* Only one port is supported. */\r
146         ( void ) pxPort;\r
147 \r
148         /* Get the next character from the buffer.  Return false if no characters\r
149         are available or arrive before xBlockTime expires. */\r
150         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
151         {\r
152                 return pdTRUE;\r
153         }\r
154         else\r
155         {\r
156                 return pdFALSE;\r
157         }\r
158 }\r
159 /*-----------------------------------------------------------*/\r
160 \r
161 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
162 {\r
163         /* Only one port is supported. */\r
164         ( void ) pxPort;\r
165 \r
166         /* Return false if after the block time there is no room on the Tx queue. */\r
167         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
168         {\r
169                 return pdFAIL;\r
170         }\r
171 \r
172         /* A critical section should not be required as xTxHasEnded will not be\r
173         written to by the ISR if it is already 0 (is this correct?). */\r
174         if( xTxHasEnded != pdFALSE )\r
175         {\r
176                 xTxHasEnded = pdFALSE;\r
177                 MCF_UART1_UIMR = serRX_INT | serTX_INT;\r
178         }\r
179 \r
180         return pdPASS;\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 void vSerialClose( xComPortHandle xPort )\r
185 {\r
186         ( void ) xPort;\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 void __cs3_isr_interrupt_78( void )\r
191 {\r
192 unsigned portCHAR ucChar;\r
193 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xDoneSomething = pdTRUE;\r
194 \r
195         while( xDoneSomething != pdFALSE )\r
196         {\r
197                 xDoneSomething = pdFALSE;\r
198 \r
199                 /* Does the tx buffer contain space? */\r
200                 if( ( MCF_UART1_USR & MCF_UART_USR_TXRDY ) != 0x00 )\r
201                 {\r
202                         /* Are there any characters queued to be sent? */\r
203                         if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
204                         {\r
205                                 /* Send the next char. */\r
206                                 MCF_UART1_UTB = ucChar;\r
207                                 xDoneSomething = pdTRUE;\r
208                         }\r
209                         else\r
210                         {\r
211                                 /* Turn off the Tx interrupt until such time as another character\r
212                                 is being transmitted. */\r
213                                 MCF_UART1_UIMR = serRX_INT;\r
214                                 xTxHasEnded = pdTRUE;\r
215                         }\r
216                 }\r
217 \r
218                 if( MCF_UART1_USR & MCF_UART_USR_RXRDY )\r
219                 {\r
220                         ucChar = MCF_UART1_URB;\r
221                         xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );\r
222                         xDoneSomething = pdTRUE;\r
223                 }\r
224         }\r
225 \r
226     portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
227 }\r
228 \r
229 \r
230 \r
231 \r
232 \r
233 \r
234 \r