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