]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_ATSAM3X_Atmel_Studio/src/serial.c
cbc91beef03ec5850bffaf318ee0f28b8b1ee82b
[freertos] / FreeRTOS / Demo / CORTEX_ATSAM3X_Atmel_Studio / src / serial.c
1 /*\r
2     FreeRTOS V7.2.0 - 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 USART1.\r
69 \r
70         ***Note*** This example uses queues to send each character into an interrupt\r
71         service routine and out of an interrupt service routine individually.  This\r
72         is done to demonstrate queues being used in an interrupt, and to deliberately\r
73         load the system to test the FreeRTOS port.  It is *NOT* meant to be an\r
74         example of an efficient implementation.  An efficient implementation should\r
75         use FIFO's or DMA if available, and only use FreeRTOS API functions when\r
76         enough has been received to warrant a task being unblocked to process the\r
77         data.\r
78 */\r
79 \r
80 /* Scheduler includes. */\r
81 #include "FreeRTOS.h"\r
82 #include "queue.h"\r
83 #include "semphr.h"\r
84 #include "comtest2.h"\r
85 \r
86 /* Library includes. */\r
87 #include "asf.h"\r
88 \r
89 /* Demo application includes. */\r
90 #include "demo_serial.h"\r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /* Misc defines. */\r
94 #define serINVALID_QUEUE                                ( ( xQueueHandle ) 0 )\r
95 #define serNO_BLOCK                                             ( ( portTickType ) 0 )\r
96 #define serPMC_USART_ID                                 ( BOARD_ID_USART )\r
97 \r
98 /* The USART supported by this file. */\r
99 #define serUSART_PORT                                   ( USART0 )\r
100 #define serUSART_IRQ                                    ( USART0_IRQn )\r
101 \r
102 /* Every bit in the interrupt mask. */\r
103 #define serMASK_ALL_INTERRUPTS                  ( 0xffffffffUL )\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /* The queue used to hold received characters. */\r
108 static xQueueHandle xRxedChars;\r
109 static xQueueHandle xCharsForTx;\r
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 \r
114 /*\r
115  * See the serial.h header file.\r
116  */\r
117 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
118 {\r
119 uint32_t ulChar;\r
120 xComPortHandle xReturn;\r
121 const sam_usart_opt_t xUSARTSettings =\r
122 {\r
123         ulWantedBaud,\r
124         US_MR_CHRL_8_BIT,\r
125         US_MR_PAR_NO,\r
126         US_MR_NBSTOP_1_BIT,\r
127         US_MR_CHMODE_NORMAL,\r
128         0 /* Only used in IrDA mode. */\r
129 };\r
130 \r
131         /* Create the queues used to hold Rx/Tx characters. */\r
132         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
133         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
134 \r
135         /* If the queues were created correctly then setup the serial port\r
136         hardware. */\r
137         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
138         {\r
139                 /* Enable the peripheral clock in the PMC. */\r
140                 pmc_enable_periph_clk( serPMC_USART_ID );\r
141 \r
142                 /* Configure USART in serial mode. */\r
143                 usart_init_rs232( serUSART_PORT, &xUSARTSettings, sysclk_get_cpu_hz() );\r
144 \r
145                 /* Disable all the interrupts. */\r
146                 usart_disable_interrupt( serUSART_PORT, serMASK_ALL_INTERRUPTS );\r
147 \r
148                 /* Enable the receiver and transmitter. */\r
149                 usart_enable_tx( serUSART_PORT );\r
150                 usart_enable_rx( serUSART_PORT );\r
151 \r
152                 /* Clear any characters before enabling interrupt. */\r
153                 usart_getchar( serUSART_PORT, &ulChar );\r
154 \r
155                 /* Enable Rx end interrupt. */\r
156                 usart_enable_interrupt( serUSART_PORT, US_IER_RXRDY );\r
157 \r
158                 /* Configure and enable interrupt of USART. */\r
159                 NVIC_SetPriority( serUSART_IRQ, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
160                 NVIC_EnableIRQ( serUSART_IRQ );\r
161         }\r
162         else\r
163         {\r
164                 xReturn = ( xComPortHandle ) 0;\r
165         }\r
166 \r
167         /* This demo file only supports a single port but we have to return\r
168         something to comply with the standard demo header file. */\r
169         return xReturn;\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
174 {\r
175         /* The port handle is not required as this driver only supports one port. */\r
176         ( void ) pxPort;\r
177 \r
178         /* Get the next character from the buffer.  Return false if no characters\r
179         are available, or arrive before xBlockTime expires. */\r
180         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
181         {\r
182                 return pdTRUE;\r
183         }\r
184         else\r
185         {\r
186                 return pdFALSE;\r
187         }\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
192 {\r
193 signed char *pxNext;\r
194 \r
195         /* A couple of parameters that this port does not use. */\r
196         ( void ) usStringLength;\r
197         ( void ) pxPort;\r
198 \r
199         /* NOTE: This implementation does not handle the queue being full as no\r
200         block time is used! */\r
201 \r
202         /* The port handle is not required as this driver only supports USART1. */\r
203         ( void ) pxPort;\r
204 \r
205         /* Send each character in the string, one at a time. */\r
206         pxNext = ( signed char * ) pcString;\r
207         while( *pxNext )\r
208         {\r
209                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
210                 pxNext++;\r
211         }\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
216 {\r
217 signed portBASE_TYPE xReturn;\r
218 \r
219         /* This simple example only supports one port. */\r
220         ( void ) pxPort;\r
221 \r
222         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )\r
223         {\r
224                 xReturn = pdPASS;\r
225                 usart_enable_interrupt( serUSART_PORT, US_IER_TXRDY );\r
226         }\r
227         else\r
228         {\r
229                 xReturn = pdFAIL;\r
230         }\r
231 \r
232         return xReturn;\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 void vSerialClose( xComPortHandle xPort )\r
237 {\r
238         /* Not supported as not required by the demo application. */\r
239         ( void ) xPort;\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 /*\r
244  * It should be noted that the com test tasks (which use make use of this file)\r
245  * are included to demonstrate queues being used to communicate between tasks\r
246  * and interrupts, and to demonstrate a context switch being performed from\r
247  * inside an interrupt service routine.  The serial driver used here is *not*\r
248  * intended to represent an efficient implementation.  Real applications should\r
249  * make use of the USARTS peripheral DMA channel (PDC).\r
250  */\r
251 void USART0_Handler( void )\r
252 {\r
253 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
254 uint8_t ucChar;\r
255 uint32_t ulChar;\r
256 uint32_t ulUSARTStatus, ulUSARTMask;\r
257 \r
258         ulUSARTStatus = usart_get_status( serUSART_PORT );\r
259         ulUSARTMask = usart_get_interrupt_mask( serUSART_PORT );\r
260         ulUSARTStatus &= ulUSARTMask;\r
261 \r
262         if( ( ulUSARTStatus & US_CSR_TXRDY ) != 0UL )\r
263         {\r
264                 /* The interrupt was caused by the TX register becoming empty.  Are\r
265                 there any more characters to transmit? */\r
266                 if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
267                 {\r
268                         /* A character was retrieved from the queue so can be sent to the\r
269                         USART now. */\r
270                         usart_putchar( serUSART_PORT, ( uint32_t ) ucChar );\r
271                 }\r
272                 else\r
273                 {\r
274                         usart_disable_interrupt( serUSART_PORT, US_IER_TXRDY );\r
275                 }\r
276         }\r
277 \r
278         if( ( ulUSARTStatus & US_CSR_RXRDY ) != 0UL )\r
279         {\r
280                 /* A character has been received on the USART, send it to the Rx\r
281                 handler task. */\r
282                 usart_getchar( serUSART_PORT, &ulChar );\r
283                 ucChar = ( uint8_t ) ( ulChar & 0xffUL );\r
284                 xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );\r
285         }\r
286 \r
287         /* If sending or receiving from a queue has caused a task to unblock, and\r
288         the unblocked task has a priority equal to or higher than the currently\r
289         running task (the task this ISR interrupted), then xHigherPriorityTaskWoken\r
290         will have automatically been set to pdTRUE within the queue send or receive\r
291         function.  portEND_SWITCHING_ISR() will then ensure that this ISR returns\r
292         directly to the higher priority unblocked task. */\r
293         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
294 }\r
295 \r
296 \r
297 \r
298 \r
299 \r
300 \r