mbed-ce / mbed-os

Arm Mbed OS is a platform operating system designed for the internet of things
https://mbed.com
Other
77 stars 14 forks source link

I2C write byte is not sending bytes #236

Open lefebvresam opened 7 months ago

lefebvresam commented 7 months ago

When I try this:

RetCode_t RA8875::GT911_WriteConfig(GTConfig *config){
    // char address[2] = { 0 };
    // address[0] = GTP_REG_CONFIG_DATA >> 8; // add H
    // address[1] = GTP_REG_CONFIG_DATA & 0xFF; // add L
    // int err = m_i2c->write(m_addr, address, 2, true);
    // if (err) {
    //     ERR("I2C result: %d", err);
    // }
    INFO("GTConfig size: %u", sizeof(GTConfig));
    m_i2c->lock();
    m_i2c->start();
    m_i2c->write_byte((uint8_t)(m_addr+1));
    m_i2c->write_byte((uint8_t)GTP_REG_CONFIG_DATA >> 8);
    m_i2c->write_byte((uint8_t)GTP_REG_CONFIG_DATA & 0xFF);
    for (int i = 0; i < sizeof(GTConfig); i++) {
        int err = m_i2c->write_byte(*((uint8_t*)config+i));
        if (err) {
            ERR("I2C result: %d", err);
        }  
    }  
    m_i2c->stop();
    m_i2c->unlock();
    // err = m_i2c->write(m_addr, (char*)config, sizeof(GTConfig));
    return noerror;
}

I get this:

New25

multiplemonomials commented 7 months ago

Hmm, are any of those I2C calls returning an error code?

JohnK1987 commented 1 month ago

Hmm, are any of those I2C calls returning an error code?

@lefebvresam any comment?

lefebvresam commented 1 month ago

I omitted this issue by not longer using those function calls. I think the bug in those calls is still there and should be further investigated.

I now use this implementation which is working correctly for my application:

RetCode_t RA8875::GT911_WriteConfig(GTConfig *config){
    GTchecksum cs;
    GT911_ReadChecksum(&cs);
    INFO("Read checksum: 0x%02X", cs.value);
    uint8_t cs_calc = GT911_CalcChecksum((uint8_t *)config+2, sizeof(GTConfig)-2);
    INFO("Calc checksum: 0x%02X", cs_calc);
    if (!configLoaded) {
        ERR("Config has not been loaded");
        return bad_parameter;
    }
    if (cs.value != cs_calc) {  // config has been changed
        int err = m_i2c->write(m_addr, (char*)config, sizeof(GTConfig));
        if (err) {
            ERR("I2C result: %d", err);
            return bad_parameter;
        }
        cs.value = cs_calc;
        cs.fresh = 1; // configuration updated 
        GT911_WriteChecksum(&cs);
        INFO("Config has been written correctly");   
        return noerror;
    }
    INFO("No differences in config");
    return noerror;
}