Skip to content

Commit aa27642

Browse files
committed
Add basic Arduino example
The C++/Arduino wrapper is used on top of the portable C code
1 parent 4c85462 commit aa27642

File tree

6 files changed

+122
-11
lines changed

6 files changed

+122
-11
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "ICM_20948.h"
2+
3+
ICM_20948_I2C myIC;
4+
5+
void setup() {
6+
// put your setup code here, to run once:
7+
8+
}
9+
10+
void loop() {
11+
// put your main code here, to run repeatedly:
12+
13+
}

examples/Example999_DirectTesting/Example999_DirectTesting.ino renamed to examples/PortableC/Example999_DirectTesting/Example999_DirectTesting.ino

File renamed without changes.

examples/Example999_I2C_Testing/Example999_I2C_Testing.ino renamed to examples/PortableC/Example999_Portable_I2C_Testing/Example999_Portable_I2C_Testing.ino

File renamed without changes.

examples/Example999_SPI_Testing/Example999_SPI_Testing.ino renamed to examples/PortableC/Example999_Portable_SPI_Testing/Example999_Portable_SPI_Testing.ino

File renamed without changes.

src/ICM_20948.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,87 @@ ICM_20948_Status_e ICM_20948_read_SPI(uint8_t reg, uint8_t* buff, uint32_t len,
1010

1111

1212

13+
// Base
14+
ICM_20948::ICM_20948(){
15+
16+
}
17+
18+
19+
20+
21+
22+
23+
24+
25+
// I2C
26+
ICM_20948_I2C::ICM_20948_I2C(){
27+
28+
}
29+
30+
ICM_20948_Status_e ICM_20948_I2C::begin(TwoWire &wirePort, bool ad0val, uint8_t ad0pin){
31+
// Associate
32+
_ad0 = ad0pin;
33+
_i2c = &wirePort;
34+
_ad0val = ad0val;
35+
36+
// Set pinmodes
37+
if(_ad0 != ICM_20948_ARD_UNUSED_PIN){ pinMode(_ad0, OUTPUT); }
38+
39+
// Set pins to default positions
40+
if(_ad0 != ICM_20948_ARD_UNUSED_PIN){ digitalWrite(_ad0, _ad0val); }
41+
42+
// _i2c->begin(); // Moved into user's sketch
43+
44+
// Set up the serif
45+
_serif.write = ICM_20948_write_I2C;
46+
_serif.read = ICM_20948_read_I2C;
47+
_serif.user = (void*)this; // refer to yourself in the user field
48+
49+
// Link the serif
50+
_device._serif = &_serif;
51+
52+
return ICM_20948_Stat_Ok;
53+
}
54+
55+
56+
57+
58+
59+
60+
61+
// SPI
62+
SPISettings ICM_20948_SPI_DEFAULT_SETTINGS(ICM_20948_SPI_DEFAULT_FREQ, ICM_20948_SPI_DEFAULT_ORDER, ICM_20948_SPI_DEFAULT_MODE);
63+
64+
ICM_20948_SPI::ICM_20948_SPI(){
65+
66+
}
67+
68+
ICM_20948_Status_e ICM_20948_SPI::begin( uint8_t csPin, SPIClass &spiPort){
69+
// Associate
70+
_spi = &spiPort;
71+
_spisettings = ICM_20948_SPI_DEFAULT_SETTINGS;
72+
_cs = csPin;
73+
74+
75+
// Set pinmodes
76+
pinMode(_cs, OUTPUT);
77+
78+
// Set pins to default positions
79+
digitalWrite(_cs, HIGH);
80+
81+
// _spi->begin(); // Moved into user's sketch
82+
83+
// Set up the serif
84+
_serif.write = ICM_20948_write_SPI;
85+
_serif.read = ICM_20948_read_SPI;
86+
_serif.user = (void*)this; // refer to yourself in the user field
87+
88+
// Link the serif
89+
_device._serif = &_serif;
90+
91+
return ICM_20948_Stat_Ok;
92+
}
93+
1394

1495

1596

src/ICM_20948.h

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,60 @@ A C++ interface to the ICM-20948
99

1010
#include "util/ICM_20948_C.h" // The C backbone
1111

12-
#include "Wire.h" // Arduino support
12+
#include "Arduino.h" // Arduino support
13+
#include "Wire.h"
1314
#include "SPI.h"
1415

16+
#define ICM_20948_ARD_UNUSED_PIN 0xFF
17+
18+
19+
// Base
1520
class ICM_20948 {
1621
private:
1722
protected:
18-
ICM_20948_Device_t device;
23+
ICM_20948_Device_t _device;
1924
public:
2025

26+
ICM_20948();
2127
};
2228

2329

24-
25-
class ICM_20948_I2C {
30+
// I2C
31+
class ICM_20948_I2C : public ICM_20948 {
2632
private:
2733
protected:
2834
public:
29-
TwoWire* _i2c;
30-
uint8_t _addr;
35+
TwoWire* _i2c;
36+
uint8_t _addr;
37+
uint8_t _ad0;
38+
bool _ad0val;
39+
ICM_20948_Serif_t _serif;
3140

3241
ICM_20948_I2C(); // Constructor
3342

43+
ICM_20948_Status_e begin(TwoWire &wirePort = Wire, bool ad0val = true, uint8_t ad0pin = ICM_20948_ARD_UNUSED_PIN);
3444

3545
};
3646

3747

38-
class ICM_20948_SPI {
48+
49+
// SPI
50+
#define ICM_20948_SPI_DEFAULT_FREQ 1000000
51+
#define ICM_20948_SPI_DEFAULT_ORDER MSBFIRST
52+
#define ICM_20948_SPI_DEFAULT_MODE SPI_MODE3
53+
54+
class ICM_20948_SPI : public ICM_20948 {
3955
private:
4056
protected:
4157
public:
42-
SPIClass* _spi;
43-
SPISettings _spisettings;
44-
uint8_t _cs;
58+
SPIClass* _spi;
59+
SPISettings _spisettings;
60+
uint8_t _cs;
61+
ICM_20948_Serif_t _serif;
4562

4663
ICM_20948_SPI(); // Constructor
4764

48-
ICM_20948_Status_e begin();
65+
ICM_20948_Status_e begin( uint8_t csPin, SPIClass &spiPort);
4966

5067
// read( )
5168
};

0 commit comments

Comments
 (0)