Commit 57c14e10 authored by Ivan Tyagov's avatar Ivan Tyagov

Extend example.

parent 3ed110ea
// Example controling MOD-IO's relays' state /*
// Based on https://www.kernel.org/doc/Documentation/i2c/dev-interface Example controling MOD-IO's relays' state
Based on https://www.kernel.org/doc/Documentation/i2c/dev-interface
To compile:
root@olimex-rt:~/osie/coupler/opc-ua-server# gcc ic2_set_relays_example.c -o ic2_set_relays_example
root@olimex-rt:~/osie/coupler/opc-ua-server# ./ic2_set_relays_example
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
...@@ -12,6 +19,7 @@ int main() ...@@ -12,6 +19,7 @@ int main()
{ {
int file; int file;
int adapter_nr = 1; /* probably dynamically determined */ int adapter_nr = 1; /* probably dynamically determined */
int addr = 0x58; /* The I2C slave's address */
char filename[20]; char filename[20];
// step 1: open device // step 1: open device
...@@ -24,7 +32,6 @@ int main() ...@@ -24,7 +32,6 @@ int main()
} }
// step 2: address the slave by its address // step 2: address the slave by its address
int addr = 0x58; /* The I2C address */
if (ioctl(file, I2C_SLAVE, addr) < 0) { if (ioctl(file, I2C_SLAVE, addr) < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */ /* ERROR HANDLING; you can check errno to see what went wrong */
printf("Error addressing i2c slave."); printf("Error addressing i2c slave.");
...@@ -36,7 +43,8 @@ int main() ...@@ -36,7 +43,8 @@ int main()
__s32 res; __s32 res;
char buf[10]; char buf[10];
buf[0] = reg; buf[0] = reg;
buf[1] = 0x05; //0x00 -all off, 0x0F - all 4 on buf[1] = 0x07; // the most right bits represent 4 relays' state: for example 00001111 - means all ON
// send as a hexadecimal number
buf[2] = 0x65; // seems irrelevant the value buf[2] = 0x65; // seems irrelevant the value
if (write(file, buf, 3) != 3) { if (write(file, buf, 3) != 3) {
/* ERROR HANDLING: i2c transaction failed */ /* ERROR HANDLING: i2c transaction failed */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment