better API and exposed to header

sipo
Ondřej Hruška 7 years ago
parent 1a225d6f06
commit 3ed13c322f
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 49
      units/i2c/unit_i2c.c
  2. 62
      units/i2c/unit_i2c.h

@ -295,7 +295,7 @@ static bool i2c_wait_until_flag(struct priv *priv, uint32_t flag, bool stop_stat
return true; return true;
} }
static bool UU_I2C_Write(Unit *unit, uint16_t addr, const uint8_t *bytes, uint32_t bcount) bool UU_I2C_Write(Unit *unit, uint16_t addr, const uint8_t *bytes, uint32_t bcount)
{ {
struct priv *priv = unit->data; struct priv *priv = unit->data;
@ -337,7 +337,7 @@ static bool UU_I2C_Write(Unit *unit, uint16_t addr, const uint8_t *bytes, uint32
return true; return true;
} }
static bool UU_I2C_Read(Unit *unit, uint16_t addr, uint8_t *dest, uint32_t bcount) bool UU_I2C_Read(Unit *unit, uint16_t addr, uint8_t *dest, uint32_t bcount)
{ {
struct priv *priv = unit->data; struct priv *priv = unit->data;
@ -387,6 +387,33 @@ static bool UU_I2C_Read(Unit *unit, uint16_t addr, uint8_t *dest, uint32_t bcoun
return true; return true;
} }
bool UU_I2C_ReadReg(Unit *unit, uint16_t addr, uint8_t regnum, uint8_t *dest, uint32_t width)
{
if (!UU_I2C_Write(unit, addr, &regnum, 1)) {
return false;
}
// we read the register as if it was a unsigned integer
if (!UU_I2C_Read(unit, addr, (uint8_t *) unit_tmp512, width)) {
return false;
}
return true;
}
bool UU_I2C_WriteReg(Unit *unit, uint16_t addr, uint8_t regnum, const uint8_t *bytes, uint32_t width)
{
PayloadBuilder pb = pb_start((uint8_t*)unit_tmp512, 512, NULL);
pb_u8(&pb, regnum);
pb_buf(&pb, bytes, width);
if (!UU_I2C_Write(unit, addr, (uint8_t *) unit_tmp512, pb_length(&pb))) {
return false;
}
return true;
}
/** Handle a request message */ /** Handle a request message */
static bool UI2C_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp) static bool UI2C_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp)
{ {
@ -427,17 +454,10 @@ static bool UI2C_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, Payl
regnum = pp_u8(pp); // register number regnum = pp_u8(pp); // register number
size = pp_u8(pp); // total number of bytes to read (allows use of auto-increment) size = pp_u8(pp); // total number of bytes to read (allows use of auto-increment)
if (!UU_I2C_Write(unit, addr, &regnum, 1)) { if (!UU_I2C_ReadReg(unit, addr, regnum, (uint8_t *) unit_tmp512, size)) {
com_respond_err(frame_id, "REG ADDR TX FAIL"); com_respond_err(frame_id, "READ REG FAIL");
return false;
}
// we read the register as if it was a unsigned integer
if (!UU_I2C_Read(unit, addr, (uint8_t *) unit_tmp512, size)) {
com_respond_err(frame_id, "REG VAL RX FAIL");
return false; return false;
} }
// and pass it to PC to handle
com_respond_buf(frame_id, MSG_SUCCESS, (uint8_t *) unit_tmp512, size); com_respond_buf(frame_id, MSG_SUCCESS, (uint8_t *) unit_tmp512, size);
break; break;
@ -445,13 +465,10 @@ static bool UI2C_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, Payl
addr = pp_u16(pp); addr = pp_u16(pp);
regnum = pp_u8(pp); // register number regnum = pp_u8(pp); // register number
PayloadBuilder pb = pb_start((uint8_t*)unit_tmp512, 512, NULL);
pb_u8(&pb, regnum);
const uint8_t *tail = pp_tail(pp, &size); const uint8_t *tail = pp_tail(pp, &size);
pb_buf(&pb, tail, size);
if (!UU_I2C_Write(unit, addr, (uint8_t *) unit_tmp512, pb_length(&pb))) { if (!UU_I2C_WriteReg(unit, addr, regnum, tail, size)) {
com_respond_err(frame_id, "REG WRT FAIL"); com_respond_err(frame_id, "WRITE REG FAIL");
return false; return false;
} }
break; break;

@ -9,4 +9,66 @@
extern const UnitDriver UNIT_I2C; extern const UnitDriver UNIT_I2C;
// Unit-to-Unit API
/**
* Raw write to I2C
*
* @param unit - I2C unit
* @param addr - device address (set highest bit if address is 10-bit)
* @param bytes - bytes to write
* @param bcount - byte count
* @return success
*/
bool UU_I2C_Write(Unit *unit, uint16_t addr, const uint8_t *bytes, uint32_t bcount);
/**
* Raw read from I2C
*
* @param unit - I2C unit
* @param addr - device address (set highest bit if address is 10-bit)
* @param dest - buffer for read bytes
* @param bcount - byte count
* @return success
*/
bool UU_I2C_Read(Unit *unit, uint16_t addr, uint8_t *dest, uint32_t bcount);
/**
* Read one or more registers from a I2C register-based device with auto-increment.
*
* @param unit - I2C unit
* @param addr - device address (set highest bit if address is 10-bit)
* @param regnum - first register number
* @param dest - destination buffer
* @param width - register width (or multiple consecutive registers total size)
* @return success
*/
bool UU_I2C_ReadReg(Unit *unit, uint16_t addr, uint8_t regnum, uint8_t *dest, uint32_t width);
/**
* Write a register value
*
* @param unit - I2C unit
* @param addr - device address (set highest bit if address is 10-bit)
* @param regnum - register number
* @param bytes - register bytes (use &byte) if just one
* @param width - register width (number of bytes)
* @return success
*/
bool UU_I2C_WriteReg(Unit *unit, uint16_t addr, uint8_t regnum, const uint8_t *bytes, uint32_t width);
/**
* Write a 8-bit register value
*
* @param unit - I2C unit
* @param addr - device address (set highest bit if address is 10-bit)
* @param regnum - register number
* @param value - byte to write to the register
* @return success
*/
static inline bool UU_I2C_WriteReg8(Unit *unit, uint16_t addr, uint8_t regnum, uint8_t value)
{
return UU_I2C_WriteReg(unit, addr, regnum, &value, 1);
}
#endif //GEX_F072_UNIT_I2C_H #endif //GEX_F072_UNIT_I2C_H

Loading…
Cancel
Save