]> git.sur5r.net Git - freertos/blob - Demo/ARM7_AT91FR40008_GCC/serial/serial.c
Update to FreeRTOS V6.1.0 release candidate.
[freertos] / Demo / ARM7_AT91FR40008_GCC / serial / serial.c
1 /*\r
2     FreeRTOS V6.1.0 - Copyright (C) 2010 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* \r
55         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0. \r
56 \r
57         This file contains all the serial port components that can be compiled to\r
58         either ARM or THUMB mode.  Components that must be compiled to ARM mode are\r
59         contained in serialISR.c.\r
60 */\r
61 \r
62 /* Standard includes. */\r
63 #include <stdlib.h>\r
64 \r
65 /* Scheduler includes. */\r
66 #include "FreeRTOS.h"\r
67 #include "queue.h"\r
68 #include "task.h"\r
69 \r
70 /* Demo application includes. */\r
71 #include "serial.h"\r
72 #include "AT91R40008.h"\r
73 #include "usart.h"\r
74 #include "pio.h"\r
75 #include "aic.h"\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /* Constants to setup and access the UART. */\r
80 #define portUSART0_AIC_CHANNEL  ( ( unsigned long ) 2 )\r
81 \r
82 #define serINVALID_QUEUE                ( ( xQueueHandle ) 0 )\r
83 #define serHANDLE                               ( ( xComPortHandle ) 1 )\r
84 #define serNO_BLOCK                             ( ( portTickType ) 0 )\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /* Queues used to hold received characters, and characters waiting to be\r
89 transmitted. */\r
90 static xQueueHandle xRxedChars; \r
91 static xQueueHandle xCharsForTx; \r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /* \r
96  * The queues are created in serialISR.c as they are used from the ISR.\r
97  * Obtain references to the queues and THRE Empty flag. \r
98  */\r
99 extern void vSerialISRCreateQueues(  unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx );\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
104 {\r
105 unsigned long ulSpeed;\r
106 unsigned long ulCD;\r
107 xComPortHandle xReturn = serHANDLE;\r
108 extern void ( vUART_ISR_Wrapper )( void );\r
109 \r
110         /* The queues are used in the serial ISR routine, so are created from\r
111         serialISR.c (which is always compiled to ARM mode. */\r
112         vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx );\r
113 \r
114         if( \r
115                 ( xRxedChars != serINVALID_QUEUE ) && \r
116                 ( xCharsForTx != serINVALID_QUEUE ) && \r
117                 ( ulWantedBaud != ( unsigned long ) 0 ) \r
118           )\r
119         {\r
120                 portENTER_CRITICAL();\r
121                 {\r
122                         /* Enable clock to USART0... */\r
123                         AT91C_BASE_PS->PS_PCER = AT91C_PS_US0;\r
124 \r
125                         /* Disable all USART0 interrupt sources to begin... */\r
126                         AT91C_BASE_US0->US_IDR = 0xFFFFFFFF;\r
127 \r
128                         /* Reset various status bits (just in case)... */\r
129                         AT91C_BASE_US0->US_CR = US_RSTSTA;\r
130 \r
131                         AT91C_BASE_PIO->PIO_PDR = TXD0 | RXD0;  /* Enable RXD and TXD pins */\r
132                         AT91C_BASE_US0->US_CR = US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS;\r
133 \r
134                         /* Clear Transmit and Receive Counters */\r
135                         AT91C_BASE_US0->US_RCR = 0;\r
136                         AT91C_BASE_US0->US_TCR = 0;\r
137 \r
138                         /* Input clock to baud rate generator is MCK */\r
139                         ulSpeed = configCPU_CLOCK_HZ * 10;  \r
140                         ulSpeed = ulSpeed / 16;\r
141                         ulSpeed = ulSpeed / ulWantedBaud;\r
142                         \r
143                         /* compute the error */\r
144                         ulCD  = ulSpeed / 10;\r
145                         if ((ulSpeed - (ulCD * 10)) >= 5)\r
146                         ulCD++;\r
147 \r
148                         /* Define the baud rate divisor register */\r
149                         AT91C_BASE_US0->US_BRGR = ulCD;\r
150 \r
151                         /* Define the USART mode */\r
152                         AT91C_BASE_US0->US_MR = US_CLKS_MCK | US_CHRL_8 | US_PAR_NO | US_NBSTOP_1 | US_CHMODE_NORMAL;\r
153 \r
154                         /* Write the Timeguard Register */\r
155                         AT91C_BASE_US0->US_TTGR = 0;\r
156 \r
157                         /* Setup the interrupt for USART0.\r
158 \r
159                         Store interrupt handler function address in USART0 vector register... */\r
160                         AT91C_BASE_AIC->AIC_SVR[ portUSART0_AIC_CHANNEL ] = (unsigned long)vUART_ISR_Wrapper;\r
161                         \r
162                         /* USART0 interrupt level-sensitive, priority 1... */\r
163                         AT91C_BASE_AIC->AIC_SMR[ portUSART0_AIC_CHANNEL ] = AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 1;\r
164                         \r
165                         /* Clear some pending USART0 interrupts (just in case)... */\r
166                         AT91C_BASE_US0->US_CR = US_RSTSTA;\r
167 \r
168                         /* Enable USART0 interrupt sources (but not Tx for now)... */\r
169                         AT91C_BASE_US0->US_IER = US_RXRDY;\r
170 \r
171                         /* Enable USART0 interrupts in the AIC... */\r
172                         AT91C_BASE_AIC->AIC_IECR = ( 1 << portUSART0_AIC_CHANNEL );\r
173 \r
174                         /* Enable receiver and transmitter... */\r
175                         AT91C_BASE_US0->US_CR = US_RXEN | US_TXEN;\r
176                 }\r
177                 portEXIT_CRITICAL();\r
178         }\r
179         else\r
180         {\r
181                 xReturn = ( xComPortHandle ) 0;\r
182         }\r
183 \r
184         return xReturn;\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
189 {\r
190         /* The port handle is not required as this driver only supports UART0. */\r
191         ( void ) pxPort;\r
192 \r
193         /* Get the next character from the buffer.  Return false if no characters\r
194         are available, or arrive before xBlockTime expires. */\r
195         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
196         {\r
197                 return pdTRUE;\r
198         }\r
199         else\r
200         {\r
201                 return pdFALSE;\r
202         }\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
207 {\r
208 signed char *pxNext;\r
209 \r
210         /* NOTE: This implementation does not handle the queue being full as no\r
211         block time is used! */\r
212 \r
213         /* The port handle is not required as this driver only supports UART0. */\r
214         ( void ) pxPort;\r
215         ( void ) usStringLength;\r
216 \r
217         /* Send each character in the string, one at a time. */\r
218         pxNext = ( signed char * ) pcString;\r
219         while( *pxNext )\r
220         {\r
221                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
222                 pxNext++;\r
223         }\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
228 {\r
229         ( void ) pxPort;\r
230 \r
231         /* Place the character in the queue of characters to be transmitted. */\r
232         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
233         {\r
234                 return pdFAIL;\r
235         }\r
236 \r
237         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
238         queue and send it.   This does not need to be in a critical section as\r
239         if the interrupt has already removed the character the next interrupt\r
240         will simply turn off the Tx interrupt again. */\r
241         AT91C_BASE_US0->US_IER = US_TXRDY;\r
242 \r
243         return pdPASS;\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 void vSerialClose( xComPortHandle xPort )\r
248 {\r
249         /* Not supported as not required by the demo application. */\r
250         ( void ) xPort;\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r