示例#1
0
文件: eeprom.c 项目: LeafGrass/ousia
/* page read */
int eeprom_read(struct eeprom_priv_s *ee, uint8 * buf, uint32 offset, uint32 nb)
{
	int ret;
	i2c_msg msgs[2];

	os_log(LOG_INFO, "%s start...\n", __func__);

	/* XXX 16 bit addr? */
	msgs[0].addr = ee->dev_addr;
	msgs[0].flags = 0;
	msgs[0].length = 1;
	offset &= 0xFF;
	msgs[0].data = (uint8 *) & offset;

	msgs[1].addr = ee->dev_addr;
	msgs[1].flags = I2C_MSG_READ;
	msgs[1].length = nb;
	msgs[1].data = buf;

	ret = i2c_master_xfer(I2C1, msgs, 2, 0);
	if (ret != 0)
		os_log(LOG_ERROR, "%s - ret = %d.\n", __func__, ret);
	else {
		_mm_dump(buf, nb, 0);
		os_log(LOG_INFO, "%s done...\n", __func__);
	}

	return ret;
}
示例#2
0
uint8 HardWire::process() {
    int8 res = i2c_master_xfer(sel_hard, &itc_msg, 1, 0);
    if (res != 0) {
        i2c_disable(sel_hard);
        i2c_master_enable(sel_hard, (I2C_BUS_RESET | dev_flags));
    }
    return 0;
}
示例#3
0
static uint8 mpr121Read(uint8 addr) {
	struct i2c_msg msgs[2];
	uint8 byte;

	byte = addr;
	msgs[0].addr = msgs[1].addr = CAPTOUCH_ADDR;
	msgs[0].length = msgs[1].length = sizeof(byte);
	msgs[0].data = msgs[1].data = &byte;
	msgs[0].flags = 0;
	msgs[1].flags = I2C_MSG_READ;
	int result = i2c_master_xfer(i2c, msgs, 2, 100);

	// retry reads
	for (int n = 0; (result == -1) && (n < 5); n++) {
		result = i2c_master_xfer(i2c, msgs, 2, 100);
	}
	if (result == -1)
		return 255;

	return byte;
}
示例#4
0
文件: eeprom.c 项目: keepmov/ousia
static void i2c_recv_byte(uint8 slave, uint8 *pdata)
{
	i2c_msg msg;
	int ret;

	msg.addr = slave;
	msg.flags = 0;
	msg.length = 1;
	msg.data = pdata;
	ret = i2c_master_xfer(I2C1, &msg, 1, 0);
	if (ret != 0)
		os_log(LOG_ERROR, "%s - ret = %d.\n", __func__, ret);
}
示例#5
0
文件: accel.cpp 项目: xobs/libmaple
static uint8
accel_read(uint8 addr)
{
    struct i2c_msg msgs[2];
    uint8 byte;

    byte = addr;
    msgs[0].addr   = msgs[1].addr   = ACCEL_ADDR;
    msgs[0].length = msgs[1].length = sizeof(byte);
    msgs[0].data   = msgs[1].data   = &byte;
    msgs[0].flags = 0;
    msgs[1].flags = I2C_MSG_READ;
    if (switch_state(&back_switch))
        i2c_master_xfer(i2c, msgs, 2, 1);
    return byte;
}
示例#6
0
uint8_t TwoWire::process() {
    int8 res = i2c_master_xfer(sel_hard, itc_msg, itc_msg_count, 0);
    if (res == I2C_ERROR_PROTOCOL) {
        if (sel_hard->error_flags & I2C_SR1_AF) { /* NACK */
            res = (sel_hard->error_flags & I2C_SR1_ADDR ? ENACKADDR : 
                                                          ENACKTRNS);
        } else if (sel_hard->error_flags & I2C_SR1_OVR) { /* Over/Underrun */
            res = EDATA;
        } else { /* Bus or Arbitration error */
            res = EOTHER;
        }
        i2c_disable(sel_hard);
        i2c_master_enable(sel_hard, (I2C_BUS_RESET | dev_flags));
    }
    return res;
}
示例#7
0
static void mpr121Write(uint8 addr, uint8 value) {
	struct i2c_msg msg;
	uint8 bytes[2];
	int result;

	bytes[0] = addr;
	bytes[1] = value;

	msg.addr = CAPTOUCH_ADDR;
	msg.flags = 0;
	msg.length = sizeof(bytes);
	msg.xferred = 0;
	msg.data = bytes;

	result = i2c_master_xfer(i2c, &msg, 1, 100);
	return;
}
示例#8
0
文件: accel.cpp 项目: xobs/libmaple
static int
accel_write(uint8 addr, uint8 value)
{
    struct i2c_msg msg;
    uint8 bytes[2];
    int result;

    bytes[0] = addr;
    bytes[1] = value;

    msg.addr    = ACCEL_ADDR;
    msg.flags   = 0;
    msg.length  = sizeof(bytes);
    msg.xferred = 0;
    msg.data    = bytes;

    if (switch_state(&back_switch))
        result = i2c_master_xfer(i2c, &msg, 1, 1);

    return result;
}
示例#9
0
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
{
  int32_t result;
  // clamp to buffer length
  if(quantity > BUFFER_LENGTH){
    quantity = BUFFER_LENGTH;
  }
   msg.addr = address<<1;
   msg.data = rxBuffer;
   msg.length = quantity;
   msg.xferred = 0;
   msg.flags |= I2C_MSG_READ;
   result = i2c_master_xfer(dev, &msg,1,10);
  
  rxBufferIndex = 0;
  rxBufferLength = msg.xferred;
  if(sendStop){
	i2c_master_release_bus(dev);
  }
  // rxBufferLength should be less than quantity on error
  return rxBufferLength;

}
示例#10
0
文件: accel.cpp 项目: xobs/libmaple
uint8
accel_read_state(int *x, int *y, int *z)
{
    struct i2c_msg msgs[2];
    signed char values[6];
    uint8 addr = 0x00; /* 10-bits read value */
    uint8 result = 0;

    accel_wakeup();
    while (!accel_ready())
        delay_us(1000);

    msgs[0].addr   = ACCEL_ADDR;
    msgs[0].length = sizeof(byte);
    msgs[0].data   = &addr;
    msgs[0].flags  = 0;

    msgs[1].addr   = ACCEL_ADDR;
    msgs[1].length = sizeof(values);
    msgs[1].data   = (uint8 *)values;
    msgs[1].flags  = I2C_MSG_READ;

    if (switch_state(&back_switch))
        result = i2c_master_xfer(i2c, msgs, 2, 1);

    if (x)
        *x = (values[1]<<2) | (values[0]);
    if (y)
        *y = (values[3]<<2) | (values[2]);
    if (z)
        *z = (values[5]<<2) | (values[4]);

    accel_sleep();

    return result;
}