Skip to content

Recurrent compiler optimization related failure of BFLB I2C driver #102040

@VynDragon

Description

@VynDragon

Describe the bug

When using the bouffalolab I2C driver, failings will appear and break the driver's functionality.

Facts list:

static int ssd1306_write_bus_i2c(const struct device *dev, uint8_t *buf, size_t len, bool command)
{
    const struct ssd1306_config *config = dev->config;

    const uint8_t control =
        command ? SSD1306_CONTROL_ALL_BYTES_CMD : SSD1306_CONTROL_ALL_BYTES_DATA;
    static uint8_t tmp[SSD1306_I2C_CHUNK_SIZE + 1];
    int ret = 0;

    if (len == 0U) {
        return 0;
    }

    k_mutex_lock(&ssd1306_i2c_buffer_lock, K_FOREVER);

    size_t off = 0;

    while (off < len) {
        size_t this_len = MIN(SSD1306_I2C_CHUNK_SIZE, len - off);

        LOG_ERR("WHAT THE FUCK IS WRONG WITH YOU DUMBASS");

        tmp[0] = control;
        memcpy(&tmp[1], &buf[off], this_len);

        ret = i2c_write_dt(&config->bus.i2c, tmp, this_len + 1);
        if (ret < 0) {
            k_mutex_unlock(&ssd1306_i2c_buffer_lock);
            return ret;
        }

        off += this_len;
    }

    k_mutex_unlock(&ssd1306_i2c_buffer_lock);

    return ret;
}

This does not work

static int ssd1306_write_bus_i2c(const struct device *dev, uint8_t *buf, size_t len, bool command)
{
    const struct ssd1306_config *config = dev->config;

    const uint8_t control =
        command ? SSD1306_CONTROL_ALL_BYTES_CMD : SSD1306_CONTROL_ALL_BYTES_DATA;
    static uint8_t tmp[SSD1306_I2C_CHUNK_SIZE + 1];
    int ret = 0;

    if (len == 0U) {
        return 0;
    }

    k_mutex_lock(&ssd1306_i2c_buffer_lock, K_FOREVER);

    size_t off = 0;

    while (off < len) {
        size_t this_len = MIN(SSD1306_I2C_CHUNK_SIZE, len - off);

        LOG_ERR("WHAT THE FUCK IS WRONG WITH YOU DUMBASS %d", this_len);

        tmp[0] = control;
        memcpy(&tmp[1], &buf[off], this_len);

        ret = i2c_write_dt(&config->bus.i2c, tmp, this_len + 1);
        if (ret < 0) {
            k_mutex_unlock(&ssd1306_i2c_buffer_lock);
            return ret;
        }

        off += this_len;
    }

    k_mutex_unlock(&ssd1306_i2c_buffer_lock);

    return ret;
}

This works.

Regression

  • This is a regression.

Steps to reproduce

Switch between ssd1306 driver versions (current and PR mentionned) and see it starts failing for no reasons.

Relevant log output

Impact

Major – Severely degrades functionality; workaround is difficult or unavailable.

Environment

  • OS: Linux x86_64
  • Toolchain: SDK 17.4
  • Commits: various

Additional Context

No response

Mitigations:

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions