]> git.sur5r.net Git - freertos/blob - Demo/ARM9_STR91X_IAR/serial/serial.c
Update to V4.7.2.
[freertos] / Demo / ARM9_STR91X_IAR / serial / serial.c
1 /*\r
2         FreeRTOS.org V4.7.2 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27 \r
28         Please ensure to read the configuration and relevant port sections of the \r
29         online documentation.\r
30 \r
31         +++ http://www.FreeRTOS.org +++\r
32         Documentation, latest information, license and contact details.  \r
33 \r
34         +++ http://www.SafeRTOS.com +++\r
35         A version that is certified for use in safety critical systems.\r
36 \r
37         +++ http://www.OpenRTOS.com +++\r
38         Commercial support, development, porting, licensing and training services.\r
39 \r
40         ***************************************************************************\r
41 */\r
42 \r
43 /*\r
44         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART1.\r
45 */\r
46 \r
47 /* Library includes. */\r
48 #include "91x_lib.h"\r
49 \r
50 /* Scheduler includes. */\r
51 #include "FreeRTOS.h"\r
52 #include "queue.h"\r
53 #include "semphr.h"\r
54 \r
55 /* Demo application includes. */\r
56 #include "serial.h"\r
57 /*-----------------------------------------------------------*/\r
58 \r
59 /* Misc defines. */\r
60 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
61 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
62 #define serTX_BLOCK_TIME                                ( 40 / portTICK_RATE_MS )\r
63 \r
64 /* Interrupt and status bit definitions. */\r
65 #define mainTXRIS 0x20  \r
66 #define mainRXRIS 0x50\r
67 #define serTX_FIFO_FULL 0x20\r
68 #define serCLEAR_ALL_INTERRUPTS 0x3ff\r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /* The queue used to hold received characters. */\r
72 static xQueueHandle xRxedChars;\r
73 \r
74 /* The semaphore used to wake a task waiting for space to become available\r
75 in the FIFO. */\r
76 static xSemaphoreHandle xTxFIFOSemaphore;\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 /* UART interrupt handler. */\r
81 void UART1_IRQHandler( void );\r
82 \r
83 /* The interrupt service routine - called from the assembly entry point. */\r
84 __arm void UART1_IRQHandler( void );\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /* Flag to indicate whether or not a task is blocked waiting for space on\r
89 the FIFO. */\r
90 static portLONG lTaskWaiting = pdFALSE;\r
91 \r
92 /*\r
93  * See the serial2.h header file.\r
94  */\r
95 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
96 {\r
97 xComPortHandle xReturn;\r
98 UART_InitTypeDef xUART1_Init;\r
99 GPIO_InitTypeDef GPIO_InitStructure;\r
100         \r
101         /* Create the queues used to hold Rx characters. */\r
102         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
103         \r
104         /* Create the semaphore used to wake a task waiting for space to become\r
105         available in the FIFO. */\r
106         vSemaphoreCreateBinary( xTxFIFOSemaphore );\r
107 \r
108         /* If the queue/semaphore was created correctly then setup the serial port\r
109         hardware. */\r
110         if( ( xRxedChars != serINVALID_QUEUE ) && ( xTxFIFOSemaphore != serINVALID_QUEUE ) )\r
111         {\r
112                 /* Pre take the semaphore so a task will block if it tries to access\r
113                 it. */\r
114                 xSemaphoreTake( xTxFIFOSemaphore, 0 );\r
115                 \r
116                 /* Configure the UART. */\r
117                 xUART1_Init.UART_WordLength = UART_WordLength_8D;\r
118                 xUART1_Init.UART_StopBits = UART_StopBits_1;\r
119                 xUART1_Init.UART_Parity = UART_Parity_No;\r
120                 xUART1_Init.UART_BaudRate = ulWantedBaud;\r
121                 xUART1_Init.UART_HardwareFlowControl = UART_HardwareFlowControl_None;\r
122                 xUART1_Init.UART_Mode = UART_Mode_Tx_Rx;\r
123                 xUART1_Init.UART_FIFO = UART_FIFO_Enable;\r
124 \r
125                 /* Enable the UART1 Clock */\r
126                 SCU_APBPeriphClockConfig( __UART1, ENABLE );\r
127                 \r
128                 /* Enable the GPIO3 Clock */\r
129                 SCU_APBPeriphClockConfig( __GPIO3, ENABLE );\r
130                 \r
131                 /* Configure UART1_Rx pin GPIO3.2 */\r
132                 GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;\r
133                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;\r
134                 GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;\r
135                 GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;\r
136                 GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1 ;\r
137                 GPIO_Init( GPIO3, &GPIO_InitStructure );\r
138                 \r
139                 /* Configure UART1_Tx pin GPIO3.3 */\r
140                 GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;\r
141                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;\r
142                 GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;\r
143                 GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;\r
144                 GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2 ;\r
145                 GPIO_Init( GPIO3, &GPIO_InitStructure );\r
146                 \r
147                 \r
148                 portENTER_CRITICAL();\r
149                 {               \r
150                         /* Configure the UART itself. */\r
151                         UART_DeInit( UART1 );           \r
152                         UART_Init( UART1, &xUART1_Init );\r
153                         UART_ITConfig( UART1, UART_IT_Receive | UART_IT_Transmit, ENABLE );\r
154                         UART1->ICR = serCLEAR_ALL_INTERRUPTS;\r
155                         UART_LoopBackConfig( UART1, DISABLE );\r
156                         UART_IrDACmd( IrDA1, DISABLE );\r
157 \r
158                         /* Configure the VIC for the UART interrupts. */                        \r
159                         VIC_Config( UART1_ITLine, VIC_IRQ, 9 );\r
160                         VIC_ITCmd( UART1_ITLine, ENABLE );\r
161 \r
162                         UART_Cmd( UART1, ENABLE );                      \r
163                         lTaskWaiting = pdFALSE;\r
164                 }\r
165                 portEXIT_CRITICAL();\r
166         }\r
167         else\r
168         {\r
169                 xReturn = ( xComPortHandle ) 0;\r
170         }\r
171 \r
172         /* This demo file only supports a single port but we have to return\r
173         something to comply with the standard demo header file. */\r
174         return xReturn;\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
179 {\r
180         /* The port handle is not required as this driver only supports one port. */\r
181         ( void ) pxPort;\r
182 \r
183         /* Get the next character from the buffer.  Return false if no characters\r
184         are available, or arrive before xBlockTime expires. */\r
185         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
186         {\r
187                 return pdTRUE;\r
188         }\r
189         else\r
190         {\r
191                 return pdFALSE;\r
192         }\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )\r
197 {\r
198 signed portCHAR *pxNext;\r
199 \r
200         /* A couple of parameters that this port does not use. */\r
201         ( void ) usStringLength;\r
202         ( void ) pxPort;\r
203 \r
204         /* NOTE: This implementation does not handle the queue being full as no\r
205         block time is used! */\r
206 \r
207         /* The port handle is not required as this driver only supports UART1. */\r
208         ( void ) pxPort;\r
209 \r
210         /* Send each character in the string, one at a time. */\r
211         pxNext = ( signed portCHAR * ) pcString;\r
212         while( *pxNext )\r
213         {\r
214                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
215                 pxNext++;\r
216         }\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
221 {\r
222 portBASE_TYPE xReturn;\r
223 \r
224         portENTER_CRITICAL();\r
225         {\r
226                 /* Can we write to the FIFO? */\r
227                 if( UART1->FR & serTX_FIFO_FULL )\r
228                 {\r
229                         /* Wait for the interrupt letting us know there is space on the\r
230                         FIFO.  It is ok to block in a critical section, interrupts will be\r
231                         enabled for other tasks once we force a switch. */\r
232                         lTaskWaiting = pdTRUE;\r
233                         \r
234                         /* Just to be a bit different this driver uses a semaphore to\r
235                         block the sending task when the FIFO is full.  The standard COMTest\r
236                         task assumes a queue of adequate length exists so does not use\r
237                         a block time.  For this demo the block time is therefore hard\r
238                         coded. */\r
239                         xReturn = xSemaphoreTake( xTxFIFOSemaphore, serTX_BLOCK_TIME );\r
240                         if( xReturn )\r
241                         {\r
242                                 UART1->DR = cOutChar;\r
243                         }\r
244                 }\r
245                 else\r
246                 {\r
247                         UART1->DR = cOutChar;\r
248                         xReturn = pdPASS;\r
249                 }\r
250         }\r
251         portEXIT_CRITICAL();\r
252 \r
253         return xReturn;\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 void vSerialClose( xComPortHandle xPort )\r
258 {\r
259         /* Not supported as not required by the demo application. */\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 void UART1_IRQHandler( void )\r
264 {\r
265 signed portCHAR cChar;\r
266 portBASE_TYPE xTaskWokenByTx = pdFALSE, xTaskWokenByPost = pdFALSE;\r
267 \r
268         while( UART1->RIS &     mainRXRIS )\r
269         {\r
270                 /* The interrupt was caused by a character being received.  Grab the\r
271                 character from the DR and place it in the queue of received\r
272                 characters. */\r
273                 cChar = UART1->DR;\r
274                 xTaskWokenByPost = xQueueSendFromISR( xRxedChars, &cChar, xTaskWokenByPost );\r
275         }       \r
276         \r
277         if( UART1->RIS & mainTXRIS )\r
278         {\r
279                 if( lTaskWaiting == pdTRUE )\r
280                 {\r
281                         /* This interrupt was caused by space becoming available on the Tx\r
282                         FIFO, wake any task that is waiting to post (if any). */\r
283                         xTaskWokenByTx = xSemaphoreGiveFromISR( xTxFIFOSemaphore, xTaskWokenByTx );\r
284                         lTaskWaiting = pdFALSE;\r
285                 }\r
286                 \r
287                 UART1->ICR = mainTXRIS;\r
288         }\r
289 \r
290         /* If a task was woken by either a character being received or a character\r
291         being transmitted then we may need to switch to another task. */\r
292         portEND_SWITCHING_ISR( ( xTaskWokenByPost || xTaskWokenByTx ) );\r
293 }\r
294 \r
295 \r
296 \r
297 \r
298 \r
299         \r