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