<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How to remove start bit from an I2C transfer in zephyr</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/88473/how-to-remove-start-bit-from-an-i2c-transfer-in-zephyr</link><description>Hello, 
 
 I am developing a driver for the vl53l5cx TOF sensor from ST, however their sequential write format looks as such: 
 
 This is problematic for me since I can get the address, and register index sent correctly with the stop bit at the end, however</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 15 Feb 2023 09:13:08 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/88473/how-to-remove-start-bit-from-an-i2c-transfer-in-zephyr" /><item><title>RE: How to remove start bit from an I2C transfer in zephyr</title><link>https://devzone.nordicsemi.com/thread/410006?ContentTypeID=1</link><pubDate>Wed, 15 Feb 2023 09:13:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5de817b6-62d6-4ef4-b4a4-9212818e06f1</guid><dc:creator>Richard Osterloh</dc:creator><description>&lt;p&gt;I&amp;#39;m struggling with this part in Zephyr too and am trying to test your solution. Have you defined writeBuf,&amp;nbsp;MAX_TX_BUFFER and MAX_RX_BUFFER globally somewhere? I was using a similar implementation to your earlier one but something is definitely wrong&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint8_t RdMulti(VL53L5CX_Platform *p_platform, uint16_t RegisterAdress, uint8_t *p_values,
		uint32_t size)
{
	uint8_t data_write[2];

	sys_put_be16(RegisterAdress, data_write);

	return i2c_write_read_dt(p_platform-&amp;gt;i2c, data_write, sizeof(data_write),
				 p_values, size);
}

uint8_t WrMulti(VL53L5CX_Platform *p_platform, uint16_t RegisterAdress, uint8_t *p_values,
		uint32_t size)
{
	struct i2c_msg msg[2];
	uint8_t data_write[2];

	sys_put_be16(RegisterAdress, data_write);

	msg[0].buf = data_write;
	msg[0].len = sizeof(data_write);
	msg[0].flags = I2C_MSG_WRITE;

	msg[1].buf = (uint8_t *)p_values;
	msg[1].len = size;
	msg[1].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
	
	return i2c_transfer_dt(p_platform-&amp;gt;i2c, msg, 2);
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to remove start bit from an I2C transfer in zephyr</title><link>https://devzone.nordicsemi.com/thread/375395?ContentTypeID=1</link><pubDate>Mon, 04 Jul 2022 16:29:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d696c16c-910f-4a51-acd2-0dc772bc1b8d</guid><dc:creator>jspiller</dc:creator><description>&lt;p&gt;Turns out there was an error in their datasheet and it should comply with the usual I2C spec. I filled the WrMulti and RdMulti the following way:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;uint8_t RdMulti(
	VL53L5CX_Platform *p_platform,
	uint16_t RegisterAddress,
	uint8_t *p_values,
	uint32_t num_read)
{
	uint8_t status = 0;
	int offset = 0;

	while (num_read &amp;gt; 0)
	{
		writeBuf[0] = (RegisterAddress &amp;gt;&amp;gt; 8) &amp;amp; 0xFF;
		writeBuf[1] = RegisterAddress &amp;amp; 0xFF;

		if (num_read &amp;gt;= MAX_RX_BUFFER)
		{
			status |= i2c_write(p_platform-&amp;gt;i2c_dev, writeBuf, 2U, p_platform-&amp;gt;address);
			status |= i2c_read(p_platform-&amp;gt;i2c_dev, p_values + offset, MAX_RX_BUFFER, p_platform-&amp;gt;address);

			num_read -= MAX_RX_BUFFER;
			RegisterAddress += MAX_RX_BUFFER;
			offset += MAX_RX_BUFFER;
		}
		else
		{
			status |= i2c_write(p_platform-&amp;gt;i2c_dev, writeBuf, 2U, p_platform-&amp;gt;address);
			status |= i2c_read(p_platform-&amp;gt;i2c_dev, p_values + offset, num_read, p_platform-&amp;gt;address);
			break;
		}
	}

	return (status);
}

uint8_t WrMulti(
	VL53L5CX_Platform *p_platform,
	uint16_t RegisterAddress,
	uint8_t *p_values,
	uint32_t num_write)
{
	uint8_t status = 0;
	uint32_t offset = 0;

	while (num_write &amp;gt; 0)
	{
		writeBuf[0] = (RegisterAddress &amp;gt;&amp;gt; 8) &amp;amp; 0xFF;
		writeBuf[1] = RegisterAddress &amp;amp; 0xFF;
		memcpy(&amp;amp;(writeBuf[2]), &amp;amp;(p_values[offset]), (MAX_TX_BUFFER - 2));

		if (num_write &amp;gt;= (MAX_TX_BUFFER - 2))
		{
			status |= i2c_write(p_platform-&amp;gt;i2c_dev, writeBuf, MAX_TX_BUFFER, p_platform-&amp;gt;address);

			num_write -= (MAX_TX_BUFFER - 2);
			RegisterAddress += (MAX_TX_BUFFER - 2);
			offset += (MAX_TX_BUFFER - 2);
		}
		else
		{
			status |= i2c_write(p_platform-&amp;gt;i2c_dev, writeBuf, num_write + 2, p_platform-&amp;gt;address);
			break;
		}
	}

	return (status);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;and WrByte/RdByte work in the same way just without the while loop since the size is only 1.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to remove start bit from an I2C transfer in zephyr</title><link>https://devzone.nordicsemi.com/thread/375239?ContentTypeID=1</link><pubDate>Mon, 04 Jul 2022 05:54:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:856e809d-487d-4d91-b6de-d23c57b059fe</guid><dc:creator>gweis</dc:creator><description>&lt;p&gt;Hey,&lt;br /&gt;Can you please share your driver for vl53l5cx on zephyr?&lt;br /&gt;I&amp;#39;m interested in it too.&lt;br /&gt;Thanks!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to remove start bit from an I2C transfer in zephyr</title><link>https://devzone.nordicsemi.com/thread/370823?ContentTypeID=1</link><pubDate>Fri, 03 Jun 2022 08:40:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6c660ae2-8961-4798-8efc-16277a669740</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;Yes I saw this in the data sheet, but I was wondering if the sequential write figure could be a misprint&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to remove start bit from an I2C transfer in zephyr</title><link>https://devzone.nordicsemi.com/thread/370741?ContentTypeID=1</link><pubDate>Thu, 02 Jun 2022 15:08:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:298cae4b-f751-4227-b982-37fec5063f44</guid><dc:creator>jspiller</dc:creator><description>&lt;p&gt;That&amp;#39;s what I thought as well, it&amp;#39;s strange that they wrote it in such a way. Perhaps it is an issue with the datasheet, I will contact ST about this. I will keep playing around with different variations to see what works. But yes it mentions here:&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1654182455109v1.png" /&gt;&lt;/p&gt;
&lt;p&gt;That P is for stop and S is for start.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Jacob&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to remove start bit from an I2C transfer in zephyr</title><link>https://devzone.nordicsemi.com/thread/370664?ContentTypeID=1</link><pubDate>Thu, 02 Jun 2022 11:18:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a8084164-19bf-4b29-905f-0067bd5189e8</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;Hm I see, that&amp;#39;s strange.&lt;/p&gt;
&lt;p&gt;It seems&amp;nbsp;odd to me that you&amp;#39;re supposed to send data after a stop sequence with no start sequence.&lt;/p&gt;
&lt;p&gt;Looking at your sensor&amp;#39;s data sheet, it says &amp;quot;All serial interface communications with the Time-of-Flight sensor must begin with a start condition.&amp;quot; and &amp;quot;A message contains a series of bytes preceded by a start condition and followed by either a stop or repeated start (another start condition but without a preceding stop condition) followed by another message.&amp;quot;&lt;/p&gt;
&lt;p&gt;Are you sure the P after the index bytes is really supposed to be a stop sequence, and not perhaps a &lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fps_nrf52840%2Ftwi.html&amp;amp;cp=4_0_0_5_28_5&amp;amp;anchor=concept_cdz_xtv_sr"&gt;repeated start sequence&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;-Einar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to remove start bit from an I2C transfer in zephyr</title><link>https://devzone.nordicsemi.com/thread/370508?ContentTypeID=1</link><pubDate>Wed, 01 Jun 2022 15:30:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:58a2b212-2d6b-4b74-b010-2cc9d1b0663a</guid><dc:creator>jspiller</dc:creator><description>&lt;p&gt;Aha, turns out that is exactly what I have done. But it still gives me a start bit before the data bytes. I removed the I2C_MSG_WRITE from the second message but still no luck.&lt;/p&gt;
&lt;p&gt;See here:&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/0552.Image-from-iOS-_2800_1_2900_.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;Using:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static inline int i2c_write_test(const struct device *dev, uint8_t *write_reg, uint8_t *write_buf, size_t num_write, uint16_t addr)
{
	struct i2c_msg msg[2];

	msg[0].buf = write_reg;
	msg[0].len = 2U;
	msg[0].flags = I2C_MSG_WRITE | I2C_MSG_STOP;

	msg[1].buf = write_buf;
	msg[1].len = num_write;
	msg[1].flags = I2C_MSG_STOP;

	return i2c_transfer(dev, msg, 2, addr);
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to remove start bit from an I2C transfer in zephyr</title><link>https://devzone.nordicsemi.com/thread/370446?ContentTypeID=1</link><pubDate>Wed, 01 Jun 2022 12:35:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e6d3d21a-514c-4bbd-a8a6-bf4710e70d28</guid><dc:creator>Einarh</dc:creator><description>&lt;p&gt;Hello!&lt;/p&gt;
&lt;p&gt;What function(s) are you currently using when you try to write? i2c_write? i2c_burst_write? i2c_transfer?&lt;/p&gt;
&lt;p&gt;It seems to me that what you want is similar to &lt;a href="https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/drivers/i2c.h#:~:text=static%20inline%20int-,i2c_burst_write,-(const%20struct"&gt;i2c_burst_write&lt;/a&gt;, but with a stop condition after sending the buffer address, you might have to set up your write manually using i2c_transfer since you want this irregular formatting.&lt;/p&gt;
&lt;p&gt;Perhaps something like this:&lt;/p&gt;
&lt;pre&gt;struct i2c_msg msg[2];&lt;br /&gt;&lt;br /&gt;msg[0].buf = &amp;amp;start_addr;&lt;br /&gt;msg[0].len = 1U;&lt;br /&gt;msg[0].flags = I2C_MSG_WRITE | I2C_MSG_STOP;&lt;br /&gt;&lt;br /&gt;msg[1].buf = (uint8_t *)buf;&lt;br /&gt;msg[1].len = num_bytes;&lt;br /&gt;msg[1].flags = I2C_MSG_WRITE | I2C_MSG_STOP;&lt;br /&gt;&lt;br /&gt;i2c_transfer(dev, msg, 2, dev_addr);&lt;/pre&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Einar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>