SAMD21 and I2CDevlib

Hi there,

I am trying to use the I2CDevlib with my MPU6050 and the SAMD21 breakout from Sparkfun.

I imported the board but am now getting this following error:

Compiling library “I2Cdev”
“C:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1/bin/arm-none-eabi-g++” -mcpu=cortex-m0plus -mthumb -c -g -Os -w -std=gnu++11 -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD -DF_CPU=48000000L -DARDUINO=10805 -DARDUINO_SAMD_ZERO -DARDUINO_ARCH_SAMD -D__SAMD21G18A__ -DUSB_VID=0x1B4F -DUSB_PID=0x8D21 -DUSBCON ‘-DUSB_MANUFACTURER=“SparkFun”’ ‘-DUSB_PRODUCT=“SFE SAMD21”’ “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\tools\CMSIS\4.5.0/CMSIS/Include/” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\tools\CMSIS-Atmel\1.1.0/CMSIS/Device/ATMEL/” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\SparkFun\hardware\samd\1.4.0\cores\arduino” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\SparkFun\hardware\samd\1.4.0\variants\arduino_zero” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\SparkFun\hardware\samd\1.4.0\libraries\SAMD_AnalogCorrection\src” “-IC:\Users\Daniel\Documents\Arduino\libraries\U8g2\src” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\SparkFun\hardware\samd\1.4.0\libraries\Wire” “-IC:\Users\Daniel\Documents\Arduino\libraries\I2Cdev” “-IC:\Users\Daniel\Documents\Arduino\libraries\MPU6050” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\SparkFun\hardware\samd\1.4.0\libraries\SPI” “C:\Users\Daniel\Documents\Arduino\libraries\I2Cdev\I2Cdev.cpp” -o “C:\Users\Daniel\AppData\Local\Temp\arduino_build_164922\libraries\I2Cdev\I2Cdev.cpp.o”
In file included from C:\Users\Daniel\Documents\Arduino\libraries\I2Cdev\I2Cdev.h:80:0,

from C:\Users\Daniel\Documents\Arduino\libraries\I2Cdev\I2Cdev.cpp:46:

C:\Users\Daniel\Documents\Arduino\libraries\I2Cdev\I2Cdev.cpp: In static member function ‘static int8_t I2Cdev::readBytes(uint8_t, uint8_t, uint8_t, uint8_t*, uint16_t)’:

C:\Users\Daniel\Documents\Arduino\libraries\I2Cdev\I2Cdev.cpp:276:62: error: ‘BUFFER_LENGTH’ was not declared in this scope

for (uint8_t k = 0; k < length; k += min(length, BUFFER_LENGTH)) {

^

C:\Users\Daniel\AppData\Local\Arduino15\packages\SparkFun\hardware\samd\1.4.0\cores\arduino/Arduino.h:100:24: note: in definition of macro ‘min’

#define min(a,b) ((a)<(b)?(a):(b))

^

C:\Users\Daniel\Documents\Arduino\libraries\I2Cdev\I2Cdev.cpp: In static member function ‘static int8_t I2Cdev::readWords(uint8_t, uint8_t, uint8_t, uint16_t*, uint16_t)’:

C:\Users\Daniel\Documents\Arduino\libraries\I2Cdev\I2Cdev.cpp:414:70: error: ‘BUFFER_LENGTH’ was not declared in this scope

for (uint8_t k = 0; k < length * 2; k += min(length * 2, BUFFER_LENGTH)) {

^

C:\Users\Daniel\AppData\Local\Arduino15\packages\SparkFun\hardware\samd\1.4.0\cores\arduino/Arduino.h:100:24: note: in definition of macro ‘min’

#define min(a,b) ((a)<(b)?(a):(b))

There is an issue report for this here: https://github.com/jrowberg/i2cdevlib/issues/271 unfortunately the author of i2cdevlib has not taken any action.

The problem is that the library relies on the macro BUFFER_LENGTH, which is set in the Arduino AVR Boards Wire library. However, the version of the Wire library used in the hardware package of your SAMD21 breakout from Sparkfun does not define that macro. Arduino SAMD Boards also has this issue, which I'm sure was inherited by the SparkFun hardware package. From this issue report: https://github.com/arduino/ArduinoCore-samd/issues/175 we can see that the Arduino developers are aware of this inconsistency but don't want to define that macro in the SAMD Wire library because it was a bad idea to use such a unoriginal macro name in the AVR Wire library since it could cause name conflicts.

The solution is explained to some extent in the first link above but not very clearly so I'll provide specific instructions for you to follow to solve the problem:

  • Select the SparkFun SAMD21 board from the Tools > Board menu
  • File > Examples > Wire > digital_potentiometer
  • Sketch > Show Sketch Folder - this will open the folder containing the SparkFun SAMD Boards Wire library's digital_potentiometer example sketch.
  • Navigate up two folder levels to the Wire folder.
  • Open the file Wire.h in a text editor
  • Add the following code at line 31:
#define BUFFER_LENGTH 64
  • Save the file.

After doing that you should be able to use the I2Cdev library with your SAMD21 board.