hw/drivers/io_expander/tca9535/src/tca9535.c
int
tca95xx_write16(struct tca9535_io_expander_dev_cfg *cfg, uint8_t reg, uint16_t value)
{
    int rc;
    os_error_t err = 0;
#ifdef TCA95xx_VERIFY_WRITES
    uint16_t readback;
#endif

    uint8_t out[3] = {reg, value&0xff, (value>>8)};
    struct hal_i2c_master_data data_struct = {
        .address = cfg->i2c_addr,
        .len = 3,
        .buffer = out
    };

    err = os_mutex_pend(cfg->i2c_mutex, OS_WAIT_FOREVER);
    if (err != OS_OK)
    {
        TCA9535_ERR("Mtx err:%d\n", err);
        STATS_INC(g_tca9535_stats, mutex_errors);
        return err;
    }

    /* Register write */
    rc = hal_i2c_master_write(cfg->i2c_num, &data_struct,
                              OS_TICKS_PER_SEC / 10, 1);
    if (rc) {
        TCA9535_ERR("w16 fail\n");
        STATS_INC(g_tca9535_stats, write_errors);
        goto exit;
    }

#ifdef TCA95xx_VERIFY_WRITES
    data_struct.buffer = &reg;
    data_struct.len = 1;

    /* Register write */
    rc = hal_i2c_master_write(cfg->i2c_num, &data_struct,
                              OS_TICKS_PER_SEC / 10, 0);

    /* Read one word back */
    data_struct.buffer = (uint8_t*)&readback;
    data_struct.len = 2;
    rc = hal_i2c_master_read(cfg->i2c_num, &data_struct,
                             OS_TICKS_PER_SEC / 10, 1);
    if (rc) {
        TCA9535_ERR("w16 ver fail\n");
        STATS_INC(g_tca9535_stats, write_errors);
        goto exit;
    }

    if (readback != value)
    {
        TCA9535_ERR("Rback err: %x != %x\r\n", readback, value);
        STATS_INC(g_tca9535_stats, write_errors);
    }
#endif
exit:
    err = os_mutex_release(cfg->i2c_mutex);
    assert(err == OS_OK);
    return rc;
}
/**
 * Writes a single word to the specified register
 *
 * @param The config
 * @param The register address to write to
 * @param The value to write
 *
 * @return 0 on success, non-zero error on failure.
 */
Writes a single word to the specified register
@param The config
@param The register address to write to
@param The value to write

@return 0 on success, non-zero error on failure.
Writes a single word to the specified register @param The config @param The register address to write to @param The value to write @return 0 on success non - zero error on failure .
int
tca95xx_write16(struct tca9535_io_expander_dev_cfg *cfg, uint8_t reg, uint16_t value)
{
    int rc;
    os_error_t err = 0;
#ifdef TCA95xx_VERIFY_WRITES
    uint16_t readback;
#endif
    uint8_t out[3] = {reg, value&0xff, (value>>8)};
    struct hal_i2c_master_data data_struct = {
        .address = cfg->i2c_addr,
        .len = 3,
        .buffer = out
    };
    err = os_mutex_pend(cfg->i2c_mutex, OS_WAIT_FOREVER);
    if (err != OS_OK)
    {
        TCA9535_ERR("Mtx err:%d\n", err);
        STATS_INC(g_tca9535_stats, mutex_errors);
        return err;
    }
    rc = hal_i2c_master_write(cfg->i2c_num, &data_struct,
                              OS_TICKS_PER_SEC / 10, 1);
    if (rc) {
        TCA9535_ERR("w16 fail\n");
        STATS_INC(g_tca9535_stats, write_errors);
        goto exit;
    }
#ifdef TCA95xx_VERIFY_WRITES
    data_struct.buffer = &reg;
    data_struct.len = 1;
    rc = hal_i2c_master_write(cfg->i2c_num, &data_struct,
                              OS_TICKS_PER_SEC / 10, 0);
    data_struct.buffer = (uint8_t*)&readback;
    data_struct.len = 2;
    rc = hal_i2c_master_read(cfg->i2c_num, &data_struct,
                             OS_TICKS_PER_SEC / 10, 1);
    if (rc) {
        TCA9535_ERR("w16 ver fail\n");
        STATS_INC(g_tca9535_stats, write_errors);
        goto exit;
    }
    if (readback != value)
    {
        TCA9535_ERR("Rback err: %x != %x\r\n", readback, value);
        STATS_INC(g_tca9535_stats, write_errors);
    }
#endif
exit:
    err = os_mutex_release(cfg->i2c_mutex);
    assert(err == OS_OK);
    return rc;
}
int tca95xx_write16 ( struct tca9535_io_expander_dev_cfg * cfg , uint8_t reg , uint16_t value ) { int rc ; os_error_t err = 0 ; #ifdef TCA95xx_VERIFY_WRITES uint16_t readback ; #endif uint8_t out [ 3 ] = { reg , value & 0xff , ( value >> 8 ) } ; struct hal_i2c_master_data data_struct = { . address = cfg -> i2c_addr , . len = 3 , . buffer = out } ; err = os_mutex_pend ( cfg -> i2c_mutex , OS_WAIT_FOREVER ) ; if ( err != OS_OK ) { TCA9535_ERR ( " \n " , err ) ; STATS_INC ( g_tca9535_stats , mutex_errors ) ; return err ; } rc = hal_i2c_master_write ( cfg -> i2c_num , & data_struct , OS_TICKS_PER_SEC / 10 , 1 ) ; if ( rc ) { TCA9535_ERR ( " \n " ) ; STATS_INC ( g_tca9535_stats , write_errors ) ; goto exit ; } #ifdef TCA95xx_VERIFY_WRITES data_struct . buffer = & reg ; data_struct . len = 1 ; rc = hal_i2c_master_write ( cfg -> i2c_num , & data_struct , OS_TICKS_PER_SEC / 10 , 0 ) ; data_struct . buffer = ( uint8_t * ) & readback ; data_struct . len = 2 ; rc = hal_i2c_master_read ( cfg -> i2c_num , & data_struct , OS_TICKS_PER_SEC / 10 , 1 ) ; if ( rc ) { TCA9535_ERR ( " \n " ) ; STATS_INC ( g_tca9535_stats , write_errors ) ; goto exit ; } if ( readback != value ) { TCA9535_ERR ( " \r \n " , readback , value ) ; STATS_INC ( g_tca9535_stats , write_errors ) ; } #endif exit : err = os_mutex_release ( cfg -> i2c_mutex ) ; assert ( err == OS_OK ) ; return rc ; }
Writes a single word to the specified register
 @param The config
 @param The register address to write to
 @param The value to write
Writes a single word to the specified register @param The config @param The register address to write to @param The value to write
/* Register write */ /* Register write */ /* Read one word back */