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