]> git.sur5r.net Git - freertos/blob - Demo/ARM7_STR75x_IAR/serial/serial.c
Prepare for V5.3.0 release.
[freertos] / Demo / ARM7_STR75x_IAR / serial / serial.c
1 /*\r
2         FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /*\r
53         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.\r
54 */\r
55 \r
56 /* Library includes. */\r
57 #include "75x_uart.h"\r
58 #include "75x_gpio.h"\r
59 #include "75x_eic.h"\r
60 #include "75x_mrcc.h"\r
61 \r
62 /* Scheduler includes. */\r
63 #include "FreeRTOS.h"\r
64 #include "queue.h"\r
65 \r
66 /* Demo application includes. */\r
67 #include "serial.h"\r
68 \r
69 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
70 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
71 \r
72 /*-----------------------------------------------------------*/\r
73 \r
74 /* Queues used to hold received characters, and characters waiting to be\r
75 transmitted. */\r
76 static xQueueHandle xRxedChars;\r
77 static xQueueHandle xCharsForTx;\r
78 \r
79 static volatile portBASE_TYPE xQueueEmpty = pdTRUE;\r
80 \r
81 /*-----------------------------------------------------------*/\r
82 \r
83 /* The interrupt service routine - called from the assembly entry point. */\r
84 __arm void vSerialISR( void );\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /*\r
89  * See the serial2.h header file.\r
90  */\r
91 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
92 {\r
93 xComPortHandle xReturn;\r
94 UART_InitTypeDef UART_InitStructure;\r
95 GPIO_InitTypeDef GPIO_InitStructure;\r
96 EIC_IRQInitTypeDef  EIC_IRQInitStructure;       \r
97 \r
98         /* Create the queues used to hold Rx and Tx characters. */\r
99         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
100         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
101 \r
102         /* If the queues were created correctly then setup the serial port\r
103         hardware. */\r
104         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
105         {\r
106                 portENTER_CRITICAL();\r
107                 {\r
108                         /* Enable the UART0 Clock. */\r
109                         MRCC_PeripheralClockConfig( MRCC_Peripheral_UART0, ENABLE );\r
110                         \r
111                         /* Configure the UART0_Tx as alternate function */\r
112                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;\r
113                         GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_11;\r
114                         GPIO_Init(GPIO0, &GPIO_InitStructure);\r
115                         \r
116                         /* Configure the UART0_Rx as input floating */\r
117                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;\r
118                         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;\r
119                         GPIO_Init(GPIO0, &GPIO_InitStructure);\r
120                         \r
121                         /* Configure UART0. */\r
122                         UART_InitStructure.UART_WordLength = UART_WordLength_8D;\r
123                         UART_InitStructure.UART_StopBits = UART_StopBits_1;\r
124                         UART_InitStructure.UART_Parity = UART_Parity_No;\r
125                         UART_InitStructure.UART_BaudRate = ulWantedBaud;\r
126                         UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;\r
127                         UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx;\r
128                         UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */\r
129                         UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */\r
130                         UART_Init(UART0, &UART_InitStructure);\r
131 \r
132                         /* Enable the UART0 */\r
133                         UART_Cmd(UART0, ENABLE);\r
134 \r
135                         /* Configure the IEC for the UART interrupts. */                        \r
136                         EIC_IRQInitStructure.EIC_IRQChannelCmd = ENABLE;\r
137                         EIC_IRQInitStructure.EIC_IRQChannel = UART0_IRQChannel;\r
138                         EIC_IRQInitStructure.EIC_IRQChannelPriority = 1;\r
139                         EIC_IRQInit(&EIC_IRQInitStructure);\r
140                         \r
141                         xQueueEmpty = pdTRUE;\r
142                         UART_ITConfig( UART0, UART_IT_Transmit | UART_IT_Receive, ENABLE );\r
143                 }\r
144                 portEXIT_CRITICAL();\r
145         }\r
146         else\r
147         {\r
148                 xReturn = ( xComPortHandle ) 0;\r
149         }\r
150 \r
151         /* This demo file only supports a single port but we have to return\r
152         something to comply with the standard demo header file. */\r
153         return xReturn;\r
154 }\r
155 /*-----------------------------------------------------------*/\r
156 \r
157 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
158 {\r
159         /* The port handle is not required as this driver only supports one port. */\r
160         ( void ) pxPort;\r
161 \r
162         /* Get the next character from the buffer.  Return false if no characters\r
163         are available, or arrive before xBlockTime expires. */\r
164         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
165         {\r
166                 return pdTRUE;\r
167         }\r
168         else\r
169         {\r
170                 return pdFALSE;\r
171         }\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )\r
176 {\r
177 signed portCHAR *pxNext;\r
178 \r
179         /* A couple of parameters that this port does not use. */\r
180         ( void ) usStringLength;\r
181         ( void ) pxPort;\r
182 \r
183         /* NOTE: This implementation does not handle the queue being full as no\r
184         block time is used! */\r
185 \r
186         /* The port handle is not required as this driver only supports UART0. */\r
187         ( void ) pxPort;\r
188 \r
189         /* Send each character in the string, one at a time. */\r
190         pxNext = ( signed portCHAR * ) pcString;\r
191         while( *pxNext )\r
192         {\r
193                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
194                 pxNext++;\r
195         }\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
200 {\r
201 portBASE_TYPE xReturn;\r
202 \r
203         /* Place the character in the queue of characters to be transmitted. */\r
204         portENTER_CRITICAL();\r
205         {\r
206                 if( xQueueEmpty == pdTRUE )\r
207                 {\r
208                         UART0->DR = cOutChar;\r
209                         xReturn = pdPASS;\r
210                 }\r
211                 else\r
212                 {\r
213                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
214                         {\r
215                                 xReturn = pdFAIL;\r
216                         }                       \r
217                         else\r
218                         {\r
219                                 xReturn = pdPASS;                               \r
220                         }\r
221                 }\r
222                 \r
223                 xQueueEmpty = pdFALSE;\r
224         }\r
225         portEXIT_CRITICAL();\r
226 \r
227         return xReturn;\r
228 }\r
229 /*-----------------------------------------------------------*/\r
230 \r
231 void vSerialClose( xComPortHandle xPort )\r
232 {\r
233         /* Not supported as not required by the demo application. */\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 __arm void vSerialISR( void )\r
238 {\r
239 signed portCHAR cChar;\r
240 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
241 \r
242         do\r
243         {\r
244                 if( UART0->MIS & UART_IT_Transmit )\r
245                 {\r
246                         /* The interrupt was caused by the THR becoming empty.  Are there any\r
247                         more characters to transmit? */\r
248                         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
249                         {\r
250                                 /* A character was retrieved from the queue so can be sent to the\r
251                                 THR now. */\r
252                                 UART0->DR = cChar;\r
253                         }\r
254                         else\r
255                         {\r
256                                 xQueueEmpty = pdTRUE;           \r
257                         }               \r
258 \r
259                         UART_ClearITPendingBit( UART0, UART_IT_Transmit );\r
260                 }\r
261         \r
262                 if( UART0->MIS & UART_IT_Receive )\r
263                 {\r
264                         /* The interrupt was caused by a character being received.  Grab the\r
265                         character from the RHR and place it in the queue of received\r
266                         characters. */\r
267                         cChar = UART0->DR;\r
268                         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
269                         UART_ClearITPendingBit( UART0, UART_IT_Receive );\r
270                 }\r
271         } while( UART0->MIS );\r
272 \r
273         /* If a task was woken by either a character being received or a character\r
274         being transmitted then we may need to switch to another task. */\r
275         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
276 }\r
277 \r
278 \r
279 \r
280 \r
281 \r
282         \r