-
-
Save RozeDoyanawa/be631c3d8379c2cea0067e0dbbaf951b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void initI2C(void) { | |
char filename[20]; | |
snprintf(filename, 19, "/dev/i2c-%d", I2C_ADAPTER_NUMBER); | |
i2cFile = open(filename, O_RDWR); | |
if (i2cFile < 0) { | |
printf("I2C File < 0, %d", errno); | |
/* ERROR HANDLING; you can check errno to see what went wrong */ | |
exit(1); | |
} | |
if (ioctl(i2cFile, I2C_TENBIT, 0) < 0) { | |
printf("IOCTL I2C_TENBIT Failed, %d", errno); | |
/* ERROR HANDLING; you can check errno to see what went wrong */ | |
exit(1); | |
} | |
if (ioctl(i2cFile, I2C_PEC, 0) < 0) { | |
printf("IOCTL I2C_PEC Failed, %d", errno); | |
/* ERROR HANDLING; you can check errno to see what went wrong */ | |
exit(1); | |
} | |
} | |
int i2c_write(unsigned char slaveID, const void *buf, unsigned int length) { | |
unsigned int padding = length % 4; | |
unsigned int paddedLength = length + (padding > 0 ? 4 - padding : 0) + 1; | |
if (!setI2CSlaveAddress(i2cFile, slaveID)) { | |
return -1; | |
} | |
return write(i2cFile, buf, paddedLength) == paddedLength; | |
} | |
int i2c_read_inputs(ARPResult *arp, void *buf, unsigned char forceImport, unsigned char *changed) { | |
int ret; | |
I2CModuleCommand *cmd = (I2CModuleCommand *) buf; | |
cmd->msg.size = sizeof(I2CModuleCommand); | |
cmd->msg.type = MSG_READ; | |
ret = i2c_write(arp->i2cid, buf, cmd->msg.size); | |
if (ret) { | |
unsigned int io_length = arp->inputs + arp->outputs + 1; | |
unsigned int length = sizeof(I2CModuleCommand) + io_length; | |
ret = read(i2cFile, buf, length); | |
if (length == ret) { | |
I2CModuleRead *moduleRead = (I2CModuleRead *) buf; | |
//printf("Read module: ret=%d, io_length=%d, size=%d, type=%d\n", ret, io_length, moduleRead->base.size, moduleRead->base.type); | |
//printMemoryMap(buf, moduleRead->base.size); | |
if (moduleRead->changed || forceImport) { | |
memcpy((arp->memoryMap) + arp->outputs, buf + sizeof(I2CModuleRead), arp->inputs); | |
*changed = 1; | |
} | |
return 0; | |
} else { | |
printf("Read size missmatch (res = %d)\n", ret); | |
return 1; | |
} | |
} else { | |
printf("(read %d inputs) Command failed write (res = %d)\n", arp->i2cid, ret); | |
return 1; | |
} | |
} | |
int i2c_write_outputs(ARPResult *arp, void *buf) { | |
int ret; | |
I2CModuleCommand *cmd = (I2CModuleCommand *) buf; | |
cmd->msg.size = (unsigned char) (sizeof(I2CModuleCommand) + arp->outputs); | |
cmd->msg.type = MSG_WRITE; | |
memcpy((unsigned char *) buf + sizeof(I2CModuleCommand), arp->memoryMap, arp->outputs); | |
ret = i2c_write(arp->i2cid, buf, cmd->msg.size); | |
if (ret) { | |
return 0; | |
} else { | |
printf("(write %d outputs) Command failed write (res = %d)\n", arp->i2cid, ret); | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment