]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MSP430X_MSP430FR5969_LaunchPad/driverlib/MSP430FR5xx_6xx/eusci_b_i2c.c
Rename /Demo/MSP430FR5969_LaunchPad to /Demo/MSP430X_MSP430FR5969_LaunchPad for consi...
[freertos] / FreeRTOS / Demo / MSP430X_MSP430FR5969_LaunchPad / driverlib / MSP430FR5xx_6xx / eusci_b_i2c.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_b_i2c.c - Driver for the eusci_b_i2c Module.\r
35 //\r
36 //*****************************************************************************\r
37 \r
38 //*****************************************************************************\r
39 //\r
40 //! \addtogroup eusci_b_i2c_api eusci_b_i2c\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_Bx__\r
49 #include "eusci_b_i2c.h"\r
50 \r
51 #include <assert.h>\r
52 \r
53 void EUSCI_B_I2C_initMaster(uint16_t baseAddress,\r
54                             EUSCI_B_I2C_initMasterParam *param)\r
55 {\r
56     uint16_t preScalarValue;\r
57 \r
58     //Disable the USCI module and clears the other bits of control register\r
59     HWREG16(baseAddress + OFS_UCBxCTLW0) = UCSWRST;\r
60 \r
61     //Configure Automatic STOP condition generation\r
62     HWREG16(baseAddress + OFS_UCBxCTLW1) &= ~UCASTP_3;\r
63     HWREG16(baseAddress + OFS_UCBxCTLW1) |= param->autoSTOPGeneration;\r
64 \r
65     //Byte Count Threshold\r
66     HWREG16(baseAddress + OFS_UCBxTBCNT) = param->byteCounterThreshold;\r
67     /*\r
68      * Configure as I2C master mode.\r
69      * UCMST = Master mode\r
70      * UCMODE_3 = I2C mode\r
71      * UCSYNC = Synchronous mode\r
72      */\r
73     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCMST + UCMODE_3 + UCSYNC;\r
74 \r
75     //Configure I2C clock source\r
76     HWREG16(baseAddress +\r
77             OFS_UCBxCTLW0) |= (param->selectClockSource + UCSWRST);\r
78 \r
79     /*\r
80      * Compute the clock divider that achieves the fastest speed less than or\r
81      * equal to the desired speed.  The numerator is biased to favor a larger\r
82      * clock divider so that the resulting clock is always less than or equal\r
83      * to the desired clock, never greater.\r
84      */\r
85     preScalarValue = (uint16_t)(param->i2cClk / param->dataRate);\r
86     HWREG16(baseAddress + OFS_UCBxBRW) = preScalarValue;\r
87 }\r
88 \r
89 void EUSCI_B_I2C_initSlave(uint16_t baseAddress,\r
90                            EUSCI_B_I2C_initSlaveParam *param)\r
91 {\r
92     //Disable the USCI module\r
93     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCSWRST;\r
94 \r
95     //Clear USCI master mode\r
96     HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~UCMST;\r
97 \r
98     //Configure I2C as Slave and Synchronous mode\r
99     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCMODE_3 + UCSYNC;\r
100 \r
101     //Set up the slave address.\r
102     HWREG16(baseAddress + OFS_UCBxI2COA0 + param->slaveAddressOffset)\r
103         = param->slaveAddress + param->slaveOwnAddressEnable;\r
104 }\r
105 \r
106 void EUSCI_B_I2C_enable(uint16_t baseAddress)\r
107 {\r
108     //Reset the UCSWRST bit to enable the USCI Module\r
109     HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~(UCSWRST);\r
110 }\r
111 \r
112 void EUSCI_B_I2C_disable(uint16_t baseAddress)\r
113 {\r
114     //Set the UCSWRST bit to disable the USCI Module\r
115     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCSWRST;\r
116 }\r
117 \r
118 void EUSCI_B_I2C_setSlaveAddress(uint16_t baseAddress,\r
119                                  uint8_t slaveAddress)\r
120 {\r
121     //Set the address of the slave with which the master will communicate.\r
122     HWREG16(baseAddress + OFS_UCBxI2CSA) = (slaveAddress);\r
123 }\r
124 \r
125 void EUSCI_B_I2C_setMode(uint16_t baseAddress,\r
126                          uint8_t mode)\r
127 {\r
128     HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~EUSCI_B_I2C_TRANSMIT_MODE;\r
129     HWREG16(baseAddress + OFS_UCBxCTLW0) |= mode;\r
130 }\r
131 \r
132 uint8_t EUSCI_B_I2C_getMode(uint16_t baseAddress)\r
133 {\r
134     //Read the I2C mode.\r
135     return ((HWREG16(baseAddress + OFS_UCBxCTLW0) & UCTR));\r
136 }\r
137 \r
138 void EUSCI_B_I2C_slavePutData(uint16_t baseAddress,\r
139                               uint8_t transmitData)\r
140 {\r
141     //Send single byte data.\r
142     HWREG16(baseAddress + OFS_UCBxTXBUF) = transmitData;\r
143 }\r
144 \r
145 uint8_t EUSCI_B_I2C_slaveGetData(uint16_t baseAddress)\r
146 {\r
147     //Read a byte.\r
148     return (HWREG16(baseAddress + OFS_UCBxRXBUF));\r
149 }\r
150 \r
151 uint16_t EUSCI_B_I2C_isBusBusy(uint16_t baseAddress)\r
152 {\r
153     //Return the bus busy status.\r
154     return (HWREG16(baseAddress + OFS_UCBxSTATW) & UCBBUSY);\r
155 }\r
156 \r
157 uint16_t EUSCI_B_I2C_masterIsStopSent(uint16_t baseAddress)\r
158 {\r
159     return (HWREG16(baseAddress + OFS_UCBxCTLW0) & UCTXSTP);\r
160 }\r
161 \r
162 uint16_t EUSCI_B_I2C_masterIsStartSent(uint16_t baseAddress)\r
163 {\r
164     return (HWREG16(baseAddress + OFS_UCBxCTLW0) & UCTXSTT);\r
165 }\r
166 \r
167 void EUSCI_B_I2C_enableInterrupt(uint16_t baseAddress,\r
168                                  uint16_t mask)\r
169 {\r
170     //Enable the interrupt masked bit\r
171     HWREG16(baseAddress + OFS_UCBxIE) |= mask;\r
172 }\r
173 \r
174 void EUSCI_B_I2C_disableInterrupt(uint16_t baseAddress,\r
175                                   uint16_t mask)\r
176 {\r
177     //Disable the interrupt masked bit\r
178     HWREG16(baseAddress + OFS_UCBxIE) &= ~(mask);\r
179 }\r
180 \r
181 void EUSCI_B_I2C_clearInterrupt(uint16_t baseAddress,\r
182                                 uint16_t mask)\r
183 {\r
184     //Clear the I2C interrupt source.\r
185     HWREG16(baseAddress + OFS_UCBxIFG) &= ~(mask);\r
186 }\r
187 \r
188 uint16_t EUSCI_B_I2C_getInterruptStatus(uint16_t baseAddress,\r
189                                         uint16_t mask)\r
190 {\r
191     //Return the interrupt status of the request masked bit.\r
192     return (HWREG16(baseAddress + OFS_UCBxIFG) & mask);\r
193 }\r
194 \r
195 void EUSCI_B_I2C_masterSendSingleByte(uint16_t baseAddress,\r
196                                       uint8_t txData)\r
197 {\r
198     //Store current TXIE status\r
199     uint16_t txieStatus = HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE;\r
200 \r
201     //Disable transmit interrupt enable\r
202     HWREG16(baseAddress + OFS_UCBxIE) &= ~(UCTXIE);\r
203 \r
204     //Send start condition.\r
205     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTR + UCTXSTT;\r
206 \r
207     //Poll for transmit interrupt flag.\r
208     while(!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG))\r
209     {\r
210         ;\r
211     }\r
212 \r
213     //Send single byte data.\r
214     HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;\r
215 \r
216     //Poll for transmit interrupt flag.\r
217     while(!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG))\r
218     {\r
219         ;\r
220     }\r
221 \r
222     //Send stop condition.\r
223     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;\r
224 \r
225     //Clear transmit interrupt flag before enabling interrupt again\r
226     HWREG16(baseAddress + OFS_UCBxIFG) &= ~(UCTXIFG);\r
227 \r
228     //Reinstate transmit interrupt enable\r
229     HWREG16(baseAddress + OFS_UCBxIE) |= txieStatus;\r
230 }\r
231 \r
232 uint8_t EUSCI_B_I2C_masterReceiveSingleByte(uint16_t baseAddress)\r
233 {\r
234     //Set USCI in Receive mode\r
235     HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~UCTR;\r
236 \r
237     //Send start\r
238     HWREG16(baseAddress + OFS_UCBxCTLW0) |= (UCTXSTT + UCTXSTP);\r
239 \r
240     //Poll for receive interrupt flag.\r
241     while(!(HWREG16(baseAddress + OFS_UCBxIFG) & UCRXIFG))\r
242     {\r
243         ;\r
244     }\r
245 \r
246     //Send single byte data.\r
247     return (HWREG16(baseAddress + OFS_UCBxRXBUF));\r
248 }\r
249 \r
250 bool EUSCI_B_I2C_masterSendSingleByteWithTimeout(uint16_t baseAddress,\r
251                                                  uint8_t txData,\r
252                                                  uint32_t timeout)\r
253 {\r
254     // Creating variable for second timeout scenario\r
255     uint32_t timeout2 = timeout;\r
256 \r
257     //Store current TXIE status\r
258     uint16_t txieStatus = HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE;\r
259 \r
260     //Disable transmit interrupt enable\r
261     HWREG16(baseAddress + OFS_UCBxIE) &= ~(UCTXIE);\r
262 \r
263     //Send start condition.\r
264     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTR + UCTXSTT;\r
265 \r
266     //Poll for transmit interrupt flag.\r
267     while((!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout)\r
268     {\r
269         ;\r
270     }\r
271 \r
272     //Check if transfer timed out\r
273     if(timeout == 0)\r
274     {\r
275         return (STATUS_FAIL);\r
276     }\r
277 \r
278     //Send single byte data.\r
279     HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;\r
280 \r
281     //Poll for transmit interrupt flag.\r
282     while((!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout2)\r
283     {\r
284         ;\r
285     }\r
286 \r
287     //Check if transfer timed out\r
288     if(timeout2 == 0)\r
289     {\r
290         return (STATUS_FAIL);\r
291     }\r
292 \r
293     //Send stop condition.\r
294     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;\r
295 \r
296     //Clear transmit interrupt flag before enabling interrupt again\r
297     HWREG16(baseAddress + OFS_UCBxIFG) &= ~(UCTXIFG);\r
298 \r
299     //Reinstate transmit interrupt enable\r
300     HWREG16(baseAddress + OFS_UCBxIE) |= txieStatus;\r
301 \r
302     return (STATUS_SUCCESS);\r
303 }\r
304 \r
305 void EUSCI_B_I2C_masterSendMultiByteStart(uint16_t baseAddress,\r
306                                           uint8_t txData)\r
307 {\r
308     //Store current transmit interrupt enable\r
309     uint16_t txieStatus = HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE;\r
310 \r
311     //Disable transmit interrupt enable\r
312     HWREG16(baseAddress + OFS_UCBxIE) &= ~(UCTXIE);\r
313 \r
314     //Send start condition.\r
315     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTR + UCTXSTT;\r
316 \r
317     //Poll for transmit interrupt flag.\r
318     while(!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG))\r
319     {\r
320         ;\r
321     }\r
322 \r
323     //Send single byte data.\r
324     HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;\r
325 \r
326     //Reinstate transmit interrupt enable\r
327     HWREG16(baseAddress + OFS_UCBxIE) |= txieStatus;\r
328 }\r
329 \r
330 bool EUSCI_B_I2C_masterSendMultiByteStartWithTimeout(uint16_t baseAddress,\r
331                                                      uint8_t txData,\r
332                                                      uint32_t timeout)\r
333 {\r
334     //Store current transmit interrupt enable\r
335     uint16_t txieStatus = HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE;\r
336 \r
337     //Disable transmit interrupt enable\r
338     HWREG16(baseAddress + OFS_UCBxIE) &= ~(UCTXIE);\r
339 \r
340     //Send start condition.\r
341     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTR + UCTXSTT;\r
342 \r
343     //Poll for transmit interrupt flag.\r
344     while((!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout)\r
345     {\r
346         ;\r
347     }\r
348 \r
349     //Check if transfer timed out\r
350     if(timeout == 0)\r
351     {\r
352         return (STATUS_FAIL);\r
353     }\r
354 \r
355     //Send single byte data.\r
356     HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;\r
357 \r
358     //Reinstate transmit interrupt enable\r
359     HWREG16(baseAddress + OFS_UCBxIE) |= txieStatus;\r
360 \r
361     return(STATUS_SUCCESS);\r
362 }\r
363 \r
364 void EUSCI_B_I2C_masterSendMultiByteNext(uint16_t baseAddress,\r
365                                          uint8_t txData)\r
366 {\r
367     //If interrupts are not used, poll for flags\r
368     if(!(HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE))\r
369     {\r
370         //Poll for transmit interrupt flag.\r
371         while(!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG))\r
372         {\r
373             ;\r
374         }\r
375     }\r
376 \r
377     //Send single byte data.\r
378     HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;\r
379 }\r
380 \r
381 bool EUSCI_B_I2C_masterSendMultiByteNextWithTimeout(uint16_t baseAddress,\r
382                                                     uint8_t txData,\r
383                                                     uint32_t timeout)\r
384 {\r
385     //If interrupts are not used, poll for flags\r
386     if(!(HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE))\r
387     {\r
388         //Poll for transmit interrupt flag.\r
389         while((!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout)\r
390         {\r
391             ;\r
392         }\r
393 \r
394         //Check if transfer timed out\r
395         if(timeout == 0)\r
396         {\r
397             return (STATUS_FAIL);\r
398         }\r
399     }\r
400 \r
401     //Send single byte data.\r
402     HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;\r
403 \r
404     return(STATUS_SUCCESS);\r
405 }\r
406 \r
407 void EUSCI_B_I2C_masterSendMultiByteFinish(uint16_t baseAddress,\r
408                                            uint8_t txData)\r
409 {\r
410     //If interrupts are not used, poll for flags\r
411     if(!(HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE))\r
412     {\r
413         //Poll for transmit interrupt flag.\r
414         while(!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG))\r
415         {\r
416             ;\r
417         }\r
418     }\r
419 \r
420     //Send single byte data.\r
421     HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;\r
422 \r
423     //Poll for transmit interrupt flag.\r
424     while(!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG))\r
425     {\r
426         ;\r
427     }\r
428 \r
429     //Send stop condition.\r
430     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;\r
431 }\r
432 \r
433 bool EUSCI_B_I2C_masterSendMultiByteFinishWithTimeout(uint16_t baseAddress,\r
434                                                       uint8_t txData,\r
435                                                       uint32_t timeout)\r
436 {\r
437     uint32_t timeout2 = timeout;\r
438 \r
439     //If interrupts are not used, poll for flags\r
440     if(!(HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE))\r
441     {\r
442         //Poll for transmit interrupt flag.\r
443         while((!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout)\r
444         {\r
445             ;\r
446         }\r
447 \r
448         //Check if transfer timed out\r
449         if(timeout == 0)\r
450         {\r
451             return (STATUS_FAIL);\r
452         }\r
453     }\r
454 \r
455     //Send single byte data.\r
456     HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;\r
457 \r
458     //Poll for transmit interrupt flag.\r
459     while((!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout2)\r
460     {\r
461         ;\r
462     }\r
463 \r
464     //Check if transfer timed out\r
465     if(timeout2 == 0)\r
466     {\r
467         return (STATUS_FAIL);\r
468     }\r
469 \r
470     //Send stop condition.\r
471     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;\r
472 \r
473     return(STATUS_SUCCESS);\r
474 }\r
475 \r
476 void EUSCI_B_I2C_masterSendStart(uint16_t baseAddress)\r
477 {\r
478     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTT;\r
479 }\r
480 \r
481 void EUSCI_B_I2C_masterSendMultiByteStop(uint16_t baseAddress)\r
482 {\r
483     //If interrupts are not used, poll for flags\r
484     if(!(HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE))\r
485     {\r
486         //Poll for transmit interrupt flag.\r
487         while(!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG))\r
488         {\r
489             ;\r
490         }\r
491     }\r
492 \r
493     //Send stop condition.\r
494     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;\r
495 }\r
496 \r
497 bool EUSCI_B_I2C_masterSendMultiByteStopWithTimeout(uint16_t baseAddress,\r
498                                                     uint32_t timeout)\r
499 {\r
500     //If interrupts are not used, poll for flags\r
501     if(!(HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE))\r
502     {\r
503         //Poll for transmit interrupt flag.\r
504         while((!(HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout)\r
505         {\r
506             ;\r
507         }\r
508 \r
509         //Check if transfer timed out\r
510         if(timeout == 0)\r
511         {\r
512             return (STATUS_FAIL);\r
513         }\r
514     }\r
515 \r
516     //Send stop condition.\r
517     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;\r
518 \r
519     return (STATUS_SUCCESS);\r
520 }\r
521 \r
522 void EUSCI_B_I2C_masterReceiveStart(uint16_t baseAddress)\r
523 {\r
524     //Set USCI in Receive mode\r
525     HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~UCTR;\r
526     //Send start\r
527     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTT;\r
528 }\r
529 \r
530 uint8_t EUSCI_B_I2C_masterReceiveMultiByteNext(uint16_t baseAddress)\r
531 {\r
532     return (HWREG16(baseAddress + OFS_UCBxRXBUF));\r
533 }\r
534 \r
535 uint8_t EUSCI_B_I2C_masterReceiveMultiByteFinish(uint16_t baseAddress)\r
536 {\r
537     //Send stop condition.\r
538     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;\r
539 \r
540     //Wait for Stop to finish\r
541     while(HWREG16(baseAddress + OFS_UCBxCTLW0) & UCTXSTP)\r
542     {\r
543         // Wait for RX buffer\r
544         while(!(HWREG16(baseAddress + OFS_UCBxIFG) & UCRXIFG))\r
545         {\r
546             ;\r
547         }\r
548     }\r
549 \r
550     //Capture data from receive buffer after setting stop bit due to\r
551     //MSP430 I2C critical timing.\r
552     return (HWREG16(baseAddress + OFS_UCBxRXBUF));\r
553 }\r
554 \r
555 bool EUSCI_B_I2C_masterReceiveMultiByteFinishWithTimeout(uint16_t baseAddress,\r
556                                                          uint8_t *txData,\r
557                                                          uint32_t timeout)\r
558 {\r
559     uint32_t timeout2 = timeout;\r
560 \r
561     //Send stop condition.\r
562     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;\r
563 \r
564     //Wait for Stop to finish\r
565     while((HWREG16(baseAddress + OFS_UCBxCTLW0) & UCTXSTP) && --timeout)\r
566     {\r
567         ;\r
568     }\r
569 \r
570     //Check if transfer timed out\r
571     if(timeout == 0)\r
572     {\r
573         return (STATUS_FAIL);\r
574     }\r
575 \r
576     // Wait for RX buffer\r
577     while((!(HWREG16(baseAddress + OFS_UCBxIFG) & UCRXIFG)) && --timeout2)\r
578     {\r
579         ;\r
580     }\r
581 \r
582     //Check if transfer timed out\r
583     if(timeout2 == 0)\r
584     {\r
585         return (STATUS_FAIL);\r
586     }\r
587 \r
588     //Capture data from receive buffer after setting stop bit due to\r
589     //MSP430 I2C critical timing.\r
590     *txData = (HWREG8(baseAddress + OFS_UCBxRXBUF));\r
591 \r
592     return (STATUS_SUCCESS);\r
593 }\r
594 \r
595 void EUSCI_B_I2C_masterReceiveMultiByteStop(uint16_t baseAddress)\r
596 {\r
597     //Send stop condition.\r
598     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;\r
599 }\r
600 \r
601 void EUSCI_B_I2C_enableMultiMasterMode(uint16_t baseAddress)\r
602 {\r
603     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCSWRST;\r
604     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCMM;\r
605 }\r
606 \r
607 void EUSCI_B_I2C_disableMultiMasterMode(uint16_t baseAddress)\r
608 {\r
609     HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCSWRST;\r
610     HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~UCMM;\r
611 }\r
612 \r
613 uint8_t EUSCI_B_I2C_masterReceiveSingle(uint16_t baseAddress)\r
614 {\r
615     //Polling RXIFG0 if RXIE is not enabled\r
616     if(!(HWREG16(baseAddress + OFS_UCBxIE) & UCRXIE0))\r
617     {\r
618         while(!(HWREG16(baseAddress + OFS_UCBxIFG) & UCRXIFG0))\r
619         {\r
620             ;\r
621         }\r
622     }\r
623 \r
624     //Read a byte.\r
625     return (HWREG16(baseAddress + OFS_UCBxRXBUF));\r
626 }\r
627 \r
628 uint32_t EUSCI_B_I2C_getReceiveBufferAddress(uint16_t baseAddress)\r
629 {\r
630     return (baseAddress + OFS_UCBxRXBUF);\r
631 }\r
632 \r
633 uint32_t EUSCI_B_I2C_getTransmitBufferAddress(uint16_t baseAddress)\r
634 {\r
635     return (baseAddress + OFS_UCBxTXBUF);\r
636 }\r
637 \r
638 #endif\r
639 //*****************************************************************************\r
640 //\r
641 //! Close the doxygen group for eusci_b_i2c_api\r
642 //! @}\r
643 //\r
644 //*****************************************************************************\r