Hello,
I am using the RPI to query an I2C device of my own making using C++, and it all works well, until the I2C worker device is turned off, or communications is severed.
I am using the below code to initialize the i2c:
Where I set I2C timeout and number of retries, before querying the board with the code below:
What happens is, I am using a Timer to query the I2C device at 10hz. And if the device is turned off, the ioctl blocks the Timer callback, meaning my program hangs.
Are there any known best practices for handling i2c errors/timeouts within the RPI? I have the same code written using python with the smbus library, and it does timeout gracefully, so I know it can be done.
Best Regards,
Can
I am using the RPI to query an I2C device of my own making using C++, and it all works well, until the I2C worker device is turned off, or communications is severed.
I am using the below code to initialize the i2c:
Code:
if((fd = open(i2c_filename.c_str(), O_RDWR)) < 0) { RCLCPP_ERROR(this->get_logger(), "Failed to open the I2C bus. Error code: %d", fd); exit(0); } if(ioctl(fd, I2C_TIMEOUT, 4) < 0) {RCLCPP_ERROR(this->get_logger(), "Failed to set I2C_TIMEOUT"); } if(ioctl(fd, I2C_RETRIES, 2) < 0) {RCLCPP_ERROR(this->get_logger(), "Failed to set I2C_RETRY"); } if((rv = ioctl(fd, I2C_SLAVE, 0x3C)) < 0) { RCLCPP_ERROR(this->get_logger(), "Failed to acquire bus access and/or talk to worker. Error code: %d", fd); exit(0); }
Code:
ioctl_data.read_write = read_write; ioctl_data.command = reg; ioctl_data.size = I2C_SMBUS_I2C_BLOCK_DATA; ioctl_data.data = &smbus_data; rv = ioctl(fd, I2C_SMBUS, &ioctl_data);
Are there any known best practices for handling i2c errors/timeouts within the RPI? I have the same code written using python with the smbus library, and it does timeout gracefully, so I know it can be done.
Best Regards,
Can
Statistics: Posted by altineller — Sun Feb 04, 2024 11:54 am — Replies 0 — Views 22