]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_AT91FR40008_GCC/serial/serialISR.c
1197416586bd1cd58bf6c839353347538112b9f5
[freertos] / FreeRTOS / Demo / ARM7_AT91FR40008_GCC / serial / serialISR.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /* \r
72   BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0. \r
73 \r
74   This file contains all the serial port components that must be compiled\r
75   to ARM mode.  The components that can be compiled to either ARM or THUMB\r
76   mode are contained in serial.c.\r
77 \r
78 */\r
79 \r
80 /* Standard includes. */\r
81 #include <stdlib.h>\r
82 \r
83 /* Scheduler includes. */\r
84 #include "FreeRTOS.h"\r
85 #include "queue.h"\r
86 #include "task.h"\r
87 \r
88 /* Demo application includes. */\r
89 #include "serial.h"\r
90 #include "AT91R40008.h"\r
91 #include "usart.h"\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /* Constant to access the AIC. */\r
96 #define serCLEAR_AIC_INTERRUPT      ( ( unsigned long ) 0 )\r
97 \r
98 /* Constants to determine the ISR source. */\r
99 #define serSOURCE_THRE                          ( ( unsigned char ) 0x02 )\r
100 #define serSOURCE_RX_TIMEOUT            ( ( unsigned char ) 0x0c )\r
101 #define serSOURCE_ERROR                         ( ( unsigned char ) 0x06 )\r
102 #define serSOURCE_RX                            ( ( unsigned char ) 0x04 )\r
103 #define serINTERRUPT_SOURCE_MASK    ( ( unsigned long ) (US_RXRDY | US_TXRDY | US_RXBRK | US_OVRE | US_FRAME | US_PARE) )\r
104 \r
105 /* Queues used to hold received characters, and characters waiting to be\r
106 transmitted. */\r
107 static QueueHandle_t xRxedChars; \r
108 static QueueHandle_t xCharsForTx; \r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /* UART0 interrupt service routine.  This can cause a context switch so MUST\r
113 be declared "naked". */\r
114 void vUART_ISR_Wrapper( void ) __attribute__ ((naked));\r
115 \r
116 /* The ISR function that actually performs the work.  This must be separate \r
117 from the wrapper to ensure the correct stack frame is set up. */\r
118 void vUART_ISR_Handler( void ) __attribute__ ((noinline));\r
119 \r
120 /*-----------------------------------------------------------*/\r
121 void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx )\r
122 {\r
123         /* Create the queues used to hold Rx and Tx characters. */\r
124         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
125         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
126 \r
127         /* Pass back a reference to the queues so the serial API file can \r
128         post/receive characters. */\r
129         *pxRxedChars = xRxedChars;\r
130         *pxCharsForTx = xCharsForTx;\r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 void vUART_ISR_Wrapper( void )\r
135 {\r
136         /* Save the context of the interrupted task. */\r
137         portSAVE_CONTEXT();\r
138 \r
139         /* Call the handler.  This must be a separate function to ensure the \r
140         stack frame is correctly set up. */\r
141         __asm volatile( "bl vUART_ISR_Handler" );\r
142 \r
143         /* Restore the context of whichever task will run next. */\r
144         portRESTORE_CONTEXT();\r
145 }\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 void vUART_ISR_Handler( void )\r
149 {\r
150 /* Now we can declare the local variables.   These must be static. */\r
151 signed char cChar;\r
152 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
153 unsigned long ulStatus;\r
154 \r
155         /* What caused the interrupt? */\r
156         ulStatus = AT91C_BASE_US0->US_CSR & AT91C_BASE_US0->US_IMR;\r
157 \r
158         if (ulStatus & US_TXRDY)\r
159         {\r
160                 /* The interrupt was caused by the THR becoming empty.  Are there any\r
161                 more characters to transmit? */\r
162                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
163                 {\r
164                         /* A character was retrieved from the queue so can be sent to the\r
165                         THR now. */\r
166                         AT91C_BASE_US0->US_THR = cChar;\r
167                 }\r
168                 else\r
169                 {\r
170                         /* Queue empty, nothing to send so turn off the Tx interrupt. */\r
171                         AT91C_BASE_US0->US_IDR = US_TXRDY;\r
172                 }    \r
173         }\r
174 \r
175         if (ulStatus & US_RXRDY)\r
176         {\r
177                 /* The interrupt was caused by the receiver getting data. */\r
178                 cChar = AT91C_BASE_US0->US_RHR;\r
179 \r
180                 xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken);\r
181         }\r
182 \r
183         /* Acknowledge the interrupt at AIC level... */\r
184         AT91C_BASE_AIC->AIC_EOICR = serCLEAR_AIC_INTERRUPT;\r
185 \r
186         /* If an event caused a task to unblock then we call "Yield from ISR" to\r
187         ensure that the unblocked task is the task that executes when the interrupt\r
188         completes if the unblocked task has a priority higher than the interrupted\r
189         task. */\r
190         if( xHigherPriorityTaskWoken )\r
191         {\r
192                 portYIELD_FROM_ISR();\r
193         }\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r