i2c Read Segmentation Fault
-
Hello,
I'm using the Omega2s+ firmware: 0.3.2 b220 and am trying to interface with i2c to various devices. I am programming in C++. I've used the (libonioni2c.so) driver before for various writing operations (led drivers), but now want to use it for an ADC, which, oddly is the first time I've needed the 'read' functionality. When I run any code with the i2c_read function, the execution ends with a segmentation fault. I've recreated this on an Omega2+ as well.
I'm assuming this is the result of the Omega i2c driver trying to access some memory after this routine is complete? and it no longer existing? I've skimmed this example code down to the produce the error. This program runs all the way through, and after the very last line, return 0, produces the fault.Why am I getting a segmentation fault? Do I need to close/kill/delete/freeup some bit of memory? Any thoughts?? Thank you!!!
#include <onion-i2c.h> #include <chrono> #include <thread> #define I2C_DEF_ADPTR 0 // Unique to Omega #include <iostream> using std::cout; // For convienience using std::endl; int main() { // register stuff cout << "Beginning..." << endl; int _address = 0x48; int status; uint8_t readValue[2]; cout <<"About to start read..." << endl; status = i2c_read( I2C_DEF_ADPTR, _address, 0x01, readValue, 2); cout << "Finished read... "; cout << "End of Program." << endl; std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // << added this just in case i2c was slow to end process return 0; }
-
readValue needs to be passed as a reference, since the contents will be modified by the i2c_read function. That's likely causing the segmentation fault.
Or change readValue to a point and allocate two bytes before passing it to i2c_read
See the documentation for the i2c_read function for more details: http://docs.onion.io/omega2-docs/i2c-c-library.html#i2c-c-read-multiple-bytes