Part Number:MSP432P401R
Hello,
I am trying to receive same data from slave device(24lc04 FRAM) which i had transmitted (transmission successful, getting data burst on SDA).
Below is my code, Pls suggest whether I am doing right for receiving data from slave?? And how to verify?
/* Slave Address for I2C Slave */
#define SLAVE_ADDRESS 0x50
#define NUM_OF_RX_BYTES 10
/* Statics */
static uint8_t TXData[10] = {0x06, 0x3A, 0x1E, 0x39, 0x1C, 0x79, 0x5D, 0x6C, 0x0C, 0x01};
static uint8_t TXByteCtr = 1;
static uint8_t sent = 0;
static uint8_t RXData[NUM_OF_RX_BYTES];
static volatile uint32_t xferIndex;
static volatile bool stopSent;
/* I2C Master Configuration Parameter */
const eUSCI_I2C_MasterConfig i2cConfig =
{
EUSCI_B_I2C_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
3000000, // SMCLK = 3MHz
EUSCI_B_I2C_SET_DATA_RATE_100KBPS, // Desired I2C Clock of 100khz
0, // No byte counter threshold
EUSCI_B_I2C_NO_AUTO_STOP // No Autostop
};
int main(void)
{
volatile uint32_t ii;
/* Disabling the Watchdog */
WDT_A_holdTimer();
P2->DIR = BIT7;
Port_init(); // Initialise ports
init_lcd();
sent = 0;
/* Select Port 1 for I2C - Set Pin 6, 7 to input Primary Module Function,
* (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
*/
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
GPIO_PIN6 + GPIO_PIN7, GPIO_PRIMARY_MODULE_FUNCTION);
stopSent = false;
// memset(RXData, 0x00, NUM_OF_REC_BYTES);
/* Initializing I2C Master to SMCLK at 100khz with no autostop */
I2C_initMaster(EUSCI_B0_BASE, &i2cConfig);
/* Specify slave address */
I2C_setSlaveAddress(EUSCI_B0_BASE, SLAVE_ADDRESS);
/* Set Master in transmit mode */
I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_MODE);
/* Enable I2C Module to start operations */
I2C_enableModule(EUSCI_B0_BASE);
/* Enable and clear the interrupt flag */
I2C_clearInterruptFlag(EUSCI_B0_BASE,
EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_NAK_INTERRUPT);
//Enable master Receive interrupt
I2C_enableInterrupt(EUSCI_B0_BASE,EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_NAK_INTERRUPT);
Interrupt_enableSleepOnIsrExit();
Interrupt_enableInterrupt(INT_EUSCIB0);
I2C_masterSendStart(EUSCI_B0_BASE);
while (1)
{
while (I2C_masterIsStopSent(EUSCI_B0_BASE) == EUSCI_B_I2C_SENDING_STOP);
// Sending the initial start condition
I2C_masterSendMultiByteStart(EUSCI_B0_BASE, TXData[0]);
Interrupt_enableSleepOnIsrExit();
PCM_gotoLPM0InterruptSafe();
/* intToStr(data, res, 1);
display2(res);*/
}
}
/*******************************************************************************
* eUSCIB0 ISR. The repeated start and transmit/recei e operations happen
* within this ISR.
*******************************************************************************/
void EUSCIB0_IRQHandler(void)
{
uint_fast16_t status;
status = I2C_getEnabledInterruptStatus(EUSCI_B0_BASE);
I2C_clearInterruptFlag(EUSCI_B0_BASE, status);
if (status & EUSCI_B_I2C_NAK_INTERRUPT)
{
I2C_masterSendMultiByteStart(EUSCI_B0_BASE, TXData[0]);
}
if (status & EUSCI_B_I2C_TRANSMIT_INTERRUPT0)
{
// Check the byte counter
if (!sent)
{
// Send the next data and decrement the byte counter
I2C_masterSendMultiByteNext(EUSCI_B0_BASE, TXData[TXByteCtr]);
sent = 1;
}
else
{
I2C_masterSendMultiByteStop(EUSCI_B0_BASE);
Interrupt_disableSleepOnIsrExit();
TXByteCtr++;
if(TXByteCtr > 10)
TXByteCtr++;
// Set in receive mode
I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE);
I2C_clearInterruptFlag(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_INTERRUPT0);
I2C_enableInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_INTERRUPT0);
// I2C_masterReceiveStart(EUSCI_B0_BASE);
// I2C_disableInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_INTERRUPT0);
sent = 0;
}
}
if (status & EUSCI_B_I2C_RECEIVE_INTERRUPT0)
{
P2->OUT ^= BIT7;
RXData[xferIndex++] = I2C_masterReceiveMultiByteNext(EUSCI_B0_BASE);
data = RXData[xferIndex];
// Resetting the index if we are at the end
if (xferIndex == NUM_OF_RX_BYTES)
xferIndex = 0;
I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_MODE);
// Enable I2C Module to start operations
I2C_enableModule(EUSCI_B0_BASE);
// Enable and clear the interrupt flag
I2C_clearInterruptFlag(EUSCI_B0_BASE,
EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_NAK_INTERRUPT);
//Enable master Receive interrupt
I2C_enableInterrupt(EUSCI_B0_BASE,EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_NAK_INTERRUPT);
// I2C_masterSendStart(EUSCI_B0_BASE);
// I2C_disableInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_INTERRUPT0);
}
}