]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4S_Atmel_Studio/src/serial.c
Update license information text files for the CLI, TCP and UDP products to be correct...
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4S_Atmel_Studio / src / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART1.\r
31         \r
32         ***Note*** This example uses queues to send each character into an interrupt\r
33         service routine and out of an interrupt service routine individually.  This\r
34         is done to demonstrate queues being used in an interrupt, and to deliberately\r
35         load the system to test the FreeRTOS port.  It is *NOT* meant to be an \r
36         example of an efficient implementation.  An efficient implementation should\r
37         use FIFO's or DMA if available, and only use FreeRTOS API functions when \r
38         enough has been received to warrant a task being unblocked to process the\r
39         data.\r
40 */\r
41 \r
42 /* Scheduler includes. */\r
43 #include "FreeRTOS.h"\r
44 #include "queue.h"\r
45 #include "semphr.h"\r
46 #include "comtest2.h"\r
47 \r
48 /* Library includes. */\r
49 #include "asf.h"\r
50 \r
51 /* Demo application includes. */\r
52 #include "demo_serial.h"\r
53 /*-----------------------------------------------------------*/\r
54 \r
55 /* Misc defines. */\r
56 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )\r
57 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )\r
58 #define serPMC_USART_ID                                 ( BOARD_ID_USART )\r
59 \r
60 /* The USART supported by this file. */\r
61 #define serUSART_PORT                                   ( USART1 )\r
62 #define serUSART_IRQ                                    ( USART1_IRQn )\r
63 \r
64 /* Every bit in the interrupt mask. */\r
65 #define serMASK_ALL_INTERRUPTS                  ( 0xffffffffUL )\r
66 \r
67 /*-----------------------------------------------------------*/\r
68 \r
69 /* The queue used to hold received characters. */\r
70 static QueueHandle_t xRxedChars;\r
71 static QueueHandle_t xCharsForTx;\r
72 \r
73 /*-----------------------------------------------------------*/\r
74 \r
75 \r
76 /*\r
77  * See the serial.h header file.\r
78  */\r
79 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
80 {\r
81 uint32_t ulChar;\r
82 xComPortHandle xReturn;\r
83 const sam_usart_opt_t xUSARTSettings = \r
84 {\r
85         ulWantedBaud,\r
86         US_MR_CHRL_8_BIT,\r
87         US_MR_PAR_NO,\r
88         US_MR_NBSTOP_1_BIT,\r
89         US_MR_CHMODE_NORMAL,    \r
90         0 /* Only used in IrDA mode. */\r
91 };\r
92 \r
93         /* Create the queues used to hold Rx/Tx characters. */\r
94         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
95         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
96         \r
97         /* If the queues were created correctly then setup the serial port\r
98         hardware. */\r
99         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
100         {\r
101                 /* Enable the peripheral clock in the PMC. */\r
102                 pmc_enable_periph_clk( serPMC_USART_ID );\r
103 \r
104                 /* Configure USART in serial mode. */\r
105                 usart_init_rs232( serUSART_PORT, &xUSARTSettings, sysclk_get_cpu_hz() );\r
106 \r
107                 /* Disable all the interrupts. */\r
108                 usart_disable_interrupt( serUSART_PORT, serMASK_ALL_INTERRUPTS );\r
109 \r
110                 /* Enable the receiver and transmitter. */\r
111                 usart_enable_tx( serUSART_PORT );\r
112                 usart_enable_rx( serUSART_PORT );\r
113                 \r
114                 /* Clear any characters before enabling interrupt. */\r
115                 usart_getchar( serUSART_PORT, &ulChar );\r
116                 \r
117                 /* Enable Rx end interrupt. */\r
118                 usart_enable_interrupt( serUSART_PORT, US_IER_RXRDY );\r
119 \r
120                 /* Configure and enable interrupt of USART. */\r
121                 NVIC_SetPriority( serUSART_IRQ, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
122                 NVIC_EnableIRQ( serUSART_IRQ );\r
123         }\r
124         else\r
125         {\r
126                 xReturn = ( xComPortHandle ) 0;\r
127         }\r
128 \r
129         /* This demo file only supports a single port but we have to return\r
130         something to comply with the standard demo header file. */\r
131         return xReturn;\r
132 }\r
133 /*-----------------------------------------------------------*/\r
134 \r
135 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
136 {\r
137         /* The port handle is not required as this driver only supports one port. */\r
138         ( void ) pxPort;\r
139 \r
140         /* Get the next character from the buffer.  Return false if no characters\r
141         are available, or arrive before xBlockTime expires. */\r
142         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
143         {\r
144                 return pdTRUE;\r
145         }\r
146         else\r
147         {\r
148                 return pdFALSE;\r
149         }\r
150 }\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
154 {\r
155 signed char *pxNext;\r
156 \r
157         /* A couple of parameters that this port does not use. */\r
158         ( void ) usStringLength;\r
159         ( void ) pxPort;\r
160 \r
161         /* NOTE: This implementation does not handle the queue being full as no\r
162         block time is used! */\r
163 \r
164         /* The port handle is not required as this driver only supports USART1. */\r
165         ( void ) pxPort;\r
166 \r
167         /* Send each character in the string, one at a time. */\r
168         pxNext = ( signed char * ) pcString;\r
169         while( *pxNext )\r
170         {               \r
171                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
172                 pxNext++;\r
173         }\r
174 }\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
178 {\r
179 signed portBASE_TYPE xReturn;\r
180 \r
181         /* This simple example only supports one port. */\r
182         ( void ) pxPort;\r
183 \r
184         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )\r
185         {\r
186                 xReturn = pdPASS;\r
187                 usart_enable_interrupt( serUSART_PORT, US_IER_TXRDY );\r
188         }\r
189         else\r
190         {\r
191                 xReturn = pdFAIL;\r
192         }\r
193 \r
194         return xReturn;\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 void vSerialClose( xComPortHandle xPort )\r
199 {\r
200         /* Not supported as not required by the demo application. */\r
201         ( void ) xPort;\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 /* \r
206  * It should be noted that the com test tasks (which use make use of this file) \r
207  * are included to demonstrate queues being used to communicate between tasks \r
208  * and interrupts, and to demonstrate a context switch being performed from \r
209  * inside an interrupt service routine.  The serial driver used here is *not* \r
210  * intended to represent an efficient implementation.  Real applications should \r
211  * make use of the USARTS peripheral DMA channel (PDC).\r
212  */\r
213 void USART1_Handler( void )\r
214 {\r
215 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
216 uint8_t ucChar;\r
217 uint32_t ulChar;\r
218 uint32_t ulUSARTStatus, ulUSARTMask;\r
219 \r
220         ulUSARTStatus = usart_get_status( serUSART_PORT );\r
221         ulUSARTMask = usart_get_interrupt_mask( serUSART_PORT );\r
222         ulUSARTStatus &= ulUSARTMask;\r
223 \r
224         if( ( ulUSARTStatus & US_CSR_TXRDY ) != 0UL )\r
225         {\r
226                 /* The interrupt was caused by the TX register becoming empty.  Are \r
227                 there any more characters to transmit? */\r
228                 if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
229                 {\r
230                         /* A character was retrieved from the queue so can be sent to the\r
231                         USART now. */\r
232                         usart_putchar( serUSART_PORT, ( uint32_t ) ucChar );\r
233                 }\r
234                 else\r
235                 {\r
236                         usart_disable_interrupt( serUSART_PORT, US_IER_TXRDY );         \r
237                 }               \r
238         }\r
239         \r
240         if( ( ulUSARTStatus & US_CSR_RXRDY ) != 0UL )\r
241         {\r
242                 /* A character has been received on the USART, send it to the Rx\r
243                 handler task. */\r
244                 usart_getchar( serUSART_PORT, &ulChar );\r
245                 ucChar = ( uint8_t ) ( ulChar & 0xffUL );\r
246                 xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );\r
247         }       \r
248 \r
249         /* If sending or receiving from a queue has caused a task to unblock, and\r
250         the unblocked task has a priority equal to or higher than the currently \r
251         running task (the task this ISR interrupted), then xHigherPriorityTaskWoken \r
252         will have automatically been set to pdTRUE within the queue send or receive \r
253         function.  portEND_SWITCHING_ISR() will then ensure that this ISR returns \r
254         directly to the higher priority unblocked task. */\r
255         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
256 }\r
257 \r
258 \r
259 \r
260 \r
261 \r
262         \r