]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MSP430X_MSP430F5438_IAR/serial.c
Update version number ready for the V8.2.3 release.
[freertos] / FreeRTOS / Demo / MSP430X_MSP430F5438_IAR / serial.c
1 /*\r
2     FreeRTOS V8.2.3 - Copyright (C) 2015 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, uses loopback\r
74  * mode, and is used to test interrupts that use the FreeRTOS API as part of\r
75  * a wider test suite.  Nor is it intended to show an efficient implementation\r
76  * of a UART interrupt service routine as queues are used to pass individual\r
77  * characters one at a time!\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 /* Misc. constants. */\r
92 #define serNO_BLOCK                             ( ( TickType_t ) 0 )\r
93 \r
94 /* The queue used to hold received characters. */\r
95 static QueueHandle_t xRxedChars;\r
96 \r
97 /* The queue used to hold characters waiting transmission. */\r
98 static QueueHandle_t xCharsForTx;\r
99 \r
100 /*-----------------------------------------------------------*/\r
101 \r
102 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
103 {\r
104 unsigned long ulBaudRateCount;\r
105 \r
106         /* Initialise the hardware. */\r
107 \r
108         /* Generate the baud rate constants for the wanted baud rate. */\r
109         ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;\r
110 \r
111         portENTER_CRITICAL();\r
112         {\r
113                 /* Create the queues used by the com test task. */\r
114                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
115                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
116 \r
117                 /* Reset UART. */\r
118                 UCA1CTL1 |= UCSWRST;\r
119 \r
120                 /* Use SMCLK. */\r
121                 UCA1CTL1 = UCSSEL0 | UCSSEL1;\r
122                 \r
123                 /* Setup baud rate low byte. */\r
124                 UCA1BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
125 \r
126                 /* Setup baud rate high byte. */\r
127                 ulBaudRateCount >>= 8UL;\r
128                 UCA1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
129 \r
130                 /* UCLISTEN sets loopback mode! */\r
131                 UCA1STAT = UCLISTEN;\r
132 \r
133                 /* Enable interrupts. */\r
134                 UCA1IE |= UCRXIE;\r
135                 \r
136                 /* Take out of reset. */\r
137                 UCA1CTL1 &= ~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 signed portBASE_TYPE 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         UCA1IE |= UCTXIE;\r
171 \r
172         return xReturn;\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 /* The implementation of this interrupt is provided to demonstrate the use\r
177 of queues from inside an interrupt service routine.  It is *not* intended to\r
178 be an efficient interrupt implementation.  A real application should make use\r
179 of the DMA.  Or, as a minimum, transmission and reception could use a simple\r
180 RAM ring buffer, and synchronise with a task using a semaphore when a complete\r
181 message has been received or transmitted. */\r
182 #pragma vector=USCI_A1_VECTOR\r
183 static __interrupt void prvUSCI_A1_ISR( void )\r
184 {\r
185 signed char cChar;\r
186 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
187 \r
188         while( ( UCA1IFG & UCRXIFG ) != 0 )\r
189         {\r
190                 /* Get the character from the UART and post it on the queue of Rxed\r
191                 characters. */\r
192                 cChar = UCA1RXBUF;\r
193                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
194         }\r
195         \r
196         /* If there is a Tx interrupt pending and the tx interrupts are enabled. */\r
197         if( ( UCA1IFG & UCTXIFG ) != 0 )\r
198         {\r
199                 /* The previous character has been transmitted.  See if there are any\r
200                 further characters waiting transmission. */\r
201                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
202                 {\r
203                         /* There was another character queued - transmit it now. */\r
204                         UCA1TXBUF = cChar;\r
205                 }\r
206                 else\r
207                 {\r
208                         /* There were no other characters to transmit - disable the Tx\r
209                         interrupt. */\r
210                         UCA1IE &= ~UCTXIE;\r
211                 }\r
212         }\r
213 \r
214         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
215         \r
216         /* If writing to a queue caused a task to unblock, and the unblocked task\r
217         has a priority equal to or above the task that this interrupt interrupted,\r
218         then lHigherPriorityTaskWoken will have been set to pdTRUE internally within\r
219         xQueuesendFromISR(), and portEND_SWITCHING_ISR() will ensure that this\r
220         interrupt returns directly to the higher priority unblocked task.\r
221         \r
222         THIS MUST BE THE LAST THING DONE IN THE ISR. */ \r
223         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
224 }\r
225 \r
226 \r