]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM9_AT91SAM9XE_IAR/serial/serial.c
f2e92d52f7f166fb446c2e01b5de5b5bb7f638f3
[freertos] / FreeRTOS / Demo / ARM9_AT91SAM9XE_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 UART0.\r
69 */\r
70 \r
71 /* Standard includes. */\r
72 #include <stdlib.h>\r
73 \r
74 /* Scheduler includes. */\r
75 #include "FreeRTOS.h"\r
76 #include "queue.h"\r
77 \r
78 /* Demo application includes. */\r
79 #include "serial.h"\r
80 \r
81 /* Atmel library includes. */\r
82 #include <usart/usart.h>\r
83 #include <aic/aic.h>\r
84 #include <pmc/pmc.h>\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /* Location of the COM0 registers. */\r
89 #define serCOM0                                                 ( ( AT91PS_USART ) AT91C_BASE_US0 )\r
90 \r
91 /* Interrupt control macros. */\r
92 #define serINTERRUPT_LEVEL                              ( 5 )\r
93 #define vInterruptOn()                                  serCOM0->US_IER = ( AT91C_US_TXRDY | AT91C_US_RXRDY )\r
94 #define vInterruptOff()                                 serCOM0->US_IDR = AT91C_US_TXRDY\r
95 \r
96 /* Misc constants. */\r
97 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
98 #define serHANDLE                                               ( ( xComPortHandle ) 1 )\r
99 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
100 #define serNO_TIMEGUARD                                 ( ( unsigned long ) 0 )\r
101 #define serNO_PERIPHERAL_B_SETUP                ( ( unsigned long ) 0 )\r
102 \r
103 \r
104 /* Queues used to hold received characters, and characters waiting to be\r
105 transmitted. */\r
106 static xQueueHandle xRxedChars;\r
107 static xQueueHandle xCharsForTx;\r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /* The interrupt service routine. */\r
112 __arm void vSerialISR( void );\r
113 \r
114 /*-----------------------------------------------------------*/\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 = serHANDLE;\r
122 \r
123         /* Create the queues used to hold Rx and Tx characters. */\r
124         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
125         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
126 \r
127         /* If the queues were created correctly then setup the serial port\r
128         hardware. */\r
129         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
130         {\r
131                 PMC_EnablePeripheral( AT91C_ID_US0 );   \r
132                 portENTER_CRITICAL();\r
133                 {\r
134                         USART_Configure( serCOM0, ( AT91C_US_CHRL_8_BITS | AT91C_US_PAR_NONE ), ulWantedBaud, configCPU_CLOCK_HZ );\r
135 \r
136                         /* Enable Rx and Tx. */\r
137                         USART_SetTransmitterEnabled( serCOM0, pdTRUE );\r
138                         USART_SetReceiverEnabled( serCOM0, pdTRUE );\r
139 \r
140                         /* Enable the Rx interrupts.  The Tx interrupts are not enabled\r
141                         until there are characters to be transmitted. */\r
142                         serCOM0->US_IER = AT91C_US_RXRDY;\r
143 \r
144                         /* Enable the interrupts in the AIC. */\r
145                         AIC_ConfigureIT( AT91C_ID_US0, AT91C_AIC_PRIOR_LOWEST, ( void (*)( void ) ) vSerialISR );\r
146                         AIC_EnableIT( AT91C_ID_US0 );\r
147                 }\r
148                 portEXIT_CRITICAL();\r
149         }\r
150         else\r
151         {\r
152                 xReturn = ( xComPortHandle ) 0;\r
153         }\r
154 \r
155         /* This demo file only supports a single port but we have to return\r
156         something to comply with the standard demo header file. */\r
157         return xReturn;\r
158 }\r
159 /*-----------------------------------------------------------*/\r
160 \r
161 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
162 {\r
163         /* The port handle is not required as this driver only supports one port. */\r
164         ( void ) pxPort;\r
165 \r
166         /* Get the next character from the buffer.  Return false if no characters\r
167         are available, or arrive before xBlockTime expires. */\r
168         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
169         {\r
170                 return pdTRUE;\r
171         }\r
172         else\r
173         {\r
174                 return pdFALSE;\r
175         }\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
180 {\r
181 signed char *pxNext;\r
182 \r
183         /* A couple of parameters that this port does not use. */\r
184         ( void ) usStringLength;\r
185         ( void ) pxPort;\r
186 \r
187         /* NOTE: This implementation does not handle the queue being full as no\r
188         block time is used! */\r
189 \r
190         /* The port handle is not required as this driver only supports UART0. */\r
191         ( void ) pxPort;\r
192 \r
193         /* Send each character in the string, one at a time. */\r
194         pxNext = ( signed char * ) pcString;\r
195         while( *pxNext )\r
196         {\r
197                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
198                 pxNext++;\r
199         }\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
204 {\r
205         /* Just to remove compiler warning. */\r
206         ( void ) pxPort;\r
207         \r
208         /* Place the character in the queue of characters to be transmitted. */\r
209         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
210         {\r
211                 return pdFAIL;\r
212         }\r
213 \r
214         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
215         queue and send it.   This does not need to be in a critical section as\r
216         if the interrupt has already removed the character the next interrupt\r
217         will simply turn off the Tx interrupt again. */\r
218         vInterruptOn();\r
219 \r
220         return pdPASS;\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 void vSerialClose( xComPortHandle xPort )\r
225 {\r
226         /* Not supported as not required by the demo application. */\r
227         ( void ) xPort;\r
228 }\r
229 /*-----------------------------------------------------------*/\r
230 \r
231 /* Serial port ISR.  This can cause a context switch so is not defined as a\r
232 standard ISR using the __irq keyword.  Instead a wrapper function is defined\r
233 within serialISR.s79 which in turn calls this function.  See the port\r
234 documentation on the FreeRTOS.org website for more information. */\r
235 __arm void vSerialISR( void )\r
236 {\r
237 unsigned long ulStatus;\r
238 signed char cChar;\r
239 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
240 \r
241         /* What caused the interrupt? */\r
242         ulStatus = serCOM0->US_CSR &= serCOM0->US_IMR;\r
243 \r
244         if( ulStatus & AT91C_US_TXRDY )\r
245         {\r
246                 /* The interrupt was caused by the THR becoming empty.  Are there any\r
247                 more characters to transmit? */\r
248                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
249                 {\r
250                         /* A character was retrieved from the queue so can be sent to the\r
251                         THR now. */\r
252                         serCOM0->US_THR = cChar;\r
253                 }\r
254                 else\r
255                 {\r
256                         /* Queue empty, nothing to send so turn off the Tx interrupt. */\r
257                         vInterruptOff();\r
258                 }               \r
259         }\r
260 \r
261         if( ulStatus & AT91C_US_RXRDY )\r
262         {\r
263                 /* The interrupt was caused by a character being received.  Grab the\r
264                 character from the RHR and place it in the queue or received\r
265                 characters. */\r
266                 cChar = serCOM0->US_RHR;\r
267                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
268         }\r
269 \r
270         /* If a task was woken by either a character being received or a character\r
271         being transmitted then we may need to switch to another task. */\r
272         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
273 \r
274         /* End the interrupt in the AIC. */\r
275         AT91C_BASE_AIC->AIC_EOICR = 0;\r
276 }\r
277 \r
278 \r
279 \r
280 \r
281         \r