mirror of https://github.com/ARMmbed/mbed-os.git
Add Repeated read/write functions.
parent
74cdab3021
commit
641ecd065a
|
@ -306,7 +306,7 @@ int I2C_Write(I2C_ConfigStruct* conf, uint8_t addr, uint8_t* data, uint32_t len)
|
|||
//Write addr
|
||||
if(I2C_WriteByte(conf, addr) != 0)
|
||||
{
|
||||
printf("Address is wrong!!\r\n");
|
||||
printf("Received NACK at address phase!!\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -322,6 +322,29 @@ int I2C_Write(I2C_ConfigStruct* conf, uint8_t addr, uint8_t* data, uint32_t len)
|
|||
return 0;//success
|
||||
}
|
||||
|
||||
int I2C_WriteRepeated(I2C_ConfigStruct* conf, uint8_t addr, uint8_t* data, uint32_t len)
|
||||
{
|
||||
int i;
|
||||
|
||||
I2C_Start(conf);
|
||||
|
||||
//Write addr
|
||||
if(I2C_WriteByte(conf, addr) != 0)
|
||||
{
|
||||
printf("Received NACK at address phase!!\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Write data
|
||||
for(i=0; i<len; i++)
|
||||
{
|
||||
if(I2C_WriteByte(conf, data[i]))
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;//success
|
||||
}
|
||||
|
||||
int I2C_Read(I2C_ConfigStruct* conf, uint8_t addr, uint8_t* data, uint32_t len)
|
||||
{
|
||||
int i;
|
||||
|
@ -331,7 +354,7 @@ int I2C_Read(I2C_ConfigStruct* conf, uint8_t addr, uint8_t* data, uint32_t len)
|
|||
//Write addr | read command
|
||||
if(I2C_WriteByte(conf, (addr | 1)) != 0)
|
||||
{
|
||||
printf("Address is wrong!!\r\n");
|
||||
printf("Received NACK at address phase!!\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -351,3 +374,30 @@ int I2C_Read(I2C_ConfigStruct* conf, uint8_t addr, uint8_t* data, uint32_t len)
|
|||
return 0;//success
|
||||
}
|
||||
|
||||
int I2C_ReadRepeated(I2C_ConfigStruct* conf, uint8_t addr, uint8_t* data, uint32_t len)
|
||||
{
|
||||
int i;
|
||||
|
||||
I2C_Start(conf);
|
||||
|
||||
//Write addr | read command
|
||||
if(I2C_WriteByte(conf, (addr | 1)) != 0)
|
||||
{
|
||||
printf("Received NACK at address phase!!\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Read data
|
||||
for(i=0; i<len; i++)
|
||||
{
|
||||
data[i] = I2C_ReadByte(conf);
|
||||
|
||||
if( i == (len - 1) )
|
||||
I2C_SendNACK(conf);
|
||||
else
|
||||
I2C_SendACK(conf);
|
||||
}
|
||||
|
||||
return 0;//success
|
||||
}
|
||||
|
||||
|
|
|
@ -72,7 +72,9 @@ void I2C_Start(I2C_ConfigStruct* conf);
|
|||
void I2C_Stop(I2C_ConfigStruct* conf);
|
||||
|
||||
int I2C_Write(I2C_ConfigStruct* conf, uint8_t addr, uint8_t* data, uint32_t len);
|
||||
int I2C_WriteRepeated(I2C_ConfigStruct* conf, uint8_t addr, uint8_t* data, uint32_t len);
|
||||
int I2C_Read(I2C_ConfigStruct* conf, uint8_t addr, uint8_t* data, uint32_t len);
|
||||
int I2C_ReadRepeated(I2C_ConfigStruct* conf, uint8_t addr, uint8_t* data, uint32_t len);
|
||||
|
||||
|
||||
#endif //__W7500X_I2C_H
|
||||
|
|
|
@ -112,22 +112,8 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
|
|||
}
|
||||
else
|
||||
{
|
||||
I2C_Start(&conf);
|
||||
|
||||
//Write addr | read command
|
||||
if(I2C_WriteByte(&conf, (address | 1)))//Nack is 1
|
||||
if(I2C_ReadRepeated(&conf, address, data, length) != 0)
|
||||
return -1;
|
||||
|
||||
//Read data
|
||||
for(i=0; i<length; i++)
|
||||
{
|
||||
data[i] = I2C_ReadByte(&conf);
|
||||
|
||||
if( i == (length - 1) )
|
||||
I2C_SendNACK(&conf);
|
||||
else
|
||||
I2C_SendACK(&conf);
|
||||
}
|
||||
}
|
||||
|
||||
return length;
|
||||
|
@ -150,18 +136,8 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
|
|||
}
|
||||
else
|
||||
{
|
||||
I2C_Start(&conf);
|
||||
|
||||
//Write addr | read command
|
||||
if(I2C_WriteByte(&conf, address))//Nack is 1
|
||||
return -1;
|
||||
|
||||
//Write data
|
||||
for(i=0; i<length; i++)
|
||||
{
|
||||
if(I2C_WriteByte(&conf, data[i])) //if received nack
|
||||
return i;
|
||||
}
|
||||
if(I2C_WriteRepeated(&conf, address, data, length) != 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return length;
|
||||
|
|
Loading…
Reference in New Issue