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