Reading Data Over I2C, SAMD21 Atmel START code unclear

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone, 

 

I am using Atmel START to help me get started with a project involving the SAMD21 microcontroller (as used on the arduino zero). Atmel Zero set up a file called hal_i2c_m_sync.c which has a read function called i2c_sync_read. I have included the code below.

 

static int32_t i2c_m_sync_read(struct io_descriptor *io, uint8_t *buf, const uint16_t n)
{
	struct i2c_m_sync_desc *i2c = CONTAINER_OF(io, struct i2c_m_sync_desc, io);
	struct _i2c_m_msg       msg;
	int32_t                 ret;

	msg.addr   = i2c->slave_addr;
	msg.len    = n;
	msg.flags  = I2C_M_STOP | I2C_M_RD;
	msg.buffer = buf;

	ret = _i2c_m_sync_transfer(&i2c->device, &msg);

	if (ret) {
		return ret;
	}

	return n;
}

 

Could someone please explain to me what the buffer is, and how to use it? I originally thought it stored the data read from the slave device, however, I don't understand how this would work if more than 1 byte needs to be read (since the last perimeter of the fxn is how many bytes to read).

 

Thank you for any help!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't get the problem, typically you would call such a function like
 

uint8_t readBuf[8]
i2c_m_sync_read(io, readBuf, sizeof(readBuf));

And yes the provided buffer will store the data read.

/Lars