s32k144 about i2c read/write api

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

s32k144 about i2c read/write api

2,385 Views
alice_th
Contributor III

does the i2c read/write function is correct below, because when read is always return 2, seems when write i2c is always busy, can you help to check whether test_tmf882x_i2c_read is correct ?


i2c_test11,TMF882X_STAT=0
tmf882x_i2c_write ret=0
tmf882x_i2c_read,fisrt ret1=0,second ret2=2,recv=0
i2c_test11,TMF882X_STAT=0
tmf882x_i2c_write ret=0
tmf882x_i2c_read,fisrt ret1=0,second ret2=2,recv=0
i2c_test11,TMF882X_STAT=0
tmf882x_i2c_write ret=0
tmf882x_i2c_read,fisrt ret1=0,second ret2=2,recv=0
i2c_test11,TMF882X_STAT=0
tmf882x_i2c_write ret=0
tmf882x_i2c_read,fisrt ret1=0,second ret2=2,recv=0

 

void delay_test11(int count)
{
while(count--)
NOP();
}
int test_tmf882x_i2c_read(uint32_t slave_addr, uint8_t reg, uint8_t *buf, int len)
{
//return read_i2c_block(slave_addr, reg, buf, len);
int ret1,ret2;
ret1 = LPI2C_DRV_MasterSendDataBlocking(INST_LPI2C1, reg, 1, false, I2C_TIME_OUT);

ret2 = LPI2C_DRV_MasterReceiveDataBlocking(INST_LPI2C1, buf, len, true,I2C_TIME_OUT);
AMS_TOF_LOG("tmf882x_i2c_read,fisrt ret1=%d,second ret2=%d,recv=%d\n",ret1,ret2,*buf);
return ret2;
}

int test_tmf882x_i2c_write(uint32_t slave_addr, uint8_t reg, uint8_t *buf, int len)
{

int i,ret;
uint8_t send[len+1];
send[0] = reg;
if(len == 1)
send[len] = *buf;
else
{
for(i=0;i<len;i++)
send[i+1] = buf[i];
}
ret = LPI2C_DRV_MasterSendDataBlocking(INST_LPI2C1, send, len+1, true,I2C_TIME_OUT);
AMS_TOF_LOG("tmf882x_i2c_write ret=%d\n",ret);
return 0;
}
void i2c_test11()
{
uint8_t send[2];
uint8_t recv[2];
recv[0] = 0x80;
recv[1] = 0x90;
uint8_t regval = 0;
LPI2C_DRV_MasterSetSlaveAddr(INST_LPI2C1,0x39,false);
while(1)
{
regval = 0x03;
test_tmf882x_i2c_write(0x39,0x80,&regval, 1);
regval = 0x00;
test_tmf882x_i2c_read(0x39,0x80, &regval, 1);
printf("i2c_test11,TMF882X_STAT=%x\n",regval);

}
}

 

int main (void)
{
//initialize System
lpi2c_master_state_t lpi2c1MasterState;
init_sys ();
//WDOG_disable();
// sensor_interface_init();
LPSPI_DRV_MasterInit(LPSPICOM1,&lpspiCom1State,&lpspiCom1_MasterConfig0);
LPUART_DRV_Init(INST_LPUART1, &lpuart1_State, &lpuart1_InitConfig0);
LPI2C_DRV_MasterInit(INST_LPI2C1, &lpi2c1_MasterConfig0,&lpi2c1MasterState);
enable_fault_handlers(); // If not set, all fault are handled by HardFault_Handler
set_fault_handlers_priority(); // priority: MemManage (1), BusFault (1), UsageFault(1)
LPI2C_DRV_MasterSetSlaveAddr(INST_LPI2C1,I2C_SLAVEADDR,false);
//while(1)
{

i2c_test11();

}

return exit_code;
}

0 Kudos
Reply
7 Replies

2,362 Views
Senlent
NXP TechSupport
NXP TechSupport

Hi@alice_thanks

I didn't see any problem. I simply looked at the data sheet of this chip. I suggest you try to read the value of the 0x0E register correctly to ensure that your circuit is ok.

0 Kudos
Reply

2,356 Views
alice_th
Contributor III

hello @Senlent 

i find this api can't used under freertos task,

when used without freertos it can get the slave i2c data.

even i use LPI2C_DRV_MasterSendData still can't used with freertos

it will stock with while(LPI2C_DRV_MasterGetTransferStatus(INST_LPI2C1,&bytesRemain) != STATUS_SUCCESS);

can you help to check it how to used in freertos ?

int test_tmf882x_i2c_read(uint32_t slave_addr, uint8_t reg, uint8_t *buf, int len)
{
//return read_i2c_block(slave_addr, reg, buf, len);
    int ret1,ret2;
    uint32_t bytesRemain = 0;
 
    ret1 = LPI2C_DRV_MasterSendData(INST_LPI2C1, &reg, 1, false);
   // while(LPI2C_DRV_MasterGetTransferStatus(INST_LPI2C1,&bytesRemain) != STATUS_SUCCESS && bytesRemain != 0);
    while(LPI2C_DRV_MasterGetTransferStatus(INST_LPI2C1,&bytesRemain) != STATUS_SUCCESS);
    ret2 = LPI2C_DRV_MasterReceiveData(INST_LPI2C1, buf, 1, true);
  //  while(LPI2C_DRV_MasterGetTransferStatus(INST_LPI2C1,&bytesRemain) != STATUS_SUCCESS && bytesRemain != 0);
    while(LPI2C_DRV_MasterGetTransferStatus(INST_LPI2C1,&bytesRemain) != STATUS_SUCCESS);
    AMS_TOF_LOG("test_i2c000 tmf882x_i2c_read,fisrt ret1=%d,second ret2=%d,recv=%d\n",ret1,ret2,*buf);
    return ret2;
}

 

0 Kudos
Reply

2,345 Views
Senlent
NXP TechSupport
NXP TechSupport
0 Kudos
Reply

2,341 Views
alice_th
Contributor III

i try it , but it doesn't work, can share the worked project, i can test it, thanks!

0 Kudos
Reply

2,325 Views
Senlent
NXP TechSupport
NXP TechSupport

Hi@alice_thanks

Sorry, I didn't find any I2C demos based on FreeRTOS, I'm not familiar with FreeRTOS issues.
Please create a new topic for your question, enter keywords such as FreeRTOS + I2C.

0 Kudos
Reply

2,340 Views
alice_th
Contributor III

i use lpi2c

int main (void)
{
//initialize System
lpi2c_master_state_t lpi2c1MasterState;
init_sys ();
//WDOG_disable();
// sensor_interface_init();
/* Initialize Interrupt priority for LP I2C driver as bus master */


LPSPI_DRV_MasterInit(LPSPICOM1,&lpspiCom1State,&lpspiCom1_MasterConfig0);
LPUART_DRV_Init(INST_LPUART1, &lpuart1_State, &lpuart1_InitConfig0);
LPI2C_DRV_MasterInit(INST_LPI2C1, &lpi2c1_MasterConfig0,&lpi2c1MasterState);
LPI2C_DRV_MasterSetSlaveAddr(INST_LPI2C1,0x39,false);
// enable_fault_handlers(); // If not set, all fault are handled by HardFault_Handler
// set_fault_handlers_priority(); // priority: MemManage (1), BusFault (1), UsageFault(1)
INT_SYS_SetPriority( LPI2C0_Master_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
//LPI2C_DRV_MasterSetSlaveAddr(INST_LPI2C1,I2C_SLAVEADDR,false);
//while(1)
{

//tof882x_init();
// i2c_test11();
//flash_read_write_test();
}
rtos_start();

Tags (1)
0 Kudos
Reply

2,319 Views
alice_th
Contributor III
0 Kudos
Reply