Exemplo n.º 1
0
unsigned char _i2c_write(unsigned char device_addr, unsigned char sub_addr, unsigned char *buff, int ByteNo)
{
    unsigned char i;

    DEBUG_MSG(("<<=====================_i2c_write=======================>>\r\n"));

    _i2c_start();
    I2C_DELAY;
    if(_i2c_write_byte(device_addr)) {
        _i2c_stop();
        DEBUG_MSG(("<<=====================Write Error - Addr=======================>>\r\n"));
        return ERROR_CODE_WRITE_ADDR;
    }
    if(_i2c_write_byte(sub_addr)) {
        _i2c_stop();
        DEBUG_MSG(("<<=====================Write Error - Addr=======================>>\r\n"));
        return ERROR_CODE_WRITE_ADDR;
    }
    for(i = 0; i<ByteNo; i++) {
        if(_i2c_write_byte(buff[i])) {
            _i2c_stop();
            DEBUG_MSG(("<<=====================Write Error - Data=======================>>\r\n"));
            return ERROR_CODE_WRITE_DATA;
        }
    }
    I2C_DELAY;
    _i2c_stop();
    I2C_DELAY_LONG;
    return ERROR_CODE_TRUE;
}
Exemplo n.º 2
0
unsigned char _i2c_read(unsigned char device_addr, unsigned char sub_addr, unsigned char *buff, int ByteNo)
{
    unsigned char i;

    DEBUG_MSG(("<<=====================_i2c_read=======================>>\r\n"));
    I2C_DELAY_LONG;
    _i2c_start();
    I2C_DELAY;
    if(_i2c_write_byte(device_addr)) {
        _i2c_stop();
        DEBUG_MSG(("<<=====================Write Error - Addr=======================>>\r\n"));
        return ERROR_CODE_READ_ADDR;
    }
    if(_i2c_write_byte(sub_addr)) {
        _i2c_stop();
        DEBUG_MSG(("<<=====================Write Error - Addr=======================>>\r\n"));
        return ERROR_CODE_READ_ADDR;
    }
    _i2c_start();
    I2C_DELAY;
    if(_i2c_write_byte(device_addr+1)) {
        _i2c_stop();
        DEBUG_MSG(("<<=====================Write Error - Addr=======================>>\r\n"));
        return ERROR_CODE_READ_ADDR;
    }
    for(i = 0; i<ByteNo; i++) buff[i] = _i2c_read_byte();

    I2C_DELAY;
    _i2c_stop();
    I2C_DELAY_LONG;
    return ERROR_CODE_TRUE;
}
Exemplo n.º 3
0
void i2c_state_machine(){
	switch(SMB0STA) {
	case SMB_RESTART:
	case SMB_START:
		STA = 0;
		STO = 0;
		//send slave address
		if (_i2c_params & I2C_WRITE)
			SMB0DAT = _i2c_send_addr & 0xfe;
		else
			SMB0DAT = _i2c_send_addr | 0x01;
		break;
	case SMB_ADDR_NACK_R:
	case SMB_ADDR_NACK:
		//restart communication
		STO = 1;
		STA = 0;
		STO = 0;
		STA = 1;
		break;
	case SMB_ADDR_ACK_R:
	case SMB_ADDR_ACK:
		//send first byte
		if(_i2c_send_len)
			SMB0DAT = _i2c_send_data[0];
		else 
			_i2c_stop();
		break;
	case SMB_DATA_ACK:
		_i2c_send_index = _i2c_send_index +1;
		//missing break: fall through
	case SMB_DATA_NACK:
		//send next byte/resend same byte
		if (_i2c_send_index < _i2c_send_len) {
			SMB0DAT = _i2c_send_data[_i2c_send_index];
		} else {
			_i2c_stop();
		}
		break;
	case SMB_DATA_R_NACK:
	case SMB_DATA_R_ACK:
		_i2c_return[_i2c_read_index++] = SMB0DAT;
		//controlla se ha finito di ricevere
		if(_i2c_read_index == 2){
			AA = 0;
			//send nack next time
		}
		if(_i2c_read_index == _i2c_read_len){
			STO = 1;
			STA = 0;
			EV_ENABLE(_i2c_callback);
			i2c_lock = 0;
			AA = 1;
		}
		break;
	default:
		break;
	}
	SI = 0;
}
Exemplo n.º 4
0
Arquivo: i2c.c Projeto: daniel-k/RIOT
int i2c_write_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;
    }

    n = _i2c_transmit(i2c, &reg, 1);

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

    n = _i2c_transmit(i2c, (uint8_t *)data, length);
    _i2c_stop(i2c);

    return n;
}
Exemplo n.º 5
0
Arquivo: i2c.c Projeto: daniel-k/RIOT
int i2c_read_bytes(i2c_t dev, uint8_t address, 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_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;
}
Exemplo n.º 6
0
int i2c_write_bytes(i2c_t dev, uint8_t address, const void *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;
    }

    n = _i2c_transmit(i2c, data, length);
    _i2c_stop(i2c);

    return n;
}
Exemplo n.º 7
0
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, void *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;
}
Exemplo n.º 8
0
// calls _i2c_start with write=0,1 depending on LSB of i2c_addr
mp_obj_t i2c_obj_start(mp_obj_t self_in) {
    pyb_i2c_obj_t *self = self_in;
    if (self->i2c_state != I2C_STATE_IDLE) {
        _i2c_stop(self->i2c_port);
        self->i2c_state = I2C_STATE_IDLE;
    }
    if (_i2c_start(self->i2c_port) == true)
        return mp_const_true;
    return mp_const_false;
}
Exemplo n.º 9
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);
}
Exemplo n.º 10
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;
}
Exemplo n.º 11
0
mp_obj_t i2c_obj_stop(mp_obj_t self_in) {
    pyb_i2c_obj_t *self = self_in;
    _i2c_stop(self->i2c_port);
    self->i2c_state = I2C_STATE_IDLE;
    return mp_const_none;
}