]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_AT91FR40008_GCC/serial/serial.c
8e59faeae1d0d464339532ef720539dbde219c4a
[freertos] / FreeRTOS / Demo / ARM7_AT91FR40008_GCC / serial / serial.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /* \r
97         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0. \r
98 \r
99         This file contains all the serial port components that can be compiled to\r
100         either ARM or THUMB mode.  Components that must be compiled to ARM mode are\r
101         contained in serialISR.c.\r
102 */\r
103 \r
104 /* Standard includes. */\r
105 #include <stdlib.h>\r
106 \r
107 /* Scheduler includes. */\r
108 #include "FreeRTOS.h"\r
109 #include "queue.h"\r
110 #include "task.h"\r
111 \r
112 /* Demo application includes. */\r
113 #include "serial.h"\r
114 #include "AT91R40008.h"\r
115 #include "usart.h"\r
116 #include "pio.h"\r
117 #include "aic.h"\r
118 \r
119 /*-----------------------------------------------------------*/\r
120 \r
121 /* Constants to setup and access the UART. */\r
122 #define portUSART0_AIC_CHANNEL  ( ( unsigned long ) 2 )\r
123 \r
124 #define serINVALID_QUEUE                ( ( QueueHandle_t ) 0 )\r
125 #define serHANDLE                               ( ( xComPortHandle ) 1 )\r
126 #define serNO_BLOCK                             ( ( TickType_t ) 0 )\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 /* Queues used to hold received characters, and characters waiting to be\r
131 transmitted. */\r
132 static QueueHandle_t xRxedChars; \r
133 static QueueHandle_t xCharsForTx; \r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 /* \r
138  * The queues are created in serialISR.c as they are used from the ISR.\r
139  * Obtain references to the queues and THRE Empty flag. \r
140  */\r
141 extern void vSerialISRCreateQueues(  unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx );\r
142 \r
143 /*-----------------------------------------------------------*/\r
144 \r
145 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
146 {\r
147 unsigned long ulSpeed;\r
148 unsigned long ulCD;\r
149 xComPortHandle xReturn = serHANDLE;\r
150 extern void ( vUART_ISR_Wrapper )( void );\r
151 \r
152         /* The queues are used in the serial ISR routine, so are created from\r
153         serialISR.c (which is always compiled to ARM mode. */\r
154         vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx );\r
155 \r
156         if( \r
157                 ( xRxedChars != serINVALID_QUEUE ) && \r
158                 ( xCharsForTx != serINVALID_QUEUE ) && \r
159                 ( ulWantedBaud != ( unsigned long ) 0 ) \r
160           )\r
161         {\r
162                 portENTER_CRITICAL();\r
163                 {\r
164                         /* Enable clock to USART0... */\r
165                         AT91C_BASE_PS->PS_PCER = AT91C_PS_US0;\r
166 \r
167                         /* Disable all USART0 interrupt sources to begin... */\r
168                         AT91C_BASE_US0->US_IDR = 0xFFFFFFFF;\r
169 \r
170                         /* Reset various status bits (just in case)... */\r
171                         AT91C_BASE_US0->US_CR = US_RSTSTA;\r
172 \r
173                         AT91C_BASE_PIO->PIO_PDR = TXD0 | RXD0;  /* Enable RXD and TXD pins */\r
174                         AT91C_BASE_US0->US_CR = US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS;\r
175 \r
176                         /* Clear Transmit and Receive Counters */\r
177                         AT91C_BASE_US0->US_RCR = 0;\r
178                         AT91C_BASE_US0->US_TCR = 0;\r
179 \r
180                         /* Input clock to baud rate generator is MCK */\r
181                         ulSpeed = configCPU_CLOCK_HZ * 10;  \r
182                         ulSpeed = ulSpeed / 16;\r
183                         ulSpeed = ulSpeed / ulWantedBaud;\r
184                         \r
185                         /* compute the error */\r
186                         ulCD  = ulSpeed / 10;\r
187                         if ((ulSpeed - (ulCD * 10)) >= 5)\r
188                         ulCD++;\r
189 \r
190                         /* Define the baud rate divisor register */\r
191                         AT91C_BASE_US0->US_BRGR = ulCD;\r
192 \r
193                         /* Define the USART mode */\r
194                         AT91C_BASE_US0->US_MR = US_CLKS_MCK | US_CHRL_8 | US_PAR_NO | US_NBSTOP_1 | US_CHMODE_NORMAL;\r
195 \r
196                         /* Write the Timeguard Register */\r
197                         AT91C_BASE_US0->US_TTGR = 0;\r
198 \r
199                         /* Setup the interrupt for USART0.\r
200 \r
201                         Store interrupt handler function address in USART0 vector register... */\r
202                         AT91C_BASE_AIC->AIC_SVR[ portUSART0_AIC_CHANNEL ] = (unsigned long)vUART_ISR_Wrapper;\r
203                         \r
204                         /* USART0 interrupt level-sensitive, priority 1... */\r
205                         AT91C_BASE_AIC->AIC_SMR[ portUSART0_AIC_CHANNEL ] = AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 1;\r
206                         \r
207                         /* Clear some pending USART0 interrupts (just in case)... */\r
208                         AT91C_BASE_US0->US_CR = US_RSTSTA;\r
209 \r
210                         /* Enable USART0 interrupt sources (but not Tx for now)... */\r
211                         AT91C_BASE_US0->US_IER = US_RXRDY;\r
212 \r
213                         /* Enable USART0 interrupts in the AIC... */\r
214                         AT91C_BASE_AIC->AIC_IECR = ( 1 << portUSART0_AIC_CHANNEL );\r
215 \r
216                         /* Enable receiver and transmitter... */\r
217                         AT91C_BASE_US0->US_CR = US_RXEN | US_TXEN;\r
218                 }\r
219                 portEXIT_CRITICAL();\r
220         }\r
221         else\r
222         {\r
223                 xReturn = ( xComPortHandle ) 0;\r
224         }\r
225 \r
226         return xReturn;\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
231 {\r
232         /* The port handle is not required as this driver only supports UART0. */\r
233         ( void ) pxPort;\r
234 \r
235         /* Get the next character from the buffer.  Return false if no characters\r
236         are available, or arrive before xBlockTime expires. */\r
237         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
238         {\r
239                 return pdTRUE;\r
240         }\r
241         else\r
242         {\r
243                 return pdFALSE;\r
244         }\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
249 {\r
250 signed char *pxNext;\r
251 \r
252         /* NOTE: This implementation does not handle the queue being full as no\r
253         block time is used! */\r
254 \r
255         /* The port handle is not required as this driver only supports UART0. */\r
256         ( void ) pxPort;\r
257         ( void ) usStringLength;\r
258 \r
259         /* Send each character in the string, one at a time. */\r
260         pxNext = ( signed char * ) pcString;\r
261         while( *pxNext )\r
262         {\r
263                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
264                 pxNext++;\r
265         }\r
266 }\r
267 /*-----------------------------------------------------------*/\r
268 \r
269 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
270 {\r
271         ( void ) pxPort;\r
272 \r
273         /* Place the character in the queue of characters to be transmitted. */\r
274         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
275         {\r
276                 return pdFAIL;\r
277         }\r
278 \r
279         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
280         queue and send it.   This does not need to be in a critical section as\r
281         if the interrupt has already removed the character the next interrupt\r
282         will simply turn off the Tx interrupt again. */\r
283         AT91C_BASE_US0->US_IER = US_TXRDY;\r
284 \r
285         return pdPASS;\r
286 }\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 void vSerialClose( xComPortHandle xPort )\r
290 {\r
291         /* Not supported as not required by the demo application. */\r
292         ( void ) xPort;\r
293 }\r
294 /*-----------------------------------------------------------*/\r
295 \r