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