/** * 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(); }
void DWire::_resetBus( void ) { /* Reset buffers */ *pTxBufferIndex = 0; *pTxBufferSize = 0; *pRxBufferIndex = 0; *pRxBufferSize = 0; /* Reset the module */ MAP_I2C_disableModule( module ); /* Perform bus clear according to I2C-bus Specification and User Manual * (UM10204) section 3.1.16 */ if (this->isMaster( )) { MAP_GPIO_setOutputLowOnPin( modulePort, moduleSCL ); for (uint_fast8_t i = 0; i < 9; i++) { MAP_GPIO_setAsOutputPin( modulePort, moduleSCL ); this->_I2CDelay( ); MAP_GPIO_setAsInputPin( modulePort, moduleSCL ); this->_I2CDelay( ); } MAP_GPIO_setAsPeripheralModuleFunctionInputPin( modulePort, moduleSCL, GPIO_PRIMARY_MODULE_FUNCTION ); } /* Re-enable the module */ MAP_I2C_enableModule( module ); }
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( ); }
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(); }
int main(void) { uint32_t ii; /* Disabling the Watchdog */ MAP_WDT_A_holdTimer(); xferIndex = 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); /* Configuring GPIO P1.0 to be an input. As the GPIO is pulled up by the * master, when the port is set to an input the pull-up defaults and the * pin is high. When the interrupt is asserted, we set the GPIO to an * output low and cause the interrupt to occur. */ MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN1); MAP_GPIO_setAsInputPin(GPIO_PORT_P1, GPIO_PIN0); /* eUSCI I2C Slave Configuration */ MAP_I2C_initSlave(EUSCI_B0_MODULE, SLAVE_ADDRESS_1, EUSCI_B_I2C_OWN_ADDRESS_OFFSET0, EUSCI_B_I2C_OWN_ADDRESS_ENABLE); /* Enable the module and enable interrupts */ MAP_I2C_enableModule(EUSCI_B0_MODULE); MAP_Interrupt_enableInterrupt(INT_EUSCIB0); /* Sleeping while not in use */ while (1) { /* Delaying between transactions */ for(ii=0;ii<4000;ii++); /* Initiating the read from the master by asserting our GPIO */ MAP_I2C_enableInterrupt(EUSCI_B0_MODULE, EUSCI_B_I2C_TRANSMIT_INTERRUPT0); MAP_GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0); MAP_Interrupt_enableSleepOnIsrExit(); MAP_PCM_gotoLPM0InterruptSafe(); } }
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(); } }
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; } }