]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MSP430X_MSP430F5438_IAR/serial.c
b9f4cf2a6b9efd933af88aa0ceba226819403f1b
[freertos] / FreeRTOS / Demo / MSP430X_MSP430F5438_IAR / serial.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - 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     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     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 \r
97 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.\r
98  *\r
99  * This is not a proper UART driver.  It only supports one port, uses loopback\r
100  * mode, and is used to test interrupts that use the FreeRTOS API as part of\r
101  * a wider test suite.  Nor is it intended to show an efficient implementation\r
102  * of a UART interrupt service routine as queues are used to pass individual\r
103  * characters one at a time!\r
104  */\r
105 \r
106 /* Standard includes. */\r
107 #include <stdlib.h>\r
108 \r
109 /* Scheduler includes. */\r
110 #include "FreeRTOS.h"\r
111 #include "queue.h"\r
112 #include "task.h"\r
113 \r
114 /* Demo application includes. */\r
115 #include "serial.h"\r
116 \r
117 /* Misc. constants. */\r
118 #define serNO_BLOCK                             ( ( TickType_t ) 0 )\r
119 \r
120 /* The queue used to hold received characters. */\r
121 static QueueHandle_t xRxedChars;\r
122 \r
123 /* The queue used to hold characters waiting transmission. */\r
124 static QueueHandle_t xCharsForTx;\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
129 {\r
130 unsigned long ulBaudRateCount;\r
131 \r
132         /* Initialise the hardware. */\r
133 \r
134         /* Generate the baud rate constants for the wanted baud rate. */\r
135         ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;\r
136 \r
137         portENTER_CRITICAL();\r
138         {\r
139                 /* Create the queues used by the com test task. */\r
140                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
141                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
142 \r
143                 /* Reset UART. */\r
144                 UCA1CTL1 |= UCSWRST;\r
145 \r
146                 /* Use SMCLK. */\r
147                 UCA1CTL1 = UCSSEL0 | UCSSEL1;\r
148                 \r
149                 /* Setup baud rate low byte. */\r
150                 UCA1BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
151 \r
152                 /* Setup baud rate high byte. */\r
153                 ulBaudRateCount >>= 8UL;\r
154                 UCA1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
155 \r
156                 /* UCLISTEN sets loopback mode! */\r
157                 UCA1STAT = UCLISTEN;\r
158 \r
159                 /* Enable interrupts. */\r
160                 UCA1IE |= UCRXIE;\r
161                 \r
162                 /* Take out of reset. */\r
163                 UCA1CTL1 &= ~UCSWRST;\r
164         }\r
165         portEXIT_CRITICAL();\r
166         \r
167         /* Note the comments at the top of this file about this not being a generic\r
168         UART driver. */\r
169         return NULL;\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
174 {\r
175         /* Get the next character from the buffer.  Return false if no characters\r
176         are available, or arrive before xBlockTime expires. */\r
177         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
178         {\r
179                 return pdTRUE;\r
180         }\r
181         else\r
182         {\r
183                 return pdFALSE;\r
184         }\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
189 {\r
190 signed portBASE_TYPE xReturn;\r
191 \r
192         /* Send the next character to the queue of characters waiting transmission,\r
193         then enable the UART Tx interrupt, just in case UART transmission has already\r
194         completed and switched itself off. */\r
195         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
196         UCA1IE |= UCTXIE;\r
197 \r
198         return xReturn;\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 /* The implementation of this interrupt is provided to demonstrate the use\r
203 of queues from inside an interrupt service routine.  It is *not* intended to\r
204 be an efficient interrupt implementation.  A real application should make use\r
205 of the DMA.  Or, as a minimum, transmission and reception could use a simple\r
206 RAM ring buffer, and synchronise with a task using a semaphore when a complete\r
207 message has been received or transmitted. */\r
208 #pragma vector=USCI_A1_VECTOR\r
209 static __interrupt void prvUSCI_A1_ISR( void )\r
210 {\r
211 signed char cChar;\r
212 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
213 \r
214         while( ( UCA1IFG & UCRXIFG ) != 0 )\r
215         {\r
216                 /* Get the character from the UART and post it on the queue of Rxed\r
217                 characters. */\r
218                 cChar = UCA1RXBUF;\r
219                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
220         }\r
221         \r
222         /* If there is a Tx interrupt pending and the tx interrupts are enabled. */\r
223         if( ( UCA1IFG & UCTXIFG ) != 0 )\r
224         {\r
225                 /* The previous character has been transmitted.  See if there are any\r
226                 further characters waiting transmission. */\r
227                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
228                 {\r
229                         /* There was another character queued - transmit it now. */\r
230                         UCA1TXBUF = cChar;\r
231                 }\r
232                 else\r
233                 {\r
234                         /* There were no other characters to transmit - disable the Tx\r
235                         interrupt. */\r
236                         UCA1IE &= ~UCTXIE;\r
237                 }\r
238         }\r
239 \r
240         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
241         \r
242         /* If writing to a queue caused a task to unblock, and the unblocked task\r
243         has a priority equal to or above the task that this interrupt interrupted,\r
244         then lHigherPriorityTaskWoken will have been set to pdTRUE internally within\r
245         xQueuesendFromISR(), and portEND_SWITCHING_ISR() will ensure that this\r
246         interrupt returns directly to the higher priority unblocked task.\r
247         \r
248         THIS MUST BE THE LAST THING DONE IN THE ISR. */ \r
249         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
250 }\r
251 \r
252 \r