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