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