Skip to content
This repository
Browse code

Updating I2Cdev class and various devices

commit fc92547377028a63489c44cfc689355059fdf3c5 1 parent 51d0ab1
Jeff Rowberg authored
8 Arduino/ADXL345/ADXL345.cpp
@@ -1642,11 +1642,11 @@ void ADXL345::setFIFOSamples(uint8_t size) {
1642 1642 * A 1 in the FIFO_TRIG bit corresponds to a trigger event occurring, and a 0
1643 1643 * means that a FIFO trigger event has not occurred.
1644 1644 * @return FIFO trigger occurred status
1645   - * @see ADXL345_RA_FIFO_CTL
  1645 + * @see ADXL345_RA_FIFO_STATUS
1646 1646 * @see ADXL345_FIFOSTAT_TRIGGER_BIT
1647 1647 */
1648 1648 bool ADXL345::getFIFOTriggerOccurred() {
1649   - I2Cdev::readBit(devAddr, ADXL345_RA_FIFO_CTL, ADXL345_FIFOSTAT_TRIGGER_BIT, buffer);
  1649 + I2Cdev::readBit(devAddr, ADXL345_RA_FIFO_STATUS, ADXL345_FIFOSTAT_TRIGGER_BIT, buffer);
1650 1650 return buffer[0];
1651 1651 }
1652 1652 /** Get FIFO length.
@@ -1658,11 +1658,11 @@ bool ADXL345::getFIFOTriggerOccurred() {
1658 1658 * available at any given time because an additional entry is available at the
1659 1659 * output filter of the I2Cdev::
1660 1660 * @return Current FIFO length
1661   - * @see ADXL345_RA_FIFO_CTL
  1661 + * @see ADXL345_RA_FIFO_STATUS
1662 1662 * @see ADXL345_FIFOSTAT_LENGTH_BIT
1663 1663 * @see ADXL345_FIFOSTAT_LENGTH_LENGTH
1664 1664 */
1665 1665 uint8_t ADXL345::getFIFOLength() {
1666   - I2Cdev::readBits(devAddr, ADXL345_RA_FIFO_CTL, ADXL345_FIFOSTAT_LENGTH_BIT, ADXL345_FIFOSTAT_LENGTH_LENGTH, buffer);
  1666 + I2Cdev::readBits(devAddr, ADXL345_RA_FIFO_STATUS, ADXL345_FIFOSTAT_LENGTH_BIT, ADXL345_FIFOSTAT_LENGTH_LENGTH, buffer);
1667 1667 return buffer[0];
1668 1668 }
2  Arduino/AK8975/Examples/AK8975_MPUEVB_heading/AK8975_MPUEVB_heading.ino
@@ -60,7 +60,7 @@ THE SOFTWARE.
60 60 // Addr pins low/high = 0x0D
61 61 // Addr pins high/low = 0x0E (default for InvenSense MPU6050 evaluation board)
62 62 // Addr pins high/high = 0x0F
63   -AK8975 mag(0x0E);
  63 +AK8975 mag(0x0C);
64 64 MPU6050 accelgyro; // address = 0x68, the default, on MPU6050 EVB
65 65
66 66 int16_t mx, my, mz;
228 Arduino/I2Cdev/I2Cdev.cpp
@@ -3,25 +3,25 @@
3 3 // 6/9/2012 by Jeff Rowberg <jeff@rowberg.net>
4 4 //
5 5 // Changelog:
6   -// 2013-05-05 - fix issue with writing bit values to words (Sasquatch/Farzanegan)
7   -// 2012-06-09 - fix major issue with reading > 32 bytes at a time with Arduino Wire
8   -// - add compiler warnings when using outdated or IDE or limited I2Cdev implementation
9   -// 2011-11-01 - fix write*Bits mask calculation (thanks sasquatch @ Arduino forums)
10   -// 2011-10-03 - added automatic Arduino version detection for ease of use
11   -// 2011-10-02 - added Gene Knight's NBWire TwoWire class implementation with small modifications
12   -// 2011-08-31 - added support for Arduino 1.0 Wire library (methods are different from 0.x)
13   -// 2011-08-03 - added optional timeout parameter to read* methods to easily change from default
14   -// 2011-08-02 - added support for 16-bit registers
15   -// - fixed incorrect Doxygen comments on some methods
16   -// - added timeout value for read operations (thanks mem @ Arduino forums)
17   -// 2011-07-30 - changed read/write function structures to return success or byte counts
18   -// - made all methods static for multi-device memory savings
19   -// 2011-07-28 - initial release
20   -
  6 +// 2013-05-06 - add Francesco Ferrara's Fastwire v0.24 implementation with small modifications
  7 +// 2013-05-05 - fix issue with writing bit values to words (Sasquatch/Farzanegan)
  8 +// 2012-06-09 - fix major issue with reading > 32 bytes at a time with Arduino Wire
  9 +// - add compiler warnings when using outdated or IDE or limited I2Cdev implementation
  10 +// 2011-11-01 - fix write*Bits mask calculation (thanks sasquatch @ Arduino forums)
  11 +// 2011-10-03 - added automatic Arduino version detection for ease of use
  12 +// 2011-10-02 - added Gene Knight's NBWire TwoWire class implementation with small modifications
  13 +// 2011-08-31 - added support for Arduino 1.0 Wire library (methods are different from 0.x)
  14 +// 2011-08-03 - added optional timeout parameter to read* methods to easily change from default
  15 +// 2011-08-02 - added support for 16-bit registers
  16 +// - fixed incorrect Doxygen comments on some methods
  17 +// - added timeout value for read operations (thanks mem @ Arduino forums)
  18 +// 2011-07-30 - changed read/write function structures to return success or byte counts
  19 +// - made all methods static for multi-device memory savings
  20 +// 2011-07-28 - initial release
21 21
22 22 /* ============================================
23 23 I2Cdev device library code is placed under the MIT license
24   -Copyright (c) 2012 Jeff Rowberg
  24 +Copyright (c) 2013 Jeff Rowberg
25 25
26 26 Permission is hereby granted, free of charge, to any person obtaining a copy
27 27 of this software and associated documentation files (the "Software"), to deal
@@ -61,18 +61,16 @@ THE SOFTWARE.
61 61 #warning - Repeated starts conditions
62 62 #warning - Timeout detection (some Wire requests block forever)
63 63 #elif ARDUINO > 100
64   - /*
65 64 #warning Using current Arduino IDE with Wire library is functionally limiting.
66 65 #warning Arduino IDE v1.0.1+ with I2CDEV_BUILTIN_FASTWIRE implementation is recommended.
67 66 #warning This I2Cdev implementation does not support:
68 67 #warning - Timeout detection (some Wire requests block forever)
69   - */
70 68 #endif
71 69 #endif
72 70
73 71 #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
74 72
75   - #error The I2CDEV_BUILTIN_FASTWIRE implementation is known to be broken right now. Patience, Iago!
  73 + //#error The I2CDEV_BUILTIN_FASTWIRE implementation is known to be broken right now. Patience, Iago!
76 74
77 75 #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE
78 76
@@ -87,10 +85,6 @@ THE SOFTWARE.
87 85 // Originally offered to the i2cdevlib project at http://arduino.cc/forum/index.php/topic,68210.30.html
88 86 TwoWire Wire;
89 87
90   -#elif I2CDEV_IMPLEMENTATION == I2CDEV_I2CMASTER_LIBRARY
91   -
92   - #warning Dunno, just don't want it to feel left out ^_^'
93   -
94 88 #endif
95 89
96 90 /** Default constructor.
@@ -293,29 +287,22 @@ int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8
293 287 if (count + 1 < length) Serial.print(" ");
294 288 #endif
295 289 }
  290 +
  291 + Wire.endTransmission();
296 292 }
297 293 #endif
298 294
299 295 #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
300   - // Fastwire library (STILL UNDER DEVELOPMENT, NON-FUNCTIONAL!)
301 296
  297 + // Fastwire library
302 298 // no loop required for fastwire
303   - uint8_t status = Fastwire::readBuf(devAddr, regAddr, data, length);
  299 + uint8_t status = Fastwire::readBuf(devAddr << 1, regAddr, data, length);
304 300 if (status == 0) {
305 301 count = length; // success
306 302 } else {
307 303 count = -1; // error
308 304 }
309 305
310   - #elif (I2CDEV_IMPLEMENTATION == I2CDEV_I2CMASTER_LIBRARY)
311   -
312   - uint8_t status = I2c.read(devAddr, regAddr, length, data);
313   - if(status == 0) {
314   - count = length;
315   - } else {
316   - count = -1 * status;
317   - }
318   -
319 306 #endif
320 307
321 308 // check for timeout
@@ -336,7 +323,7 @@ int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8
336 323 * @param length Number of words to read
337 324 * @param data Buffer to store read data in
338 325 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
339   - * @return Number of words read (negative value indicates failure)
  326 + * @return Number of words read (0 indicates failure)
340 327 */
341 328 int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout) {
342 329 #ifdef I2CDEV_SERIAL_DEBUG
@@ -449,15 +436,17 @@ int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint1
449 436 }
450 437 msb = !msb;
451 438 }
  439 +
  440 + Wire.endTransmission();
452 441 }
453 442 #endif
454 443
455 444 #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
456   - // Fastwire library (STILL UNDER DEVELOPMENT, NON-FUNCTIONAL!)
457 445
  446 + // Fastwire library
458 447 // no loop required for fastwire
459 448 uint16_t intermediate[(uint8_t)length];
460   - uint8_t status = Fastwire::readBuf(devAddr, regAddr, (uint8_t *)intermediate, (uint8_t)(length * 2));
  449 + uint8_t status = Fastwire::readBuf(devAddr << 1, regAddr, (uint8_t *)intermediate, (uint8_t)(length * 2));
461 450 if (status == 0) {
462 451 count = length; // success
463 452 for (uint8_t i = 0; i < length; i++) {
@@ -467,18 +456,6 @@ int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint1
467 456 count = -1; // error
468 457 }
469 458
470   - #elif (I2CDEV_IMPLEMENTATION == I2CDEV_I2CMASTER_LIBRARY)
471   -
472   - uint16_t intermediate[(uint8_t)length];
473   - uint8_t status = I2c.read(devAddr, regAddr, length*2, (uint8_t *)intermediate);
474   - if(status == 0) {
475   - count = length;
476   - for(uint8_t i = 0; i < length; i++) {
477   - data[i] = (intermediate[2*i] << 8) | intermediate[2*i + i];
478   - }
479   - } else {
480   - count = -1 * status;
481   - }
482 459 #endif
483 460
484 461 if (timeout > 0 && millis() - t1 >= timeout && count < length) count = -1; // timeout
@@ -622,27 +599,30 @@ bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_
622 599 #elif (I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100)
623 600 Wire.beginTransmission(devAddr);
624 601 Wire.write((uint8_t) regAddr); // send address
  602 + #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
  603 + Fastwire::beginTransmission(devAddr);
  604 + Fastwire::write(regAddr);
625 605 #endif
626 606 for (uint8_t i = 0; i < length; i++) {
  607 + #ifdef I2CDEV_SERIAL_DEBUG
  608 + Serial.print(data[i], HEX);
  609 + if (i + 1 < length) Serial.print(" ");
  610 + #endif
627 611 #if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE)
628 612 Wire.send((uint8_t) data[i]);
629 613 #elif (I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100)
630 614 Wire.write((uint8_t) data[i]);
631 615 #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
632   - status = Fastwire::write(devAddr, regAddr, data[i]);
633   - Serial.println(status);
634   - #elif (I2CDEV_IMPLEMENTATION == I2CDEV_I2CMASTER_LIBRARY)
635   - status = I2c.write(devAddr, regAddr, data[i]);
636   - #endif
637   - #ifdef I2CDEV_SERIAL_DEBUG
638   - Serial.print(data[i], HEX);
639   - if (i + 1 < length) Serial.print(" ");
  616 + Fastwire::write((uint8_t) data[i]);
640 617 #endif
641 618 }
642 619 #if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE)
643 620 Wire.endTransmission();
644 621 #elif (I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100)
645 622 status = Wire.endTransmission();
  623 + #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
  624 + Fastwire::stop();
  625 + //status = Fastwire::endTransmission();
646 626 #endif
647 627 #ifdef I2CDEV_SERIAL_DEBUG
648 628 Serial.println(". Done.");
@@ -674,30 +654,34 @@ bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16
674 654 #elif (I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100)
675 655 Wire.beginTransmission(devAddr);
676 656 Wire.write(regAddr); // send address
  657 + #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
  658 + Fastwire::beginTransmission(devAddr);
  659 + Fastwire::write(regAddr);
677 660 #endif
678 661 for (uint8_t i = 0; i < length * 2; i++) {
679   - #if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE)
680   - Wire.send((uint8_t)(data[i++] >> 8)); // send MSB
681   - Wire.send((uint8_t)data[i]); // send LSB
682   - #elif (I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100)
683   - Wire.write((uint8_t)(data[i++] >> 8)); // send MSB
684   - Wire.write((uint8_t)data[i]); // send LSB
685   - #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
686   - status = Fastwire::write(devAddr, regAddr, (uint8_t)(data[i++] >> 8));
687   - status = Fastwire::write(devAddr, regAddr + 1, (uint8_t)data[i]);
688   - #elif (I2CDEV_IMPLEMENTATION == I2CDEV_I2CMASTER_LIBRARY)
689   - status = I2c.write((uint8_t)devAddr, (uint8_t)regAddr, (uint8_t)(data[i++] >> 8));
690   - status = I2c.write((uint8_t)devAddr, (uint8_t)regAddr + 1, (uint8_t)data[i]);
691   - #endif
692 662 #ifdef I2CDEV_SERIAL_DEBUG
693 663 Serial.print(data[i], HEX);
694 664 if (i + 1 < length) Serial.print(" ");
695 665 #endif
  666 + #if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE)
  667 + Wire.send((uint8_t)(data[i] >> 8)); // send MSB
  668 + Wire.send((uint8_t)data[i++]); // send LSB
  669 + #elif (I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100)
  670 + Wire.write((uint8_t)(data[i] >> 8)); // send MSB
  671 + Wire.write((uint8_t)data[i++]); // send LSB
  672 + #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
  673 + Fastwire::write((uint8_t)(data[i] >> 8)); // send MSB
  674 + status = Fastwire::write((uint8_t)data[i++]); // send LSB
  675 + if (status != 0) break;
  676 + #endif
696 677 }
697 678 #if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE)
698 679 Wire.endTransmission();
699 680 #elif (I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100)
700 681 status = Wire.endTransmission();
  682 + #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE)
  683 + Fastwire::stop();
  684 + //status = Fastwire::endTransmission();
701 685 #endif
702 686 #ifdef I2CDEV_SERIAL_DEBUG
703 687 Serial.println(". Done.");
@@ -711,12 +695,23 @@ bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16
711 695 uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
712 696
713 697 #if I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  698 + // I2C library
  699 + //////////////////////
  700 + // Copyright(C) 2012
  701 + // Francesco Ferrara
  702 + // ferrara[at]libero[point]it
  703 + //////////////////////
  704 +
714 705 /*
715   - FastWire 0.2
716   - This is a library to help faster programs to read I2C devices.
717   - Copyright(C) 2011 Francesco Ferrara
718   - occhiobello at gmail dot com
719   - */
  706 + FastWire
  707 + - 0.24 added stop
  708 + - 0.23 added reset
  709 +
  710 + This is a library to help faster programs to read I2C devices.
  711 + Copyright(C) 2012 Francesco Ferrara
  712 + occhiobello at gmail dot com
  713 + [used by Jeff Rowberg for I2Cdevlib with permission]
  714 + */
720 715
721 716 boolean Fastwire::waitInt() {
722 717 int l = 250;
@@ -747,7 +742,30 @@ uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
747 742 TWCR = 1 << TWEN; // enable twi module, no interrupt
748 743 }
749 744
750   - byte Fastwire::write(byte device, byte address, byte value) {
  745 + // added by Jeff Rowberg 2013-05-07:
  746 + // Arduino Wire-style "beginTransmission" function
  747 + // (takes 7-bit device address like the Wire method, NOT 8-bit: 0x68, not 0xD0/0xD1)
  748 + byte Fastwire::beginTransmission(byte device) {
  749 + byte twst, retry;
  750 + retry = 2;
  751 + do {
  752 + TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO) | (1 << TWSTA);
  753 + if (!waitInt()) return 1;
  754 + twst = TWSR & 0xF8;
  755 + if (twst != TW_START && twst != TW_REP_START) return 2;
  756 +
  757 + //Serial.print(device, HEX);
  758 + //Serial.print(" ");
  759 + TWDR = device << 1; // send device address without read bit (1)
  760 + TWCR = (1 << TWINT) | (1 << TWEN);
  761 + if (!waitInt()) return 3;
  762 + twst = TWSR & 0xF8;
  763 + } while (twst == TW_MT_SLA_NACK && retry-- > 0);
  764 + if (twst != TW_MT_SLA_ACK) return 4;
  765 + return 0;
  766 + }
  767 +
  768 + byte Fastwire::writeBuf(byte device, byte address, byte *data, byte num) {
751 769 byte twst, retry;
752 770
753 771 retry = 2;
@@ -757,6 +775,8 @@ uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
757 775 twst = TWSR & 0xF8;
758 776 if (twst != TW_START && twst != TW_REP_START) return 2;
759 777
  778 + //Serial.print(device, HEX);
  779 + //Serial.print(" ");
760 780 TWDR = device & 0xFE; // send device address without read bit (1)
761 781 TWCR = (1 << TWINT) | (1 << TWEN);
762 782 if (!waitInt()) return 3;
@@ -764,18 +784,36 @@ uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
764 784 } while (twst == TW_MT_SLA_NACK && retry-- > 0);
765 785 if (twst != TW_MT_SLA_ACK) return 4;
766 786
  787 + //Serial.print(address, HEX);
  788 + //Serial.print(" ");
767 789 TWDR = address; // send data to the previously addressed device
768 790 TWCR = (1 << TWINT) | (1 << TWEN);
769 791 if (!waitInt()) return 5;
770 792 twst = TWSR & 0xF8;
771 793 if (twst != TW_MT_DATA_ACK) return 6;
772 794
773   - TWDR = value; // send data to the previously addressed device
  795 + for (byte i = 0; i < num; i++) {
  796 + //Serial.print(data[i], HEX);
  797 + //Serial.print(" ");
  798 + TWDR = data[i]; // send data to the previously addressed device
  799 + TWCR = (1 << TWINT) | (1 << TWEN);
  800 + if (!waitInt()) return 7;
  801 + twst = TWSR & 0xF8;
  802 + if (twst != TW_MT_DATA_ACK) return 8;
  803 + }
  804 + //Serial.print("\n");
  805 +
  806 + return 0;
  807 + }
  808 +
  809 + byte Fastwire::write(byte value) {
  810 + byte twst;
  811 + //Serial.println(value, HEX);
  812 + TWDR = value; // send data
774 813 TWCR = (1 << TWINT) | (1 << TWEN);
775   - if (!waitInt()) return 7;
  814 + if (!waitInt()) return 1;
776 815 twst = TWSR & 0xF8;
777   - if (twst != TW_MT_DATA_ACK) return 8;
778   -
  816 + if (twst != TW_MT_DATA_ACK) return 2;
779 817 return 0;
780 818 }
781 819
@@ -789,6 +827,8 @@ uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
789 827 twst = TWSR & 0xF8;
790 828 if (twst != TW_START && twst != TW_REP_START) return 17;
791 829
  830 + //Serial.print(device, HEX);
  831 + //Serial.print(" ");
792 832 TWDR = device & 0xfe; // send device address to write
793 833 TWCR = (1 << TWINT) | (1 << TWEN);
794 834 if (!waitInt()) return 18;
@@ -796,6 +836,8 @@ uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
796 836 } while (twst == TW_MT_SLA_NACK && retry-- > 0);
797 837 if (twst != TW_MT_SLA_ACK) return 19;
798 838
  839 + //Serial.print(address, HEX);
  840 + //Serial.print(" ");
799 841 TWDR = address; // send data to the previously addressed device
800 842 TWCR = (1 << TWINT) | (1 << TWEN);
801 843 if (!waitInt()) return 20;
@@ -811,6 +853,8 @@ uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
811 853 twst = TWSR & 0xF8;
812 854 if (twst != TW_START && twst != TW_REP_START) return 23;
813 855
  856 + //Serial.print(device, HEX);
  857 + //Serial.print(" ");
814 858 TWDR = device | 0x01; // send device address with the read bit (1)
815 859 TWCR = (1 << TWINT) | (1 << TWEN);
816 860 if (!waitInt()) return 24;
@@ -818,19 +862,33 @@ uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
818 862 } while (twst == TW_MR_SLA_NACK && retry-- > 0);
819 863 if (twst != TW_MR_SLA_ACK) return 25;
820 864
821   - for(uint8_t i = 0; i < num; i++) {
  865 + for (uint8_t i = 0; i < num; i++) {
822 866 if (i == num - 1)
823   - TWCR = (1 << TWINT) | (1 << TWEN);
  867 + TWCR = (1 << TWINT) | (1 << TWEN);
824 868 else
825 869 TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
826 870 if (!waitInt()) return 26;
827 871 twst = TWSR & 0xF8;
828 872 if (twst != TW_MR_DATA_ACK && twst != TW_MR_DATA_NACK) return twst;
829 873 data[i] = TWDR;
  874 + //Serial.print(data[i], HEX);
  875 + //Serial.print(" ");
830 876 }
  877 + //Serial.print("\n");
  878 + stop();
  879 +
  880 + return 0;
  881 + }
831 882
  883 + void Fastwire::reset() {
  884 + TWCR = 0;
  885 + }
  886 +
  887 + byte Fastwire::stop() {
  888 + TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
  889 + if (!waitInt()) return 1;
832 890 return 0;
833   - }
  891 + }
834 892 #endif
835 893
836 894 #if I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE
@@ -940,7 +998,7 @@ uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
940 998
941 999 twi_Write_Vars *ptwv = 0;
942 1000 static void (*fNextInterruptFunction)(void) = 0;
943   -
  1001 +
944 1002 void twi_Finish(byte bRetVal) {
945 1003 if (ptwv) {
946 1004 free(ptwv);
@@ -1297,7 +1355,7 @@ uint16_t I2Cdev::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
1297 1355 txBufferIndex = 0;
1298 1356 txBufferLength = 0;
1299 1357 }
1300   -
  1358 +
1301 1359 uint8_t TwoWire::endTransmission(uint16_t timeout) {
1302 1360 // transmit buffer (blocking)
1303 1361 //int8_t ret =
61 Arduino/I2Cdev/I2Cdev.h
@@ -3,24 +3,25 @@
3 3 // 6/9/2012 by Jeff Rowberg <jeff@rowberg.net>
4 4 //
5 5 // Changelog:
6   -// 2013-05-05 - fix issue with writing bit values to words (Sasquatch/Farzanegan)
7   -// 2012-06-09 - fix major issue with reading > 32 bytes at a time with Arduino Wire
8   -// - add compiler warnings when using outdated or IDE or limited I2Cdev implementation
9   -// 2011-11-01 - fix write*Bits mask calculation (thanks sasquatch @ Arduino forums)
10   -// 2011-10-03 - added automatic Arduino version detection for ease of use
11   -// 2011-10-02 - added Gene Knight's NBWire TwoWire class implementation with small modifications
12   -// 2011-08-31 - added support for Arduino 1.0 Wire library (methods are different from 0.x)
13   -// 2011-08-03 - added optional timeout parameter to read* methods to easily change from default
14   -// 2011-08-02 - added support for 16-bit registers
15   -// - fixed incorrect Doxygen comments on some methods
16   -// - added timeout value for read operations (thanks mem @ Arduino forums)
17   -// 2011-07-30 - changed read/write function structures to return success or byte counts
18   -// - made all methods static for multi-device memory savings
19   -// 2011-07-28 - initial release
  6 +// 2013-05-06 - add Francesco Ferrara's Fastwire v0.24 implementation with small modifications
  7 +// 2013-05-05 - fix issue with writing bit values to words (Sasquatch/Farzanegan)
  8 +// 2012-06-09 - fix major issue with reading > 32 bytes at a time with Arduino Wire
  9 +// - add compiler warnings when using outdated or IDE or limited I2Cdev implementation
  10 +// 2011-11-01 - fix write*Bits mask calculation (thanks sasquatch @ Arduino forums)
  11 +// 2011-10-03 - added automatic Arduino version detection for ease of use
  12 +// 2011-10-02 - added Gene Knight's NBWire TwoWire class implementation with small modifications
  13 +// 2011-08-31 - added support for Arduino 1.0 Wire library (methods are different from 0.x)
  14 +// 2011-08-03 - added optional timeout parameter to read* methods to easily change from default
  15 +// 2011-08-02 - added support for 16-bit registers
  16 +// - fixed incorrect Doxygen comments on some methods
  17 +// - added timeout value for read operations (thanks mem @ Arduino forums)
  18 +// 2011-07-30 - changed read/write function structures to return success or byte counts
  19 +// - made all methods static for multi-device memory savings
  20 +// 2011-07-28 - initial release
20 21
21 22 /* ============================================
22 23 I2Cdev device library code is placed under the MIT license
23   -Copyright (c) 2012 Jeff Rowberg
  24 +Copyright (c) 2013 Jeff Rowberg
24 25
25 26 Permission is hereby granted, free of charge, to any person obtaining a copy
26 27 of this software and associated documentation files (the "Software"), to deal
@@ -45,6 +46,12 @@ THE SOFTWARE.
45 46 #ifndef _I2CDEV_H_
46 47 #define _I2CDEV_H_
47 48
  49 +// -----------------------------------------------------------------------------
  50 +// I2C interface implementation setting
  51 +// -----------------------------------------------------------------------------
  52 +#define I2CDEV_IMPLEMENTATION I2CDEV_ARDUINO_WIRE
  53 +//#define I2CDEV_IMPLEMENTATION I2CDEV_BUILTIN_FASTWIRE
  54 +
48 55 // comment this out if you are using a non-optimal IDE/implementation setting
49 56 // but want the compiler to shut up about it
50 57 #define I2CDEV_IMPLEMENTATION_WARNINGS
@@ -56,14 +63,6 @@ THE SOFTWARE.
56 63 #define I2CDEV_BUILTIN_NBWIRE 2 // Tweaked Wire object from Gene Knight's NBWire project
57 64 // ^^^ NBWire implementation is still buggy w/some interrupts!
58 65 #define I2CDEV_BUILTIN_FASTWIRE 3 // FastWire object from Francesco Ferrara's project
59   - // ^^^ FastWire implementation in I2Cdev is INCOMPLETE!
60   -#define I2CDEV_I2CMASTER_LIBRARY 4 // I2C object from DSSCircuits I2C-Master Library at
61   - // https://github.com/DSSCircuits/I2C-Master-Library
62   -
63   -// -----------------------------------------------------------------------------
64   -// I2C interface implementation setting
65   -// -----------------------------------------------------------------------------
66   -#define I2CDEV_IMPLEMENTATION I2CDEV_ARDUINO_WIRE
67 66
68 67 // -----------------------------------------------------------------------------
69 68 // Arduino-style "Serial.print" debug constant (uncomment to enable)
@@ -78,13 +77,7 @@ THE SOFTWARE.
78 77 #endif
79 78 #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
80 79 #include <Wire.h>
81   - #else
82   - #if I2CDEV_IMPLEMENTATION == I2CDEV_I2CMASTER_LIBRARY
83   - #include <I2C.h>
84   - #endif
85 80 #endif
86   -#else
87   - #include "ArduinoWrapper.h"
88 81 #endif
89 82
90 83 // 1000ms default read timeout (modify with "I2Cdev::readTimeout = [ms];")
@@ -117,9 +110,9 @@ class I2Cdev {
117 110
118 111 #if I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
119 112 //////////////////////
120   - // FastWire 0.2
  113 + // FastWire 0.24
121 114 // This is a library to help faster programs to read I2C devices.
122   - // Copyright(C) 2011
  115 + // Copyright(C) 2012
123 116 // Francesco Ferrara
124 117 //////////////////////
125 118
@@ -150,8 +143,12 @@ class I2Cdev {
150 143
151 144 public:
152 145 static void setup(int khz, boolean pullup);
153   - static byte write(byte device, byte address, byte value);
  146 + static byte beginTransmission(byte device);
  147 + static byte write(byte value);
  148 + static byte writeBuf(byte device, byte address, byte *data, byte num);
154 149 static byte readBuf(byte device, byte address, byte *data, byte num);
  150 + static void reset();
  151 + static byte stop();
155 152 };
156 153 #endif
157 154
54 Arduino/MPU6050/Examples/MPU6050_DMP6/MPU6050_DMP6.ino
@@ -3,17 +3,19 @@
3 3 // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
4 4 //
5 5 // Changelog:
6   -// 2012-06-21 - added note about Arduino 1.0.1 + Leonardo compatibility error
7   -// 2012-06-20 - improved FIFO overflow handling and simplified read process
8   -// 2012-06-19 - completely rearranged DMP initialization code and simplification
9   -// 2012-06-13 - pull gyro and accel data from FIFO packet instead of reading directly
10   -// 2012-06-09 - fix broken FIFO read sequence and change interrupt detection to RISING
11   -// 2012-06-05 - add gravity-compensated initial reference frame acceleration output
12   -// - add 3D math helper file to DMP6 example sketch
13   -// - add Euler output and Yaw/Pitch/Roll output formats
14   -// 2012-06-04 - remove accel offset clearing for better results (thanks Sungon Lee)
15   -// 2012-06-01 - fixed gyro sensitivity to be 2000 deg/sec instead of 250
16   -// 2012-05-30 - basic DMP initialization working
  6 +// 2013-05-08 - added seamless Fastwire support
  7 +// - added note about gyro calibration
  8 +// 2012-06-21 - added note about Arduino 1.0.1 + Leonardo compatibility error
  9 +// 2012-06-20 - improved FIFO overflow handling and simplified read process
  10 +// 2012-06-19 - completely rearranged DMP initialization code and simplification
  11 +// 2012-06-13 - pull gyro and accel data from FIFO packet instead of reading directly
  12 +// 2012-06-09 - fix broken FIFO read sequence and change interrupt detection to RISING
  13 +// 2012-06-05 - add gravity-compensated initial reference frame acceleration output
  14 +// - add 3D math helper file to DMP6 example sketch
  15 +// - add Euler output and Yaw/Pitch/Roll output formats
  16 +// 2012-06-04 - remove accel offset clearing for better results (thanks Sungon Lee)
  17 +// 2012-06-01 - fixed gyro sensitivity to be 2000 deg/sec instead of 250
  18 +// 2012-05-30 - basic DMP initialization working
17 19
18 20 /* ============================================
19 21 I2Cdev device library code is placed under the MIT license
@@ -39,10 +41,6 @@ THE SOFTWARE.
39 41 ===============================================
40 42 */
41 43
42   -// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
43   -// is used in I2Cdev.h
44   -#include "Wire.h"
45   -
46 44 // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
47 45 // for both classes must be in the include path of your project
48 46 #include "I2Cdev.h"
@@ -50,11 +48,18 @@ THE SOFTWARE.
50 48 #include "MPU6050_6Axis_MotionApps20.h"
51 49 //#include "MPU6050.h" // not necessary if using MotionApps include file
52 50
  51 +// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
  52 +// is used in I2Cdev.h
  53 +#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  54 + #include "Wire.h"
  55 +#endif
  56 +
53 57 // class default I2C address is 0x68
54 58 // specific I2C addresses may be passed as a parameter here
55 59 // AD0 low = 0x68 (default for SparkFun breakout and InvenSense evaluation board)
56 60 // AD0 high = 0x69
57 61 MPU6050 mpu;
  62 +//MPU6050 mpu(0x69); // <-- use for AD0 high
58 63
59 64 /* =========================================================================
60 65 NOTE: In addition to connection 3.3v, GND, SDA, and SCL, this sketch
@@ -92,7 +97,7 @@ MPU6050 mpu;
92 97 // from the FIFO. Note this also requires gravity vector calculations.
93 98 // Also note that yaw/pitch/roll angles suffer from gimbal lock (for
94 99 // more info, see: http://en.wikipedia.org/wiki/Gimbal_lock)
95   -//#define OUTPUT_READABLE_YAWPITCHROLL
  100 +#define OUTPUT_READABLE_YAWPITCHROLL
96 101
97 102 // uncomment "OUTPUT_READABLE_REALACCEL" if you want to see acceleration
98 103 // components with gravity removed. This acceleration reference frame is
@@ -109,7 +114,7 @@ MPU6050 mpu;
109 114
110 115 // uncomment "OUTPUT_TEAPOT" if you want output that matches the
111 116 // format used for the InvenSense teapot demo
112   -#define OUTPUT_TEAPOT
  117 +//#define OUTPUT_TEAPOT
113 118
114 119
115 120
@@ -155,7 +160,12 @@ void dmpDataReady() {
155 160
156 161 void setup() {
157 162 // join I2C bus (I2Cdev library doesn't do this automatically)
158   - Wire.begin();
  163 + #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  164 + Wire.begin();
  165 + TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
  166 + #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  167 + Fastwire::setup(400, true);
  168 + #endif
159 169
160 170 // initialize serial communication
161 171 // (115200 chosen because it is required for Teapot Demo output, but it's
@@ -186,7 +196,13 @@ void setup() {
186 196 // load and configure the DMP
187 197 Serial.println(F("Initializing DMP..."));
188 198 devStatus = mpu.dmpInitialize();
189   -
  199 +
  200 + // supply your own gyro offsets here, scaled for min sensitivity
  201 + mpu.setXGyroOffset(220);
  202 + mpu.setYGyroOffset(76);
  203 + mpu.setZGyroOffset(-85);
  204 + mpu.setZAccelOffset(1788); // 1688 factory default for my test chip
  205 +
190 206 // make sure it worked (returns 0 if so)
191 207 if (devStatus == 0) {
192 208 // turn on the DMP, now that it's ready
85 Arduino/MPU6050/Examples/MPU6050_raw/MPU6050_raw.ino
@@ -3,7 +3,9 @@
3 3 // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
4 4 //
5 5 // Changelog:
6   -// 2011-10-07 - initial release
  6 +// 2013-05-08 - added multiple output formats
  7 +// - added seamless Fastwire support
  8 +// 2011-10-07 - initial release
7 9
8 10 /* ============================================
9 11 I2Cdev device library code is placed under the MIT license
@@ -29,30 +31,51 @@ THE SOFTWARE.
29 31 ===============================================
30 32 */
31 33
32   -// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
33   -// is used in I2Cdev.h
34   -#include "Wire.h"
35   -
36 34 // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
37 35 // for both classes must be in the include path of your project
38 36 #include "I2Cdev.h"
39 37 #include "MPU6050.h"
40 38
  39 +// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
  40 +// is used in I2Cdev.h
  41 +#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  42 + #include "Wire.h"
  43 +#endif
  44 +
41 45 // class default I2C address is 0x68
42 46 // specific I2C addresses may be passed as a parameter here
43 47 // AD0 low = 0x68 (default for InvenSense evaluation board)
44 48 // AD0 high = 0x69
45 49 MPU6050 accelgyro;
  50 +//MPU6050 accelgyro(0x69); // <-- use for AD0 high
46 51
47 52 int16_t ax, ay, az;
48 53 int16_t gx, gy, gz;
49 54
  55 +
  56 +
  57 +// uncomment "OUTPUT_READABLE_ACCELGYRO" if you want to see a tab-separated
  58 +// list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read,
  59 +// not so easy to parse, and slow(er) over UART.
  60 +#define OUTPUT_READABLE_ACCELGYRO
  61 +
  62 +// uncomment "OUTPUT_BINARY_ACCELGYRO" to send all 6 axes of data as 16-bit
  63 +// binary, one right after the other. This is very fast (as fast as possible
  64 +// without compression or data loss), and easy to parse, but impossible to read
  65 +// for a human.
  66 +//#define OUTPUT_BINARY_ACCELGYRO
  67 +
  68 +
50 69 #define LED_PIN 13
51 70 bool blinkState = false;
52 71
53 72 void setup() {
54 73 // join I2C bus (I2Cdev library doesn't do this automatically)
55   - Wire.begin();
  74 + #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  75 + Wire.begin();
  76 + #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  77 + Fastwire::setup(400, true);
  78 + #endif
56 79
57 80 // initialize serial communication
58 81 // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
@@ -67,6 +90,29 @@ void setup() {
67 90 Serial.println("Testing device connections...");
68 91 Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
69 92
  93 + // use the code below to change accel/gyro offset values
  94 + /*
  95 + Serial.println("Updating internal sensor offsets...");
  96 + // -76 -2359 1688 0 0 0
  97 + Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
  98 + Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
  99 + Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
  100 + Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
  101 + Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
  102 + Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
  103 + Serial.print("\n");
  104 + accelgyro.setXGyroOffset(220);
  105 + accelgyro.setYGyroOffset(76);
  106 + accelgyro.setZGyroOffset(-85);
  107 + Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
  108 + Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
  109 + Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
  110 + Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
  111 + Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
  112 + Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
  113 + Serial.print("\n");
  114 + */
  115 +
70 116 // configure Arduino LED for
71 117 pinMode(LED_PIN, OUTPUT);
72 118 }
@@ -79,14 +125,25 @@ void loop() {
79 125 //accelgyro.getAcceleration(&ax, &ay, &az);
80 126 //accelgyro.getRotation(&gx, &gy, &gz);
81 127
82   - // display tab-separated accel/gyro x/y/z values
83   - Serial.print("a/g:\t");
84   - Serial.print(ax); Serial.print("\t");
85   - Serial.print(ay); Serial.print("\t");
86   - Serial.print(az); Serial.print("\t");
87   - Serial.print(gx); Serial.print("\t");
88   - Serial.print(gy); Serial.print("\t");
89   - Serial.println(gz);
  128 + #ifdef OUTPUT_READABLE_ACCELGYRO
  129 + // display tab-separated accel/gyro x/y/z values
  130 + Serial.print("a/g:\t");
  131 + Serial.print(ax); Serial.print("\t");
  132 + Serial.print(ay); Serial.print("\t");
  133 + Serial.print(az); Serial.print("\t");
  134 + Serial.print(gx); Serial.print("\t");
  135 + Serial.print(gy); Serial.print("\t");
  136 + Serial.println(gz);
  137 + #endif
  138 +
  139 + #ifdef OUTPUT_BINARY_ACCELGYRO
  140 + Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF));
  141 + Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF));
  142 + Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF));
  143 + Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF));
  144 + Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF));
  145 + Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF));
  146 + #endif
90 147
91 148 // blink LED to indicate activity
92 149 blinkState = !blinkState;
2  Arduino/MPU6050/MPU6050.h
@@ -38,7 +38,7 @@ THE SOFTWARE.
38 38 #define _MPU6050_H_
39 39
40 40 #include "I2Cdev.h"
41   -#include <avr/pgmspace.h>
  41 +//#include <avr/pgmspace.h>
42 42
43 43
44 44
46 Arduino/MPU6050/MPU6050_6Axis_MotionApps20.h
@@ -40,7 +40,51 @@ THE SOFTWARE.
40 40 #define MPU6050_INCLUDE_DMP_MOTIONAPPS20
41 41
42 42 #include "MPU6050.h"
43   -#include <avr/pgmspace.h>
  43 +
  44 +// Tom Carpenter's conditional PROGMEM code
  45 +// http://forum.arduino.cc/index.php?topic=129407.0
  46 +#ifndef __arm__
  47 + #include <avr/pgmspace.h>
  48 +#else
  49 + // Teensy 3.0 library conditional PROGMEM code from Paul Stoffregen
  50 + #ifndef __PGMSPACE_H_
  51 + #define __PGMSPACE_H_ 1
  52 + #include <inttypes.h>
  53 +
  54 + #define PROGMEM
  55 + #define PGM_P const char *
  56 + #define PSTR(str) (str)
  57 + #define F(x) x
  58 +
  59 + typedef void prog_void;
  60 + typedef char prog_char;
  61 + typedef unsigned char prog_uchar;
  62 + typedef int8_t prog_int8_t;
  63 + typedef uint8_t prog_uint8_t;
  64 + typedef int16_t prog_int16_t;
  65 + typedef uint16_t prog_uint16_t;
  66 + typedef int32_t prog_int32_t;
  67 + typedef uint32_t prog_uint32_t;
  68 +
  69 + #define strcpy_P(dest, src) strcpy((dest), (src))
  70 + #define strcat_P(dest, src) strcat((dest), (src))
  71 + #define strcmp_P(a, b) strcmp((a), (b))
  72 +
  73 + #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
  74 + #define pgm_read_word(addr) (*(const unsigned short *)(addr))
  75 + #define pgm_read_dword(addr) (*(const unsigned long *)(addr))
  76 + #define pgm_read_float(addr) (*(const float *)(addr))
  77 +
  78 + #define pgm_read_byte_near(addr) pgm_read_byte(addr)
  79 + #define pgm_read_word_near(addr) pgm_read_word(addr)
  80 + #define pgm_read_dword_near(addr) pgm_read_dword(addr)
  81 + #define pgm_read_float_near(addr) pgm_read_float(addr)
  82 + #define pgm_read_byte_far(addr) pgm_read_byte(addr)
  83 + #define pgm_read_word_far(addr) pgm_read_word(addr)
  84 + #define pgm_read_dword_far(addr) pgm_read_dword(addr)
  85 + #define pgm_read_float_far(addr) pgm_read_float(addr)
  86 + #endif
  87 +#endif
44 88
45 89 /* Source is from the InvenSense MotionApps v2 demo code. Original source is
46 90 * unavailable, unless you happen to be amazing as decompiling binary by
46 Arduino/MPU6050/MPU6050_9Axis_MotionApps41.h
@@ -40,7 +40,51 @@ THE SOFTWARE.
40 40 #define MPU6050_INCLUDE_DMP_MOTIONAPPS41
41 41
42 42 #include "MPU6050.h"
43   -#include <avr/pgmspace.h>
  43 +
  44 +// Tom Carpenter's conditional PROGMEM code
  45 +// http://forum.arduino.cc/index.php?topic=129407.0
  46 +#ifndef __arm__
  47 + #include <avr/pgmspace.h>
  48 +#else
  49 + // Teensy 3.0 library conditional PROGMEM code from Paul Stoffregen
  50 + #ifndef __PGMSPACE_H_
  51 + #define __PGMSPACE_H_ 1
  52 + #include <inttypes.h>
  53 +
  54 + #define PROGMEM
  55 + #define PGM_P const char *
  56 + #define PSTR(str) (str)
  57 + #define F(x) x
  58 +
  59 + typedef void prog_void;
  60 + typedef char prog_char;
  61 + typedef unsigned char prog_uchar;
  62 + typedef int8_t prog_int8_t;
  63 + typedef uint8_t prog_uint8_t;
  64 + typedef int16_t prog_int16_t;
  65 + typedef uint16_t prog_uint16_t;
  66 + typedef int32_t prog_int32_t;
  67 + typedef uint32_t prog_uint32_t;
  68 +
  69 + #define strcpy_P(dest, src) strcpy((dest), (src))
  70 + #define strcat_P(dest, src) strcat((dest), (src))
  71 + #define strcmp_P(a, b) strcmp((a), (b))
  72 +
  73 + #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
  74 + #define pgm_read_word(addr) (*(const unsigned short *)(addr))
  75 + #define pgm_read_dword(addr) (*(const unsigned long *)(addr))
  76 + #define pgm_read_float(addr) (*(const float *)(addr))
  77 +
  78 + #define pgm_read_byte_near(addr) pgm_read_byte(addr)
  79 + #define pgm_read_word_near(addr) pgm_read_word(addr)
  80 + #define pgm_read_dword_near(addr) pgm_read_dword(addr)
  81 + #define pgm_read_float_near(addr) pgm_read_float(addr)
  82 + #define pgm_read_byte_far(addr) pgm_read_byte(addr)
  83 + #define pgm_read_word_far(addr) pgm_read_word(addr)
  84 + #define pgm_read_dword_far(addr) pgm_read_dword(addr)
  85 + #define pgm_read_float_far(addr) pgm_read_float(addr)
  86 + #endif
  87 +#endif
44 88
45 89 // NOTE! Enabling DEBUG adds about 3.3kB to the flash program size.
46 90 // Debug output is now working even on ATMega328P MCUs (e.g. Arduino Uno)

0 comments on commit fc92547

Please sign in to comment.
Something went wrong with that request. Please try again.