Skip to content

Commit b88e841

Browse files
committed
//
//Iain Frew - 8/30/17 // //IDE is Visual Studio using Vmicro add-in for Arduino development // //This is a port of Hari Nair project to esp32 and using the DMP functionaility of //the 9250 IMU. // //The majortity of this code was some great work by Jeff Rowberg for the //mpu6050 library and the i2cdevlib interface, and Hari Nair for the Vario design. //The calibration came from Luis Ródenas. //I integrated Jeff's MPU6050 library to take advantage of the 6050 dmp processing which works fine //on esp8266 but a bit tempremental on esp32 as there are some issues with timing using Jeff's library //and the I2C bus. You need to be able to read in chunks using a buffer size of about 16. //So for my chips and setup I played around with the buffer_length set in i2cdevlib.cpp and eventually chose 16. //Anything larger tends to screw up the data being read. Also found that the readings are noisy when //reading from the DMP at 200Hz as jeff found. So reduced ODR to 100Hz and modified the code accordingly to take //that into account. //I have a define to enable/disable DMP processing rather than calculate manually the rotations that Hari used. //Seems to be accurate and reduces the loop elapsed time by a factor of 3. //Correct calibration I have found is key but on two gy-86, values of 1700 and 1200 for kfzVariance an kfazVariance //seem to work best. Configuration in the config.h file of zero theshold 20 and climb threshold 20 //also produce good results. // //To build, you only need the esp32 core library installed as this sketch uses the preference, wire and wifi libraries // // //This code uses the gy-86 IMU and the reference system is different than the CJMCU-117 that Hari used. If you use the //cjmcu-117 module then just turn the board upside down and you get the same reults as the gy-86 which assumes the chips are up facing you //when used. // gy-86 interface // 3.3V 3.3v // GND GND // SCL GPIO21 // SDA GPIO22 // INT GPIO12 // audio GPIO13 // calibration button GPIO15
0 parents  commit b88e841

22 files changed

+9298
-0
lines changed

I2Cdev.cpp

+1,479
Large diffs are not rendered by default.

I2Cdev.h

+278
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
// I2Cdev library collection - Main I2C device class header file
2+
// Abstracts bit and byte I2C R/W functions into a convenient class
3+
// 2013-06-05 by Jeff Rowberg <[email protected]>
4+
//
5+
// Changelog:
6+
// 2015-10-30 - simondlevy : support i2c_t3 for Teensy3.1
7+
// 2013-05-06 - add Francesco Ferrara's Fastwire v0.24 implementation with small modifications
8+
// 2013-05-05 - fix issue with writing bit values to words (Sasquatch/Farzanegan)
9+
// 2012-06-09 - fix major issue with reading > 32 bytes at a time with Arduino Wire
10+
// - add compiler warnings when using outdated or IDE or limited I2Cdev implementation
11+
// 2011-11-01 - fix write*Bits mask calculation (thanks sasquatch @ Arduino forums)
12+
// 2011-10-03 - added automatic Arduino version detection for ease of use
13+
// 2011-10-02 - added Gene Knight's NBWire TwoWire class implementation with small modifications
14+
// 2011-08-31 - added support for Arduino 1.0 Wire library (methods are different from 0.x)
15+
// 2011-08-03 - added optional timeout parameter to read* methods to easily change from default
16+
// 2011-08-02 - added support for 16-bit registers
17+
// - fixed incorrect Doxygen comments on some methods
18+
// - added timeout value for read operations (thanks mem @ Arduino forums)
19+
// 2011-07-30 - changed read/write function structures to return success or byte counts
20+
// - made all methods static for multi-device memory savings
21+
// 2011-07-28 - initial release
22+
23+
/* ============================================
24+
I2Cdev device library code is placed under the MIT license
25+
Copyright (c) 2013 Jeff Rowberg
26+
27+
Permission is hereby granted, free of charge, to any person obtaining a copy
28+
of this software and associated documentation files (the "Software"), to deal
29+
in the Software without restriction, including without limitation the rights
30+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31+
copies of the Software, and to permit persons to whom the Software is
32+
furnished to do so, subject to the following conditions:
33+
34+
The above copyright notice and this permission notice shall be included in
35+
all copies or substantial portions of the Software.
36+
37+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
43+
THE SOFTWARE.
44+
===============================================
45+
*/
46+
47+
#ifndef _I2CDEV_H_
48+
#define _I2CDEV_H_
49+
50+
// -----------------------------------------------------------------------------
51+
// I2C interface implementation setting
52+
// -----------------------------------------------------------------------------
53+
#ifndef I2CDEV_IMPLEMENTATION
54+
#define I2CDEV_IMPLEMENTATION I2CDEV_ARDUINO_WIRE
55+
//#define I2CDEV_IMPLEMENTATION I2CDEV_BUILTIN_FASTWIRE
56+
#endif // I2CDEV_IMPLEMENTATION
57+
58+
// comment this out if you are using a non-optimal IDE/implementation setting
59+
// but want the compiler to shut up about it
60+
#define I2CDEV_IMPLEMENTATION_WARNINGS
61+
62+
// -----------------------------------------------------------------------------
63+
// I2C interface implementation options
64+
// -----------------------------------------------------------------------------
65+
#define I2CDEV_ARDUINO_WIRE 1 // Wire object from Arduino
66+
#define I2CDEV_BUILTIN_NBWIRE 2 // Tweaked Wire object from Gene Knight's NBWire project
67+
// ^^^ NBWire implementation is still buggy w/some interrupts!
68+
#define I2CDEV_BUILTIN_FASTWIRE 3 // FastWire object from Francesco Ferrara's project
69+
#define I2CDEV_I2CMASTER_LIBRARY 4 // I2C object from DSSCircuits I2C-Master Library at https://github.com/DSSCircuits/I2C-Master-Library
70+
71+
// -----------------------------------------------------------------------------
72+
// Arduino-style "Serial.print" debug constant (uncomment to enable)
73+
// -----------------------------------------------------------------------------
74+
//#define I2CDEV_SERIAL_DEBUG
75+
76+
#ifdef ARDUINO
77+
#if ARDUINO < 100
78+
#include "WProgram.h"
79+
#else
80+
#include "Arduino.h"
81+
#endif
82+
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
83+
#include <Wire.h>
84+
#endif
85+
#if I2CDEV_IMPLEMENTATION == I2CDEV_I2CMASTER_LIBRARY
86+
#include <I2C.h>
87+
#endif
88+
#endif
89+
90+
#ifdef SPARK
91+
#include <spark_wiring_i2c.h>
92+
#define ARDUINO 101
93+
#endif
94+
95+
96+
// 1000ms default read timeout (modify with "I2Cdev::readTimeout = [ms];")
97+
#define I2CDEV_DEFAULT_READ_TIMEOUT 1000
98+
99+
class I2Cdev {
100+
public:
101+
I2Cdev();
102+
103+
static int8_t readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout);
104+
static int8_t readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout);
105+
static int8_t readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout);
106+
static int8_t readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout);
107+
static int8_t readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout);
108+
static int8_t readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout);
109+
static int8_t readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout);
110+
static int8_t readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout);
111+
112+
static bool writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data);
113+
static bool writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data);
114+
static bool writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data);
115+
static bool writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data);
116+
static bool writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data);
117+
static bool writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data);
118+
static bool writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data);
119+
static bool writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data);
120+
121+
static uint16_t readTimeout;
122+
};
123+
124+
#if I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
125+
//////////////////////
126+
// FastWire 0.24
127+
// This is a library to help faster programs to read I2C devices.
128+
// Copyright(C) 2012
129+
// Francesco Ferrara
130+
//////////////////////
131+
132+
/* Master */
133+
#define TW_START 0x08
134+
#define TW_REP_START 0x10
135+
136+
/* Master Transmitter */
137+
#define TW_MT_SLA_ACK 0x18
138+
#define TW_MT_SLA_NACK 0x20
139+
#define TW_MT_DATA_ACK 0x28
140+
#define TW_MT_DATA_NACK 0x30
141+
#define TW_MT_ARB_LOST 0x38
142+
143+
/* Master Receiver */
144+
#define TW_MR_ARB_LOST 0x38
145+
#define TW_MR_SLA_ACK 0x40
146+
#define TW_MR_SLA_NACK 0x48
147+
#define TW_MR_DATA_ACK 0x50
148+
#define TW_MR_DATA_NACK 0x58
149+
150+
#define TW_OK 0
151+
#define TW_ERROR 1
152+
153+
class Fastwire {
154+
private:
155+
static boolean waitInt();
156+
157+
public:
158+
static void setup(int khz, boolean pullup);
159+
static byte beginTransmission(byte device);
160+
static byte write(byte value);
161+
static byte writeBuf(byte device, byte address, byte *data, byte num);
162+
static byte readBuf(byte device, byte address, byte *data, byte num);
163+
static void reset();
164+
static byte stop();
165+
};
166+
#endif
167+
168+
#if I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE
169+
// NBWire implementation based heavily on code by Gene Knight <[email protected]>
170+
// Originally posted on the Arduino forum at http://arduino.cc/forum/index.php/topic,70705.0.html
171+
// Originally offered to the i2cdevlib project at http://arduino.cc/forum/index.php/topic,68210.30.html
172+
173+
#define NBWIRE_BUFFER_LENGTH 32
174+
175+
class TwoWire {
176+
private:
177+
static uint8_t rxBuffer[];
178+
static uint8_t rxBufferIndex;
179+
static uint8_t rxBufferLength;
180+
181+
static uint8_t txAddress;
182+
static uint8_t txBuffer[];
183+
static uint8_t txBufferIndex;
184+
static uint8_t txBufferLength;
185+
186+
// static uint8_t transmitting;
187+
static void (*user_onRequest)(void);
188+
static void (*user_onReceive)(int);
189+
static void onRequestService(void);
190+
static void onReceiveService(uint8_t*, int);
191+
192+
public:
193+
TwoWire();
194+
void begin();
195+
void begin(uint8_t);
196+
void begin(int);
197+
void beginTransmission(uint8_t);
198+
//void beginTransmission(int);
199+
uint8_t endTransmission(uint16_t timeout=0);
200+
void nbendTransmission(void (*function)(int)) ;
201+
uint8_t requestFrom(uint8_t, int, uint16_t timeout=0);
202+
//uint8_t requestFrom(int, int);
203+
void nbrequestFrom(uint8_t, int, void (*function)(int));
204+
void send(uint8_t);
205+
void send(uint8_t*, uint8_t);
206+
//void send(int);
207+
void send(char*);
208+
uint8_t available(void);
209+
uint8_t receive(void);
210+
void onReceive(void (*)(int));
211+
void onRequest(void (*)(void));
212+
};
213+
214+
#define TWI_READY 0
215+
#define TWI_MRX 1
216+
#define TWI_MTX 2
217+
#define TWI_SRX 3
218+
#define TWI_STX 4
219+
220+
#define TW_WRITE 0
221+
#define TW_READ 1
222+
223+
#define TW_MT_SLA_NACK 0x20
224+
#define TW_MT_DATA_NACK 0x30
225+
226+
#define CPU_FREQ 16000000L
227+
#define TWI_FREQ 100000L
228+
#define TWI_BUFFER_LENGTH 32
229+
230+
/* TWI Status is in TWSR, in the top 5 bits: TWS7 - TWS3 */
231+
232+
#define TW_STATUS_MASK (_BV(TWS7)|_BV(TWS6)|_BV(TWS5)|_BV(TWS4)|_BV(TWS3))
233+
#define TW_STATUS (TWSR & TW_STATUS_MASK)
234+
#define TW_START 0x08
235+
#define TW_REP_START 0x10
236+
#define TW_MT_SLA_ACK 0x18
237+
#define TW_MT_SLA_NACK 0x20
238+
#define TW_MT_DATA_ACK 0x28
239+
#define TW_MT_DATA_NACK 0x30
240+
#define TW_MT_ARB_LOST 0x38
241+
#define TW_MR_ARB_LOST 0x38
242+
#define TW_MR_SLA_ACK 0x40
243+
#define TW_MR_SLA_NACK 0x48
244+
#define TW_MR_DATA_ACK 0x50
245+
#define TW_MR_DATA_NACK 0x58
246+
#define TW_ST_SLA_ACK 0xA8
247+
#define TW_ST_ARB_LOST_SLA_ACK 0xB0
248+
#define TW_ST_DATA_ACK 0xB8
249+
#define TW_ST_DATA_NACK 0xC0
250+
#define TW_ST_LAST_DATA 0xC8
251+
#define TW_SR_SLA_ACK 0x60
252+
#define TW_SR_ARB_LOST_SLA_ACK 0x68
253+
#define TW_SR_GCALL_ACK 0x70
254+
#define TW_SR_ARB_LOST_GCALL_ACK 0x78
255+
#define TW_SR_DATA_ACK 0x80
256+
#define TW_SR_DATA_NACK 0x88
257+
#define TW_SR_GCALL_DATA_ACK 0x90
258+
#define TW_SR_GCALL_DATA_NACK 0x98
259+
#define TW_SR_STOP 0xA0
260+
#define TW_NO_INFO 0xF8
261+
#define TW_BUS_ERROR 0x00
262+
263+
//#define _MMIO_BYTE(mem_addr) (*(volatile uint8_t *)(mem_addr))
264+
//#define _SFR_BYTE(sfr) _MMIO_BYTE(_SFR_ADDR(sfr))
265+
266+
#ifndef sbi // set bit
267+
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
268+
#endif // sbi
269+
270+
#ifndef cbi // clear bit
271+
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
272+
#endif // cbi
273+
274+
extern TwoWire Wire;
275+
276+
#endif // I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE
277+
278+
#endif /* _I2CDEV_H_ */

0 commit comments

Comments
 (0)