]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_STR75x_GCC/serial/serial.c
43db30dd1971195e5cf7cff76707b727788986a1
[freertos] / FreeRTOS / Demo / ARM7_STR75x_GCC / serial / serial.c
1 /*\r
2     FreeRTOS V8.0.0:rc1 - 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 distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! 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 UART0.\r
68 */\r
69 \r
70 \r
71 /*-----------------------------------------------------------\r
72  * Components that can be compiled to either ARM or THUMB mode are\r
73  * contained in this file.c  The ISR routines, which can only be compiled\r
74  * to ARM mode, are contained in serialISR.c.\r
75  *----------------------------------------------------------*/\r
76 \r
77 \r
78 \r
79 /* Library includes. */\r
80 #include "75x_uart.h"\r
81 #include "75x_gpio.h"\r
82 #include "75x_eic.h"\r
83 #include "75x_mrcc.h"\r
84 \r
85 /* Scheduler includes. */\r
86 #include "FreeRTOS.h"\r
87 #include "queue.h"\r
88 \r
89 /* Demo application includes. */\r
90 #include "serial.h"\r
91 \r
92 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )\r
93 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 /* Queues used to hold received characters, and characters waiting to be\r
98 transmitted. */\r
99 static QueueHandle_t xRxedChars;\r
100 static QueueHandle_t xCharsForTx;\r
101 \r
102 static volatile portBASE_TYPE xQueueEmpty = pdTRUE;\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 /* The interrupt service routine - called from the assembly entry point. */\r
107 void vSerialISR( void );\r
108 void vConfigureQueues( QueueHandle_t xQForRx, QueueHandle_t xQForTx, volatile portBASE_TYPE *pxEmptyFlag );\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /*\r
113  * See the serial2.h header file.\r
114  */\r
115 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
116 {\r
117 xComPortHandle xReturn;\r
118 UART_InitTypeDef UART_InitStructure;\r
119 GPIO_InitTypeDef GPIO_InitStructure;\r
120 EIC_IRQInitTypeDef  EIC_IRQInitStructure;       \r
121 \r
122         /* Create the queues used to hold Rx and Tx characters. */\r
123         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
124         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
125 \r
126         /* If the queues were created correctly then setup the serial port\r
127         hardware. */\r
128         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
129         {\r
130         \r
131                 vConfigureQueues( xRxedChars, xCharsForTx, &xQueueEmpty );\r
132         \r
133                 portENTER_CRITICAL();\r
134                 {\r
135                         /* Enable the UART0 Clock. */\r
136                         MRCC_PeripheralClockConfig( MRCC_Peripheral_UART0, ENABLE );\r
137                         \r
138                         /* Configure the UART0_Tx as alternate function */\r
139                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;\r
140                         GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_11;\r
141                         GPIO_Init(GPIO0, &GPIO_InitStructure);\r
142                         \r
143                         /* Configure the UART0_Rx as input floating */\r
144                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;\r
145                         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;\r
146                         GPIO_Init(GPIO0, &GPIO_InitStructure);\r
147                         \r
148                         /* Configure UART0. */\r
149                         UART_InitStructure.UART_WordLength = UART_WordLength_8D;\r
150                         UART_InitStructure.UART_StopBits = UART_StopBits_1;\r
151                         UART_InitStructure.UART_Parity = UART_Parity_No;\r
152                         UART_InitStructure.UART_BaudRate = ulWantedBaud;\r
153                         UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;\r
154                         UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx;\r
155                         UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */\r
156                         UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */\r
157                         UART_Init(UART0, &UART_InitStructure);\r
158 \r
159                         /* Enable the UART0 */\r
160                         UART_Cmd(UART0, ENABLE);\r
161 \r
162                         /* Configure the IEC for the UART interrupts. */                        \r
163                         EIC_IRQInitStructure.EIC_IRQChannelCmd = ENABLE;\r
164                         EIC_IRQInitStructure.EIC_IRQChannel = UART0_IRQChannel;\r
165                         EIC_IRQInitStructure.EIC_IRQChannelPriority = 1;\r
166                         EIC_IRQInit(&EIC_IRQInitStructure);\r
167                         \r
168                         xQueueEmpty = pdTRUE;\r
169                         UART_ITConfig( UART0, UART_IT_Transmit | UART_IT_Receive, ENABLE );\r
170                 }\r
171                 portEXIT_CRITICAL();\r
172         }\r
173         else\r
174         {\r
175                 xReturn = ( xComPortHandle ) 0;\r
176         }\r
177 \r
178         /* This demo file only supports a single port but we have to return\r
179         something to comply with the standard demo header file. */\r
180         return xReturn;\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
185 {\r
186         /* The port handle is not required as this driver only supports one port. */\r
187         ( void ) pxPort;\r
188 \r
189         /* Get the next character from the buffer.  Return false if no characters\r
190         are available, or arrive before xBlockTime expires. */\r
191         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
192         {\r
193                 return pdTRUE;\r
194         }\r
195         else\r
196         {\r
197                 return pdFALSE;\r
198         }\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
203 {\r
204 signed char *pxNext;\r
205 \r
206         /* A couple of parameters that this port does not use. */\r
207         ( void ) usStringLength;\r
208         ( void ) pxPort;\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 \r
216         /* Send each character in the string, one at a time. */\r
217         pxNext = ( signed char * ) pcString;\r
218         while( *pxNext )\r
219         {\r
220                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
221                 pxNext++;\r
222         }\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
227 {\r
228 portBASE_TYPE xReturn;\r
229 \r
230         /* Place the character in the queue of characters to be transmitted. */\r
231         portENTER_CRITICAL();\r
232         {\r
233                 if( xQueueEmpty == pdTRUE )\r
234                 {\r
235                         UART0->DR = cOutChar;\r
236                         xReturn = pdPASS;\r
237                 }\r
238                 else\r
239                 {\r
240                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
241                         {\r
242                                 xReturn = pdFAIL;\r
243                         }                       \r
244                         else\r
245                         {\r
246                                 xReturn = pdPASS;                               \r
247                         }\r
248                 }\r
249                 \r
250                 xQueueEmpty = pdFALSE;\r
251         }\r
252         portEXIT_CRITICAL();\r
253 \r
254         return xReturn;\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 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 \r
265 \r
266 \r
267 \r
268         \r