]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ColdFire_MCF52221_CodeWarrior/sources/serial/serial.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / ColdFire_MCF52221_CodeWarrior / sources / serial / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 \r
30 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.\r
31 \r
32 NOTE:  This driver is primarily to test the scheduler functionality.  It does\r
33 not effectively use the buffers or DMA and is therefore not intended to be\r
34 an example of an efficient driver. */\r
35 \r
36 /* Standard include file. */\r
37 #include <stdlib.h>\r
38 \r
39 /* Scheduler include files. */\r
40 #include "FreeRTOS.h"\r
41 #include "queue.h"\r
42 #include "task.h"\r
43 \r
44 /* Demo app include files. */\r
45 #include "serial.h"\r
46 \r
47 /* Hardware definitions. */\r
48 #define serNO_PARITY            ( ( unsigned char ) 0x02 << 3 )\r
49 #define ser8DATA_BITS           ( ( unsigned char ) 0x03 )\r
50 #define ser1STOP_BIT            ( ( unsigned char ) 0x07 )\r
51 #define serSYSTEM_CLOCK         ( ( unsigned char ) 0xdd )\r
52 #define serTX_ENABLE            ( ( unsigned char ) 0x04 )\r
53 #define serRX_ENABLE            ( ( unsigned char ) 0x01 )\r
54 #define serTX_INT                       ( ( unsigned char ) 0x01 )\r
55 #define serRX_INT                       ( ( unsigned char ) 0x02 )\r
56 \r
57 \r
58 /* The queues used to communicate between tasks and ISR's. */\r
59 static QueueHandle_t xRxedChars;\r
60 static QueueHandle_t xCharsForTx;\r
61 \r
62 /* Flag used to indicate the tx status. */\r
63 static portBASE_TYPE xTxHasEnded = pdTRUE;\r
64 \r
65 /*-----------------------------------------------------------*/\r
66 \r
67 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
68 {\r
69 const unsigned long ulBaudRateDivisor = ( configCPU_CLOCK_HZ / ( 32UL * ulWantedBaud ) );\r
70 \r
71         /* Create the queues used by the com test task. */\r
72         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
73         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
74 \r
75         xTxHasEnded = pdTRUE;\r
76 \r
77         /* Set the pins to UART mode. */\r
78         MCF_GPIO_PUAPAR |= MCF_GPIO_PUAPAR_UTXD0_UTXD0;\r
79         MCF_GPIO_PUAPAR |= MCF_GPIO_PUAPAR_URXD0_URXD0;\r
80 \r
81         /* Reset the peripheral. */\r
82         MCF_UART0_UCR = MCF_UART_UCR_RESET_RX;\r
83         MCF_UART0_UCR = MCF_UART_UCR_RESET_TX;\r
84         MCF_UART0_UCR = MCF_UART_UCR_RESET_ERROR;\r
85         MCF_UART0_UCR = MCF_UART_UCR_RESET_BKCHGINT;\r
86         MCF_UART0_UCR = MCF_UART_UCR_RESET_MR | MCF_UART_UCR_RX_DISABLED | MCF_UART_UCR_TX_DISABLED;\r
87 \r
88         /* Configure the UART. */\r
89         MCF_UART0_UMR1 = serNO_PARITY | ser8DATA_BITS;\r
90         MCF_UART0_UMR2 = ser1STOP_BIT;\r
91         MCF_UART0_UCSR = serSYSTEM_CLOCK;\r
92 \r
93         MCF_UART0_UBG1 = ( unsigned char ) ( ( ulBaudRateDivisor >> 8UL ) & 0xffUL );\r
94         MCF_UART0_UBG2 = ( unsigned char ) ( ulBaudRateDivisor & 0xffUL );\r
95 \r
96         /* Turn it on. */\r
97         MCF_UART0_UCR = serTX_ENABLE | serRX_ENABLE;\r
98 \r
99         /* Configure the interrupt controller.  Run the UARTs above the kernel\r
100         interrupt priority for demo purposes. */\r
101     MCF_INTC0_ICR13 = ( ( configMAX_SYSCALL_INTERRUPT_PRIORITY - 1  ) << 3 );\r
102     MCF_INTC0_IMRL &= ~( MCF_INTC_IMRL_INT_MASK13 | 0x01 );\r
103 \r
104         /* The Tx interrupt is not enabled until there is data to send. */\r
105         MCF_UART0_UIMR = serRX_INT;\r
106 \r
107         /* Only a single port is implemented so we don't need to return anything. */\r
108         return NULL;\r
109 }\r
110 /*-----------------------------------------------------------*/\r
111 \r
112 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
113 {\r
114         /* Only one port is supported. */\r
115         ( void ) pxPort;\r
116 \r
117         /* Get the next character from the buffer.  Return false if no characters\r
118         are available or arrive before xBlockTime expires. */\r
119         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
120         {\r
121                 return pdTRUE;\r
122         }\r
123         else\r
124         {\r
125                 return pdFALSE;\r
126         }\r
127 }\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
131 {\r
132         /* Only one port is supported. */\r
133         ( void ) pxPort;\r
134 \r
135         /* Return false if after the block time there is no room on the Tx queue. */\r
136         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
137         {\r
138                 return pdFAIL;\r
139         }\r
140 \r
141         /* A critical section should not be required as xTxHasEnded will not be\r
142         written to by the ISR if it is already 0 (is this correct?). */\r
143         if( xTxHasEnded != pdFALSE )\r
144         {\r
145                 xTxHasEnded = pdFALSE;\r
146                 MCF_UART0_UIMR = serRX_INT | serTX_INT;\r
147         }\r
148 \r
149         return pdPASS;\r
150 }\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 void vSerialClose( xComPortHandle xPort )\r
154 {\r
155         ( void ) xPort;\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 __declspec(interrupt:0) void vUART0InterruptHandler( void )\r
160 {\r
161 unsigned char ucChar;\r
162 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xDoneSomething = pdTRUE;\r
163 \r
164         while( xDoneSomething != pdFALSE )\r
165         {\r
166                 xDoneSomething = pdFALSE;\r
167 \r
168                 /* Does the tx buffer contain space? */\r
169                 if( ( MCF_UART0_USR & MCF_UART_USR_TXRDY ) != 0x00 )\r
170                 {\r
171                         /* Are there any characters queued to be sent? */\r
172                         if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
173                         {\r
174                                 /* Send the next char. */\r
175                                 MCF_UART0_UTB = ucChar;\r
176                                 xDoneSomething = pdTRUE;\r
177                         }\r
178                         else\r
179                         {\r
180                                 /* Turn off the Tx interrupt until such time as another character\r
181                                 is being transmitted. */\r
182                                 MCF_UART0_UIMR = serRX_INT;\r
183                                 xTxHasEnded = pdTRUE;\r
184                         }\r
185                 }\r
186 \r
187                 if( MCF_UART0_USR & MCF_UART_USR_RXRDY )\r
188                 {\r
189                         ucChar = MCF_UART0_URB;\r
190                         xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );\r
191                         xDoneSomething = pdTRUE;\r
192                 }\r
193         }\r
194 \r
195     portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
196 }\r
197 \r
198 \r
199 \r
200 \r
201 \r
202 \r
203 \r