void initI2CModule(void)
{
	unsigned int flags = EUSCI_B_I2C_TRANSMIT_INTERRUPT0 |
						 EUSCI_B_I2C_RECEIVE_INTERRUPT0 |
						 EUSCI_B_I2C_TRANSMIT_INTERRUPT1 |
						 EUSCI_B_I2C_RECEIVE_INTERRUPT1 |
						 EUSCI_B_I2C_STOP_INTERRUPT |
						 EUSCI_B_I2C_NAK_INTERRUPT;
#if 0
    /* Select Port B1 for I2C - Set Pin 4, 5 to input Primary Module Function,
     *   (UCB1SIMO/UCB1SDA, UCB1SOMI/UCB1SCL).
     */
    GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P6,
            GPIO_PIN4 + GPIO_PIN5, GPIO_PRIMARY_MODULE_FUNCTION);


    /* eUSCI I2C Slave Configuration */
    I2C_initSlave(EUSCI_B1_BASE, SLAVE_ADDRESS, EUSCI_B_I2C_OWN_ADDRESS_OFFSET0,
            EUSCI_B_I2C_OWN_ADDRESS_ENABLE);

    I2C_initSlave(EUSCI_B1_BASE, 0xC, EUSCI_B_I2C_OWN_ADDRESS_OFFSET1,
    	            EUSCI_B_I2C_OWN_ADDRESS_ENABLE);

    I2C_setMode(EUSCI_B1_BASE, EUSCI_B_I2C_RECEIVE_MODE);
    /* Enable the module and enable interrupts */
    I2C_enableModule(EUSCI_B1_BASE);
    MAP_I2C_clearInterruptFlag(EUSCI_B1_BASE, EUSCI_B_I2C_RECEIVE_INTERRUPT0);
    MAP_I2C_enableInterrupt(EUSCI_B1_BASE, EUSCI_B_I2C_RECEIVE_INTERRUPT0);
    MAP_Interrupt_enableInterrupt(INT_EUSCIB1);
#else
    /* eUSCI I2C Slave Configuration */
    I2C_initSlave(EUSCI_B0_BASE, SLAVE_ADDRESS, EUSCI_B_I2C_OWN_ADDRESS_OFFSET0,
            EUSCI_B_I2C_OWN_ADDRESS_ENABLE);
#if 1
    I2C_initSlave(EUSCI_B0_BASE, 0xC, EUSCI_B_I2C_OWN_ADDRESS_OFFSET1,
	            EUSCI_B_I2C_OWN_ADDRESS_ENABLE);
#endif

    /* Select Port B2 for I2C - Set Pin 6, 7 to input Primary Module Function
     */
    MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
            GPIO_PIN6 | GPIO_PIN7, GPIO_PRIMARY_MODULE_FUNCTION);

    /* Define the SMbus alert pin */
    MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P5, GPIO_PIN0);
    MAP_GPIO_setAsOutputPin(GPIO_PORT_P5, GPIO_PIN0);


    I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE);
    /* Enable the module and enable interrupts */
    I2C_enableModule(EUSCI_B0_BASE);
    MAP_I2C_clearInterruptFlag(EUSCI_B0_BASE, flags);
    MAP_Interrupt_setPriority(INT_EUSCIB0,  configMAX_SYSCALL_INTERRUPT_PRIORITY);
    MAP_I2C_enableInterrupt(EUSCI_B0_BASE, flags);
    MAP_Interrupt_enableInterrupt(INT_EUSCIB0);
#endif
}
/*******************************************************************************
 * eUSCIB1 ISR. The repeated start and transmit/receive operations happen
 * within this ISR.
 ******************************************************************************/
void euscib1_isr(void)
{
    uint_fast16_t status;
    status = MAP_I2C_getEnabledInterruptStatus(EUSCI_B0_BASE);
    MAP_I2C_clearInterruptFlag(EUSCI_B0_BASE, status);
    static bool inAlert = false;


    if (status & EUSCI_B_I2C_STOP_INTERRUPT) {
#ifdef EVDEBUG
    	eventArr[eventIx++] = STOP;
#endif
    	if (inAlert) {
    		inAlert = false;
    		I2C_AlertDone();
    	}
    	else
    		i2c_l2_buf.stop();
    	return;
    }
    if (status & EUSCI_B_I2C_NAK_INTERRUPT) {
#ifdef EVDEBUG
        eventArr[eventIx++] = NAK;
#endif
        i2c_l2_buf.nak();
        return;
    }


    /* RXIFG */
    if (status & EUSCI_B_I2C_RECEIVE_INTERRUPT0)
    {
#ifdef EVDEBUG
    	eventArr[eventIx++] = RECEIVE;
#endif
    	i2c_l2_buf.put(MAP_I2C_slaveGetData(EUSCI_B0_BASE));
    }

    /* TXIFG */
    if (status & EUSCI_B_I2C_TRANSMIT_INTERRUPT0)
    {
#ifdef EVDEBUG
    	eventArr[eventIx++] = TRANSMIT;
#endif
        MAP_I2C_slavePutData(EUSCI_B0_BASE, i2c_l2_buf.get());
    }

    if (status & EUSCI_B_I2C_TRANSMIT_INTERRUPT1)
    {
    	// SMbus alert response. Put my address as a reply
    #ifdef EVDEBUG
       	eventArr[eventIx++] = TRANSMIT_SMBUS_ADDR;
    #endif
       	inAlert = true;
    	MAP_I2C_slavePutData(EUSCI_B0_BASE, SLAVE_ADDRESS << 1);
    }
#ifdef EVDEBUG
    if (eventIx == 50) eventIx = 0;
#endif
}
/******************************************************************************
 * The USCI_B0 data ISR RX vector is used to move received data from the I2C
 * master to the MSP432 memory.
 ******************************************************************************/
void euscib0_isr(void)
{
    uint_fast16_t status;

    status = MAP_I2C_getEnabledInterruptStatus(EUSCI_B0_MODULE);
    MAP_I2C_clearInterruptFlag(EUSCI_B0_MODULE, status);

    /* RXIFG for Slave Address 1*/
    if (status & EUSCI_B_I2C_TRANSMIT_INTERRUPT0)
    {
        MAP_I2C_slavePutData(EUSCI_B0_MODULE, TXData[xferIndex++]);

        /* Resetting the index if we are at the end. Also resetting the GPIO
         * if we are on the last byte */
        if (xferIndex  == NUM_OF_RX_BYTES)
        {
            MAP_I2C_disableInterrupt(EUSCI_B0_MODULE, EUSCI_B_I2C_TRANSMIT_INTERRUPT0);
            MAP_GPIO_setAsInputPin(GPIO_PORT_P1, GPIO_PIN0);
            MAP_Interrupt_disableSleepOnIsrExit();
            xferIndex = 0;

            /* IMPORTANT: Clearing out the TX Buffer for the next transfer.
             * This is required to compensate for the shift/buffer settings
             * of the I2C module */
            MAP_I2C_slavePutData(EUSCI_B0_MODULE, 0);
        }
    }
}
/******************************************************************************
 * The USCI_B0 data ISR RX vector is used to move received data from the I2C
 * master to the MSP432 memory.
 ******************************************************************************/
void euscib0_isr(void)
{
    uint_fast16_t status;

    status = MAP_I2C_getEnabledInterruptStatus(EUSCI_B0_MODULE);
    MAP_I2C_clearInterruptFlag(EUSCI_B0_MODULE, status);

    /* RXIFG for Slave Address 1*/
    if (status & EUSCI_B_I2C_RECEIVE_INTERRUPT0)
    {
        RXData_0[xferIndex_0++] = MAP_I2C_slaveGetData(EUSCI_B0_MODULE);

        /* Resetting the index if we are at the end */
        if (xferIndex_0 == NUM_OF_RX_BYTES)
            xferIndex_0 = 0;
    }

    /* RXIFG for Slave Address 1*/
    if (status & EUSCI_B_I2C_RECEIVE_INTERRUPT1)
    {
        RXData_1[xferIndex_1++] = MAP_I2C_slaveGetData(EUSCI_B0_MODULE);

        /* Resetting the index if we are at the end */
        if (xferIndex_1 == NUM_OF_RX_BYTES)
            xferIndex_1 = 0;
    }
}
コード例 #5
0
ファイル: DWire.cpp プロジェクト: stefanlinden/DWire
/**
 * Called to set the eUSCI module in 'master' mode
 */
void DWire::_initMaster(const eUSCI_I2C_MasterConfig * i2cConfig) {

	// Initialise the pins
	MAP_GPIO_setAsPeripheralModuleFunctionInputPin(modulePort, modulePins,
	GPIO_PRIMARY_MODULE_FUNCTION);

	// Initializing I2C Master to SMCLK at 400kbs with no autostop
	MAP_I2C_initMaster(module, i2cConfig);

	// Specify slave address
	MAP_I2C_setSlaveAddress(module, slaveAddress);

	// Set Master in transmit mode
	MAP_I2C_setMode(module, EUSCI_B_I2C_TRANSMIT_MODE);

	// Enable I2C Module to start operations
	MAP_I2C_enableModule(module);

	// Enable and clear the interrupt flag
	MAP_I2C_clearInterruptFlag(module,
			EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_NAK_INTERRUPT
					+ EUSCI_B_I2C_RECEIVE_INTERRUPT0);

	// Enable master interrupts
	MAP_I2C_enableInterrupt(module,
			EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_NAK_INTERRUPT
					+ EUSCI_B_I2C_RECEIVE_INTERRUPT0);

	// Register the interrupts on the correct module
	MAP_Interrupt_enableInterrupt(intModule);
	MAP_Interrupt_enableMaster();
}
コード例 #6
0
ファイル: DWire.cpp プロジェクト: DelfiSpace/DWire
void DWire::_initSlave( void ) 
{
    // Init the pins
    MAP_GPIO_setAsPeripheralModuleFunctionInputPin( modulePort, modulePins,
    GPIO_PRIMARY_MODULE_FUNCTION );

    // initialise driverlib
    MAP_I2C_initSlave( module, slaveAddress, EUSCI_B_I2C_OWN_ADDRESS_OFFSET0,
    EUSCI_B_I2C_OWN_ADDRESS_ENABLE );

    // Enable the module and enable interrupts
    MAP_I2C_enableModule( module );
    MAP_I2C_clearInterruptFlag( module,
            EUSCI_B_I2C_RECEIVE_INTERRUPT0 | EUSCI_B_I2C_STOP_INTERRUPT
                    | EUSCI_B_I2C_TRANSMIT_INTERRUPT0 | EUSCI_B_I2C_CLOCK_LOW_TIMEOUT_INTERRUPT );
    MAP_I2C_enableInterrupt( module,
            EUSCI_B_I2C_RECEIVE_INTERRUPT0 | EUSCI_B_I2C_STOP_INTERRUPT
                    | EUSCI_B_I2C_TRANSMIT_INTERRUPT0 | EUSCI_B_I2C_CLOCK_LOW_TIMEOUT_INTERRUPT );

    /* Enable the clock low timeout */
    EUSCI_B_CMSIS( module )->CTLW1 = (EUSCI_B_CMSIS( module )->CTLW1
            & ~EUSCI_B_CTLW1_CLTO_MASK) | 0xC0;

    MAP_Interrupt_enableInterrupt( intModule );
    MAP_Interrupt_enableMaster( );
}
コード例 #7
0
ファイル: DWire.cpp プロジェクト: DelfiSpace/DWire
/**
 * End the transmission and transmit the tx buffer's contents over the bus
 * it returns false if succesful
 */
bool DWire::endTransmission( bool sendStop ) 
{
    // return, if there is nothing to transmit
    if (!*pTxBufferIndex) 
    {
        return true;
    }

    // Wait until any ongoing (incoming) transmissions are finished
    timeout = 0xFFFF;
    while ( MAP_I2C_masterIsStopSent( module ) == EUSCI_B_I2C_SENDING_STOP
            && timeout)
        timeout--;

    if (!timeout) 
    {
        /* If we can't start the transmission, then reset everything */
        _resetBus( );
        return true;
    }

    this->sendStop = sendStop;
    gotNAK = false;

    // Clear the interrupt flags and enable
    MAP_I2C_clearInterruptFlag( module,
    EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_NAK_INTERRUPT );

    MAP_I2C_enableInterrupt( module,
    EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_NAK_INTERRUPT );

    // Set the master into transmit mode
    MAP_I2C_setMode( module, EUSCI_B_I2C_TRANSMIT_MODE );

    // Send the start condition and initial byte
    (*pTxBufferSize) = *pTxBufferIndex;

    // Send the first byte, triggering the TX interrupt
    MAP_I2C_masterSendMultiByteStartWithTimeout( module, pTxBuffer[0],
    TIMEOUTLIMIT );

    // make sure the transmitter buffer has been flushed
    timeout = TIMEOUTLIMIT;
    while (*pTxBufferIndex && timeout)
        timeout--;

    if (!timeout) 
    {
        _resetBus( );
        return true;
    }

    if (gotNAK) 
    {
        _I2CDelay( );
        MAP_I2C_masterReceiveMultiByteStop( module );
    }
    return gotNAK;
}
コード例 #8
0
ファイル: DWire.cpp プロジェクト: stefanlinden/DWire
/**
 * Request data from a SLAVE as a MASTER
 */
uint8_t DWire::requestFrom(uint_fast8_t slaveAddress, uint_fast8_t numBytes) {
	// No point of doing anything else if there we're not a MASTER
	if (busRole != BUS_ROLE_MASTER)
		return 0;

	if (*pTxBufferIndex > 0) {
		endTransmission(false);
	}

	while (!sendStop)
		;

	// Re-initialise the rx buffer
	*pRxBufferSize = numBytes;
	*pRxBufferIndex = 0;

	// Configure the correct slave
	MAP_I2C_setSlaveAddress(module, slaveAddress);
	this->slaveAddress = slaveAddress;

	MAP_I2C_disableInterrupt(module, EUSCI_B_I2C_TRANSMIT_INTERRUPT0);

	// Set the master into receive mode
	MAP_I2C_setMode(module, EUSCI_B_I2C_RECEIVE_MODE);

	// Send the START
	MAP_I2C_masterReceiveStart(module);

	// Send a stop early if we're only requesting one byte
	// to prevent timing issues
	if (numBytes == 1) {
		MAP_I2C_masterReceiveMultiByteStop(module);
	}

	// Initialize the flag showing the status of the request
	requestDone = false;
	gotNAK = false;

	// Wait until the request is done
	while (!requestDone)
		;

	MAP_I2C_setMode(module, EUSCI_B_I2C_TRANSMIT_MODE);

	MAP_I2C_enableInterrupt(module, EUSCI_B_I2C_TRANSMIT_INTERRUPT0);
	MAP_I2C_clearInterruptFlag(module, EUSCI_B_I2C_TRANSMIT_INTERRUPT0);

	// Reset the buffer
	(*pRxBufferIndex) = 0;
	(*pRxBufferSize) = 0;

	if (gotNAK) {
		return 0;
	} else {
		return rxReadLength;
	}
}
int main(void)
{
    /* Disabling the Watchdog */
    MAP_WDT_A_holdTimer();
    xferIndex_0 = 0;
    xferIndex_1 = 0;

    /* Select Port 1 for I2C - Set Pin 6, 7 to input Primary Module Function,
     *   (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
     */
    MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
        GPIO_PIN6 + GPIO_PIN7, GPIO_PRIMARY_MODULE_FUNCTION);

    /* eUSCI I2C Slave Configuration */

    /* Slave Address 1 (0x48) */
    MAP_I2C_initSlave(EUSCI_B0_MODULE, SLAVE_ADDRESS_1,
            EUSCI_B_I2C_OWN_ADDRESS_OFFSET0,
            EUSCI_B_I2C_OWN_ADDRESS_ENABLE);

    /* Slave Address 1 (0x49) */
    MAP_I2C_initSlave(EUSCI_B0_MODULE, SLAVE_ADDRESS_2,
            EUSCI_B_I2C_OWN_ADDRESS_OFFSET1,
            EUSCI_B_I2C_OWN_ADDRESS_ENABLE);

    /* Set in receive mode */
    MAP_I2C_setMode(EUSCI_B0_MODULE, EUSCI_B_I2C_RECEIVE_MODE);

    /* Enable the module and enable interrupts */
    MAP_I2C_enableModule(EUSCI_B0_MODULE);
    MAP_I2C_clearInterruptFlag(EUSCI_B0_MODULE,
    EUSCI_B_I2C_RECEIVE_INTERRUPT0 | EUSCI_B_I2C_RECEIVE_INTERRUPT1);
    MAP_I2C_enableInterrupt(EUSCI_B0_MODULE,
        EUSCI_B_I2C_RECEIVE_INTERRUPT0 | EUSCI_B_I2C_RECEIVE_INTERRUPT1);
    MAP_Interrupt_enableSleepOnIsrExit();
    MAP_Interrupt_enableInterrupt(INT_EUSCIB0);
    MAP_Interrupt_enableMaster();

    /* Sleeping while not in use */
    while (1)
    {
        MAP_PCM_gotoLPM0();
    }
}
/*******************************************************************************
 * The USCIAB0TX_ISR is structured such that it can be used to transmit any
 * number of bytes by pre-loading TXByteCtr with the byte count. Also, TXData
 * points to the next byte to transmit.
 *******************************************************************************/
void euscib0_isr(void)
{
    uint_fast16_t status;

    status = MAP_I2C_getEnabledInterruptStatus(EUSCI_B0_MODULE);
    MAP_I2C_clearInterruptFlag(EUSCI_B0_MODULE, status);

    if (status & EUSCI_B_I2C_NAK_INTERRUPT)
    {
        MAP_I2C_masterSendStart(EUSCI_B0_MODULE);
        return;
    }

    if (status & EUSCI_B_I2C_TRANSMIT_INTERRUPT0)
    {
        MAP_I2C_setSlaveAddress(EUSCI_B0_MODULE, SLAVE_ADDRESS_1);
    }

    if (status & EUSCI_B_I2C_TRANSMIT_INTERRUPT1)
    {
        MAP_I2C_setSlaveAddress(EUSCI_B0_MODULE, SLAVE_ADDRESS_2);
    }

    /* Check the byte counter */
    if (TXByteCtr)
    {
        /* Send the next data and decrement the byte counter */
        MAP_I2C_masterSendMultiByteNext(EUSCI_B0_MODULE, TXData++);
        TXByteCtr--;
    }
    else
    {
        MAP_I2C_masterSendMultiByteStop(EUSCI_B0_MODULE);
        sendStopCondition = true;
        sendToSlaveAddress2 = !sendToSlaveAddress2;
        MAP_Interrupt_disableSleepOnIsrExit();
    }

}
コード例 #11
0
ファイル: DWire.cpp プロジェクト: stefanlinden/DWire
void DWire::_initSlave(void) {
	// Init the pins
	MAP_GPIO_setAsPeripheralModuleFunctionInputPin(modulePort, modulePins,
	GPIO_PRIMARY_MODULE_FUNCTION);

	// initialise driverlib
	MAP_I2C_initSlave(module, slaveAddress, EUSCI_B_I2C_OWN_ADDRESS_OFFSET0,
	EUSCI_B_I2C_OWN_ADDRESS_ENABLE);

	// Enable the module
	MAP_I2C_enableModule(module);

	// Enable the module and enable interrupts
	MAP_I2C_enableModule(module);
	MAP_I2C_clearInterruptFlag(module,
			EUSCI_B_I2C_RECEIVE_INTERRUPT0 | EUSCI_B_I2C_STOP_INTERRUPT
					| EUSCI_B_I2C_TRANSMIT_INTERRUPT0);
	MAP_I2C_enableInterrupt(module,
			EUSCI_B_I2C_RECEIVE_INTERRUPT0 | EUSCI_B_I2C_STOP_INTERRUPT
					| EUSCI_B_I2C_TRANSMIT_INTERRUPT0);
	//MAP_Interrupt_enableSleepOnIsrExit();
	MAP_Interrupt_enableInterrupt(intModule);
	MAP_Interrupt_enableMaster();
}
コード例 #12
0
ファイル: DWire.cpp プロジェクト: stefanlinden/DWire
/**
 * The main (global) interrupt  handler
 */
void IRQHandler(IRQParam param) {

	uint_fast16_t status;

	status = MAP_I2C_getEnabledInterruptStatus(param.module);
	MAP_I2C_clearInterruptFlag(param.module, status);

	/* RXIFG */
	// Triggered when data has been received
	if (status & EUSCI_B_I2C_RECEIVE_INTERRUPT0) {

		// If the rxBufferSize > 0, then we're a master performing a request
		if (*param.rxBufferSize > 0) {
			param.rxBuffer[*param.rxBufferIndex] =
			MAP_I2C_masterReceiveMultiByteNext(param.module);
			(*param.rxBufferIndex)++;

			if (*param.rxBufferIndex == *param.rxBufferSize) {
				DWire * instance = getInstance(param.module);
				if (instance) {
					instance->_finishRequest();
				}
			}

			// Otherwise we're a slave receiving data
		} else {
			param.rxBuffer[*param.rxBufferIndex] = MAP_I2C_slaveGetData(
					param.module);
			(*param.rxBufferIndex)++;
		}
	}

	// As master: triggered when a byte has been transmitted
	// As slave: triggered on request */
	if (status & EUSCI_B_I2C_TRANSMIT_INTERRUPT0) {
		DWire * instance = getInstance(param.module);

		if (instance) {

			// If the module is setup as a master, then we're transmitting data
			if (instance->isMaster()) {
				// If we've transmitted the last byte from the buffer, then send a stop
				if (!(*param.txBufferIndex)) {
					if (instance->_isSendStop(false))
						MAP_I2C_masterSendMultiByteStop(param.module);
					instance->_isSendStop(true);

				} else {
					// If we still have data left in the buffer, then transmit that
					MAP_I2C_masterSendMultiByteNext(param.module,
							param.txBuffer[(*param.txBufferSize)
									- (*param.txBufferIndex)]);
					(*param.txBufferIndex)--;
				}
				// Otherwise we're a slave and a master is requesting data
			} else {
				instance->_handleRequestSlave();
			}
		}
	}

	// Handle a NAK
	if (status & EUSCI_B_I2C_NAK_INTERRUPT) {
		//MAP_I2C_masterSendStart(param.module);
		DWire * instance = getInstance(param.module);
		//*param.txBufferIndex = 0;
		if (instance)
			if (*param.rxBufferSize > 0) {
			}
		instance->_finishRequest(true);
	}

	/* STPIFG
	 * Called when a STOP is received
	 */
	if (status & EUSCI_B_I2C_STOP_INTERRUPT) {
		DWire * instance = getInstance(param.module);
		if (instance) {
			if (*param.txBufferIndex != 0 && !instance->isMaster()) {
				MAP_I2C_slavePutData(instance->module, 0);
				*param.rxBufferIndex = 0;
				*param.rxBufferSize = 0;
			} else if (*param.rxBufferIndex != 0) {
				instance->_handleReceive(param.rxBuffer);
			}
		}
	}
}
コード例 #13
0
ファイル: DWire.cpp プロジェクト: DelfiSpace/DWire
/**
 * Request data from a SLAVE as a MASTER
 */
uint8_t DWire::requestFrom( uint_fast8_t slaveAddress, uint_fast8_t numBytes ) 
{
    // No point of doing anything else if there we're not a MASTER
    if (busRole != BUS_ROLE_MASTER)
        return 0;

    // still something to send? Flush the TX buffer but do not send a STOP
    if (*pTxBufferIndex > 0) 
    {
    	// this is a repeated start: no point in trying to receive if we fail finishing the transmission
        if (endTransmission( false ))
        {
        	return 0;
        }
    } 
    else 
    {
        // Wait until any request is finished
        timeout = TIMEOUTLIMIT;
        while ( MAP_I2C_masterIsStopSent( module ) == EUSCI_B_I2C_SENDING_STOP
                && timeout)
            timeout--;
    }

    if (!timeout) 
    {
        /* If we get a timeout, then reset everything */
        _resetBus( );
        return 0;
    }

    // Re-initialise the rx buffer
    // and make sure we never request 1 byte only
    // this is an anomalous behaviour of the MSP432 related to the double
    // buffering of I2C. This is a workaround.
    if (numBytes == 1) 
    {
        *pRxBufferSize = 2;
    } else 
    {
        *pRxBufferSize = numBytes;
    }
    *pRxBufferIndex = 0;

    // Configure the correct slave
    MAP_I2C_setSlaveAddress( module, slaveAddress );
    this->slaveAddress = slaveAddress;

    MAP_I2C_clearInterruptFlag( module,
    EUSCI_B_I2C_RECEIVE_INTERRUPT0 | EUSCI_B_I2C_NAK_INTERRUPT );
    MAP_I2C_enableInterrupt( module,
    EUSCI_B_I2C_RECEIVE_INTERRUPT0 | EUSCI_B_I2C_NAK_INTERRUPT );

    // Set the master into receive mode
    MAP_I2C_setMode( module, EUSCI_B_I2C_RECEIVE_MODE );

    // Initialize the flag showing the status of the request
    requestDone = false;
    gotNAK = false;

    // Send the START
    MAP_I2C_masterReceiveStart( module );

    // Wait until the request is done
    timeout = TIMEOUTLIMIT;
    while (!requestDone && timeout)
        timeout--;

    if (!timeout)
    {
        /* If we get a timeout, then reset everything */
        _resetBus( );
        return 0;
    }

    if (gotNAK) 
    {
        _I2CDelay( );
        MAP_I2C_masterReceiveMultiByteStop( module );
        return 0;
    } 
    else 
    {
        if (numBytes == 1)
        {
            return --(*pRxBufferSize);
        }
        else
        {
            return *pRxBufferSize;
        }
    }
}
int main(void)
{
    volatile uint32_t ii;

    /* Disabling the Watchdog */
    MAP_WDT_A_holdTimer();

    /* Select Port 1 for I2C - Set Pin 6, 7 to input Primary Module Function,
     *   (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
     */
    MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
            GPIO_PIN6 + GPIO_PIN7, GPIO_PRIMARY_MODULE_FUNCTION);
    sendStopCondition = false;
    sendToSlaveAddress2 = false;

    /* Initializing I2C Master to SMCLK at 400kbs with no autostop */
    MAP_I2C_initMaster(EUSCI_B0_MODULE, &i2cConfig);

    /* Specify slave address. For start we will do our first slave address */
    MAP_I2C_setSlaveAddress(EUSCI_B0_MODULE, SLAVE_ADDRESS_1);

    /* Set Master in receive mode */
    MAP_I2C_setMode(EUSCI_B0_MODULE, EUSCI_B_I2C_TRANSMIT_MODE);

    /* Enable I2C Module to start operations */
    MAP_I2C_enableModule(EUSCI_B0_MODULE);

    /* Enable and clear the interrupt flag */
    MAP_I2C_clearInterruptFlag(EUSCI_B0_MODULE,
            EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_NAK_INTERRUPT);

    //Enable master Receive interrupt
    MAP_I2C_enableInterrupt(EUSCI_B0_MODULE,
            EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_NAK_INTERRUPT);
    MAP_Interrupt_enableInterrupt(INT_EUSCIB0);
    
    while (1)
    {

        /* Load Byte Counter */
        TXByteCtr = 4;

        /* Making sure the last transaction has been completely sent out */
        while (MAP_I2C_masterIsStopSent(EUSCI_B0_MODULE) == EUSCI_B_I2C_SENDING_STOP);

        /* Sending the initial start condition for appropriate
         *   slave addresses */
        if(sendToSlaveAddress2)
        {
            MAP_I2C_setSlaveAddress(EUSCI_B0_MODULE, SLAVE_ADDRESS_2);
        }
        else
        {
            MAP_I2C_setSlaveAddress(EUSCI_B0_MODULE, SLAVE_ADDRESS_1);
        }

        MAP_I2C_masterSendMultiByteStart(EUSCI_B0_MODULE, TXData++);

        while(sendStopCondition == false)
        {
            /* Go to sleep while data is being transmitted */
            MAP_Interrupt_enableSleepOnIsrExit();
            MAP_PCM_gotoLPM0InterruptSafe();
        }

        sendStopCondition = false;
    }
}