DevLab_ICM20948 1.0.2 ICM-20948 9-Axis Motion Sensor Driver

Loading...
Searching...
No Matches
7Semi_I2C_Interface.h
Go to the documentation of this file.
1#pragma once
2#include "7Semi_Interface.h"
3#include <Wire.h>
4
5 class I2C_Interface : public Interface_7Semi
6{
7 public:
8 TwoWire *i2c = nullptr;
9 uint8_t address = 0;
10
11 bool beginI2C(uint8_t addr, TwoWire &wire, uint32_t speed,
12 uint8_t sda = 255, uint8_t scl = 255)
13 {
14 address = addr;
15 i2c = &wire;
16
17#if defined(ESP32)
18 if (sda != 255 && scl != 255)
19 i2c->begin(sda, scl);
20 else
21 i2c->begin();
22#else
23 i2c->begin();
24#endif
25 // i2c->setClock(speed);
26
27 // Probe device
28 i2c->beginTransmission(address);
29 return (i2c->endTransmission() == 0);
30 }
31
32 int8_t read(uint8_t reg, uint8_t *data, uint32_t len) override
33 {
34 if (!i2c)
35 return -1;
36 i2c->beginTransmission(address);
37 i2c->write(reg );
38
39 if (i2c->endTransmission(false) != 0)
40 return -1;
41
42 delay(1);
43
44 if (len > 255)
45 return -3;
46 uint8_t received = i2c->requestFrom(address, (uint8_t)len);
47 if (received != len)
48 return -2;
49
50 for (uint32_t i = 0; i < len; i++)
51 {
52 if (!i2c->available())
53 return -4;
54 data[i] = i2c->read();
55 }
56 // Serial.print("R-Reg: ");
57 // Serial.print(reg, HEX);
58 // Serial.print(" |Data: ");
59 // for (int i = 0; i < len; i++)
60 // {
61 // Serial.print(" ");
62 // Serial.print(data[i], HEX);
63 // }
64 // Serial.println();
65
66 return 0;
67 }
68
69 bool beginSPI(uint8_t, SPIClass &, uint32_t,
70 uint8_t, uint8_t, uint8_t) override
71 {
72 return false;
73 }
74
75 int8_t write(uint8_t reg, const uint8_t *data, uint32_t len) override
76 {
77 if (!i2c)
78 return -1;
79
80 i2c->beginTransmission(address);
81 i2c->write(reg);
82 for (uint32_t i = 0; i < len; i++)
83 i2c->write(data[i]);
84
85 uint8_t status = i2c->endTransmission();
86
87 // Serial.print("W-Reg: ");
88 // Serial.print(reg, HEX);
89 // Serial.print(" |Data: ");
90 // for (int i = 0; i < len; i++)
91 // {
92 // Serial.print(" ");
93 // Serial.print(data[i], HEX);
94 // }
95 // Serial.println();
96 return (status == 0) ? 0 : -status;
97 }
98};
7Semi_Interface.h
I2C_Interface
Definition 7Semi_I2C_Interface.h:6
I2C_Interface::address
uint8_t address
Definition 7Semi_I2C_Interface.h:9
I2C_Interface::write
int8_t write(uint8_t reg, const uint8_t *data, uint32_t len) override
Definition 7Semi_I2C_Interface.h:75
I2C_Interface::beginSPI
bool beginSPI(uint8_t, SPIClass &, uint32_t, uint8_t, uint8_t, uint8_t) override
Definition 7Semi_I2C_Interface.h:69
I2C_Interface::read
int8_t read(uint8_t reg, uint8_t *data, uint32_t len) override
Definition 7Semi_I2C_Interface.h:32
I2C_Interface::beginI2C
bool beginI2C(uint8_t addr, TwoWire &wire, uint32_t speed, uint8_t sda=255, uint8_t scl=255)
Definition 7Semi_I2C_Interface.h:11
I2C_Interface::i2c
TwoWire * i2c
Definition 7Semi_I2C_Interface.h:8
Interface_7Semi
Definition 7Semi_Interface.h:15