<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>MQX Software SolutionsのトピックRe: I2C Slave sample code</title>
    <link>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165491#M1546</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Welcome to the club. If you speed about the thread "&lt;A __default_attr="72178" class="jive_macro jive_macro_thread default_title" href="https://community.freescale.com/thread/72178" jivemacro="thread" title="how to implement a SLAVE-mode MQX i2c driver??"&gt;how to implement a SLAVE-mode MQX i2c driver??&lt;/A&gt;﻿", you're right, it is old (started one year ago), but it is still alive and we still searching/waiting for the slave I2C sample code.&lt;/P&gt;&lt;P&gt;There's some code in this thread but it's a very basic usage of slave mode I2C that is not enough for a real world usage.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 08 Sep 2011 12:57:10 GMT</pubDate>
    <dc:creator>trailman</dc:creator>
    <dc:date>2011-09-08T12:57:10Z</dc:date>
    <item>
      <title>I2C Slave sample code</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165490#M1545</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there some sample slave I2C code for MQX available?&amp;nbsp; There are old threads that talk about it but perhaps those threads are stale?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Sep 2011 05:30:03 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165490#M1545</guid>
      <dc:creator>intevac_coder</dc:creator>
      <dc:date>2011-09-08T05:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: I2C Slave sample code</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165491#M1546</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Welcome to the club. If you speed about the thread "&lt;A __default_attr="72178" class="jive_macro jive_macro_thread default_title" href="https://community.freescale.com/thread/72178" jivemacro="thread" title="how to implement a SLAVE-mode MQX i2c driver??"&gt;how to implement a SLAVE-mode MQX i2c driver??&lt;/A&gt;﻿", you're right, it is old (started one year ago), but it is still alive and we still searching/waiting for the slave I2C sample code.&lt;/P&gt;&lt;P&gt;There's some code in this thread but it's a very basic usage of slave mode I2C that is not enough for a real world usage.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Sep 2011 12:57:10 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165491#M1546</guid>
      <dc:creator>trailman</dc:creator>
      <dc:date>2011-09-08T12:57:10Z</dc:date>
    </item>
    <item>
      <title>Re: I2C Slave sample code</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165492#M1547</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I tried to use the code in that example but it doesn't work on the tower system on I2C0.&amp;nbsp; No luck.&amp;nbsp; I'm going to try it on I2C1 after I rewire things.&amp;nbsp; Hopefully I'll come up with something &amp;amp; I'll let you know.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Sep 2011 23:05:58 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165492#M1547</guid>
      <dc:creator>intevac_coder</dc:creator>
      <dc:date>2011-09-08T23:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: I2C Slave sample code</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165493#M1548</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've got the I2C slave driver "half" working.&amp;nbsp; The unit receives bytes on the I2C0 port.&amp;nbsp; However, there is what I call a lag on the data coming in.&amp;nbsp; It seems as though there has to be a junk byte on the end of reading as a slave in order for things to work correctly.&amp;nbsp; When I want to&amp;nbsp;receive 2 bytes, I have to transmit 3 bytes.&amp;nbsp; Interestingly, the&amp;nbsp;3rd byte is always dumped and never shows up.&amp;nbsp;&amp;nbsp;Writing as a slave to the master works just fine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The tough part is that reading as a master in IO_IOCTL_I2C_SET_MASTER_MODE works just fine.&amp;nbsp; The number of bytes matches and everything syncs up.&amp;nbsp; But in slave, as far as I've gotten, there has to be the junk byte on the end from the master to the K40 as a slave.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Here is my code for i2c slave:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008000"&gt;// Globals&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;FILE_PTR &lt;FONT size="2"&gt;fd_i2c_slave;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" size="2"&gt;#define I2C0_SLAVE_ADDR 0x50&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" size="2"&gt;void init_function(void)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000" size="2"&gt;{&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008000"&gt;// Open the I2C driver, and assign a I2C device handler&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;fd_i2c_slave = fopen (&lt;FONT size="2"&gt;&lt;FONT color="#2A00FF" size="2"&gt;&lt;FONT color="#2A00FF" size="2"&gt;"i2c0:"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;, NULL);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;FONT color="#7F0055" size="2"&gt;&lt;FONT color="#7F0055" size="2"&gt;if&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;(fd_i2c_slave == NULL)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;{&lt;/P&gt;&lt;P align="left"&gt;printf (&lt;FONT size="2"&gt;&lt;FONT color="#2A00FF" size="2"&gt;&lt;FONT color="#2A00FF" size="2"&gt;"Failed to open the I2C0 slave driver!\n"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;}&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT color="#3F7F5F" size="2"&gt;&lt;FONT color="#3F7F5F" size="2"&gt;// Set I2C into slave mode &amp;amp; set the slave address&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;ioctl (fd_i2c_slave, IO_IOCTL_I2C_SET_SLAVE_MODE, NULL) ;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;}&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;// Receive&amp;nbsp;2 bytes as a slave&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;void receive_function(void)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;{&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;uint_8 &lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;recv_buffer[1] = {0};&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;uint_32 result;&lt;/P&gt;&lt;P align="left"&gt;uint_32 param;&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P align="left"&gt;&lt;FONT color="#008000"&gt;// Set the slave address&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;param = I2C0_SLAVE_ADDR;&lt;/P&gt;&lt;P align="left"&gt;&amp;nbsp;result = ioctl(fd_i2c_slave, IO_IOCTL_I2C_SET_STATION_ADDRESS, &amp;amp;param);&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;FONT color="#7F0055" size="2"&gt;&lt;FONT color="#7F0055" size="2"&gt;if&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;(result != MQX_OK)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;{&lt;/P&gt;&lt;P align="left"&gt;printf(&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT color="#2A00FF" size="2"&gt;&lt;FONT color="#2A00FF" size="2"&gt;"I2C0 slave mode address ERROR\nCP&amp;gt;"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT color="#3F7F5F" size="2"&gt;&lt;FONT color="#3F7F5F" size="2"&gt;//Set how many bytes to read&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;result = ioctl (fd_i2c_slave, IO_IOCTL_I2C_SET_RX_REQUEST, &amp;amp;bytestoread);&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT color="#3F7F5F" size="2"&gt;&lt;FONT color="#3F7F5F" size="2"&gt;//buffer to read data into, size of unit, # of bytes to read, I2C device handler&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;result = fread (&amp;amp;recv_buffer, 1, bytestoread, fd_i2c_slave);&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT color="#3F7F5F" size="2"&gt;&lt;FONT color="#3F7F5F" size="2"&gt;//Wait for completion&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;fflush (fd_i2c_slave);&lt;/P&gt;&lt;P&gt;printf(&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT color="#2A00FF" size="2"&gt;&lt;FONT color="#2A00FF" size="2"&gt;"I2C0 received 0:%x 1:%x "&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2"&gt;, recv_buffer[0], recv_buffer[1]);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;}&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 Sep 2011 03:32:31 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165493#M1548</guid>
      <dc:creator>intevac_coder</dc:creator>
      <dc:date>2011-09-13T03:32:31Z</dc:date>
    </item>
    <item>
      <title>Re: I2C Slave sample code</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165494#M1549</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks very much for the time spent to make it work !&lt;/P&gt;&lt;P&gt;I would like to help on the topic, but the board and Codewarrior (with its license) on which I work is at work and I can not currently work on it (i'm on some other projects).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However I have some remarks on your code :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;uint_8 &lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;recv_buffer[1] = {0};&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;﻿&amp;nbsp; defines a buffer of only one byte, with first byte explicitely inited to 0. For your test you need at least 2 bytes, so to be safe I think you should use : uint_8 &lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;recv_buffer[3] = {0,0,0};&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;﻿﻿&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I'm right, you set bytestoread﻿ to 2 and then "result = fread (&amp;amp;recv_buffer, 1, bytestoread, fd_i2c_slave);﻿" returns when 3 bytes have been sent by master. In this case, what is the value in "result" ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 17 Sep 2011 22:35:57 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165494#M1549</guid>
      <dc:creator>trailman</dc:creator>
      <dc:date>2011-09-17T22:35:57Z</dc:date>
    </item>
    <item>
      <title>Re: I2C Slave sample code</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165495#M1550</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You're right - I posted incomplete code.&amp;nbsp; Sorry!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found the file i2c_pol_ki2c.c located in C:\Program Files\Freescale\Freescale MQX 3.7\mqx\source\io\i2c\polled\&lt;/P&gt;&lt;P&gt;has a bug that needs fixing in 2 locations.&amp;nbsp; Attached is my updated version of the code with fixes.&amp;nbsp; The 2 fixes are outlined below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;About line 818 near the end of _ki2c_polled_rx_tx() in the slave receive section, the i counter is decremented to account for the I2C address coming in.&amp;nbsp; However, this throws off the to be received byte count by 1, causing the function to always be lagging.&amp;nbsp; Here's the correction to the polled Freescale driver:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;io_info_ptr-&amp;gt;STATE = I2C_STATE_ADDRESSED_AS_SLAVE_RX;&lt;/P&gt;&lt;P&gt;io_info_ptr-&amp;gt;ONTHEWAY = TRUE;&lt;/P&gt;&lt;P&gt;tmp = i2c_ptr-&amp;gt;D; /* dummy read to release bus */&lt;/P&gt;&lt;P&gt;// WAM! BUG - DECREMENTING i HERE CAUSES EXTRA BYTE TO ATTEMPT TO BE READ ON I2C SLAVE RECEIVE&lt;/P&gt;&lt;P&gt;// i RESTARTS AT 0. IF LENGTH = 2, THERE ARE 3 BYTE READS INSTEAD OF 2 BYTE READS.&lt;/P&gt;&lt;P&gt;// SINGLE LINE BELOW COMMENTED OUT.&lt;/P&gt;&lt;P&gt;//i--;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Making this adjustment requires an adjustment on about line 869 to be made.&amp;nbsp; The comparison isn't correct any more, either:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;// WAM! IN ORDER FOR ABOVE CHANGE TO WORK, THE COMPARE MUST NOW BE &amp;lt;= INSTEAD OF &amp;lt;&lt;/P&gt;&lt;P&gt;// if(i &amp;lt; length)&lt;/P&gt;&lt;P&gt;if (i &amp;lt;= length)&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; i2c_ptr-&amp;gt;S |= I2C_S_IICIF_MASK; /* clear IICIF */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the corrected application code.&amp;nbsp; The below example acts like a slave device reciving a register and&amp;nbsp;making 2 different&amp;nbsp;replies.&amp;nbsp; It runs in polling mode.&amp;nbsp; Unfortunately this consumes the CPU and effectively blocks tasks with a lower priority.&amp;nbsp; After doing some experimenting, I think the slave interrupt driver has a bug as well.&amp;nbsp; &lt;IMG alt=":smileysad:" class="emoticon emoticon-smileysad" id="smileysad" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-sad.gif" title="Smiley Sad" /&gt;&amp;nbsp; Maybe I can get a consulting fee?&amp;nbsp; &lt;IMG alt=":smileyindifferent:" class="emoticon emoticon-smileyindifferent" id="smileyindifferent" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-indifferent.gif" title="Smiley Indifferent" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Exactly the code I have to make registered responses as slave work.&amp;nbsp; The semaphore was created in main.c to manage access to the I2C0 device.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;P&gt;void task_i2c_slave(uint_32 initial_data)&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; I2C_STATISTICS_STRUCT stats;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; initialize_i2c0_slave();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; while(TRUE)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i2c0_reg_reply();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; _time_delay(DELAY_I2C0SLAVE_TASK);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;void initialize_i2c0_slave(void)&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; fd_i2c_slave = fopen ("i2c0:", NULL); // Polled version&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if (fd_i2c_slave == NULL)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf ("Failed to open the I2C0 slave driver!\n");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _time_delay (200L);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _mqx_exit (1L);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; // Set I2C into slave mode &amp;amp; set the slave address&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if( I2C_OK == ioctl (fd_i2c_slave, IO_IOCTL_I2C_SET_SLAVE_MODE, NULL) )&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; { printf("I2C0 slave mode GOOD\n"); }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; else&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; { printf("I2C0 slave mode ERROR\n"); }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#define REGRD0 0X01&lt;/P&gt;&lt;P&gt;#define REGRD1 0x02&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;void i2c0_reg_reply(void)&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;static uint_8 buffer[2] = {0x92, 0x15}; // Data to send to master request&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; uint_32 result;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; uint_32 param;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; uint_8 recv_buffer[2] = {0, 0};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; uint_8 send1[4] = {0xca, 0x19, 0x83, 0xf5}; // Pretend register data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; uint_8 send2[5] = {0x83, 0x02, 0xb6, 0xb7, 0x2b}; // pretend register data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; uint_8 bytecount = 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; uint_8 switchval;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; result = _lwsem_wait(&amp;amp;lwsem_i2c0); // Capture the semaphore&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; // This if statement must be in here, otherwise the I2C doesn't ACK correctly&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if(result != MQX_OK)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; { printf("\ni2c0_reg_reply lwsem_wait N2 failed 0x%X\n", result); }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; param = I2C0_SLAVE_ADDR;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; //result = ioctl(fd_i2c_slave, IO_IOCTL_I2C_SET_STATION_ADDRESS, &amp;amp;param);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; result = ioctl(fd_i2c_slave, IO_IOCTL_I2C_SET_DESTINATION_ADDRESS, &amp;amp;param);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if(result != MQX_OK)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; { printf("I2C0 slave mode address ERROR\nCP&amp;gt;"); }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; param = I2C0_SLAVE_ADDR;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; bytecount = 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; result = ioctl(fd_i2c_slave, IO_IOCTL_I2C_SET_STATION_ADDRESS, &amp;amp;param);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; ioctl (fd_i2c_slave, IO_IOCTL_I2C_SET_RX_REQUEST, &amp;amp;bytecount);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; result = fread (recv_buffer, 1, bytecount, fd_i2c_slave);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; ioctl(fd_i2c_slave, IO_IOCTL_I2C_STOP, NULL); // Get driver to original state&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; // Take the received register value and act on it&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; switchval = recv_buffer[0]; // Capture the desired register address&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; switch(switchval)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; case REGRD0:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;bytecount = 4;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; param = I2C0_SLAVE_ADDR;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = ioctl(fd_i2c_slave, IO_IOCTL_I2C_SET_STATION_ADDRESS, &amp;amp;param);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fwrite (&amp;amp;send1, 1, bytecount, fd_i2c_slave);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ioctl (fd_i2c_slave, IO_IOCTL_I2C_STOP, NULL);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; case REGRD1:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; bytecount = 5;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; param = I2C0_SLAVE_ADDR;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = ioctl(fd_i2c_slave, IO_IOCTL_I2C_SET_STATION_ADDRESS, &amp;amp;param);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fwrite (&amp;amp;send2, 1, bytecount, fd_i2c_slave);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ioctl (fd_i2c_slave, IO_IOCTL_I2C_STOP, NULL);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; break;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; default:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; printf("\n%s\n%s", INVALID_I2C_REGISTER, COMMAND_PROMPT);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; result = _lwsem_post(&amp;amp;lwsem_i2c0); // Release the semaphore&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Sep 2011 03:00:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165495#M1550</guid>
      <dc:creator>intevac_coder</dc:creator>
      <dc:date>2011-09-20T03:00:09Z</dc:date>
    </item>
    <item>
      <title>Re: I2C Slave sample code</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165496#M1551</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Wow ! Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the first time I see a functional slave mode I2C example for MQX (with read+write as slave).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In addition to that, now I know that if I need to make it work with the interrupt driven I2C driver (the one I use for my MQX application), I'll have to spend some time to debug the MQX driver (this was not expected).&lt;/P&gt;&lt;P&gt;But as I will probably need a both master and slave interface, I guess it won't be easy ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 22 Sep 2011 17:26:04 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/I2C-Slave-sample-code/m-p/165496#M1551</guid>
      <dc:creator>trailman</dc:creator>
      <dc:date>2011-09-22T17:26:04Z</dc:date>
    </item>
  </channel>
</rss>

