]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_AT91SAM3U256_IAR/serial/serial.c
Updated pack to FreeRTOS 10.4.4
[cmsis-freertos] / Demo / CORTEX_AT91SAM3U256_IAR / serial / serial.c
1 /*
2  * FreeRTOS V202107.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
29         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
30 */
31
32 /* Standard includes. */
33 #include <stdlib.h>
34
35 /* Scheduler includes. */
36 #include "FreeRTOS.h"
37 #include "queue.h"
38
39 /* Demo application includes. */
40 #include "serial.h"
41
42 /* Library includes. */
43 #include "usart/usart.h"
44 #include "pmc/pmc.h"
45 #include "irq/irq.h"
46 #include "pio/pio.h"
47
48 /*-----------------------------------------------------------*/
49
50 /* Interrupt control macros. */
51 #define serINTERRUPT_LEVEL                              ( 5 )
52 #define vInterruptOn()                                  BOARD_USART_BASE->US_IER = ( AT91C_US_TXRDY | AT91C_US_RXRDY )
53 #define vInterruptOff()                                 BOARD_USART_BASE->US_IDR = AT91C_US_TXRDY
54
55 /* Misc constants. */
56 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )
57 #define serHANDLE                                               ( ( xComPortHandle ) 1 )
58 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )
59 #define serNO_TIMEGUARD                                 ( ( unsigned long ) 0 )
60 #define serNO_PERIPHERAL_B_SETUP                ( ( unsigned long ) 0 )
61
62
63 /* Queues used to hold received characters, and characters waiting to be
64 transmitted. */
65 static QueueHandle_t xRxedChars;
66 static QueueHandle_t xCharsForTx;
67
68 /*-----------------------------------------------------------*/
69
70 /* The interrupt service routine - called from the assembly entry point. */
71 void vSerialISR( void );
72
73 /*-----------------------------------------------------------*/
74
75 /*
76  * See the serial2.h header file.
77  */
78 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
79 {
80 xComPortHandle xReturn = serHANDLE;
81 extern void ( vUART_ISR )( void );
82 const Pin xUSART_Pins[] = { BOARD_PIN_USART_RXD, BOARD_PIN_USART_TXD };
83
84
85         /* Create the queues used to hold Rx and Tx characters. */
86         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
87         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
88
89         /* If the queues were created correctly then setup the serial port
90         hardware. */
91         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
92         {
93                 portENTER_CRITICAL();
94                 {
95                         /* Enable the peripheral clock in the PMC. */
96                         PMC_EnablePeripheral( BOARD_ID_USART );
97                 
98                         /* Configure the USART. */
99                         USART_Configure( BOARD_USART_BASE, AT91C_US_CHRL_8_BITS | AT91C_US_PAR_NONE | AT91C_US_NBSTOP_1_BIT, ulWantedBaud, configCPU_CLOCK_HZ );
100                 
101                         /* Configure the interrupt.  Note the pre-emption priority is set
102                         in bits [8:15] of the priority value passed as the parameter. */
103                         IRQ_ConfigureIT( BOARD_ID_USART, ( configMAX_SYSCALL_INTERRUPT_PRIORITY << 8 ), vSerialISR );
104                         IRQ_EnableIT( BOARD_ID_USART );
105                 
106                         /* Enable receiver & transmitter. */
107                         USART_SetTransmitterEnabled( BOARD_USART_BASE, pdTRUE );
108                         USART_SetReceiverEnabled( BOARD_USART_BASE, pdTRUE );
109                         
110                         /* Configure IO for USART use. */
111                         PIO_Configure( xUSART_Pins, PIO_LISTSIZE( xUSART_Pins ) );
112                 }
113                 portEXIT_CRITICAL();
114         }
115         else
116         {
117                 xReturn = ( xComPortHandle ) 0;
118         }
119
120         /* This demo file only supports a single port but we have to return
121         something to comply with the standard demo header file. */
122         return xReturn;
123 }
124 /*-----------------------------------------------------------*/
125
126 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
127 {
128         /* The port handle is not required as this driver only supports one port. */
129         ( void ) pxPort;
130
131         /* Get the next character from the buffer.  Return false if no characters
132         are available, or arrive before xBlockTime expires. */
133         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
134         {
135                 return pdTRUE;
136         }
137         else
138         {
139                 return pdFALSE;
140         }
141 }
142 /*-----------------------------------------------------------*/
143
144 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
145 {
146 signed char *pxNext;
147
148         /* A couple of parameters that this port does not use. */
149         ( void ) usStringLength;
150         ( void ) pxPort;
151
152         /* NOTE: This implementation does not handle the queue being full as no
153         block time is used! */
154
155         /* The port handle is not required as this driver only supports UART0. */
156         ( void ) pxPort;
157
158         /* Send each character in the string, one at a time. */
159         pxNext = ( signed char * ) pcString;
160         while( *pxNext )
161         {
162                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
163                 pxNext++;
164         }
165 }
166 /*-----------------------------------------------------------*/
167
168 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
169 {
170         /* Place the character in the queue of characters to be transmitted. */
171         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
172         {
173                 return pdFAIL;
174         }
175
176         /* Turn on the Tx interrupt so the ISR will remove the character from the
177         queue and send it.   This does not need to be in a critical section as
178         if the interrupt has already removed the character the next interrupt
179         will simply turn off the Tx interrupt again. */
180         vInterruptOn();
181
182         return pdPASS;
183 }
184 /*-----------------------------------------------------------*/
185
186 void vSerialClose( xComPortHandle xPort )
187 {
188         /* Not supported as not required by the demo application. */
189 }
190 /*-----------------------------------------------------------*/
191
192 void vSerialISR( void )
193 {
194 unsigned long ulStatus;
195 signed char cChar;
196 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
197
198         /* What caused the interrupt? */
199         ulStatus = BOARD_USART_BASE->US_CSR &= BOARD_USART_BASE->US_IMR;
200
201         if( ulStatus & AT91C_US_TXRDY )
202         {
203                 /* The interrupt was caused by the THR becoming empty.  Are there any
204                 more characters to transmit? */
205                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
206                 {
207                         /* A character was retrieved from the queue so can be sent to the
208                         THR now. */
209                         BOARD_USART_BASE->US_THR = cChar;
210                 }
211                 else
212                 {
213                         /* Queue empty, nothing to send so turn off the Tx interrupt. */
214                         vInterruptOff();
215                 }               
216         }
217
218         if( ulStatus & AT91C_US_RXRDY )
219         {
220                 /* The interrupt was caused by a character being received.  Grab the
221                 character from the RHR and place it in the queue or received
222                 characters. */
223                 cChar = BOARD_USART_BASE->US_RHR;
224                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
225         }
226
227         /* If a task was woken by either a character being received or a character
228         being transmitted then we may need to switch to another task. */
229         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
230 }
231
232
233
234
235