]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_AT91FR40008_GCC/serial/serial.c
fd3149ebb69fce5eba3f7d8e1243e6368aeec8f1
[freertos] / FreeRTOS / Demo / ARM7_AT91FR40008_GCC / serial / serial.c
1 /*\r
2     FreeRTOS V8.2.3 - Copyright (C) 2015 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         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0. \r
72 \r
73         This file contains all the serial port components that can be compiled to\r
74         either ARM or THUMB mode.  Components that must be compiled to ARM mode are\r
75         contained in serialISR.c.\r
76 */\r
77 \r
78 /* Standard includes. */\r
79 #include <stdlib.h>\r
80 \r
81 /* Scheduler includes. */\r
82 #include "FreeRTOS.h"\r
83 #include "queue.h"\r
84 #include "task.h"\r
85 \r
86 /* Demo application includes. */\r
87 #include "serial.h"\r
88 #include "AT91R40008.h"\r
89 #include "usart.h"\r
90 #include "pio.h"\r
91 #include "aic.h"\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /* Constants to setup and access the UART. */\r
96 #define portUSART0_AIC_CHANNEL  ( ( unsigned long ) 2 )\r
97 \r
98 #define serINVALID_QUEUE                ( ( QueueHandle_t ) 0 )\r
99 #define serHANDLE                               ( ( xComPortHandle ) 1 )\r
100 #define serNO_BLOCK                             ( ( TickType_t ) 0 )\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 /* Queues used to hold received characters, and characters waiting to be\r
105 transmitted. */\r
106 static QueueHandle_t xRxedChars; \r
107 static QueueHandle_t xCharsForTx; \r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /* \r
112  * The queues are created in serialISR.c as they are used from the ISR.\r
113  * Obtain references to the queues and THRE Empty flag. \r
114  */\r
115 extern void vSerialISRCreateQueues(  unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx );\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
120 {\r
121 unsigned long ulSpeed;\r
122 unsigned long ulCD;\r
123 xComPortHandle xReturn = serHANDLE;\r
124 extern void ( vUART_ISR_Wrapper )( void );\r
125 \r
126         /* The queues are used in the serial ISR routine, so are created from\r
127         serialISR.c (which is always compiled to ARM mode. */\r
128         vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx );\r
129 \r
130         if( \r
131                 ( xRxedChars != serINVALID_QUEUE ) && \r
132                 ( xCharsForTx != serINVALID_QUEUE ) && \r
133                 ( ulWantedBaud != ( unsigned long ) 0 ) \r
134           )\r
135         {\r
136                 portENTER_CRITICAL();\r
137                 {\r
138                         /* Enable clock to USART0... */\r
139                         AT91C_BASE_PS->PS_PCER = AT91C_PS_US0;\r
140 \r
141                         /* Disable all USART0 interrupt sources to begin... */\r
142                         AT91C_BASE_US0->US_IDR = 0xFFFFFFFF;\r
143 \r
144                         /* Reset various status bits (just in case)... */\r
145                         AT91C_BASE_US0->US_CR = US_RSTSTA;\r
146 \r
147                         AT91C_BASE_PIO->PIO_PDR = TXD0 | RXD0;  /* Enable RXD and TXD pins */\r
148                         AT91C_BASE_US0->US_CR = US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS;\r
149 \r
150                         /* Clear Transmit and Receive Counters */\r
151                         AT91C_BASE_US0->US_RCR = 0;\r
152                         AT91C_BASE_US0->US_TCR = 0;\r
153 \r
154                         /* Input clock to baud rate generator is MCK */\r
155                         ulSpeed = configCPU_CLOCK_HZ * 10;  \r
156                         ulSpeed = ulSpeed / 16;\r
157                         ulSpeed = ulSpeed / ulWantedBaud;\r
158                         \r
159                         /* compute the error */\r
160                         ulCD  = ulSpeed / 10;\r
161                         if ((ulSpeed - (ulCD * 10)) >= 5)\r
162                         ulCD++;\r
163 \r
164                         /* Define the baud rate divisor register */\r
165                         AT91C_BASE_US0->US_BRGR = ulCD;\r
166 \r
167                         /* Define the USART mode */\r
168                         AT91C_BASE_US0->US_MR = US_CLKS_MCK | US_CHRL_8 | US_PAR_NO | US_NBSTOP_1 | US_CHMODE_NORMAL;\r
169 \r
170                         /* Write the Timeguard Register */\r
171                         AT91C_BASE_US0->US_TTGR = 0;\r
172 \r
173                         /* Setup the interrupt for USART0.\r
174 \r
175                         Store interrupt handler function address in USART0 vector register... */\r
176                         AT91C_BASE_AIC->AIC_SVR[ portUSART0_AIC_CHANNEL ] = (unsigned long)vUART_ISR_Wrapper;\r
177                         \r
178                         /* USART0 interrupt level-sensitive, priority 1... */\r
179                         AT91C_BASE_AIC->AIC_SMR[ portUSART0_AIC_CHANNEL ] = AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 1;\r
180                         \r
181                         /* Clear some pending USART0 interrupts (just in case)... */\r
182                         AT91C_BASE_US0->US_CR = US_RSTSTA;\r
183 \r
184                         /* Enable USART0 interrupt sources (but not Tx for now)... */\r
185                         AT91C_BASE_US0->US_IER = US_RXRDY;\r
186 \r
187                         /* Enable USART0 interrupts in the AIC... */\r
188                         AT91C_BASE_AIC->AIC_IECR = ( 1 << portUSART0_AIC_CHANNEL );\r
189 \r
190                         /* Enable receiver and transmitter... */\r
191                         AT91C_BASE_US0->US_CR = US_RXEN | US_TXEN;\r
192                 }\r
193                 portEXIT_CRITICAL();\r
194         }\r
195         else\r
196         {\r
197                 xReturn = ( xComPortHandle ) 0;\r
198         }\r
199 \r
200         return xReturn;\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
205 {\r
206         /* The port handle is not required as this driver only supports UART0. */\r
207         ( void ) pxPort;\r
208 \r
209         /* Get the next character from the buffer.  Return false if no characters\r
210         are available, or arrive before xBlockTime expires. */\r
211         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
212         {\r
213                 return pdTRUE;\r
214         }\r
215         else\r
216         {\r
217                 return pdFALSE;\r
218         }\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
223 {\r
224 signed char *pxNext;\r
225 \r
226         /* NOTE: This implementation does not handle the queue being full as no\r
227         block time is used! */\r
228 \r
229         /* The port handle is not required as this driver only supports UART0. */\r
230         ( void ) pxPort;\r
231         ( void ) usStringLength;\r
232 \r
233         /* Send each character in the string, one at a time. */\r
234         pxNext = ( signed char * ) pcString;\r
235         while( *pxNext )\r
236         {\r
237                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
238                 pxNext++;\r
239         }\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
244 {\r
245         ( void ) pxPort;\r
246 \r
247         /* Place the character in the queue of characters to be transmitted. */\r
248         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
249         {\r
250                 return pdFAIL;\r
251         }\r
252 \r
253         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
254         queue and send it.   This does not need to be in a critical section as\r
255         if the interrupt has already removed the character the next interrupt\r
256         will simply turn off the Tx interrupt again. */\r
257         AT91C_BASE_US0->US_IER = US_TXRDY;\r
258 \r
259         return pdPASS;\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 void vSerialClose( xComPortHandle xPort )\r
264 {\r
265         /* Not supported as not required by the demo application. */\r
266         ( void ) xPort;\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r