From 3ed13c322faf3fd990d4f98805adbd3ca5135483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Thu, 4 Jan 2018 01:34:18 +0100 Subject: [PATCH] better API and exposed to header --- units/i2c/unit_i2c.c | 49 ++++++++++++++++++++++------------ units/i2c/unit_i2c.h | 62 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 16 deletions(-) diff --git a/units/i2c/unit_i2c.c b/units/i2c/unit_i2c.c index 424285b..f68664c 100644 --- a/units/i2c/unit_i2c.c +++ b/units/i2c/unit_i2c.c @@ -295,7 +295,7 @@ static bool i2c_wait_until_flag(struct priv *priv, uint32_t flag, bool stop_stat 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; @@ -337,7 +337,7 @@ static bool UU_I2C_Write(Unit *unit, uint16_t addr, const uint8_t *bytes, uint32 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; @@ -387,6 +387,33 @@ static bool UU_I2C_Read(Unit *unit, uint16_t addr, uint8_t *dest, uint32_t bcoun 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, ®num, 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 */ 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 size = pp_u8(pp); // total number of bytes to read (allows use of auto-increment) - if (!UU_I2C_Write(unit, addr, ®num, 1)) { - com_respond_err(frame_id, "REG ADDR TX 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"); + if (!UU_I2C_ReadReg(unit, addr, regnum, (uint8_t *) unit_tmp512, size)) { + com_respond_err(frame_id, "READ REG FAIL"); return false; } - // and pass it to PC to handle com_respond_buf(frame_id, MSG_SUCCESS, (uint8_t *) unit_tmp512, size); break; @@ -445,13 +465,10 @@ static bool UI2C_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, Payl addr = pp_u16(pp); 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); - pb_buf(&pb, tail, size); - if (!UU_I2C_Write(unit, addr, (uint8_t *) unit_tmp512, pb_length(&pb))) { - com_respond_err(frame_id, "REG WRT FAIL"); + if (!UU_I2C_WriteReg(unit, addr, regnum, tail, size)) { + com_respond_err(frame_id, "WRITE REG FAIL"); return false; } break; diff --git a/units/i2c/unit_i2c.h b/units/i2c/unit_i2c.h index eb287a0..1301710 100644 --- a/units/i2c/unit_i2c.h +++ b/units/i2c/unit_i2c.h @@ -9,4 +9,66 @@ 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