Note that there are some explanatory texts on larger screens.

plurals
  1. POLinking smbus.c to smbus.h?
    primarykey
    data
    text
    <p>I'm creating an altitude ai at using <a href="http://www.raspberrypi.org/phpBB3/viewtopic.php?t=16968&amp;p=233656" rel="nofollow">this code</a>, and at the end of the post, it noted,</p> <blockquote> <p>To compile, make sure you downloaded the smbus.c and smbus.h files, changed smbus.c to correctly link to smbus.h, and added the DEFINE line mentioned above to smbus.c so the compiler doesn't complain.</p> </blockquote> <p>here is smbus.c,</p> <pre><code>#include &lt;errno.h&gt; #include &lt;i2c/smbus.h&gt; #include &lt;sys/ioctl.h&gt; #include &lt;linux/types.h&gt; #include &lt;linux/i2c.h&gt; #include &lt;linux/i2c-dev.h&gt; /* Compatibility defines */ #ifndef I2C_SMBUS_I2C_BLOCK_BROKEN #define I2C_SMBUS_I2C_BLOCK_BROKEN I2C_SMBUS_I2C_BLOCK_DATA #endif #ifndef I2C_FUNC_SMBUS_PEC #define I2C_FUNC_SMBUS_PEC I2C_FUNC_SMBUS_HWPEC_CALC #endif __s32 i2c_smbus_access(int file, char read_write, __u8 command, int size, union i2c_smbus_data *data) { struct i2c_smbus_ioctl_data args; __s32 err; args.read_write = read_write; args.command = command; args.size = size; args.data = data; err = ioctl(file, I2C_SMBUS, &amp;args); if (err == -1) err = -errno; return err; } __s32 i2c_smbus_write_quick(int file, __u8 value) { return i2c_smbus_access(file, value, 0, I2C_SMBUS_QUICK, NULL); } __s32 i2c_smbus_read_byte(int file) { union i2c_smbus_data data; int err; err = i2c_smbus_access(file, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &amp;data); if (err &lt; 0) return err; return 0x0FF &amp; data.byte; } __s32 i2c_smbus_write_byte(int file, __u8 value) { return i2c_smbus_access(file, I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); } __s32 i2c_smbus_read_byte_data(int file, __u8 command) { union i2c_smbus_data data; int err; err = i2c_smbus_access(file, I2C_SMBUS_READ, command, I2C_SMBUS_BYTE_DATA, &amp;data); if (err &lt; 0) return err; return 0x0FF &amp; data.byte; } __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value) { union i2c_smbus_data data; data.byte = value; return i2c_smbus_access(file, I2C_SMBUS_WRITE, command, I2C_SMBUS_BYTE_DATA, &amp;data); } __s32 i2c_smbus_read_word_data(int file, __u8 command) { union i2c_smbus_data data; int err; err = i2c_smbus_access(file, I2C_SMBUS_READ, command, I2C_SMBUS_WORD_DATA, &amp;data); if (err &lt; 0) return err; return 0x0FFFF &amp; data.word; } __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value) { union i2c_smbus_data data; data.word = value; return i2c_smbus_access(file, I2C_SMBUS_WRITE, command, I2C_SMBUS_WORD_DATA, &amp;data); } __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value) { union i2c_smbus_data data; data.word = value; if (i2c_smbus_access(file, I2C_SMBUS_WRITE, command, I2C_SMBUS_PROC_CALL, &amp;data)) return -1; else return 0x0FFFF &amp; data.word; } /* Returns the number of read bytes */ __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values) { union i2c_smbus_data data; int i, err; err = i2c_smbus_access(file, I2C_SMBUS_READ, command, I2C_SMBUS_BLOCK_DATA, &amp;data); if (err &lt; 0) return err; for (i = 1; i &lt;= data.block[0]; i++) values[i-1] = data.block[i]; return data.block[0]; } __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, const __u8 *values) { union i2c_smbus_data data; int i; if (length &gt; I2C_SMBUS_BLOCK_MAX) length = I2C_SMBUS_BLOCK_MAX; for (i = 1; i &lt;= length; i++) data.block[i] = values[i-1]; data.block[0] = length; return i2c_smbus_access(file, I2C_SMBUS_WRITE, command, I2C_SMBUS_BLOCK_DATA, &amp;data); } /* Returns the number of read bytes */ /* Until kernel 2.6.22, the length is hardcoded to 32 bytes. If you ask for less than 32 bytes, your code will only work with kernels 2.6.23 and later. */ __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command, __u8 length, __u8 *values) { union i2c_smbus_data data; int i, err; if (length &gt; I2C_SMBUS_BLOCK_MAX) length = I2C_SMBUS_BLOCK_MAX; data.block[0] = length; err = i2c_smbus_access(file, I2C_SMBUS_READ, command, length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN : I2C_SMBUS_I2C_BLOCK_DATA, &amp;data); if (err &lt; 0) return err; for (i = 1; i &lt;= data.block[0]; i++) values[i-1] = data.block[i]; return data.block[0]; } __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command, __u8 length, const __u8 *values) { union i2c_smbus_data data; int i; if (length &gt; I2C_SMBUS_BLOCK_MAX) length = I2C_SMBUS_BLOCK_MAX; for (i = 1; i &lt;= length; i++) data.block[i] = values[i-1]; data.block[0] = length; return i2c_smbus_access(file, I2C_SMBUS_WRITE, command, I2C_SMBUS_I2C_BLOCK_BROKEN, &amp;data); } /* Returns the number of read bytes */ __s32 i2c_smbus_block_process_call(int file, __u8 command, __u8 length, __u8 *values) { union i2c_smbus_data data; int i, err; if (length &gt; I2C_SMBUS_BLOCK_MAX) length = I2C_SMBUS_BLOCK_MAX; for (i = 1; i &lt;= length; i++) data.block[i] = values[i-1]; data.block[0] = length; err = i2c_smbus_access(file, I2C_SMBUS_WRITE, command, I2C_SMBUS_BLOCK_PROC_CALL, &amp;data); if (err &lt; 0) return err; for (i = 1; i &lt;= data.block[0]; i++) values[i-1] = data.block[i]; return data.block[0]; } </code></pre> <p>and here is smbus.h,</p> <pre><code>#ifndef LIB_I2C_SMBUS_H #define LIB_I2C_SMBUS_H #include &lt;linux/types.h&gt; #include &lt;linux/i2c.h&gt; extern __s32 i2c_smbus_access(int file, char read_write, __u8 command, int size, union i2c_smbus_data *data); extern __s32 i2c_smbus_write_quick(int file, __u8 value); extern __s32 i2c_smbus_read_byte(int file); extern __s32 i2c_smbus_write_byte(int file, __u8 value); extern __s32 i2c_smbus_read_byte_data(int file, __u8 command); extern __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value); extern __s32 i2c_smbus_read_word_data(int file, __u8 command); extern __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value); extern __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value); /* Returns the number of read bytes */ extern __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values); extern __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, const __u8 *values); /* Returns the number of read bytes */ /* Until kernel 2.6.22, the length is hardcoded to 32 bytes. If you ask for less than 32 bytes, your code will only work with kernels 2.6.23 and later. */ extern __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command, __u8 length, __u8 *values); extern __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command, __u8 length, const __u8 *values); /* Returns the number of read bytes */ extern __s32 i2c_smbus_block_process_call(int file, __u8 command, __u8 length, __u8 *values); #endif /* LIB_I2C_SMBUS_H */ </code></pre> <p>finally, here is the Define command mentioned earlier,</p> <blockquote> <p>'#define NULL 0</p> </blockquote> <p>With this information, how do I go about linking the two files and defining null? </p>
    singulars
    1. This table or related slice is empty.
    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.
 

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