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