Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Pham_Hung
Level 1
Level 1
5 questions asked 5 likes given 25 sign-ins

please help me: explain me how to check the length and start and end of the packet why like for example:

/* Check the length of the packet */
if (PACKET_SIZE == I2CS_I2CSlaveGetWriteBufSize())
{

/* Check the start and end of the packet */
if ((PACKET_EOP == i2cWriteBuffer[PACKET_EOP_POS]) &&
(PACKET_SOP == i2cWriteBuffer[PACKET_SOP_POS]))
{
/* Update the PWM compare values to control the RGB LED */
PWM_Red_WriteCompare(i2cWriteBuffer[PACKET_RED_POS]);
PWM_Green_WriteCompare(i2cWriteBuffer[PACKET_GREEN_POS]);
PWM_Blue_WriteCompare(i2cWriteBuffer[PACKET_BLUE_POS]);
i2cReadBuffer[PACKET_STS_POS] = STS_CMD_DONE;
}
else /* Bad packet format, set error */
{
errorDetected = 1;
}
}

My code master I use:

void DS1307_SetRegByte(uint8_t regAddr, uint8_t val) {
I2C_Master_I2CMasterSendStart(DS1307_ADDRESS,I2C_Master_I2C_WRITE_XFER_MODE,DS1307_TIMEOUT);
//I2C_Master_I2CMasterWriteByte(regAddr,DS1307_TIMEOUT);
I2C_Master_I2CMasterWriteByte(val,DS1307_TIMEOUT);
I2C_Master_I2CMasterSendStop(DS1307_TIMEOUT);
}

0 Likes
2 Replies
ncbs
Moderator
Moderator
Moderator
500 replies posted 50 likes received 250 sign-ins

Hi @Pham_Hung ,

Are you creating a custom command buffer and including the SOP and EOP there? Or do you want to identify the Start and End conditions of I2C communication?

The length of the I2C packet will be equal to the buffer's size being transported from master to slave or vice versa.

For custom command packets to be sent over I2C, please refer to CE220818 - PSoC 6 MCU I2C Master code example [link to code example zip file, link to the CE PDF].

Regards,
Nikhil

0 Likes
Pham_Hung
Level 1
Level 1
5 questions asked 5 likes given 25 sign-ins

thank for your reply, after several tries again with i2c master ,I understand that ckeck start and of the packet for packet master send and packet slave receive.Then I posted a problem with the slave i2c responding to the wrong register address.

Mater I2c send:

uint16_t Master_GetRegBuffer( uint8_t add_slave,uint8_t add_register) {
uint8_t start,data1,data2,stop;
I2C_Master_I2CMasterSendStart(add_slave,I2C_Master_I2C_WRITE_XFER_MODE,DS1307_TIMEOUT);//slave address
I2C_Master_I2CMasterWriteByte(add_register,DS1307_TIMEOUT); //registor address
I2C_Master_I2CMasterSendRestart(add_slave,I2C_Master_I2C_READ_XFER_MODE,DS1307_TIMEOUT);//repeated start,rw=1
I2C_Master_I2CMasterReadByte(I2C_Master_I2C_ACK_DATA,&start,DS1307_TIMEOUT); //start
I2C_Master_I2CMasterReadByte(I2C_Master_I2C_ACK_DATA,&data1,DS1307_TIMEOUT); //data
I2C_Master_I2CMasterReadByte(I2C_Master_I2C_ACK_DATA,&data2,DS1307_TIMEOUT); //data
I2C_Master_I2CMasterReadByte(I2C_Master_I2C_NAK_DATA,&stop,DS1307_TIMEOUT); //end
I2C_Master_I2CMasterSendStop(DS1307_TIMEOUT); //stop
if(start==0x01&&stop==0x17u)
{
return ((data1)|(data2<<8));
}
else
{
return 0;
}

}

 

Master send 4 registers consecutively:add_register but slave gets the value of the previous register and not of the current register,following is my  slave code

void I2C_Slave_I2C_ISR_ExitCallback()
{
if ((0u != (I2C_Slave_I2C_SSTAT_WR_BUSY & I2C_Slave_I2CSlaveStatus()))&&(0u == (I2C_Slave_I2C_SSTAT_WR_CMPLT & I2C_Slave_I2CSlaveStatus())))
{
register_address=i2cWriteBuffer[PACKET_RAS_POS];
}
/* Check if write is complete */
if (0u != (I2C_Slave_I2C_SSTAT_WR_CMPLT & I2C_Slave_I2CSlaveStatus()))
{
// register_address=i2cWriteBuffer[PACKET_RAS_POS];
/* Check the length of the packet */
if (PACKET_SIZE == I2C_Slave_I2CSlaveGetWriteBufSize())
{
register_address=0;
/* Check the start and end of the packet */
if ((PACKET_EOP == i2cWriteBuffer[PACKET_EOP_POS]) &&
(PACKET_SOP == i2cWriteBuffer[PACKET_SOP_POS]))
{
//a= i2cWriteBuffer[0]+i2cWriteBuffer[1]+i2cWriteBuffer[2];
}
else /* Bad packet format, set error */
{
errorDetected = 1;
//a=1;
}
}
/* Clear the slave write buffer for next write */
I2C_Slave_I2CSlaveClearWriteBuf();
(void)I2C_Slave_I2CSlaveClearWriteStatus();
}
/* Read Complete: Expose buffer to master */
if(0u != (I2C_Slave_I2C_SSTAT_RD_CMPLT & I2C_Slave_I2CSlaveStatus()))
{
slave_i2c_repont(register_address);
/* Clear the slave read buffer for next read */
I2C_Slave_I2CSlaveClearReadBuf();
(void) I2C_Slave_I2CSlaveClearReadStatus();
}
}

can you explain to me why the slave responds with the address of the previous register and not the current register?

 

0 Likes