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