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