Exemple #1
0
mp_obj_t i2c_obj_readAndStop(mp_obj_t self_in) {
    pyb_i2c_obj_t *self = self_in;
    if (self->i2c_state != I2C_STATE_READ) {
        if (_i2c_restart(self->i2c_port, self->i2c_addr, 0) == false) {
            _i2c_stop(self->i2c_port);
            self->i2c_state = I2C_STATE_IDLE;
            return mp_const_false;
        }
    }
    uint8_t data = _i2c_read_nack(self->i2c_port);
    self->i2c_state = I2C_STATE_IDLE;
    return mp_obj_new_int(data);
}
Exemple #2
0
mp_obj_t i2c_obj_write(mp_obj_t self_in, mp_obj_t data_in) {
    pyb_i2c_obj_t *self = self_in;
    if (self->i2c_state != I2C_STATE_WRITE) {
        if (_i2c_restart(self->i2c_port, self->i2c_addr, 1) == false) {
            _i2c_stop(self->i2c_port);
            self->i2c_state = I2C_STATE_IDLE;
            return mp_const_false;
        }
        self->i2c_state = I2C_STATE_WRITE;
    }
    uint8_t data = mp_obj_get_int(data_in);
    if (_i2c_send_byte(self->i2c_port, data) == false)
        return mp_const_false;
    return mp_const_true;
}
Exemple #3
0
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length)
{
    I2C_Type *i2c;
    int n = 0;

    switch (dev) {
#if I2C_0_EN

        case I2C_0:
            i2c = I2C_0_DEV;
            break;
#endif

        default:
            return -1;
    }

    if (_i2c_start(i2c, address, I2C_FLAG_WRITE)) {
        _i2c_reset(i2c);
        return -1;
    }

    /* send reg */
    n = _i2c_transmit(i2c, &reg, 1);

    if (!n) {
        _i2c_stop(i2c);
        return n;
    }

    if (_i2c_restart(i2c, address, I2C_FLAG_READ)) {
        _i2c_reset(i2c);
        return -1;
    }

    n = _i2c_receive(i2c, (uint8_t *)data, length);
    if (n < 0) {
        _i2c_reset(i2c);
        return -1;
    }

    _i2c_stop(i2c);

    return n;
}