]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_STR75x_GCC/serial/serial.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / ARM7_STR75x_GCC / serial / serial.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 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     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /*\r
71         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.\r
72 */\r
73 \r
74 \r
75 /*-----------------------------------------------------------\r
76  * Components that can be compiled to either ARM or THUMB mode are\r
77  * contained in this file.c  The ISR routines, which can only be compiled\r
78  * to ARM mode, are contained in serialISR.c.\r
79  *----------------------------------------------------------*/\r
80 \r
81 \r
82 \r
83 /* Library includes. */\r
84 #include "75x_uart.h"\r
85 #include "75x_gpio.h"\r
86 #include "75x_eic.h"\r
87 #include "75x_mrcc.h"\r
88 \r
89 /* Scheduler includes. */\r
90 #include "FreeRTOS.h"\r
91 #include "queue.h"\r
92 \r
93 /* Demo application includes. */\r
94 #include "serial.h"\r
95 \r
96 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )\r
97 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /* Queues used to hold received characters, and characters waiting to be\r
102 transmitted. */\r
103 static QueueHandle_t xRxedChars;\r
104 static QueueHandle_t xCharsForTx;\r
105 \r
106 static volatile portBASE_TYPE xQueueEmpty = pdTRUE;\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 /* The interrupt service routine - called from the assembly entry point. */\r
111 void vSerialISR( void );\r
112 void vConfigureQueues( QueueHandle_t xQForRx, QueueHandle_t xQForTx, volatile portBASE_TYPE *pxEmptyFlag );\r
113 \r
114 /*-----------------------------------------------------------*/\r
115 \r
116 /*\r
117  * See the serial2.h header file.\r
118  */\r
119 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
120 {\r
121 xComPortHandle xReturn;\r
122 UART_InitTypeDef UART_InitStructure;\r
123 GPIO_InitTypeDef GPIO_InitStructure;\r
124 EIC_IRQInitTypeDef  EIC_IRQInitStructure;       \r
125 \r
126         /* Create the queues used to hold Rx and Tx characters. */\r
127         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
128         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
129 \r
130         /* If the queues were created correctly then setup the serial port\r
131         hardware. */\r
132         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
133         {\r
134         \r
135                 vConfigureQueues( xRxedChars, xCharsForTx, &xQueueEmpty );\r
136         \r
137                 portENTER_CRITICAL();\r
138                 {\r
139                         /* Enable the UART0 Clock. */\r
140                         MRCC_PeripheralClockConfig( MRCC_Peripheral_UART0, ENABLE );\r
141                         \r
142                         /* Configure the UART0_Tx as alternate function */\r
143                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;\r
144                         GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_11;\r
145                         GPIO_Init(GPIO0, &GPIO_InitStructure);\r
146                         \r
147                         /* Configure the UART0_Rx as input floating */\r
148                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;\r
149                         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;\r
150                         GPIO_Init(GPIO0, &GPIO_InitStructure);\r
151                         \r
152                         /* Configure UART0. */\r
153                         UART_InitStructure.UART_WordLength = UART_WordLength_8D;\r
154                         UART_InitStructure.UART_StopBits = UART_StopBits_1;\r
155                         UART_InitStructure.UART_Parity = UART_Parity_No;\r
156                         UART_InitStructure.UART_BaudRate = ulWantedBaud;\r
157                         UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;\r
158                         UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx;\r
159                         UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */\r
160                         UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */\r
161                         UART_Init(UART0, &UART_InitStructure);\r
162 \r
163                         /* Enable the UART0 */\r
164                         UART_Cmd(UART0, ENABLE);\r
165 \r
166                         /* Configure the IEC for the UART interrupts. */                        \r
167                         EIC_IRQInitStructure.EIC_IRQChannelCmd = ENABLE;\r
168                         EIC_IRQInitStructure.EIC_IRQChannel = UART0_IRQChannel;\r
169                         EIC_IRQInitStructure.EIC_IRQChannelPriority = 1;\r
170                         EIC_IRQInit(&EIC_IRQInitStructure);\r
171                         \r
172                         xQueueEmpty = pdTRUE;\r
173                         UART_ITConfig( UART0, UART_IT_Transmit | UART_IT_Receive, ENABLE );\r
174                 }\r
175                 portEXIT_CRITICAL();\r
176         }\r
177         else\r
178         {\r
179                 xReturn = ( xComPortHandle ) 0;\r
180         }\r
181 \r
182         /* This demo file only supports a single port but we have to return\r
183         something to comply with the standard demo header file. */\r
184         return xReturn;\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
189 {\r
190         /* The port handle is not required as this driver only supports one port. */\r
191         ( void ) pxPort;\r
192 \r
193         /* Get the next character from the buffer.  Return false if no characters\r
194         are available, or arrive before xBlockTime expires. */\r
195         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
196         {\r
197                 return pdTRUE;\r
198         }\r
199         else\r
200         {\r
201                 return pdFALSE;\r
202         }\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
207 {\r
208 signed char *pxNext;\r
209 \r
210         /* A couple of parameters that this port does not use. */\r
211         ( void ) usStringLength;\r
212         ( void ) pxPort;\r
213 \r
214         /* NOTE: This implementation does not handle the queue being full as no\r
215         block time is used! */\r
216 \r
217         /* The port handle is not required as this driver only supports UART0. */\r
218         ( void ) pxPort;\r
219 \r
220         /* Send each character in the string, one at a time. */\r
221         pxNext = ( signed char * ) pcString;\r
222         while( *pxNext )\r
223         {\r
224                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
225                 pxNext++;\r
226         }\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
231 {\r
232 portBASE_TYPE xReturn;\r
233 \r
234         /* Place the character in the queue of characters to be transmitted. */\r
235         portENTER_CRITICAL();\r
236         {\r
237                 if( xQueueEmpty == pdTRUE )\r
238                 {\r
239                         UART0->DR = cOutChar;\r
240                         xReturn = pdPASS;\r
241                 }\r
242                 else\r
243                 {\r
244                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
245                         {\r
246                                 xReturn = pdFAIL;\r
247                         }                       \r
248                         else\r
249                         {\r
250                                 xReturn = pdPASS;                               \r
251                         }\r
252                 }\r
253                 \r
254                 xQueueEmpty = pdFALSE;\r
255         }\r
256         portEXIT_CRITICAL();\r
257 \r
258         return xReturn;\r
259 }\r
260 /*-----------------------------------------------------------*/\r
261 \r
262 void vSerialClose( xComPortHandle xPort )\r
263 {\r
264         /* Not supported as not required by the demo application. */\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 \r
269 \r
270 \r
271 \r
272         \r