]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_AT91FR40008_GCC/serial/serial.c
d449372c94af55436fb89dfdae8a0108a4b0af67
[freertos] / FreeRTOS / Demo / ARM7_AT91FR40008_GCC / serial / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.1.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* \r
29         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0. \r
30 \r
31         This file contains all the serial port components that can be compiled to\r
32         either ARM or THUMB mode.  Components that must be compiled to ARM mode are\r
33         contained in serialISR.c.\r
34 */\r
35 \r
36 /* Standard includes. */\r
37 #include <stdlib.h>\r
38 \r
39 /* Scheduler includes. */\r
40 #include "FreeRTOS.h"\r
41 #include "queue.h"\r
42 #include "task.h"\r
43 \r
44 /* Demo application includes. */\r
45 #include "serial.h"\r
46 #include "AT91R40008.h"\r
47 #include "usart.h"\r
48 #include "pio.h"\r
49 #include "aic.h"\r
50 \r
51 /*-----------------------------------------------------------*/\r
52 \r
53 /* Constants to setup and access the UART. */\r
54 #define portUSART0_AIC_CHANNEL  ( ( unsigned long ) 2 )\r
55 \r
56 #define serINVALID_QUEUE                ( ( QueueHandle_t ) 0 )\r
57 #define serHANDLE                               ( ( xComPortHandle ) 1 )\r
58 #define serNO_BLOCK                             ( ( TickType_t ) 0 )\r
59 \r
60 /*-----------------------------------------------------------*/\r
61 \r
62 /* Queues used to hold received characters, and characters waiting to be\r
63 transmitted. */\r
64 static QueueHandle_t xRxedChars; \r
65 static QueueHandle_t xCharsForTx; \r
66 \r
67 /*-----------------------------------------------------------*/\r
68 \r
69 /* \r
70  * The queues are created in serialISR.c as they are used from the ISR.\r
71  * Obtain references to the queues and THRE Empty flag. \r
72  */\r
73 extern void vSerialISRCreateQueues(  unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx );\r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
78 {\r
79 unsigned long ulSpeed;\r
80 unsigned long ulCD;\r
81 xComPortHandle xReturn = serHANDLE;\r
82 extern void ( vUART_ISR_Wrapper )( void );\r
83 \r
84         /* The queues are used in the serial ISR routine, so are created from\r
85         serialISR.c (which is always compiled to ARM mode. */\r
86         vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx );\r
87 \r
88         if( \r
89                 ( xRxedChars != serINVALID_QUEUE ) && \r
90                 ( xCharsForTx != serINVALID_QUEUE ) && \r
91                 ( ulWantedBaud != ( unsigned long ) 0 ) \r
92           )\r
93         {\r
94                 portENTER_CRITICAL();\r
95                 {\r
96                         /* Enable clock to USART0... */\r
97                         AT91C_BASE_PS->PS_PCER = AT91C_PS_US0;\r
98 \r
99                         /* Disable all USART0 interrupt sources to begin... */\r
100                         AT91C_BASE_US0->US_IDR = 0xFFFFFFFF;\r
101 \r
102                         /* Reset various status bits (just in case)... */\r
103                         AT91C_BASE_US0->US_CR = US_RSTSTA;\r
104 \r
105                         AT91C_BASE_PIO->PIO_PDR = TXD0 | RXD0;  /* Enable RXD and TXD pins */\r
106                         AT91C_BASE_US0->US_CR = US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS;\r
107 \r
108                         /* Clear Transmit and Receive Counters */\r
109                         AT91C_BASE_US0->US_RCR = 0;\r
110                         AT91C_BASE_US0->US_TCR = 0;\r
111 \r
112                         /* Input clock to baud rate generator is MCK */\r
113                         ulSpeed = configCPU_CLOCK_HZ * 10;  \r
114                         ulSpeed = ulSpeed / 16;\r
115                         ulSpeed = ulSpeed / ulWantedBaud;\r
116                         \r
117                         /* compute the error */\r
118                         ulCD  = ulSpeed / 10;\r
119                         if ((ulSpeed - (ulCD * 10)) >= 5)\r
120                         ulCD++;\r
121 \r
122                         /* Define the baud rate divisor register */\r
123                         AT91C_BASE_US0->US_BRGR = ulCD;\r
124 \r
125                         /* Define the USART mode */\r
126                         AT91C_BASE_US0->US_MR = US_CLKS_MCK | US_CHRL_8 | US_PAR_NO | US_NBSTOP_1 | US_CHMODE_NORMAL;\r
127 \r
128                         /* Write the Timeguard Register */\r
129                         AT91C_BASE_US0->US_TTGR = 0;\r
130 \r
131                         /* Setup the interrupt for USART0.\r
132 \r
133                         Store interrupt handler function address in USART0 vector register... */\r
134                         AT91C_BASE_AIC->AIC_SVR[ portUSART0_AIC_CHANNEL ] = (unsigned long)vUART_ISR_Wrapper;\r
135                         \r
136                         /* USART0 interrupt level-sensitive, priority 1... */\r
137                         AT91C_BASE_AIC->AIC_SMR[ portUSART0_AIC_CHANNEL ] = AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 1;\r
138                         \r
139                         /* Clear some pending USART0 interrupts (just in case)... */\r
140                         AT91C_BASE_US0->US_CR = US_RSTSTA;\r
141 \r
142                         /* Enable USART0 interrupt sources (but not Tx for now)... */\r
143                         AT91C_BASE_US0->US_IER = US_RXRDY;\r
144 \r
145                         /* Enable USART0 interrupts in the AIC... */\r
146                         AT91C_BASE_AIC->AIC_IECR = ( 1 << portUSART0_AIC_CHANNEL );\r
147 \r
148                         /* Enable receiver and transmitter... */\r
149                         AT91C_BASE_US0->US_CR = US_RXEN | US_TXEN;\r
150                 }\r
151                 portEXIT_CRITICAL();\r
152         }\r
153         else\r
154         {\r
155                 xReturn = ( xComPortHandle ) 0;\r
156         }\r
157 \r
158         return xReturn;\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
163 {\r
164         /* The port handle is not required as this driver only supports UART0. */\r
165         ( void ) pxPort;\r
166 \r
167         /* Get the next character from the buffer.  Return false if no characters\r
168         are available, or arrive before xBlockTime expires. */\r
169         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
170         {\r
171                 return pdTRUE;\r
172         }\r
173         else\r
174         {\r
175                 return pdFALSE;\r
176         }\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
181 {\r
182 signed char *pxNext;\r
183 \r
184         /* NOTE: This implementation does not handle the queue being full as no\r
185         block time is used! */\r
186 \r
187         /* The port handle is not required as this driver only supports UART0. */\r
188         ( void ) pxPort;\r
189         ( void ) usStringLength;\r
190 \r
191         /* Send each character in the string, one at a time. */\r
192         pxNext = ( signed char * ) pcString;\r
193         while( *pxNext )\r
194         {\r
195                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
196                 pxNext++;\r
197         }\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
202 {\r
203         ( void ) pxPort;\r
204 \r
205         /* Place the character in the queue of characters to be transmitted. */\r
206         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
207         {\r
208                 return pdFAIL;\r
209         }\r
210 \r
211         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
212         queue and send it.   This does not need to be in a critical section as\r
213         if the interrupt has already removed the character the next interrupt\r
214         will simply turn off the Tx interrupt again. */\r
215         AT91C_BASE_US0->US_IER = US_TXRDY;\r
216 \r
217         return pdPASS;\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 void vSerialClose( xComPortHandle xPort )\r
222 {\r
223         /* Not supported as not required by the demo application. */\r
224         ( void ) xPort;\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r