Commit ca4702ae authored by Ivan Tyagov's avatar Ivan Tyagov

OPC-Ua server should support more than one I2C devices (MOD-IOs). First steps for this.

parent 5bf65e50
...@@ -20,16 +20,23 @@ ...@@ -20,16 +20,23 @@
// global relay state // global relay state
uint8_t myrelay = 0; uint8_t myrelay = 0;
static int setRelayState(int command) { // the default addresses of MOD-IOs
const int MOD_IO_0_ADDR = 0x58;
const int MOD_IO_1_ADDR = 0x59;
// the block device at host machine
static const char I2C_BLOCK_DEVICE_NAME[] = "/dev/i2c-1";
static int setRelayState(int command, int i2c_addr) {
/* /*
* Set relays' state over I2C * Set relays' state over I2C
*/ */
int file; int file;
int adapter_nr = 1; /* probably dynamically determined */
char filename[20]; char filename[20];
// step 1: open device // step 1: open device
snprintf(filename, 19, "/dev/i2c-%d", adapter_nr); snprintf(filename, 19, I2C_BLOCK_DEVICE_NAME);
file = open(filename, O_RDWR); file = open(filename, O_RDWR);
if (file < 0) { if (file < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */ /* ERROR HANDLING; you can check errno to see what went wrong */
...@@ -38,8 +45,7 @@ static int setRelayState(int command) { ...@@ -38,8 +45,7 @@ static int setRelayState(int command) {
} }
// 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, i2c_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.");
exit(1); exit(1);
...@@ -144,11 +150,11 @@ static void afterWriteTime0(UA_Server *server, ...@@ -144,11 +150,11 @@ static void afterWriteTime0(UA_Server *server,
UA_Int32 hrValue = *(UA_Int32 *)data->value.data; UA_Int32 hrValue = *(UA_Int32 *)data->value.data;
if (hrValue > 0){ if (hrValue > 0){
myrelay |= 1UL << 0; // relay0 myrelay |= 1UL << 0; // relay0
setRelayState(myrelay); setRelayState(myrelay, MOD_IO_0_ADDR);
} }
else { else {
myrelay &= ~(1UL << 0); // relay0 myrelay &= ~(1UL << 0); // relay0
setRelayState(myrelay); setRelayState(myrelay, MOD_IO_0_ADDR);
} }
} }
} }
...@@ -161,11 +167,11 @@ static void afterWriteTime1(UA_Server *server, ...@@ -161,11 +167,11 @@ static void afterWriteTime1(UA_Server *server,
UA_Int32 hrValue = *(UA_Int32 *)data->value.data; UA_Int32 hrValue = *(UA_Int32 *)data->value.data;
if (hrValue > 0){ if (hrValue > 0){
myrelay |= 1UL << 1; myrelay |= 1UL << 1;
setRelayState(myrelay); setRelayState(myrelay, MOD_IO_0_ADDR);
} }
else { else {
myrelay &= ~(1UL << 1); myrelay &= ~(1UL << 1);
setRelayState(myrelay); setRelayState(myrelay, MOD_IO_0_ADDR);
} }
} }
} }
...@@ -178,11 +184,11 @@ static void afterWriteTime2(UA_Server *server, ...@@ -178,11 +184,11 @@ static void afterWriteTime2(UA_Server *server,
UA_Int32 hrValue = *(UA_Int32 *)data->value.data; UA_Int32 hrValue = *(UA_Int32 *)data->value.data;
if (hrValue > 0){ if (hrValue > 0){
myrelay |= 1UL << 2; myrelay |= 1UL << 2;
setRelayState(myrelay); setRelayState(myrelay, MOD_IO_0_ADDR);
} }
else { else {
myrelay &= ~(1UL << 2); myrelay &= ~(1UL << 2);
setRelayState(myrelay); setRelayState(myrelay, MOD_IO_0_ADDR);
} }
} }
} }
...@@ -195,11 +201,11 @@ static void afterWriteTime3(UA_Server *server, ...@@ -195,11 +201,11 @@ static void afterWriteTime3(UA_Server *server,
UA_Int32 hrValue = *(UA_Int32 *)data->value.data; UA_Int32 hrValue = *(UA_Int32 *)data->value.data;
if (hrValue > 0){ if (hrValue > 0){
myrelay |= 1UL << 3; myrelay |= 1UL << 3;
setRelayState(myrelay); setRelayState(myrelay, MOD_IO_0_ADDR);
} }
else { else {
myrelay &= ~(1UL << 3); myrelay &= ~(1UL << 3);
setRelayState(myrelay); setRelayState(myrelay, MOD_IO_0_ADDR);
} }
} }
} }
...@@ -279,7 +285,7 @@ static void stopHandler(int sign) { ...@@ -279,7 +285,7 @@ static void stopHandler(int sign) {
int main(void) { int main(void) {
// set all relays to OFF at startup // set all relays to OFF at startup
setRelayState(0x00); setRelayState(0x00, MOD_IO_0_ADDR);
signal(SIGINT, stopHandler); signal(SIGINT, stopHandler);
signal(SIGTERM, stopHandler); signal(SIGTERM, stopHandler);
...@@ -298,7 +304,7 @@ int main(void) { ...@@ -298,7 +304,7 @@ int main(void) {
UA_Server_delete(server); UA_Server_delete(server);
// set all relays to OFF at startup // set all relays to OFF at startup
setRelayState(0x00); setRelayState(0x00, MOD_IO_0_ADDR);
return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE; return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
} }
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