Esempio n. 1
0
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
    int count;

    i2c_start(obj);

    // Send the slave address
    i2c_do_write(obj, (address | 0x01), 1);

    // Wait until we have transmitted and the ADDR byte is set
    i2c_wait_addr_rx(obj);

    // Read in all except last byte
    for (count = 0; count < (length - 1); count++) {
        int value = i2c_do_read(obj, 0);
        data[count] = (char) value;
    }

    // read in last byte
    int value = i2c_do_read(obj, 1);
    data[count] = (char) value;

    // If not repeated start, send stop.
    if (stop) {
        i2c_stop(obj);
    }

    return length;
}
Esempio n. 2
0
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
{
    int status, count, errorResult;
    obj->i2c->ADDRESS         = (address >> 1);
    obj->i2c->SHORTS          = 1;  // to trigger suspend task when a byte is received
    obj->i2c->EVENTS_RXDREADY = 0;
    obj->i2c->TASKS_STARTRX   = 1;

    // Read in all except last byte
    for (count = 0; count < (length - 1); count++) {
        status = i2c_do_read(obj, &data[count], 0);
        if (status) {
            errorResult = checkError(obj);
            i2c_reset(obj);
            if (errorResult<0) {
                return errorResult;
            }
            return count;
        }
    }

    // read in last byte
    status = i2c_do_read(obj, &data[length - 1], 1);
    if (status) {
        i2c_reset(obj);
        return length - 1;
    }
    // If not repeated start, send stop.
    if (stop) {
        while (!obj->i2c->EVENTS_STOPPED) {
        }
        obj->i2c->EVENTS_STOPPED = 0;
    }
    return length;
}
Esempio n. 3
0
int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
{
	int rc = i2c_do_read(chip, addr, alen, buffer, len);

	i2c_stop();

	return rc;
}
Esempio n. 4
0
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
    int count, status;

    status = i2c_start(obj);

    if ((status != 0x10) && (status != 0x08)) {
        i2c_stop(obj);
        return status;
    }

    status = i2c_do_write(obj, (address | 0x01));
    if (status != 0x40) {
        i2c_stop(obj);
        return status;
    }

    // Read in all except last byte
    for (count = 0; count < (length - 1); count++) {
        int value = i2c_do_read(obj, 0);
        status = i2c_status(obj);
        if (status != 0x50) {
            i2c_stop(obj);
            return status;
        }
        data[count] = (char) value;
    }

    // read in last byte
    int value = i2c_do_read(obj, 1);
    status = i2c_status(obj);
    if (status != 0x58) {
        i2c_stop(obj);
        return status;
    }

    data[count] = (char) value;

    // If not repeated start, send stop.
    if (stop) {
        if(i2c_stop(obj) == 1)
            return 1;
    }

    return 0;
}
Esempio n. 5
0
int i2c_byte_read(i2c_t *obj, int last)
{
    char data;
    int status;

    status = i2c_do_read(obj, &data, last);
    if (status) {
        i2c_reset(obj);
    }
    return data;
}
Esempio n. 6
0
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
    int count, status;
    
    i2c_start(obj);
    
    status = i2c_do_write(obj, (address | 0x01), 1);
    if (status != 0x01) {
        i2c_stop(obj);
        return I2C_ERROR_NO_SLAVE;
    }
    
    // Read in all except last byte
    for (count = 0; count < (length - 1); count++) {
        int value = i2c_do_read(obj, 0);
        status = i2c_status(obj);
        if (status != 0x00) {
            i2c_stop(obj);
            return count;
        }
        data[count] = (char) value;
    }
    
    // read in last byte
    int value = i2c_do_read(obj, 1);
    status = i2c_status(obj);
    if (status != 0x01) {
        i2c_stop(obj);
        return length - 1;
    }
    
    data[count] = (char) value;
    
    // If not repeated start, send stop.
    if (stop) {
        i2c_stop(obj);
    } else {
        repeated_start = 1;
    }
    
    return length;
}
Esempio n. 7
0
int i2c_byte_read(i2c_t *obj, int last) {
    int status;

    /* wait for it to arrive */
    status = i2c_wait_RDRF(obj);
    if (status != 0) {
        i2c_set_err_noslave(obj, 1);
        return I2C_ERROR_NO_SLAVE;
    }
    
    return (i2c_do_read(obj, last));
}
Esempio n. 8
0
int i2c_byte_read(i2c_t *obj, int last) {
    char data;
    
    // set rx mode
    obj->i2c->C1 &= ~I2C_C1_TX_MASK;
    
    if(first_read) {
        // first dummy read
        i2c_do_read(obj, &data, 0);
        first_read = 0;
    }
    
    if (last) {
        // set tx mode
        obj->i2c->C1 |= I2C_C1_TX_MASK;
        return obj->i2c->D;
    }
        
    i2c_do_read(obj, &data, last);
    
    return data;
}
Esempio n. 9
0
int i2c_byte_read(i2c_t *obj, int last) {
    char data;

    // set rx mode
    i2c_hal_set_direction(obj->instance, kI2CReceive);

    // Setup read
    i2c_do_read(obj, &data, last);

    // set tx mode
    i2c_hal_set_direction(obj->instance, kI2CTransmit);
    return i2c_hal_read(obj->instance);
}
Esempio n. 10
0
int i2c_byte_read(i2c_t *obj, int last) {
    char data;
    uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
    // set rx mode
    I2C_HAL_SetDirMode(i2c_addrs[obj->instance], kI2CReceive);

    // Setup read
    i2c_do_read(obj, &data, last);

    // set tx mode
    I2C_HAL_SetDirMode(i2c_addrs[obj->instance], kI2CSend);
    return I2C_HAL_ReadByte(i2c_addrs[obj->instance]);
}
Esempio n. 11
0
int i2c_byte_read(i2c_t *obj, int last) {
    int status;
    int data;

    data = i2c_do_read(obj, last);
    /* wait for it to arrive */
    status = i2c_wait_RDRF(obj);
    if (status != 0) {
        i2c_set_SR2_NACKF_STOP(obj);
        return I2C_ERROR_NO_SLAVE;
    }
    
    return data;
}
Esempio n. 12
0
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
    uint8_t count;
    char dummy_read, *ptr;

    if (i2c_start(obj)) {
        i2c_stop(obj);
        return 1;
    }

    if (i2c_do_write(obj, (address | 0x01))) {
        i2c_stop(obj);
        return 1;
    }

    // set rx mode
    obj->i2c->C1 &= ~I2C_C1_TX_MASK;

    // Read in bytes
    for (count = 0; count < (length); count++) {
        ptr = (count == 0) ? &dummy_read : &data[count - 1];
        uint8_t stop_ = (count == (length - 1)) ? 1 : 0;
        if (i2c_do_read(obj, ptr, stop_)) {
            i2c_stop(obj);
            return 1;
        }
    }

    // If not repeated start, send stop.
    if (stop) {
        i2c_stop(obj);
    }

    // last read
    data[count-1] = obj->i2c->D;

    return 0;
}
Esempio n. 13
0
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
    int count;
    char dummy_read, *ptr;

    if (i2c_start(obj)) {
        i2c_stop(obj);
        return I2C_ERROR_BUS_BUSY;
    }

    if (i2c_do_write(obj, (address | 0x01))) {
        i2c_stop(obj);
        return I2C_ERROR_NO_SLAVE;
    }

    // set rx mode
    uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
    I2C_HAL_SetDirMode(i2c_addrs[obj->instance], kI2CReceive);

    // Read in bytes
    for (count = 0; count < (length); count++) {
        ptr = (count == 0) ? &dummy_read : &data[count - 1];
        uint8_t stop_ = (count == (length - 1)) ? 1 : 0;
        if (i2c_do_read(obj, ptr, stop_)) {
            i2c_stop(obj);
            return count;
        }
    }

    // If not repeated start, send stop.
    if (stop)
        i2c_stop(obj);

    // last read
    data[count-1] = I2C_HAL_ReadByte(i2c_addrs[obj->instance]);

    return length;
}
Esempio n. 14
0
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
    int count;
    char dummy_read, *ptr;

    if (i2c_start(obj)) {
        i2c_stop(obj);
        return I2C_ERROR_BUS_BUSY;
    }

    if (i2c_do_write(obj, (address | 0x01))) {
        i2c_stop(obj);
        return I2C_ERROR_NO_SLAVE;
    }

    // set rx mode
    i2c_hal_set_direction(obj->instance, kI2CReceive);

    // Read in bytes
    for (count = 0; count < (length); count++) {
        ptr = (count == 0) ? &dummy_read : &data[count - 1];
        uint8_t stop_ = (count == (length - 1)) ? 1 : 0;
        if (i2c_do_read(obj, ptr, stop_)) {
            i2c_stop(obj);
            return count;
        }
    }

    // If not repeated start, send stop.
    if (stop)
        i2c_stop(obj);

    // last read
    data[count-1] = i2c_hal_read(obj->instance);

    return length;
}
Esempio n. 15
0
int i2c_byte_read(i2c_t *obj, int last) {
    obj->dummy = 1;
    
    return (i2c_do_read(obj, last) & 0xFF);
}
Esempio n. 16
0
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
    int count = 0;
    int status;
    int value;
    volatile uint32_t work_reg = 0;

    // full reset
    i2c_reg_reset(obj);
    obj->dummy = 1;
    
    status = i2c_start(obj);

    if (status == 0xff) {
        i2c_stop(obj);
        i2c_wait_STOP(obj);
        return I2C_ERROR_BUS_BUSY;
    }

    status = i2c_do_write(obj, (address | 0x01));
    if (status & 0x01) {
        i2c_stop(obj);
        i2c_wait_STOP(obj);
        return I2C_ERROR_NO_SLAVE;
    }
    
    /* wati RDRF */
    i2c_wait_RDRF(obj);
    /* check ACK/NACK */
    if ((REG(SR2.UINT32) & (1 << 4) == 1)) {
        /* Slave sends NACK */
        i2c_stop(obj);
        // dummy read
        value = REG(DRR.UINT32);
        i2c_wait_STOP(obj);
        return I2C_ERROR_NO_SLAVE;
    }
    
    // Read in all except last byte
    if (length > 2) {
        for (count = 0; count < (length - 1); count++) {
            if (count == (length - 2)) {
                value = i2c_do_read(obj, 1);
            } else if ((length >= 3) && (count == (length - 3))) {
                value = i2c_do_read(obj, 2);
            } else {
                value = i2c_do_read(obj, 0);
            }
            status = i2c_status(obj);
            if (status & 0x10) {
                i2c_stop(obj);
                i2c_wait_STOP(obj);
                return count;
            }
            data[count] = (char) value;
        }
    } else if (length == 2) {
        /* Set MR3 WATI bit is 1 */;
        REG(MR3.UINT32) |= (1 << 6);
        // dummy read
        value = REG(DRR.UINT32);
        // wait for it to arrive
        i2c_wait_RDRF(obj);
        // send a NOT ACK
        REG(MR3.UINT32) |= (1 <<4);
        REG(MR3.UINT32) |=  (1 <<3);
        REG(MR3.UINT32) &= ~(1 <<4);
        data[count] = (char)REG(DRR.UINT32);
        count++;
    } else if (length == 1) {
        /* Set MR3 WATI bit is 1 */;
        REG(MR3.UINT32) |= (1 << 6);
        // send a NOT ACK
        REG(MR3.UINT32) |= (1 <<4);
        REG(MR3.UINT32) |=  (1 <<3);
        REG(MR3.UINT32) &= ~(1 <<4);
        // dummy read
        value = REG(DRR.UINT32);
    } else {
        // Do Nothing
    }

    // read in last byte
    i2c_wait_RDRF(obj);
    // If not repeated start, send stop.
    if (stop) {
        /* RIICnSR2.STOP = 0 */
        REG(SR2.UINT32) &= ~(1 << 3);
        /* RIICnCR2.SP   = 1 */
        REG(CR2.UINT32) |= (1 << 3);
        /* RIICnDRR read */
        value = REG(DRR.UINT32) & 0xFF;
        data[count] = (char) value;
        /* RIICnMR3.WAIT = 0 */
        REG(MR3.UINT32) &= ~(1 << 6);
        i2c_wait_STOP(obj);
    } else {
        /* RIICnDRR read */
        value = REG(DRR.UINT32) & 0xFF;
        data[count] = (char) value;
    }

    return length;
}
Esempio n. 17
0
File: i2c_api.c Progetto: pan-/mbed
int i2c_byte_read(i2c_t *obj, int last) {
    return (i2c_do_read(obj, last) & 0xFF);
}
Esempio n. 18
0
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
    int count = 0;
    int status;
    int value;
    volatile uint32_t work_reg = 0;

    if(length <= 0) {
        return 0;
    }
    i2c_set_MR3_ACK(obj);
    /* There is a STOP condition for last processing */
    if (obj->last_stop_flag != 0) {
        status = i2c_start(obj);
        if (status != 0) {
            i2c_set_err_noslave(obj);
            return I2C_ERROR_BUS_BUSY;
        }
    }
    obj->last_stop_flag = stop;
    /*  Send Slave address */
    status = i2c_read_address_write(obj, (address | 0x01));
    if (status != 0) {
        i2c_set_err_noslave(obj);
        return I2C_ERROR_NO_SLAVE;
    }
    /* wait RDRF */
    status = i2c_wait_RDRF(obj);
    /* check ACK/NACK */
    if ((status != 0) || ((REG(SR2.UINT32) & SR2_NACKF) != 0)) {
        /* Slave sends NACK */
        (void)i2c_set_STOP(obj);
        /* dummy read */
        value = REG(DRR.UINT32);
        (void)i2c_wait_STOP(obj);
        i2c_set_SR2_NACKF_STOP(obj);
        obj->last_stop_flag = 1;
        return I2C_ERROR_NO_SLAVE;
    }
    /* Read in all except last byte */
    if (length > 2) {
        /* dummy read */
        value = REG(DRR.UINT32);
        for (count = 0; count < (length - 1); count++) {
            /* wait for it to arrive */
            status = i2c_wait_RDRF(obj);
            if (status != 0) {
                i2c_set_err_noslave(obj);
                return I2C_ERROR_NO_SLAVE;
            }
            /* Recieve the data */
            if (count == (length - 2)) {
                value = i2c_do_read(obj, 1);
            } else if ((length >= 3) && (count == (length - 3))) {
                value = i2c_do_read(obj, 2);
            } else {
                value = i2c_do_read(obj, 0);
            }
            data[count] = (char)value;
        }
    } else if (length == 2) {
        /* Set MR3 WATI bit is 1 */
        REG(MR3.UINT32) |= MR3_WAIT;
        /* dummy read */
        value = REG(DRR.UINT32);
        /* wait for it to arrive */
        status = i2c_wait_RDRF(obj);
        if (status != 0) {
            i2c_set_err_noslave(obj);
            return I2C_ERROR_NO_SLAVE;
        }
        i2c_set_MR3_NACK(obj);
        data[count] = (char)REG(DRR.UINT32);
        count++;
    } else {
        /* length == 1 */
        /* Set MR3 WATI bit is 1 */;
        REG(MR3.UINT32) |=  MR3_WAIT;
        i2c_set_MR3_NACK(obj);
        /* dummy read */
        value = REG(DRR.UINT32);
    }
    /* wait for it to arrive */
    status = i2c_wait_RDRF(obj);
    if (status != 0) {
        i2c_set_err_noslave(obj);
        return I2C_ERROR_NO_SLAVE;
    }

    /* If not repeated start, send stop. */
    if (stop) {
        (void)i2c_set_STOP(obj);
        /* RIICnDRR read */
        value = (REG(DRR.UINT32) & 0xFF);
        data[count] = (char)value;
        /* RIICnMR3.WAIT = 0 */
        REG(MR3.UINT32) &= ~MR3_WAIT;
        (void)i2c_wait_STOP(obj);
        i2c_set_SR2_NACKF_STOP(obj);
    } else {
        (void)i2c_restart(obj);
        /* RIICnDRR read */
        value = (REG(DRR.UINT32) & 0xFF);
        data[count] = (char)value;
        /* RIICnMR3.WAIT = 0 */
        REG(MR3.UINT32) &= ~MR3_WAIT;
        (void)i2c_wait_START(obj);
        /* SR2.START = 0 */
        REG(SR2.UINT32) &= ~SR2_START;
    }

    return length;
}