]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MSP430X_MSP430F5438_CCS/Demo_Source/serial.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / MSP430X_MSP430F5438_CCS / Demo_Source / serial.c
1 /*\r
2     FreeRTOS V8.1.2 - 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     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS 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.\r
68  *\r
69  * This is not a proper UART driver.  It only supports one port, uses loopback\r
70  * mode, and is used to test interrupts that use the FreeRTOS API as part of \r
71  * a wider test suite.  Nor is it intended to show an efficient implementation\r
72  * of a UART interrupt service routine as queues are used to pass individual\r
73  * characters one at a time!\r
74  */\r
75 \r
76 /* Standard includes. */\r
77 #include <stdlib.h>\r
78 \r
79 /* Scheduler includes. */\r
80 #include "FreeRTOS.h"\r
81 #include "queue.h"\r
82 #include "task.h"\r
83 \r
84 /* Demo application includes. */\r
85 #include "serial.h"\r
86 \r
87 /* Misc. constants. */\r
88 #define serNO_BLOCK                             ( ( TickType_t ) 0 )\r
89 \r
90 /* The queue used to hold received characters. */\r
91 static QueueHandle_t xRxedChars;\r
92 \r
93 /* The queue used to hold characters waiting transmission. */\r
94 static QueueHandle_t xCharsForTx;\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
99 {\r
100 unsigned long ulBaudRateCount;\r
101 \r
102         /* Initialise the hardware. */\r
103 \r
104         /* Generate the baud rate constants for the wanted baud rate. */\r
105         ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;\r
106 \r
107         portENTER_CRITICAL();\r
108         {\r
109                 /* Create the queues used by the com test task. */\r
110                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
111                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
112 \r
113                 /* Reset UART. */\r
114                 UCA1CTL1 |= UCSWRST;\r
115 \r
116                 /* Use SMCLK. */\r
117                 UCA1CTL1 = UCSSEL0 | UCSSEL1;\r
118                 \r
119                 /* Setup baud rate low byte. */\r
120                 UCA1BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
121 \r
122                 /* Setup baud rate high byte. */\r
123                 ulBaudRateCount >>= 8UL;\r
124                 UCA1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
125 \r
126                 /* UCLISTEN sets loopback mode! */\r
127                 UCA1STAT = UCLISTEN;\r
128 \r
129                 /* Enable interrupts. */\r
130                 UCA1IE |= UCRXIE;\r
131                 \r
132                 /* Take out of reset. */\r
133                 UCA1CTL1 &= ~UCSWRST;\r
134         }\r
135         portEXIT_CRITICAL();\r
136         \r
137         /* Note the comments at the top of this file about this not being a generic\r
138         UART driver. */\r
139         return NULL;\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
144 {\r
145         /* Get the next character from the buffer.  Return false if no characters\r
146         are available, or arrive before xBlockTime expires. */\r
147         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
148         {\r
149                 return pdTRUE;\r
150         }\r
151         else\r
152         {\r
153                 return pdFALSE;\r
154         }\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
159 {\r
160 signed portBASE_TYPE xReturn;\r
161 \r
162         /* Send the next character to the queue of characters waiting transmission,\r
163         then enable the UART Tx interrupt, just in case UART transmission has already\r
164         completed and switched itself off. */\r
165         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
166         UCA1IE |= UCTXIE;\r
167         \r
168         return xReturn;\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 /* The implementation of this interrupt is provided to demonstrate the use\r
173 of queues from inside an interrupt service routine.  It is *not* intended to\r
174 be an efficient interrupt implementation.  A real application should make use\r
175 of the DMA.  Or, as a minimum, transmission and reception could use a simple\r
176 RAM ring buffer, and synchronise with a task using a semaphore when a complete\r
177 message has been received or transmitted. */\r
178 #pragma vector=USCI_A1_VECTOR\r
179 interrupt void prvUSCI_A1_ISR( void )\r
180 {\r
181 signed char cChar;\r
182 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
183 \r
184         if( ( UCA1IFG & UCRXIFG ) != 0 )\r
185         {\r
186                 /* Get the character from the UART and post it on the queue of Rxed\r
187                 characters. */\r
188                 cChar = UCA1RXBUF;\r
189                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
190         }       \r
191         else if( ( UCA1IFG & UCTXIFG ) != 0 )\r
192         {\r
193                 /* The previous character has been transmitted.  See if there are any\r
194                 further characters waiting transmission. */\r
195                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
196                 {\r
197                         /* There was another character queued - transmit it now. */\r
198                         UCA1TXBUF = cChar;\r
199                 }\r
200                 else\r
201                 {\r
202                         /* There were no other characters to transmit - disable the Tx\r
203                         interrupt. */\r
204                         UCA1IE &= ~UCTXIE;\r
205                 }\r
206         }\r
207         \r
208         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
209         \r
210         /* If writing to a queue caused a task to unblock, and the unblocked task\r
211         has a priority equal to or above the task that this interrupt interrupted,\r
212         then lHigherPriorityTaskWoken will have been set to pdTRUE internally within\r
213         xQueuesendFromISR(), and portEND_SWITCHING_ISR() will ensure that this\r
214         interrupt returns directly to the higher priority unblocked task. \r
215         \r
216         THIS MUST BE THE LAST THING DONE IN THE ISR. */ \r
217         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
218 }\r
219 \r
220 \r