]> begriffs open source - cmsis-freertos/blob - Demo/ARM7_AT91SAM7S64_IAR/serial/serial.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / ARM7_AT91SAM7S64_IAR / serial / serial.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70 /* 
71         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. 
72 */
73
74 /* Standard includes. */ 
75 #include <stdlib.h>
76
77 /* Scheduler includes. */
78 #include "FreeRTOS.h"
79 #include "queue.h"
80
81 /* Demo application includes. */
82 #include "serial.h"
83
84 /*-----------------------------------------------------------*/
85
86 /* Location of the COM0 registers. */
87 #define serCOM0                                                 ( ( AT91PS_USART ) AT91C_BASE_US0 )
88
89 /* Interrupt control macros. */
90 #define serINTERRUPT_LEVEL                              ( 5 )
91 #define vInterruptOn()                                  AT91F_US_EnableIt( serCOM0, AT91C_US_TXRDY | AT91C_US_RXRDY )
92 #define vInterruptOff()                                 AT91F_US_DisableIt( serCOM0, AT91C_US_TXRDY )
93
94 /* Misc constants. */
95 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )
96 #define serHANDLE                                               ( ( xComPortHandle ) 1 )
97 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )
98 #define serNO_TIMEGUARD                                 ( ( unsigned long ) 0 )
99 #define serNO_PERIPHERAL_B_SETUP                ( ( unsigned long ) 0 )
100
101
102 /* Queues used to hold received characters, and characters waiting to be
103 transmitted. */
104 static QueueHandle_t xRxedChars; 
105 static QueueHandle_t xCharsForTx; 
106
107 /*-----------------------------------------------------------*/
108
109 /* Interrupt entry point written in the assembler file serialISR.s79. */
110 extern void vSerialISREntry( void );
111
112 /* The interrupt service routine - called from the assembly entry point. */
113 __arm void vSerialISR( void );
114
115 /*-----------------------------------------------------------*/
116
117 /*
118  * See the serial2.h header file.
119  */
120 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
121 {
122 xComPortHandle xReturn = serHANDLE;
123 extern void ( vUART_ISR )( void );
124
125         /* Create the queues used to hold Rx and Tx characters. */
126         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
127         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
128
129         /* If the queues were created correctly then setup the serial port 
130         hardware. */
131         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
132         {
133                 portENTER_CRITICAL();
134                 {
135                         /* Enable the USART clock. */
136                         AT91F_PMC_EnablePeriphClock( AT91C_BASE_PMC, 1 << AT91C_ID_US0 );
137
138                         AT91F_PIO_CfgPeriph( AT91C_BASE_PIOA, ( ( unsigned long ) AT91C_PA5_RXD0 ) | ( ( unsigned long ) AT91C_PA6_TXD0 ), serNO_PERIPHERAL_B_SETUP );
139
140                         /* Set the required protocol. */
141                         AT91F_US_Configure( serCOM0, configCPU_CLOCK_HZ, AT91C_US_ASYNC_MODE, ulWantedBaud, serNO_TIMEGUARD );
142
143                         /* Enable Rx and Tx. */
144                         serCOM0->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
145
146                         /* Enable the Rx interrupts.  The Tx interrupts are not enabled
147                         until there are characters to be transmitted. */
148                 AT91F_US_EnableIt( serCOM0, AT91C_US_RXRDY );
149
150                         /* Enable the interrupts in the AIC. */
151                         AT91F_AIC_ConfigureIt( AT91C_BASE_AIC, AT91C_ID_US0, serINTERRUPT_LEVEL, AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, ( void (*)( void ) ) vSerialISREntry );
152                         AT91F_AIC_EnableIt( AT91C_BASE_AIC, AT91C_ID_US0 );
153                 }
154                 portEXIT_CRITICAL();
155         }
156         else
157         {
158                 xReturn = ( xComPortHandle ) 0;
159         }
160
161         /* This demo file only supports a single port but we have to return 
162         something to comply with the standard demo header file. */
163         return xReturn;
164 }
165 /*-----------------------------------------------------------*/
166
167 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
168 {
169         /* The port handle is not required as this driver only supports one port. */
170         ( void ) pxPort;
171
172         /* Get the next character from the buffer.  Return false if no characters
173         are available, or arrive before xBlockTime expires. */
174         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
175         {
176                 return pdTRUE;
177         }
178         else
179         {
180                 return pdFALSE;
181         }
182 }
183 /*-----------------------------------------------------------*/
184
185 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
186 {
187 signed char *pxNext;
188
189         /* A couple of parameters that this port does not use. */
190         ( void ) usStringLength;
191         ( void ) pxPort;
192
193         /* NOTE: This implementation does not handle the queue being full as no
194         block time is used! */
195
196         /* The port handle is not required as this driver only supports UART0. */
197         ( void ) pxPort;
198
199         /* Send each character in the string, one at a time. */
200         pxNext = ( signed char * ) pcString;
201         while( *pxNext )
202         {
203                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
204                 pxNext++;
205         }
206 }
207 /*-----------------------------------------------------------*/
208
209 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
210 {
211         /* Place the character in the queue of characters to be transmitted. */
212         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
213         {
214                 return pdFAIL;
215         }
216
217         /* Turn on the Tx interrupt so the ISR will remove the character from the
218         queue and send it.   This does not need to be in a critical section as
219         if the interrupt has already removed the character the next interrupt
220         will simply turn off the Tx interrupt again. */
221         vInterruptOn();
222
223         return pdPASS;
224 }
225 /*-----------------------------------------------------------*/
226
227 void vSerialClose( xComPortHandle xPort )
228 {
229         /* Not supported as not required by the demo application. */
230 }
231 /*-----------------------------------------------------------*/
232
233 /* Serial port ISR.  This can cause a context switch so is not defined as a
234 standard ISR using the __irq keyword.  Instead a wrapper function is defined
235 within serialISR.s79 which in turn calls this function.  See the port
236 documentation on the FreeRTOS.org website for more information. */
237 __arm void vSerialISR( void )
238 {
239 unsigned long ulStatus;
240 signed char cChar;
241 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
242
243         /* What caused the interrupt? */
244         ulStatus = serCOM0->US_CSR &= serCOM0->US_IMR;
245
246         if( ulStatus & AT91C_US_TXRDY )
247         {
248                 /* The interrupt was caused by the THR becoming empty.  Are there any
249                 more characters to transmit? */
250                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
251                 {
252                         /* A character was retrieved from the queue so can be sent to the
253                         THR now. */
254                         serCOM0->US_THR = cChar;
255                 }
256                 else
257                 {
258                         /* Queue empty, nothing to send so turn off the Tx interrupt. */
259                         vInterruptOff();
260                 }               
261         }
262
263         if( ulStatus & AT91C_US_RXRDY )
264         {
265                 /* The interrupt was caused by a character being received.  Grab the
266                 character from the RHR and place it in the queue or received 
267                 characters. */
268                 cChar = serCOM0->US_RHR;
269                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
270         }
271
272         /* If a task was woken by either a character being received or a character 
273         being transmitted then we may need to switch to another task. */
274         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
275
276         /* End the interrupt in the AIC. */
277         AT91C_BASE_AIC->AIC_EOICR = 0;
278 }
279
280
281
282
283