]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MSP430X_MSP430FR5969_LaunchPad_IAR_CCS/Full_Demo/serial.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / MSP430X_MSP430FR5969_LaunchPad_IAR_CCS / Full_Demo / 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.\r
72  *\r
73  * This is not a proper UART driver.  It only supports one port, and is not\r
74  * intended to show an efficient implementation as queues are used to pass\r
75  * individual characters one at a time!  This is ok for a slow interface, such\r
76  * as a command line interface (which this driver is used for), but is too\r
77  * inefficient for higher bandwidth applications.\r
78  */\r
79 \r
80 /* Standard includes. */\r
81 #include <stdlib.h>\r
82 \r
83 /* Scheduler includes. */\r
84 #include "FreeRTOS.h"\r
85 #include "queue.h"\r
86 #include "task.h"\r
87 \r
88 /* Demo application includes. */\r
89 #include "serial.h"\r
90 \r
91 /* TI includes. */\r
92 #include "driverlib.h"\r
93 \r
94 /* Misc. constants. */\r
95 #define serNO_BLOCK                             ( ( TickType_t ) 0 )\r
96 \r
97 /* The queue used to hold received characters. */\r
98 static QueueHandle_t xRxedChars;\r
99 \r
100 /* The queue used to hold characters waiting transmission. */\r
101 static QueueHandle_t xCharsForTx;\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
106 {\r
107 unsigned long ulBaudRateCount;\r
108 \r
109         /* Initialise the hardware. */\r
110 \r
111         /* Generate the baud rate constants for the wanted baud rate. */\r
112         ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;\r
113 \r
114         portENTER_CRITICAL();\r
115         {\r
116                 /* Create the queues used by the com test task. */\r
117                 xRxedChars = xQueueCreate( uxQueueLength, ( UBaseType_t ) sizeof( signed char ) );\r
118                 xCharsForTx = xQueueCreate( uxQueueLength, ( UBaseType_t ) sizeof( signed char ) );\r
119 \r
120                 /* Reset UART. */\r
121                 UCA0CTL1 |= UCSWRST;\r
122 \r
123                 /* Use SMCLK. */\r
124                 UCA0CTL1 = UCSSEL0 | UCSSEL1;\r
125 \r
126                 /* Setup baud rate low byte. */\r
127                 UCA0BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
128 \r
129                 /* Setup baud rate high byte. */\r
130                 ulBaudRateCount >>= 8UL;\r
131                 UCA0BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
132 \r
133                 /* Enable interrupts. */\r
134                 UCA0IE |= UCRXIE;\r
135 \r
136                 /* Take out of reset. */\r
137                 UCA0CTL1 &= ~UCSWRST;\r
138         }\r
139         portEXIT_CRITICAL();\r
140 \r
141         /* Note the comments at the top of this file about this not being a generic\r
142         UART driver. */\r
143         return NULL;\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
148 {\r
149         /* Get the next character from the buffer.  Return false if no characters\r
150         are available, or arrive before xBlockTime expires. */\r
151         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
152         {\r
153                 return pdTRUE;\r
154         }\r
155         else\r
156         {\r
157                 return pdFALSE;\r
158         }\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
163 {\r
164 BaseType_t xReturn;\r
165 \r
166         /* Send the next character to the queue of characters waiting transmission,\r
167         then enable the UART Tx interrupt, just in case UART transmission has already\r
168         completed and switched itself off. */\r
169         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
170         UCA0IE |= UCTXIE;\r
171 \r
172         return xReturn;\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
177 {\r
178 UBaseType_t uxChar;\r
179 const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 100 );\r
180 \r
181         /* The driver only supports one port so the pxPort parameter is not used. */\r
182         ( void ) pxPort;\r
183 \r
184         for( uxChar = 0; uxChar < usStringLength; uxChar++ )\r
185         {\r
186                 if( xQueueSend( xCharsForTx, &( pcString[ uxChar ] ), xMaxBlockTime ) == pdFALSE )\r
187                 {\r
188                         break;\r
189                 }\r
190                 else\r
191                 {\r
192                         UCA0IE |= UCTXIE;\r
193                 }\r
194         }\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 /* The implementation of this interrupt is provided to demonstrate the use\r
199 of queues from inside an interrupt service routine.  It is *not* intended to\r
200 be an efficient interrupt implementation.  A real application should make use\r
201 of the DMA.  Or, as a minimum, transmission and reception could use a simple\r
202 RAM ring buffer, and synchronise with a task using a semaphore when a complete\r
203 message has been received or transmitted. */\r
204 #pragma vector=USCI_A0_VECTOR\r
205 __interrupt void prvUSCI_A0_ISR( void )\r
206 {\r
207 signed char cChar;\r
208 BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
209 \r
210         while( ( UCA0IFG & UCRXIFG ) != 0 )\r
211         {\r
212                 /* Get the character from the UART and post it on the queue of Rxed\r
213                 characters. */\r
214                 cChar = UCA0RXBUF;\r
215                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
216         }\r
217 \r
218         /* If there is a Tx interrupt pending and the tx interrupts are enabled. */\r
219         if( ( UCA0IFG & UCTXIFG ) != 0 )\r
220         {\r
221                 /* The previous character has been transmitted.  See if there are any\r
222                 further characters waiting transmission. */\r
223                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
224                 {\r
225                         /* There was another character queued - transmit it now. */\r
226                         UCA0TXBUF = cChar;\r
227                 }\r
228                 else\r
229                 {\r
230                         /* There were no other characters to transmit - disable the Tx\r
231                         interrupt. */\r
232                         UCA0IE &= ~UCTXIE;\r
233                 }\r
234         }\r
235 \r
236         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
237 \r
238         /* If writing to a queue caused a task to unblock, and the unblocked task\r
239         has a priority equal to or above the task that this interrupt interrupted,\r
240         then lHigherPriorityTaskWoken will have been set to pdTRUE internally within\r
241         xQueuesendFromISR(), and portEND_SWITCHING_ISR() will ensure that this\r
242         interrupt returns directly to the higher priority unblocked task.\r
243 \r
244         THIS MUST BE THE LAST THING DONE IN THE ISR. */\r
245         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
246 }\r
247 \r
248 \r