]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MSP430X_MSP430F5438_CCS/Demo_Source/serial.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / MSP430X_MSP430F5438_CCS / Demo_Source / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 \r
30 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.\r
31  *\r
32  * This is not a proper UART driver.  It only supports one port, uses loopback\r
33  * mode, and is used to test interrupts that use the FreeRTOS API as part of \r
34  * a wider test suite.  Nor is it intended to show an efficient implementation\r
35  * of a UART interrupt service routine as queues are used to pass individual\r
36  * characters one at a time!\r
37  */\r
38 \r
39 /* Standard includes. */\r
40 #include <stdlib.h>\r
41 \r
42 /* Scheduler includes. */\r
43 #include "FreeRTOS.h"\r
44 #include "queue.h"\r
45 #include "task.h"\r
46 \r
47 /* Demo application includes. */\r
48 #include "serial.h"\r
49 \r
50 /* Misc. constants. */\r
51 #define serNO_BLOCK                             ( ( TickType_t ) 0 )\r
52 \r
53 /* The queue used to hold received characters. */\r
54 static QueueHandle_t xRxedChars;\r
55 \r
56 /* The queue used to hold characters waiting transmission. */\r
57 static QueueHandle_t xCharsForTx;\r
58 \r
59 /*-----------------------------------------------------------*/\r
60 \r
61 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
62 {\r
63 unsigned long ulBaudRateCount;\r
64 \r
65         /* Initialise the hardware. */\r
66 \r
67         /* Generate the baud rate constants for the wanted baud rate. */\r
68         ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;\r
69 \r
70         portENTER_CRITICAL();\r
71         {\r
72                 /* Create the queues used by the com test task. */\r
73                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
74                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
75 \r
76                 /* Reset UART. */\r
77                 UCA1CTL1 |= UCSWRST;\r
78 \r
79                 /* Use SMCLK. */\r
80                 UCA1CTL1 = UCSSEL0 | UCSSEL1;\r
81                 \r
82                 /* Setup baud rate low byte. */\r
83                 UCA1BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
84 \r
85                 /* Setup baud rate high byte. */\r
86                 ulBaudRateCount >>= 8UL;\r
87                 UCA1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );\r
88 \r
89                 /* UCLISTEN sets loopback mode! */\r
90                 UCA1STAT = UCLISTEN;\r
91 \r
92                 /* Enable interrupts. */\r
93                 UCA1IE |= UCRXIE;\r
94                 \r
95                 /* Take out of reset. */\r
96                 UCA1CTL1 &= ~UCSWRST;\r
97         }\r
98         portEXIT_CRITICAL();\r
99         \r
100         /* Note the comments at the top of this file about this not being a generic\r
101         UART driver. */\r
102         return NULL;\r
103 }\r
104 /*-----------------------------------------------------------*/\r
105 \r
106 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
107 {\r
108         /* Get the next character from the buffer.  Return false if no characters\r
109         are available, or arrive before xBlockTime expires. */\r
110         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
111         {\r
112                 return pdTRUE;\r
113         }\r
114         else\r
115         {\r
116                 return pdFALSE;\r
117         }\r
118 }\r
119 /*-----------------------------------------------------------*/\r
120 \r
121 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
122 {\r
123 signed portBASE_TYPE xReturn;\r
124 \r
125         /* Send the next character to the queue of characters waiting transmission,\r
126         then enable the UART Tx interrupt, just in case UART transmission has already\r
127         completed and switched itself off. */\r
128         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );\r
129         UCA1IE |= UCTXIE;\r
130         \r
131         return xReturn;\r
132 }\r
133 /*-----------------------------------------------------------*/\r
134 \r
135 /* The implementation of this interrupt is provided to demonstrate the use\r
136 of queues from inside an interrupt service routine.  It is *not* intended to\r
137 be an efficient interrupt implementation.  A real application should make use\r
138 of the DMA.  Or, as a minimum, transmission and reception could use a simple\r
139 RAM ring buffer, and synchronise with a task using a semaphore when a complete\r
140 message has been received or transmitted. */\r
141 #pragma vector=USCI_A1_VECTOR\r
142 interrupt void prvUSCI_A1_ISR( void )\r
143 {\r
144 signed char cChar;\r
145 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
146 \r
147         if( ( UCA1IFG & UCRXIFG ) != 0 )\r
148         {\r
149                 /* Get the character from the UART and post it on the queue of Rxed\r
150                 characters. */\r
151                 cChar = UCA1RXBUF;\r
152                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
153         }       \r
154         else if( ( UCA1IFG & UCTXIFG ) != 0 )\r
155         {\r
156                 /* The previous character has been transmitted.  See if there are any\r
157                 further characters waiting transmission. */\r
158                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
159                 {\r
160                         /* There was another character queued - transmit it now. */\r
161                         UCA1TXBUF = cChar;\r
162                 }\r
163                 else\r
164                 {\r
165                         /* There were no other characters to transmit - disable the Tx\r
166                         interrupt. */\r
167                         UCA1IE &= ~UCTXIE;\r
168                 }\r
169         }\r
170         \r
171         __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );\r
172         \r
173         /* If writing to a queue caused a task to unblock, and the unblocked task\r
174         has a priority equal to or above the task that this interrupt interrupted,\r
175         then lHigherPriorityTaskWoken will have been set to pdTRUE internally within\r
176         xQueuesendFromISR(), and portEND_SWITCHING_ISR() will ensure that this\r
177         interrupt returns directly to the higher priority unblocked task. \r
178         \r
179         THIS MUST BE THE LAST THING DONE IN THE ISR. */ \r
180         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
181 }\r
182 \r
183 \r