]> begriffs open source - cmsis-freertos/blob - Demo/msp430_CrossWorks/serial/serial.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / msp430_CrossWorks / serial / serial.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70
71 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.   
72  * 
73  * This file only supports UART 1
74  */
75
76 /* Standard includes. */
77 #include <stdlib.h>
78
79 /* Scheduler includes. */
80 #include "FreeRTOS.h"
81 #include "queue.h"
82 #include "task.h"
83
84 /* Demo application includes. */
85 #include "serial.h"
86
87 /* Constants required to setup the hardware. */
88 #define serTX_AND_RX                    ( ( unsigned char ) 0x03 )
89
90 /* Misc. constants. */
91 #define serNO_BLOCK                             ( ( TickType_t ) 0 )
92
93 /* Enable the UART Tx interrupt. */
94 #define vInterruptOn() IFG2 |= UTXIFG1
95
96 /* The queue used to hold received characters. */
97 static QueueHandle_t xRxedChars; 
98
99 /* The queue used to hold characters waiting transmission. */
100 static QueueHandle_t xCharsForTx; 
101
102 static volatile short sTHREEmpty;
103
104 /*-----------------------------------------------------------*/
105
106 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
107 {
108 unsigned long ulBaudRateCount;
109
110         /* Initialise the hardware. */
111
112         /* Generate the baud rate constants for the wanted baud rate. */
113         ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;
114
115         portENTER_CRITICAL();
116         {
117                 /* Create the queues used by the com test task. */
118                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
119                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
120
121                 /* Reset UART. */
122                 UCTL1 |= SWRST;
123
124                 /* Set pin function. */
125                 P4SEL |= serTX_AND_RX;
126
127                 /* All other bits remain at zero for n, 8, 1 interrupt driven operation. 
128                 LOOPBACK MODE!*/
129                 U1CTL |= CHAR + LISTEN;
130                 U1TCTL |= SSEL1;
131
132                 /* Setup baud rate low byte. */
133                 U1BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
134
135                 /* Setup baud rate high byte. */
136                 ulBaudRateCount >>= 8UL;
137                 U1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
138
139                 /* Enable ports. */
140                 ME2 |= UTXE1 + URXE1;
141
142                 /* Set. */
143                 UCTL1 &= ~SWRST;
144
145                 /* Nothing in the buffer yet. */
146                 sTHREEmpty = pdTRUE;
147
148                 /* Enable interrupts. */
149                 IE2 |= URXIE1 + UTXIE1;
150         }
151         portEXIT_CRITICAL();
152         
153         /* Unlike other ports, this serial code does not allow for more than one
154         com port.  We therefore don't return a pointer to a port structure and can
155         instead just return NULL. */
156         return NULL;
157 }
158 /*-----------------------------------------------------------*/
159
160 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
161 {
162         /* Get the next character from the buffer.  Return false if no characters
163         are available, or arrive before xBlockTime expires. */
164         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
165         {
166                 return pdTRUE;
167         }
168         else
169         {
170                 return pdFALSE;
171         }
172 }
173 /*-----------------------------------------------------------*/
174
175 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
176 {
177 signed portBASE_TYPE xReturn;
178
179         /* Transmit a character. */
180
181         portENTER_CRITICAL();
182         {
183                 if( sTHREEmpty == pdTRUE )
184                 {
185                         /* If sTHREEmpty is true then the UART Tx ISR has indicated that 
186                         there are no characters queued to be transmitted - so we can
187                         write the character directly to the shift Tx register. */
188                         sTHREEmpty = pdFALSE;
189                         U1TXBUF = cOutChar;
190                         xReturn = pdPASS;
191                 }
192                 else
193                 {
194                         /* sTHREEmpty is false, so there are still characters waiting to be
195                         transmitted.  We have to queue this character so it gets 
196                         transmitted     in turn. */
197
198                         /* Return false if after the block time there is no room on the Tx 
199                         queue.  It is ok to block inside a critical section as each task
200                         maintains it's own critical section status. */
201                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
202
203                         /* Depending on queue sizing and task prioritisation:  While we 
204                         were blocked waiting to post on the queue interrupts were not 
205                         disabled.  It is possible that the serial ISR has emptied the 
206                         Tx queue, in which case we need to start the Tx off again
207                         writing directly to the Tx register. */
208                         if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )
209                         {
210                                 /* Get back the character we just posted. */
211                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
212                                 sTHREEmpty = pdFALSE;
213                                 U1TXBUF = cOutChar;
214                         }
215                 }
216         }
217         portEXIT_CRITICAL();
218
219         return pdPASS;
220 }
221 /*-----------------------------------------------------------*/
222
223 #if configINTERRUPT_EXAMPLE_METHOD == 1
224
225         /*
226          * UART RX interrupt service routine.
227          */
228         void vRxISR( void ) __interrupt[ UART1RX_VECTOR ]
229         {
230         signed char cChar;
231         portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
232         
233                 /* Get the character from the UART and post it on the queue of Rxed 
234                 characters. */
235                 cChar = U1RXBUF;
236         
237                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
238
239                 if( xHigherPriorityTaskWoken )
240                 {
241                         /*If the post causes a task to wake force a context switch 
242                         as the woken task may have a higher priority than the task we have 
243                         interrupted. */
244                         taskYIELD();
245                 }
246
247         /* Make sure any low power mode bits are clear before leaving the ISR. */
248         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
249         }
250         /*-----------------------------------------------------------*/
251         
252         /*
253          * UART Tx interrupt service routine.
254          */
255         void vTxISR( void ) __interrupt[ UART1TX_VECTOR ]
256         {
257         signed char cChar;
258         portBASE_TYPE xTaskWoken = pdFALSE;
259         
260                 /* The previous character has been transmitted.  See if there are any
261                 further characters waiting transmission. */
262         
263                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
264                 {
265                         /* There was another character queued - transmit it now. */
266                         U1TXBUF = cChar;
267                 }
268                 else
269                 {
270                         /* There were no other characters to transmit. */
271                         sTHREEmpty = pdTRUE;
272                 }
273
274         /* Make sure any low power mode bits are clear before leaving the ISR. */
275         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
276         }
277     /*-----------------------------------------------------------*/
278
279 #elif configINTERRUPT_EXAMPLE_METHOD == 2
280
281     /* This is a standard C function as an assembly file wrapper is used as an
282     interrupt entry point. */
283         void vRxISR( void )
284         {
285         signed char cChar;
286         portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
287         
288                 /* Get the character from the UART and post it on the queue of Rxed 
289                 characters. */
290                 cChar = U1RXBUF;
291         
292                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
293
294         /*If the post causes a task to wake force a context switch 
295         as the woken task may have a higher priority than the task we have 
296         interrupted. */
297         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
298         }
299         /*-----------------------------------------------------------*/
300         
301     /* This is a standard C function as an assembly file wrapper is used as an
302     interrupt entry point. */
303         void vTxISR( void )
304         {
305         signed char cChar;
306         portBASE_TYPE xTaskWoken = pdFALSE;
307         
308                 /* The previous character has been transmitted.  See if there are any
309                 further characters waiting transmission. */
310         
311                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
312                 {
313                         /* There was another character queued - transmit it now. */
314                         U1TXBUF = cChar;
315                 }
316                 else
317                 {
318                         /* There were no other characters to transmit. */
319                         sTHREEmpty = pdTRUE;
320                 }
321         }
322
323 #endif /* configINTERRUPT_EXAMPLE_METHOD */
324 /*-----------------------------------------------------------*/