<?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 get command/data on I2C Slave, which has been send from I2C master.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/60707/how-to-get-command-data-on-i2c-slave-which-has-been-send-from-i2c-master</link><description>Hi All, 
 
 I am trying to implement I2C Slave functionality on NRF52832, So NRF52832 will act as a I2C slave. 
 So i found there is an example TWIS Slave and TWI Master Mode Drivers Example ( https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 29 Apr 2020 10:09:17 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/60707/how-to-get-command-data-on-i2c-slave-which-has-been-send-from-i2c-master" /><item><title>RE: How to get command/data on I2C Slave, which has been send from I2C master.</title><link>https://devzone.nordicsemi.com/thread/247282?ContentTypeID=1</link><pubDate>Wed, 29 Apr 2020 10:09:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e8857ae0-8a55-4193-bb76-a7551266ec97</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;That code seems to do nothing TWIS-related. I was thinking something in the line of this (not compiled/tested):&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define TWIS_MAX_LENGHT 20
uint8_t tx_buffer[TWIS_MAX_LENGHT] = {0};
uint8_t rx_buffer[TWIS_MAX_LENGHT] = {0};

uint8_t rx_length = 0;

uint8_t command1[4] = {0xA1, 0xA2, 0x01, 0x02};
uint8_t command2[2] = {0xA4, 0xA5};

uint8_t command1_response[16] = {0x00};
uint8_t command2_response[8] = {0x55};
  
static void twis_event_handler(nrf_drv_twis_evt_t const * const p_event)
{
	debug_printf(&amp;quot; \nevent handler&amp;quot;);
	switch (p_event-&amp;gt;type)
	{
	case TWIS_EVT_READ_REQ:
		if (p_event-&amp;gt;data.buf_req)
		{
		nrf_drv_twis_tx_prepare(&amp;amp;m_twis, tx_buffer, TWIS_MAX_LENGHT);
		}
		break;
	case TWIS_EVT_READ_DONE:
		if(p_event-&amp;gt;data.tx_amount == 4 &amp;amp;&amp;amp; memcmp(tx_buffer, command1, 4) == 0)
		{
			// Fill rx_buffer with correct response for command1
			memcpy(rx_buffer, command1_response, sizeof(command1_response));
			rx_length = sizeof(command1_response);
		}
		else if(p_event-&amp;gt;data.tx_amount == 2 &amp;amp;&amp;amp; memcmp(tx_buffer, command2, 2) == 0)
		{
			// Fill rx_buffer with correct response for command2
			memcpy(rx_buffer, command2_response, sizeof(command2_response));
			rx_length = sizeof(command2_response);
		}
		else
		{
			// Fill buffer with default char to tell master command is not supported/recognized
			memset(rx_buffer, 0xFF, TWIS_MAX_LENGHT);
			rx_length = TWIS_MAX_LENGHT
		}
		break;
	case TWIS_EVT_WRITE_REQ:
		if (p_event-&amp;gt;data.buf_req)
		{
			nrf_drv_twis_rx_prepare(&amp;amp;m_twis, rx_buffer, rx_length);
		}
		break;
	case TWIS_EVT_WRITE_DONE:
		// Clear buffer
		memset(rx_buffer, 0x00, TWIS_MAX_LENGHT);
		break;

	case TWIS_EVT_READ_ERROR:
	case TWIS_EVT_WRITE_ERROR:
	case TWIS_EVT_GENERAL_ERROR:
		m_error_flag = true;
		break;
		default:
		break;
	}
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to get command/data on I2C Slave, which has been send from I2C master.</title><link>https://devzone.nordicsemi.com/thread/246920?ContentTypeID=1</link><pubDate>Mon, 27 Apr 2020 16:04:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:88b362c2-1e51-4d2c-9110-fe3010e9ff90</guid><dc:creator>pradeep8892</dc:creator><description>&lt;p&gt;Hi Jorgen,&lt;/p&gt;
&lt;p&gt;May you please confirm, is the following implementation correct&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;uint8_t buffer[20] = {0};&lt;br /&gt;int i = 0;&lt;br /&gt;int j = 0;&lt;br /&gt; static void twis_event_handler(nrf_drv_twis_evt_t const * const p_event)&lt;br /&gt; {&lt;br /&gt; debug_printf(&amp;quot; \nevent handler&amp;quot;);&lt;br /&gt; switch (p_event-&amp;gt;type)&lt;br /&gt; {&lt;br /&gt; case TWIS_EVT_READ_REQ:&lt;br /&gt; if (p_event-&amp;gt;data.buf_req)&lt;br /&gt; {&lt;br /&gt; buffer[i] = p_event-&amp;gt;data.rx_amount;&lt;br /&gt; i++;&lt;br /&gt; }&lt;br /&gt; break;&lt;br /&gt; case TWIS_EVT_READ_DONE:&lt;br /&gt; i = 0;&lt;br /&gt; break;&lt;br /&gt; case TWIS_EVT_WRITE_REQ:&lt;br /&gt; if (p_event-&amp;gt;data.buf_req)&lt;br /&gt; {&lt;/p&gt;
&lt;p&gt;buffer[j] = p_event-&amp;gt;data.tx_amount;&lt;br /&gt; j++;&lt;br /&gt; break;&lt;br /&gt; }&lt;br /&gt; break;&lt;br /&gt; case TWIS_EVT_WRITE_DONE:&lt;br /&gt; j = 0;&lt;br /&gt; break;&lt;/p&gt;
&lt;p&gt;case TWIS_EVT_READ_ERROR:&lt;br /&gt; case TWIS_EVT_WRITE_ERROR:&lt;br /&gt; case TWIS_EVT_GENERAL_ERROR:&lt;br /&gt; m_error_flag = true;&lt;br /&gt; break;&lt;br /&gt; default:&lt;br /&gt; break;&lt;br /&gt; }&lt;br /&gt; }&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to get command/data on I2C Slave, which has been send from I2C master.</title><link>https://devzone.nordicsemi.com/thread/246880?ContentTypeID=1</link><pubDate>Mon, 27 Apr 2020 13:30:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:690d6d48-ff19-423f-af31-54b8c3a301fa</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;You define the receive buffers in the application, so it should be easy to keep track of what buffer is used, either&amp;nbsp;a global buffer for all transfers or separate buffers along with a variable indicating which buffer is currently in use. You can then access this buffer directly in the&amp;nbsp;&lt;span&gt;WRITE_DONE event. The valid bytes in the buffer is indicated by the&amp;nbsp;p_event-&amp;gt;data.rx_amount variable.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to get command/data on I2C Slave, which has been send from I2C master.</title><link>https://devzone.nordicsemi.com/thread/246864?ContentTypeID=1</link><pubDate>Mon, 27 Apr 2020 13:12:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:018b7800-7e8e-4cb1-8f8b-22e8f4fe856c</guid><dc:creator>pradeep8892</dc:creator><description>&lt;p&gt;Hi Jargen,&lt;/p&gt;
&lt;p&gt;I understood your point. But how can i receive and store the data. May you please share some code to fetch that data/command in&amp;nbsp;&lt;span&gt;TWIS_EVT_READ/WRITE_DONE, it would great help.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks &amp;amp; Regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Pradeep&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to get command/data on I2C Slave, which has been send from I2C master.</title><link>https://devzone.nordicsemi.com/thread/246837?ContentTypeID=1</link><pubDate>Mon, 27 Apr 2020 12:42:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:46d75fad-8560-42ca-8d7c-77796ba6487f</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;You will receive events in the&amp;nbsp;twis_event_handler both when a read/write is requested (TWIS_EVT_READ/WRITE_REQ) and when it is done (TWIS_EVT_READ/WRITE_DONE). When the TWI master is done writing the command, you can check the received command in the WRITE_DONE event and prepare the TX buffer with the appropriate response. When the TWI master starts the read operation, you can provide the buffer in the&amp;nbsp;READ_REQ event.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Jørgen&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>