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