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