/*******************************************************************************
 * 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();
    }

}
示例#2
0
/**
 * 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);
			}
		}
	}
}