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