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