]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MSP430FR5969_LaunchPad/driverlib/MSP430FR5xx_6xx/eusci_a_uart.c
Start of an MSP430FR5969 IAR project - currently running Blinky only.
[freertos] / FreeRTOS / Demo / MSP430FR5969_LaunchPad / driverlib / MSP430FR5xx_6xx / eusci_a_uart.c
1 /* --COPYRIGHT--,BSD\r
2  * Copyright (c) 2014, Texas Instruments Incorporated\r
3  * All rights reserved.\r
4  *\r
5  * Redistribution and use in source and binary forms, with or without\r
6  * modification, are permitted provided that the following conditions\r
7  * are met:\r
8  *\r
9  * *  Redistributions of source code must retain the above copyright\r
10  *    notice, this list of conditions and the following disclaimer.\r
11  *\r
12  * *  Redistributions in binary form must reproduce the above copyright\r
13  *    notice, this list of conditions and the following disclaimer in the\r
14  *    documentation and/or other materials provided with the distribution.\r
15  *\r
16  * *  Neither the name of Texas Instruments Incorporated nor the names of\r
17  *    its contributors may be used to endorse or promote products derived\r
18  *    from this software without specific prior written permission.\r
19  *\r
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;\r
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR\r
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
31  * --/COPYRIGHT--*/\r
32 //*****************************************************************************\r
33 //\r
34 // eusci_a_uart.c - Driver for the eusci_a_uart Module.\r
35 //\r
36 //*****************************************************************************\r
37 \r
38 //*****************************************************************************\r
39 //\r
40 //! \addtogroup eusci_a_uart_api eusci_a_uart\r
41 //! @{\r
42 //\r
43 //*****************************************************************************\r
44 \r
45 #include "inc/hw_regaccess.h"\r
46 #include "inc/hw_memmap.h"\r
47 \r
48 #ifdef __MSP430_HAS_EUSCI_Ax__\r
49 #include "eusci_a_uart.h"\r
50 \r
51 #include <assert.h>\r
52 \r
53 bool EUSCI_A_UART_init(uint16_t baseAddress,\r
54                        EUSCI_A_UART_initParam *param)\r
55 {\r
56     bool retVal = STATUS_SUCCESS;\r
57 \r
58     //Disable the USCI Module\r
59     HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCSWRST;\r
60 \r
61     //Clock source select\r
62     HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~UCSSEL_3;\r
63     HWREG16(baseAddress + OFS_UCAxCTLW0) |= param->selectClockSource;\r
64 \r
65     //MSB, LSB select\r
66     HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~UCMSB;\r
67     HWREG16(baseAddress + OFS_UCAxCTLW0) |= param->msborLsbFirst;\r
68 \r
69     //UCSPB = 0(1 stop bit) OR 1(2 stop bits)\r
70     HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~UCSPB;\r
71     HWREG16(baseAddress + OFS_UCAxCTLW0) |= param->numberofStopBits;\r
72 \r
73     //Parity\r
74     switch(param->parity)\r
75     {\r
76     case EUSCI_A_UART_NO_PARITY:\r
77         //No Parity\r
78         HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~UCPEN;\r
79         break;\r
80     case EUSCI_A_UART_ODD_PARITY:\r
81         //Odd Parity\r
82         HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCPEN;\r
83         HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~UCPAR;\r
84         break;\r
85     case EUSCI_A_UART_EVEN_PARITY:\r
86         //Even Parity\r
87         HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCPEN;\r
88         HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCPAR;\r
89         break;\r
90     }\r
91 \r
92     //BaudRate Control Register\r
93     HWREG16(baseAddress + OFS_UCAxBRW) = param->clockPrescalar;\r
94     //Modulation Control Register\r
95     HWREG16(baseAddress + OFS_UCAxMCTLW) = ((param->secondModReg << 8)\r
96                                             + (param->firstModReg <<\r
97     4) + param->overSampling);\r
98 \r
99     //Asynchronous mode & 8 bit character select & clear mode\r
100     HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~(UCSYNC +\r
101                                               UC7BIT +\r
102                                               UCMODE_3\r
103                                               );\r
104 \r
105     //Configure  UART mode.\r
106     HWREG16(baseAddress + OFS_UCAxCTLW0) |= param->uartMode;\r
107 \r
108     //Reset UCRXIE, UCBRKIE, UCDORM, UCTXADDR, UCTXBRK\r
109     HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~(UCRXEIE + UCBRKIE + UCDORM +\r
110                                               UCTXADDR + UCTXBRK\r
111                                               );\r
112     return (retVal);\r
113 }\r
114 \r
115 void EUSCI_A_UART_transmitData(uint16_t baseAddress,\r
116                                uint8_t transmitData)\r
117 {\r
118     //If interrupts are not used, poll for flags\r
119     if(!(HWREG16(baseAddress + OFS_UCAxIE) & UCTXIE))\r
120     {\r
121         //Poll for transmit interrupt flag\r
122         while(!(HWREG16(baseAddress + OFS_UCAxIFG) & UCTXIFG))\r
123         {\r
124             ;\r
125         }\r
126     }\r
127 \r
128     HWREG16(baseAddress + OFS_UCAxTXBUF) = transmitData;\r
129 }\r
130 \r
131 uint8_t EUSCI_A_UART_receiveData(uint16_t baseAddress)\r
132 {\r
133     //If interrupts are not used, poll for flags\r
134     if(!(HWREG16(baseAddress + OFS_UCAxIE) & UCRXIE))\r
135     {\r
136         //Poll for receive interrupt flag\r
137         while(!(HWREG16(baseAddress + OFS_UCAxIFG) & UCRXIFG))\r
138         {\r
139             ;\r
140         }\r
141     }\r
142 \r
143     return (HWREG16(baseAddress + OFS_UCAxRXBUF));\r
144 }\r
145 \r
146 void EUSCI_A_UART_enableInterrupt(uint16_t baseAddress,\r
147                                   uint8_t mask)\r
148 {\r
149     uint8_t locMask;\r
150 \r
151     locMask = (mask & (EUSCI_A_UART_RECEIVE_INTERRUPT\r
152                        | EUSCI_A_UART_TRANSMIT_INTERRUPT\r
153                        | EUSCI_A_UART_STARTBIT_INTERRUPT\r
154                        | EUSCI_A_UART_TRANSMIT_COMPLETE_INTERRUPT));\r
155 \r
156     HWREG16(baseAddress + OFS_UCAxIE) |= locMask;\r
157 \r
158     locMask = (mask & (EUSCI_A_UART_RECEIVE_ERRONEOUSCHAR_INTERRUPT\r
159                        | EUSCI_A_UART_BREAKCHAR_INTERRUPT));\r
160     HWREG16(baseAddress + OFS_UCAxCTLW0) |= locMask;\r
161 }\r
162 \r
163 void EUSCI_A_UART_disableInterrupt(uint16_t baseAddress,\r
164                                    uint8_t mask)\r
165 {\r
166     uint8_t locMask;\r
167 \r
168     locMask = (mask & (EUSCI_A_UART_RECEIVE_INTERRUPT\r
169                        | EUSCI_A_UART_TRANSMIT_INTERRUPT\r
170                        | EUSCI_A_UART_STARTBIT_INTERRUPT\r
171                        | EUSCI_A_UART_TRANSMIT_COMPLETE_INTERRUPT));\r
172     HWREG16(baseAddress + OFS_UCAxIE) &= ~locMask;\r
173 \r
174     locMask = (mask & (EUSCI_A_UART_RECEIVE_ERRONEOUSCHAR_INTERRUPT\r
175                        | EUSCI_A_UART_BREAKCHAR_INTERRUPT));\r
176     HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~locMask;\r
177 }\r
178 \r
179 uint8_t EUSCI_A_UART_getInterruptStatus(uint16_t baseAddress,\r
180                                         uint8_t mask)\r
181 {\r
182     return (HWREG16(baseAddress + OFS_UCAxIFG) & mask);\r
183 }\r
184 \r
185 void EUSCI_A_UART_clearInterrupt(uint16_t baseAddress,\r
186                                  uint8_t mask)\r
187 {\r
188     //Clear the UART interrupt source.\r
189     HWREG16(baseAddress + OFS_UCAxIFG) &= ~(mask);\r
190 }\r
191 \r
192 void EUSCI_A_UART_enable(uint16_t baseAddress)\r
193 {\r
194     //Reset the UCSWRST bit to enable the USCI Module\r
195     HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~(UCSWRST);\r
196 }\r
197 \r
198 void EUSCI_A_UART_disable(uint16_t baseAddress)\r
199 {\r
200     //Set the UCSWRST bit to disable the USCI Module\r
201     HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCSWRST;\r
202 }\r
203 \r
204 uint8_t EUSCI_A_UART_queryStatusFlags(uint16_t baseAddress,\r
205                                       uint8_t mask)\r
206 {\r
207     return (HWREG16(baseAddress + OFS_UCAxSTATW) & mask);\r
208 }\r
209 \r
210 void EUSCI_A_UART_setDormant(uint16_t baseAddress)\r
211 {\r
212     HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCDORM;\r
213 }\r
214 \r
215 void EUSCI_A_UART_resetDormant(uint16_t baseAddress)\r
216 {\r
217     HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~UCDORM;\r
218 }\r
219 \r
220 void EUSCI_A_UART_transmitAddress(uint16_t baseAddress,\r
221                                   uint8_t transmitAddress)\r
222 {\r
223     //Set UCTXADDR bit\r
224     HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCTXADDR;\r
225 \r
226     //Place next byte to be sent into the transmit buffer\r
227     HWREG16(baseAddress + OFS_UCAxTXBUF) = transmitAddress;\r
228 }\r
229 \r
230 void EUSCI_A_UART_transmitBreak(uint16_t baseAddress)\r
231 {\r
232     //Set UCTXADDR bit\r
233     HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCTXBRK;\r
234 \r
235     //If current mode is automatic baud-rate detection\r
236     if(EUSCI_A_UART_AUTOMATIC_BAUDRATE_DETECTION_MODE ==\r
237        (HWREG16(baseAddress + OFS_UCAxCTLW0) &\r
238         EUSCI_A_UART_AUTOMATIC_BAUDRATE_DETECTION_MODE))\r
239     {\r
240         HWREG16(baseAddress +\r
241                 OFS_UCAxTXBUF) = EUSCI_A_UART_AUTOMATICBAUDRATE_SYNC;\r
242     }\r
243     else\r
244     {\r
245         HWREG16(baseAddress + OFS_UCAxTXBUF) = DEFAULT_SYNC;\r
246     }\r
247 \r
248     //If interrupts are not used, poll for flags\r
249     if(!(HWREG16(baseAddress + OFS_UCAxIE) & UCTXIE))\r
250     {\r
251         //Poll for transmit interrupt flag\r
252         while(!(HWREG16(baseAddress + OFS_UCAxIFG) & UCTXIFG))\r
253         {\r
254             ;\r
255         }\r
256     }\r
257 }\r
258 \r
259 uint32_t EUSCI_A_UART_getReceiveBufferAddress(uint16_t baseAddress)\r
260 {\r
261     return (baseAddress + OFS_UCAxRXBUF);\r
262 }\r
263 \r
264 uint32_t EUSCI_A_UART_getTransmitBufferAddress(uint16_t baseAddress)\r
265 {\r
266     return (baseAddress + OFS_UCAxTXBUF);\r
267 }\r
268 \r
269 void EUSCI_A_UART_selectDeglitchTime(uint16_t baseAddress,\r
270                                      uint16_t deglitchTime)\r
271 {\r
272     HWREG16(baseAddress + OFS_UCAxCTLW1) &= ~(UCGLIT1 + UCGLIT0);\r
273 \r
274     HWREG16(baseAddress + OFS_UCAxCTLW1) |= deglitchTime;\r
275 }\r
276 \r
277 #endif\r
278 //*****************************************************************************\r
279 //\r
280 //! Close the doxygen group for eusci_a_uart_api\r
281 //! @}\r
282 //\r
283 //*****************************************************************************\r