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