To allow compatibility to a program, I need to process each byte I receive from I2C EEPROM before save them. I can't save them and process later.
I've looked into EEPROM polling example and found this receiver function:
uint16_t I2C_ControllerReceiver(struct I2CHandle *I2C_Params)
{
uint16_t status;
uint16_t attemptCount;
uint32_t base = I2C_Params->base;
I2C_disableFIFO(base);
I2C_enableFIFO(base);
status = I2C_TransmittargetAddress_ControlBytes(I2C_Params);
if(status)
{
return status;
}
uint16_t numofSixteenByte = (I2C_Params->NumOfDataBytes) / I2C_FIFO_LEVEL;
uint16_t remainingBytes = (I2C_Params->NumOfDataBytes) % I2C_FIFO_LEVEL;
I2C_setConfig(base, (I2C_CONTROLLER_RECEIVE_MODE|I2C_REPEAT_MODE));
I2C_sendStartCondition(base);
uint16_t i,count = 0,buff_pos=0;
while(count < numofSixteenByte)
{
status = handleNACK(base);
if(status)
{
return status;
}
count++;
attemptCount = 1;
while(!(I2C_getRxFIFOStatus(base) == I2C_FIFO_RXFULL) && attemptCount <= 9 * (I2C_FIFO_RXFULL + 2U))
{
DEVICE_DELAY_US(I2C_Params->Delay_us);
attemptCount++;
}
for(i=0; i<I2C_FIFO_LEVEL; i++)
{
I2C_Params->pRX_MsgBuffer[buff_pos++] = I2C_getData(base);
}
}
attemptCount = 1;
while(!(I2C_getRxFIFOStatus(base) == remainingBytes) && attemptCount <= 9 * (remainingBytes + 2U))
{
DEVICE_DELAY_US(I2C_Params->Delay_us);
attemptCount++;
}
I2C_sendStopCondition(base);
for(i=0; i<remainingBytes; i++)
{
I2C_Params->pRX_MsgBuffer[buff_pos++] = I2C_getData(base);
}
status = handleNACK(base);
if(status)
{
return status;
}
I2C_disableFIFO(base);
attemptCount = 1;
while(I2C_getStopConditionStatus(base) && attemptCount <= 3U);
{
DEVICE_DELAY_US(I2C_Params->Delay_us);
attemptCount++;
}
return SUCCESS;
}
Works very well, the only problem is that it continue to ask bytes until stopCondition is reached and, almost always, I get 1 more byte than asked number of bytes from this function.
What I'd like to ask if I can set how much byte I want to receive and be sure that this will be respected.
Something like:
I2C_setConfig(base, (I2C_CONTROLLER_RECEIVE_MODE));
I2C_setDataCount(base, nRxBytes);
But is not working.
Another question I'd like to ask if I can decide WHEN to receive next byte. To explain I need to get 1 byte, process it, then get another byte etc.
I don't need any interrupt or something particular. Just polling each rx byte.