Note that there are some explanatory texts on larger screens.

plurals
  1. POwrite() returns -1 when writing to I2C_SLAVE device
    primarykey
    data
    text
    <p>I've read through the <a href="http://www.mjmwired.net/kernel/Documentation/i2c/dev-interface" rel="nofollow">Linux kernel documents on i2c</a> and written a code to try to replicate the command <code>i2cset -y 0 0x60 0x05 0xff</code></p> <p>The code that I've written is here:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;linux/i2c.h&gt; #include &lt;linux/i2c-dev.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;sys/ioctl.h&gt; #include &lt;stdint.h&gt; #include &lt;string.h&gt; int main(){ int file; file = open("/dev/i2c-0", O_RDWR); if (file &lt; 0) { exit(1); } int addr = 0x60; if(ioctl(file, I2C_SLAVE, addr) &lt; 0){ exit(1); } __u8 reg = 0x05; __u8 res; __u8 data = 0xff; int written = write(file, &amp;reg, 1); printf("write returned %d\n", written); written = write(file, &amp;data, 1); printf("write returned %d\n", written); } </code></pre> <p>When I compile and run this code I get: write returned -1<br> write returned -1</p> <p>I've tried to follow exactly what the docs tell me, my understanding is that the address is set first with the call to <code>ioctl</code>, then I need to <code>write()</code> the register and then the data that I want sent to the register.</p> <p>I've also tried to use use SMbus, but I can't get my code to compile using this, it complains at the linking stage that it can't find the functions.</p> <p>Have I made any mistakes in this code? I'm a beginner to <code>i2c</code> and don't have a lot of experience with <code>c</code> either.</p> <p>EDIT: errno give the following message: <code>Operation not supported</code>. I am logged in as root on this machine though, so I don't think it can be a permissions thing, although I may be wrong.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload