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