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