]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil/driverlib/i2c.c
Update MSP432 projects to use updated driver library files.
[freertos] / FreeRTOS / Demo / CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil / driverlib / i2c.c
1 /*
2  * -------------------------------------------
3  *    MSP432 DriverLib - v3_10_00_09 
4  * -------------------------------------------
5  *
6  * --COPYRIGHT--,BSD,BSD
7  * Copyright (c) 2014, Texas Instruments Incorporated
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * *  Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  *
17  * *  Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * *  Neither the name of Texas Instruments Incorporated nor the names of
22  *    its contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  * --/COPYRIGHT--*/
37 #include <i2c.h>
38 #include <interrupt.h>
39 #include <debug.h>
40 #include <hw_memmap.h>
41
42 void I2C_initMaster(uint32_t moduleInstance, const eUSCI_I2C_MasterConfig *config)
43 {
44     uint_fast16_t preScalarValue;
45
46     ASSERT(
47             (EUSCI_B_I2C_CLOCKSOURCE_ACLK == config->selectClockSource)
48             || (EUSCI_B_I2C_CLOCKSOURCE_SMCLK
49                     == config->selectClockSource));
50
51     ASSERT(
52             (EUSCI_B_I2C_SET_DATA_RATE_400KBPS == config->dataRate)
53             || (EUSCI_B_I2C_SET_DATA_RATE_100KBPS == config->dataRate)
54             || (EUSCI_B_I2C_SET_DATA_RATE_1MBPS == config->dataRate));
55
56     ASSERT(
57             (EUSCI_B_I2C_NO_AUTO_STOP == config->autoSTOPGeneration)
58             || (EUSCI_B_I2C_SET_BYTECOUNT_THRESHOLD_FLAG
59                     == config->autoSTOPGeneration)
60             || (EUSCI_B_I2C_SEND_STOP_AUTOMATICALLY_ON_BYTECOUNT_THRESHOLD
61                     == config->autoSTOPGeneration));
62
63     /* Disable the USCI module and clears the other bits of control register */
64     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_SWRST_OFS) =
65                 1;
66
67     /* Configure Automatic STOP condition generation */
68     EUSCI_B_CMSIS(moduleInstance)->CTLW1 =
69             (EUSCI_B_CMSIS(moduleInstance)->CTLW1 & ~EUSCI_B_CTLW1_ASTP_MASK)
70                     | (config->autoSTOPGeneration);
71
72     /* Byte Count Threshold */
73     EUSCI_B_CMSIS(moduleInstance)->TBCNT = config->byteCounterThreshold;
74
75     /*
76      * Configure as I2C master mode.
77      * UCMST = Master mode
78      * UCMODE_3 = I2C mode
79      * UCSYNC = Synchronous mode
80      */
81     EUSCI_B_CMSIS(moduleInstance)->CTLW0 =
82             (EUSCI_B_CMSIS(moduleInstance)->CTLW0 & ~EUSCI_B_CTLW0_SSEL_MASK)
83                     | (config->selectClockSource | EUSCI_B_CTLW0_MST
84                                 | EUSCI_B_CTLW0_MODE_3 | EUSCI_B_CTLW0_SYNC
85                             | EUSCI_B_CTLW0_SWRST);
86
87     /*
88      * Compute the clock divider that achieves the fastest speed less than or
89      * equal to the desired speed.  The numerator is biased to favor a larger
90      * clock divider so that the resulting clock is always less than or equal
91      * to the desired clock, never greater.
92      */
93     preScalarValue = (uint16_t) (config->i2cClk / config->dataRate);
94
95     EUSCI_B_CMSIS(moduleInstance)->BRW = preScalarValue;
96 }
97
98 void I2C_initSlave(uint32_t moduleInstance, uint_fast16_t slaveAddress,
99         uint_fast8_t slaveAddressOffset, uint32_t slaveOwnAddressEnable)
100 {
101     ASSERT(
102             (EUSCI_B_I2C_OWN_ADDRESS_OFFSET0 == slaveAddressOffset)
103             || (EUSCI_B_I2C_OWN_ADDRESS_OFFSET1 == slaveAddressOffset)
104             || (EUSCI_B_I2C_OWN_ADDRESS_OFFSET2 == slaveAddressOffset)
105             || (EUSCI_B_I2C_OWN_ADDRESS_OFFSET3 == slaveAddressOffset));
106
107     /* Disable the USCI module */
108     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_SWRST_OFS) =
109                 1;
110
111     /* Clear USCI master mode */
112     EUSCI_B_CMSIS(moduleInstance)->CTLW0 =
113             (EUSCI_B_CMSIS(moduleInstance)->CTLW0 & (~EUSCI_B_CTLW0_MST))
114                     | (EUSCI_B_CTLW0_MODE_3 + EUSCI_B_CTLW0_SYNC);
115
116     /* Set up the slave address. */
117     HWREG16((uint32_t)&EUSCI_B_CMSIS(moduleInstance)->I2COA0 + slaveAddressOffset) =
118                 slaveAddress + slaveOwnAddressEnable;
119 }
120
121 void I2C_enableModule(uint32_t moduleInstance)
122 {
123     /* Reset the UCSWRST bit to enable the USCI Module */
124     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_SWRST_OFS) =
125                 0;
126 }
127
128 void I2C_disableModule(uint32_t moduleInstance)
129 {
130     /* Set the UCSWRST bit to disable the USCI Module */
131     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_SWRST_OFS) =
132                 1;
133     ;
134 }
135
136 void I2C_setSlaveAddress(uint32_t moduleInstance, uint_fast16_t slaveAddress)
137 {
138     /* Set the address of the slave with which the master will communicate */
139     EUSCI_B_CMSIS(moduleInstance)->I2CSA = (slaveAddress);
140 }
141
142 void I2C_setMode(uint32_t moduleInstance, uint_fast8_t mode)
143 {
144     ASSERT(
145             (EUSCI_B_I2C_TRANSMIT_MODE == mode)
146             || (EUSCI_B_I2C_RECEIVE_MODE == mode));
147
148     EUSCI_B_CMSIS(moduleInstance)->CTLW0 =
149             (EUSCI_B_CMSIS(moduleInstance)->CTLW0
150                     & (~EUSCI_B_I2C_TRANSMIT_MODE)) | mode;
151
152 }
153
154 uint8_t I2C_masterReceiveSingleByte(uint32_t moduleInstance)
155 {
156     //Set USCI in Receive mode
157     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_TR_OFS) = 0;
158
159     //Send start
160     EUSCI_B_CMSIS(moduleInstance)->CTLW0 |= (EUSCI_B_CTLW0_TXSTT + EUSCI_B_CTLW0_TXSTP);
161
162     //Poll for receive interrupt flag.
163     while (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG, EUSCI_B_IFG_RXIFG_OFS))
164         ;
165
166     //Send single byte data.
167     return (EUSCI_B_CMSIS(moduleInstance)->RXBUF & EUSCI_B_RXBUF_RXBUF_MASK);
168 }
169
170 void I2C_slavePutData(uint32_t moduleInstance, uint8_t transmitData)
171 {
172     //Send single byte data.
173     EUSCI_B_CMSIS(moduleInstance)->TXBUF = transmitData;
174 }
175
176 uint8_t I2C_slaveGetData(uint32_t moduleInstance)
177 {
178     //Read a byte.
179     return (EUSCI_B_CMSIS(moduleInstance)->RXBUF & EUSCI_B_RXBUF_RXBUF_MASK);
180 }
181
182 uint8_t I2C_isBusBusy(uint32_t moduleInstance)
183 {
184     //Return the bus busy status.
185     return BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->STATW,
186                 EUSCI_B_STATW_BBUSY_OFS);
187 }
188
189 void I2C_masterSendSingleByte(uint32_t moduleInstance, uint8_t txData)
190 {
191     //Store current TXIE status
192     uint16_t txieStatus = EUSCI_B_CMSIS(moduleInstance)->IE & EUSCI_B_IE_TXIE0;
193
194     //Disable transmit interrupt enable
195     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IE, EUSCI_B_IE_TXIE0_OFS) = 0;
196
197     //Send start condition.
198     EUSCI_B_CMSIS(moduleInstance)->CTLW0 |= EUSCI_B_CTLW0_TR + EUSCI_B_CTLW0_TXSTT;
199
200     //Poll for transmit interrupt flag.
201     while (!(EUSCI_B_CMSIS(moduleInstance)->IFG & EUSCI_B_IFG_TXIFG))
202         ;
203
204     //Send single byte data.
205     EUSCI_B_CMSIS(moduleInstance)->TXBUF = txData;
206
207     //Poll for transmit interrupt flag.
208     while (!(EUSCI_B_CMSIS(moduleInstance)->IFG & EUSCI_B_IFG_TXIFG))
209         ;
210
211     //Send stop condition.
212     EUSCI_B_CMSIS(moduleInstance)->CTLW0 |= EUSCI_B_CTLW0_TXSTP;
213
214     //Clear transmit interrupt flag before enabling interrupt again
215     EUSCI_B_CMSIS(moduleInstance)->IFG &= ~(EUSCI_B_IFG_TXIFG);
216
217     //Reinstate transmit interrupt enable
218     EUSCI_B_CMSIS(moduleInstance)->IE |= txieStatus;
219 }
220
221 bool I2C_masterSendSingleByteWithTimeout(uint32_t moduleInstance,
222         uint8_t txData, uint32_t timeout)
223 {
224     uint_fast16_t txieStatus;
225     uint32_t timeout2 = timeout;
226
227     ASSERT(timeout > 0);
228
229     //Store current TXIE status
230     txieStatus = EUSCI_B_CMSIS(moduleInstance)->IE & EUSCI_B_IE_TXIE0;
231
232     //Disable transmit interrupt enable
233     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IE,EUSCI_B_IE_TXIE0_OFS) = 0;
234
235     //Send start condition.
236     EUSCI_B_CMSIS(moduleInstance)->CTLW0 |= EUSCI_B_CTLW0_TR + EUSCI_B_CTLW0_TXSTT;
237
238     //Poll for transmit interrupt flag.
239     while ((!(EUSCI_B_CMSIS(moduleInstance)->IFG & EUSCI_B_IFG_TXIFG)) && --timeout)
240         ;
241
242     //Check if transfer timed out
243     if (timeout == 0)
244         return false;
245
246     //Send single byte data.
247     EUSCI_B_CMSIS(moduleInstance)->TXBUF = txData;
248
249     //Poll for transmit interrupt flag.
250     while ((!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG, EUSCI_B_IFG_TXIFG0_OFS))
251             && --timeout2)
252         ;
253
254     //Check if transfer timed out
255     if (timeout2 == 0)
256         return false;
257
258     //Send stop condition.
259     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_TXSTP_OFS) = 1;
260
261     //Clear transmit interrupt flag before enabling interrupt again
262     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG,EUSCI_B_IFG_TXIFG0_OFS) = 0;
263
264     //Reinstate transmit interrupt enable
265     EUSCI_B_CMSIS(moduleInstance)->IE |= txieStatus;
266
267     return true;
268 }
269
270 void I2C_masterSendMultiByteStart(uint32_t moduleInstance, uint8_t txData)
271 {
272     //Store current transmit interrupt enable
273     uint16_t txieStatus = EUSCI_B_CMSIS(moduleInstance)->IE & EUSCI_B_IE_TXIE0;
274
275     //Disable transmit interrupt enable
276     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IE, EUSCI_B_IE_TXIE0_OFS) = 0;
277
278     //Send start condition.
279     EUSCI_B_CMSIS(moduleInstance)->CTLW0 |= EUSCI_B_CTLW0_TR + EUSCI_B_CTLW0_TXSTT;
280
281     //Poll for transmit interrupt flag.
282     while (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG, EUSCI_B_IFG_TXIFG0_OFS))
283         ;
284
285     //Send single byte data.
286     EUSCI_B_CMSIS(moduleInstance)->TXBUF = txData;
287
288     //Reinstate transmit interrupt enable
289     EUSCI_B_CMSIS(moduleInstance)->IE |= txieStatus;
290 }
291
292 bool I2C_masterSendMultiByteStartWithTimeout(uint32_t moduleInstance,
293         uint8_t txData, uint32_t timeout)
294 {
295     uint_fast16_t txieStatus;
296
297     ASSERT(timeout > 0);
298
299     //Store current transmit interrupt enable
300     txieStatus = EUSCI_B_CMSIS(moduleInstance)->IE & EUSCI_B_IE_TXIE0;
301
302     //Disable transmit interrupt enable
303     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IE,EUSCI_B_IE_TXIE0_OFS) = 0;
304
305     //Send start condition.
306     EUSCI_B_CMSIS(moduleInstance)->CTLW0 |= EUSCI_B_CTLW0_TR + EUSCI_B_CTLW0_TXSTT;
307
308     //Poll for transmit interrupt flag.
309     while ((!(BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG, EUSCI_B_IFG_TXIFG0_OFS))
310             && --timeout))
311         ;
312
313     //Check if transfer timed out
314     if (timeout == 0)
315         return false;
316
317     //Send single byte data.
318     EUSCI_B_CMSIS(moduleInstance)->TXBUF = txData;
319
320     //Reinstate transmit interrupt enable
321     EUSCI_B_CMSIS(moduleInstance)->IE |= txieStatus;
322
323     return true;
324 }
325
326 void I2C_masterSendMultiByteNext(uint32_t moduleInstance, uint8_t txData)
327 {
328     //If interrupts are not used, poll for flags
329     if (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IE, EUSCI_B_IE_TXIE0_OFS))
330     {
331         //Poll for transmit interrupt flag.
332         while
333             (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG, EUSCI_B_IFG_TXIFG0_OFS))
334             ;
335     }
336
337     //Send single byte data.
338     EUSCI_B_CMSIS(moduleInstance)->TXBUF = txData;
339 }
340
341 bool I2C_masterSendMultiByteNextWithTimeout(uint32_t moduleInstance,
342         uint8_t txData, uint32_t timeout)
343 {
344     ASSERT(timeout > 0);
345
346     //If interrupts are not used, poll for flags
347     if (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IE, EUSCI_B_IE_TXIE0_OFS))
348     {
349         //Poll for transmit interrupt flag.
350         while ((!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG,
351                 EUSCI_B_IFG_TXIFG0_OFS)) && --timeout)
352             ;
353
354         //Check if transfer timed out
355         if (timeout == 0)
356             return false;
357     }
358
359     //Send single byte data.
360     EUSCI_B_CMSIS(moduleInstance)->TXBUF = txData;
361
362     return true;
363 }
364
365 void I2C_masterSendMultiByteFinish(uint32_t moduleInstance, uint8_t txData)
366 {
367     //If interrupts are not used, poll for flags
368     if (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IE, EUSCI_B_IE_TXIE0_OFS))
369     {
370         //Poll for transmit interrupt flag.
371         while
372             (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG, EUSCI_B_IFG_TXIFG0_OFS))
373             ;
374     }
375
376     //Send single byte data.
377     EUSCI_B_CMSIS(moduleInstance)->TXBUF = txData;
378
379     //Poll for transmit interrupt flag.
380     while (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG, EUSCI_B_IFG_TXIFG0_OFS))
381         ;
382
383     //Send stop condition.
384     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_TXSTP_OFS) = 1;
385 }
386
387 bool I2C_masterSendMultiByteFinishWithTimeout(uint32_t moduleInstance,
388         uint8_t txData, uint32_t timeout)
389 {
390     uint32_t timeout2 = timeout;
391
392     ASSERT(timeout > 0);
393
394     //If interrupts are not used, poll for flags
395     if (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IE, EUSCI_B_IE_TXIE0_OFS))
396     {
397         //Poll for transmit interrupt flag.
398         while ((!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG,
399                 EUSCI_B_IFG_TXIFG0_OFS)) && --timeout)
400             ;
401
402         //Check if transfer timed out
403         if (timeout == 0)
404             return false;
405     }
406
407     //Send single byte data.
408     EUSCI_B_CMSIS(moduleInstance)->TXBUF = txData;
409
410     //Poll for transmit interrupt flag.
411     while ((!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG, EUSCI_B_IFG_TXIFG0_OFS))
412             && --timeout2)
413         ;
414
415     //Check if transfer timed out
416     if (timeout2 == 0)
417         return false;
418
419     //Send stop condition.
420     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_TXSTP_OFS) = 1;
421
422     return true;
423 }
424
425 void I2C_masterSendMultiByteStop(uint32_t moduleInstance)
426 {
427     //If interrupts are not used, poll for flags
428     if (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IE, EUSCI_B_IE_TXIE0_OFS))
429     {
430         //Poll for transmit interrupt flag.
431         while
432             (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG, EUSCI_B_IFG_TXIFG0_OFS))
433             ;
434     }
435
436     //Send stop condition.
437     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_TXSTP_OFS) = 1;
438 }
439
440 bool I2C_masterSendMultiByteStopWithTimeout(uint32_t moduleInstance,
441         uint32_t timeout)
442 {
443     ASSERT(timeout > 0);
444
445     //If interrupts are not used, poll for flags
446     if (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IE, EUSCI_B_IE_TXIE0_OFS))
447     {
448         //Poll for transmit interrupt flag.
449         while ((!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG,
450                 EUSCI_B_IFG_TXIFG0_OFS)) && --timeout)
451             ;
452
453         //Check if transfer timed out
454         if (timeout == 0)
455             return false;
456     }
457
458     //Send stop condition.
459     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_TXSTP_OFS) = 1;
460
461     return 0x01;
462 }
463
464 void I2C_masterReceiveStart(uint32_t moduleInstance)
465 {
466     //Set USCI in Receive mode
467     EUSCI_B_CMSIS(moduleInstance)->CTLW0 =
468             (EUSCI_B_CMSIS(moduleInstance)->CTLW0 & (~EUSCI_B_CTLW0_TR))
469                         | EUSCI_B_CTLW0_TXSTT;
470 }
471
472 uint8_t I2C_masterReceiveMultiByteNext(uint32_t moduleInstance)
473 {
474     return (EUSCI_B_CMSIS(moduleInstance)->RXBUF & EUSCI_B_RXBUF_RXBUF_MASK);
475 }
476
477 uint8_t I2C_masterReceiveMultiByteFinish(uint32_t moduleInstance)
478 {
479     //Send stop condition.
480     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_TXSTP_OFS) =
481                 1;
482
483     //Wait for Stop to finish
484     while (BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0, EUSCI_B_CTLW0_TXSTP_OFS))
485     {
486         // Wait for RX buffer
487         while (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG, EUSCI_B_IFG_RXIFG_OFS))
488             ;
489     }
490
491     /* Capture data from receive buffer after setting stop bit due to
492      MSP430 I2C critical timing. */
493     return (EUSCI_B_CMSIS(moduleInstance)->RXBUF & EUSCI_B_RXBUF_RXBUF_MASK);
494 }
495
496 bool I2C_masterReceiveMultiByteFinishWithTimeout(uint32_t moduleInstance,
497         uint8_t *txData, uint32_t timeout)
498 {
499     uint32_t timeout2 = timeout;
500
501     ASSERT(timeout > 0);
502
503     //Send stop condition.
504     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_TXSTP_OFS) = 1;
505
506     //Wait for Stop to finish
507     while (BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0, EUSCI_B_CTLW0_TXSTP_OFS)
508             && --timeout)
509         ;
510
511     //Check if transfer timed out
512     if (timeout == 0)
513         return false;
514
515     // Wait for RX buffer
516     while ((!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG, EUSCI_B_IFG_RXIFG_OFS))
517             && --timeout2)
518         ;
519
520     //Check if transfer timed out
521     if (timeout2 == 0)
522         return false;
523
524     //Capture data from receive buffer after setting stop bit due to
525     //MSP430 I2C critical timing.
526     *txData = (EUSCI_B_CMSIS(moduleInstance)->RXBUF & EUSCI_B_RXBUF_RXBUF_MASK);
527
528     return true;
529 }
530
531 void I2C_masterReceiveMultiByteStop(uint32_t moduleInstance)
532 {
533     //Send stop condition.
534     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_TXSTP_OFS) = 1;
535 }
536
537 uint8_t I2C_masterReceiveSingle(uint32_t moduleInstance)
538 {
539     //Polling RXIFG0 if RXIE is not enabled
540     if (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IE, EUSCI_B_IE_RXIE0_OFS))
541     {
542         while (!BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->IFG,
543                         EUSCI_B_IFG_RXIFG0_OFS))
544             ;
545     }
546
547     //Read a byte.
548     return (EUSCI_B_CMSIS(moduleInstance)->RXBUF & EUSCI_B_RXBUF_RXBUF_MASK) ;
549 }
550
551 uint32_t I2C_getReceiveBufferAddressForDMA(uint32_t moduleInstance)
552 {
553     return (uint32_t)&EUSCI_B_CMSIS(moduleInstance)->RXBUF;
554 }
555
556 uint32_t I2C_getTransmitBufferAddressForDMA(uint32_t moduleInstance)
557 {
558     return (uint32_t)&EUSCI_B_CMSIS(moduleInstance)->TXBUF;
559 }
560
561 uint8_t I2C_masterIsStopSent(uint32_t moduleInstance)
562 {
563     return BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,
564                 EUSCI_B_CTLW0_TXSTP_OFS);
565 }
566
567 bool I2C_masterIsStartSent(uint32_t moduleInstance)
568 {
569     return BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,
570                 EUSCI_B_CTLW0_TXSTT_OFS);
571 }
572
573 void I2C_masterSendStart(uint32_t moduleInstance)
574 {
575     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_TXSTT_OFS) =
576                 1;
577 }
578
579 void I2C_enableMultiMasterMode(uint32_t moduleInstance)
580 {
581     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_SWRST_OFS) =
582                 1;
583     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_MM_OFS) = 1;
584 }
585
586 void I2C_disableMultiMasterMode(uint32_t moduleInstance)
587 {
588     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_SWRST_OFS) =
589                 1;
590     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_MM_OFS) = 0;
591 }
592
593 void I2C_enableInterrupt(uint32_t moduleInstance, uint_fast16_t mask)
594 {
595     ASSERT(
596             0x00
597             == (mask
598                     & ~(EUSCI_B_I2C_STOP_INTERRUPT
599                             + EUSCI_B_I2C_START_INTERRUPT
600                             + EUSCI_B_I2C_NAK_INTERRUPT
601                             + EUSCI_B_I2C_ARBITRATIONLOST_INTERRUPT
602                             + EUSCI_B_I2C_BIT9_POSITION_INTERRUPT
603                             + EUSCI_B_I2C_CLOCK_LOW_TIMEOUT_INTERRUPT
604                             + EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT
605                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT0
606                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT1
607                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT2
608                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT3
609                             + EUSCI_B_I2C_RECEIVE_INTERRUPT0
610                             + EUSCI_B_I2C_RECEIVE_INTERRUPT1
611                             + EUSCI_B_I2C_RECEIVE_INTERRUPT2
612                             + EUSCI_B_I2C_RECEIVE_INTERRUPT3)));
613
614     //Enable the interrupt masked bit
615     EUSCI_B_CMSIS(moduleInstance)->IE |= mask;
616 }
617
618 void I2C_disableInterrupt(uint32_t moduleInstance, uint_fast16_t mask)
619 {
620     ASSERT(
621             0x00
622             == (mask
623                     & ~(EUSCI_B_I2C_STOP_INTERRUPT
624                             + EUSCI_B_I2C_START_INTERRUPT
625                             + EUSCI_B_I2C_NAK_INTERRUPT
626                             + EUSCI_B_I2C_ARBITRATIONLOST_INTERRUPT
627                             + EUSCI_B_I2C_BIT9_POSITION_INTERRUPT
628                             + EUSCI_B_I2C_CLOCK_LOW_TIMEOUT_INTERRUPT
629                             + EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT
630                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT0
631                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT1
632                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT2
633                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT3
634                             + EUSCI_B_I2C_RECEIVE_INTERRUPT0
635                             + EUSCI_B_I2C_RECEIVE_INTERRUPT1
636                             + EUSCI_B_I2C_RECEIVE_INTERRUPT2
637                             + EUSCI_B_I2C_RECEIVE_INTERRUPT3)));
638
639     //Disable the interrupt masked bit
640     EUSCI_B_CMSIS(moduleInstance)->IE &= ~(mask);
641 }
642
643 void I2C_clearInterruptFlag(uint32_t moduleInstance, uint_fast16_t mask)
644 {
645     ASSERT(
646             0x00
647             == (mask
648                     & ~(EUSCI_B_I2C_STOP_INTERRUPT
649                             + EUSCI_B_I2C_START_INTERRUPT
650                             + EUSCI_B_I2C_NAK_INTERRUPT
651                             + EUSCI_B_I2C_ARBITRATIONLOST_INTERRUPT
652                             + EUSCI_B_I2C_BIT9_POSITION_INTERRUPT
653                             + EUSCI_B_I2C_CLOCK_LOW_TIMEOUT_INTERRUPT
654                             + EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT
655                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT0
656                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT1
657                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT2
658                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT3
659                             + EUSCI_B_I2C_RECEIVE_INTERRUPT0
660                             + EUSCI_B_I2C_RECEIVE_INTERRUPT1
661                             + EUSCI_B_I2C_RECEIVE_INTERRUPT2
662                             + EUSCI_B_I2C_RECEIVE_INTERRUPT3)));
663     //Clear the I2C interrupt source.
664     EUSCI_B_CMSIS(moduleInstance)->IFG &= ~(mask);
665 }
666
667 uint_fast16_t I2C_getInterruptStatus(uint32_t moduleInstance, uint16_t mask)
668 {
669     ASSERT(
670             0x00
671             == (mask
672                     & ~(EUSCI_B_I2C_STOP_INTERRUPT
673                             + EUSCI_B_I2C_START_INTERRUPT
674                             + EUSCI_B_I2C_NAK_INTERRUPT
675                             + EUSCI_B_I2C_ARBITRATIONLOST_INTERRUPT
676                             + EUSCI_B_I2C_BIT9_POSITION_INTERRUPT
677                             + EUSCI_B_I2C_CLOCK_LOW_TIMEOUT_INTERRUPT
678                             + EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT
679                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT0
680                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT1
681                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT2
682                             + EUSCI_B_I2C_TRANSMIT_INTERRUPT3
683                             + EUSCI_B_I2C_RECEIVE_INTERRUPT0
684                             + EUSCI_B_I2C_RECEIVE_INTERRUPT1
685                             + EUSCI_B_I2C_RECEIVE_INTERRUPT2
686                             + EUSCI_B_I2C_RECEIVE_INTERRUPT3)));
687     //Return the interrupt status of the request masked bit.
688     return EUSCI_B_CMSIS(moduleInstance)->IFG & mask;
689 }
690
691 uint_fast16_t I2C_getEnabledInterruptStatus(uint32_t moduleInstance)
692 {
693     return I2C_getInterruptStatus(moduleInstance,
694     EUSCI_B_CMSIS(moduleInstance)->IE);
695 }
696
697 uint_fast16_t I2C_getMode(uint32_t moduleInstance)
698 {
699     //Read the I2C mode.
700     return (EUSCI_B_CMSIS(moduleInstance)->CTLW0 & EUSCI_B_CTLW0_TR);
701 }
702
703 void I2C_registerInterrupt(uint32_t moduleInstance, void (*intHandler)(void))
704 {
705     switch (moduleInstance)
706     {
707     case EUSCI_B0_BASE:
708         Interrupt_registerInterrupt(INT_EUSCIB0, intHandler);
709         Interrupt_enableInterrupt(INT_EUSCIB0);
710         break;
711     case EUSCI_B1_BASE:
712         Interrupt_registerInterrupt(INT_EUSCIB1, intHandler);
713         Interrupt_enableInterrupt(INT_EUSCIB1);
714         break;
715 #ifdef EUSCI_B2_BASE
716     case EUSCI_B2_BASE:
717         Interrupt_registerInterrupt(INT_EUSCIB2, intHandler);
718         Interrupt_enableInterrupt(INT_EUSCIB2);
719         break;
720 #endif
721 #ifdef EUSCI_B3_BASE
722     case EUSCI_B3_BASE:
723         Interrupt_registerInterrupt(INT_EUSCIB3, intHandler);
724         Interrupt_enableInterrupt(INT_EUSCIB3);
725         break;
726 #endif
727     default:
728         ASSERT(false);
729     }
730 }
731
732 void I2C_unregisterInterrupt(uint32_t moduleInstance)
733 {
734     switch (moduleInstance)
735     {
736     case EUSCI_B0_BASE:
737         Interrupt_disableInterrupt(INT_EUSCIB0);
738         Interrupt_unregisterInterrupt(INT_EUSCIB0);
739         break;
740     case EUSCI_B1_BASE:
741         Interrupt_disableInterrupt(INT_EUSCIB1);
742         Interrupt_unregisterInterrupt(INT_EUSCIB1);
743         break;
744 #ifdef EUSCI_B2_BASE
745     case EUSCI_B2_BASE:
746         Interrupt_disableInterrupt(INT_EUSCIB2);
747         Interrupt_unregisterInterrupt(INT_EUSCIB2);
748         break;
749 #endif
750 #ifdef EUSCI_B3_BASE
751     case EUSCI_B3_BASE:
752         Interrupt_disableInterrupt(INT_EUSCIB3);
753         Interrupt_unregisterInterrupt(INT_EUSCIB3);
754         break;
755 #endif
756     default:
757         ASSERT(false);
758     }
759 }
760
761 void I2C_slaveSendNAK(uint32_t moduleInstance)
762 {
763     BITBAND_PERI(EUSCI_B_CMSIS(moduleInstance)->CTLW0,EUSCI_B_CTLW0_TXNACK_OFS)
764                 = 1;
765 }