]> git.sur5r.net Git - freertos/blob - Demo/MSP430X_MSP430F5438_CCS/Demo_Source/serial.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / MSP430X_MSP430F5438_CCS / Demo_Source / serial.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 \r
68 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.\r
69  *\r
70  * This is not a proper UART driver.  It only supports one port, uses loopback\r
71  * mode, and is used to test interrupts that use the FreeRTOS API as part of \r
72  * a wider test suite.  Nor is it intended to show an efficient implementation\r
73  * of a UART interrupt service routine as queues are used to pass individual\r
74  * characters one at a time!\r
75  */\r
76 \r
77 /* Standard includes. */\r
78 #include <stdlib.h>\r
79 \r
80 /* Scheduler includes. */\r
81 #include "FreeRTOS.h"\r
82 #include "queue.h"\r
83 #include "task.h"\r
84 \r
85 /* Demo application includes. */\r
86 #include "serial.h"\r
87 \r
88 /* Misc. constants. */\r
89 #define serNO_BLOCK                             ( ( portTickType ) 0 )\r
90 \r
91 /* The queue used to hold received characters. */\r
92 static xQueueHandle xRxedChars;\r
93 \r
94 /* The queue used to hold characters waiting transmission. */\r
95 static xQueueHandle xCharsForTx;\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
100 {\r
101 unsigned portLONG ulBaudRateCount;\r
102 \r
103         /* Initialise the hardware. */\r
104 \r
105         /* Generate the baud rate constants for the wanted baud rate. */\r
106         ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;\r
107 \r
108         portENTER_CRITICAL();\r
109         {\r
110                 /* Create the queues used by the com test task. */\r
111                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
112                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
113 \r
114                 /* Reset UART. */\r
115                 UCA1CTL1 |= UCSWRST;\r
116 \r
117                 /* Use SMCLK. */\r
118                 UCA1CTL1 = UCSSEL0 | UCSSEL1;\r
119                 \r
120                 /* Setup baud rate low byte. */\r
121                 UCA1BR0 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
122 \r
123                 /* Setup baud rate high byte. */\r
124                 ulBaudRateCount >>= 8UL;\r
125                 UCA1BR1 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
126 \r
127                 /* UCLISTEN sets loopback mode! */\r
128                 UCA1STAT = UCLISTEN;\r
129 \r
130                 /* Enable interrupts. */\r
131                 UCA1IE |= UCRXIE;\r
132                 \r
133                 /* Take out of reset. */\r
134                 UCA1CTL1 &= ~UCSWRST;\r
135         }\r
136         portEXIT_CRITICAL();\r
137         \r
138         /* Note the comments at the top of this file about this not being a generic\r
139         UART driver. */\r
140         return NULL;\r
141 }\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
145 {\r
146         /* Get the next character from the buffer.  Return false if no characters\r
147         are available, or arrive before xBlockTime expires. */\r
148         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
149         {\r
150                 return pdTRUE;\r
151         }\r
152         else\r
153         {\r
154                 return pdFALSE;\r
155         }\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
160 {\r
161 signed portBASE_TYPE xReturn;\r
162 \r
163         /* Send the next character to the queue of characters waiting transmission,\r
164         then enable the UART Tx interrupt, just in case UART transmission has already\r
165         completed and switched itself off. */\r
166         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
167         UCA1IE |= UCTXIE;\r
168         \r
169         return xReturn;\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 /* The implementation of this interrupt is provided to demonstrate the use\r
174 of queues from inside an interrupt service routine.  It is *not* intended to\r
175 be an efficient interrupt implementation.  A real application should make use\r
176 of the DMA.  Or, as a minimum, transmission and reception could use a simple\r
177 RAM ring buffer, and synchronise with a task using a semaphore when a complete\r
178 message has been received or transmitted. */\r
179 #pragma vector=USCI_A1_VECTOR\r
180 interrupt void prvUSCI_A1_ISR( void )\r
181 {\r
182 signed char cChar;\r
183 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
184 \r
185         if( ( UCA1IFG & UCRXIFG ) != 0 )\r
186         {\r
187                 /* Get the character from the UART and post it on the queue of Rxed\r
188                 characters. */\r
189                 cChar = UCA1RXBUF;\r
190                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
191         }       \r
192         else 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