/******************************************************************************
 * 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);
        }
    }
}
Exemplo n.º 2
0
/**
 * 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;
	}
}