40 bool success = Wire.begin(pin_sda, pin_scl);
43 Wire.begin(pin_sda, pin_scl);
46 Wire.setClock(400000);
69bool I2CReadBytes(
byte address,
byte reg,
byte *destination,
int num_bytes) {
70 if (NULL == destination) {
73 Wire.beginTransmission(address);
74 if (!Wire.write(reg)) {
75 Wire.endTransmission(
true);
78 Wire.endTransmission(
false);
79 if (num_bytes == Wire.requestFrom(address, (uint8_t)num_bytes)) {
81 for (
int i=0; i < num_bytes; i++) {
82 return_value = Wire.read();
83 if (return_value >= 0) {
84 destination[i] = (byte)return_value;
103 Wire.beginTransmission(address);
106 if (I2C_ERROR_OK == Wire.endTransmission()) {
121 unsigned int num_bytes) {
122 Wire.beginTransmission(address);
124 if (num_bytes != Wire.write(value, num_bytes)) {
126 Wire.endTransmission();
129 if( I2C_ERROR_OK == Wire.endTransmission() ){
148 if (pRegWriteList == NULL) {
149 return SENSOR_ERROR_BAD_ADDRESS;
159 while (pCmd->writeTo != 0xFFFF) {
164 if (!
I2CWriteByte(peripheralAddress, pCmd->writeTo, pCmd->value)) {
165 return SENSOR_ERROR_WRITE;
170 return SENSOR_ERROR_NONE;
178 uint16_t peripheralAddress,
180 uint8_t *pOutBuffer) {
185 if (pReadList == NULL || pOutBuffer == NULL) {
186 return SENSOR_ERROR_BAD_ADDRESS;
192 for (pBuf = pOutBuffer; pCmd->numBytes != 0; pCmd++) {
195 if (!
I2CReadBytes(peripheralAddress, pCmd->readFrom, pBuf,
197 return SENSOR_ERROR_READ;
199 pBuf += pCmd->numBytes;
201 return SENSOR_ERROR_NONE;
205 uint16_t peripheralAddress,
208 uint8_t *pOutBuffer) {
210 if(
I2CReadBytes((
byte)peripheralAddress, (
byte)offset, pOutBuffer,
212 {
return SENSOR_ERROR_NONE;
215 return SENSOR_ERROR_READ;
The driver_sensors_types.h file contains sensor state structs and error definitions.
bool I2CReadBytes(byte address, byte reg, byte *destination, int num_bytes)
Read num_bytes bytes from address starting at register. Assumes device auto-increments the register....
bool I2CWriteBytes(byte address, byte reg, const byte *value, unsigned int num_bytes)
Write multiple bytes starting at register to I2C address Assumes device auto-increments the I2C regis...
int8_t Sensor_I2C_Write_List(registerDeviceInfo_t *devInfo, uint16_t peripheralAddress, const registerwritelist_t *pRegWriteList)
Write register data to a sensor.
bool I2CReadByte(byte address, byte reg, byte *destination)
Read single byte from address and place in destination Returns true if successful,...
bool I2CWriteByte(byte address, byte reg, byte value)
Write single byte to register at address Returns true if successful, false if error.
bool I2CInitialize(int pin_sda, int pin_scl)
Initialize the I2C system at max clock rate supported by sensors. pin_sda and pin_scl indicate the pi...
int32_t Sensor_I2C_Read(registerDeviceInfo_t *devInfo, uint16_t peripheralAddress, const registerReadlist_t *pReadList, uint8_t *pOutBuffer)
Read register data from a sensor.
The hal_i2c.h file declares low-level interface functions for reading and writing sensor registers us...
This structure defines the device specific info required by register I/O.
This structure defines the Read command List.
This structure defines the Write command List.