I’m in the middle of writing the software for our very first LoRa device, and the module we used as a basis is based around an Atmel SAM D20 MCU. Which means writing code against Atmel Software Framework (ASF). I figured that porting the code to read a sensor over I2C to ASF would be very straight forward, but it took me 2 days. So for my future self: here’s how to read data from the MPU-6050 over I2C in ASF.
Arduino
The Arduino code is pretty clear. It uses the Wire.h library, sends a wakeup command, and then you can start reading data.
Now, let’s port it…
ASF
First things first, let’s set up the I2C bus. Atmel uses something called SERCOM as their serial communication interface. I think on my module there are six or something, so look in the datasheet of your module to see which SERCOM bus is configured for I2C (in my datasheet it was listed in the pinout).
Please note that ADDRESS can also be 0x69, if AD0 is high.
Resistors
When doing I2C normally you need two pull up resistors on SCL and SDA. Verify whether your board / dev kit already has these resistors in place, otherwise adding them as well on your breadboard will probably throw an error.
Powering up the sensor
To start communicating with the sensor we need to power it up. If you look at the Arduino code it looks like it is sending two packets, but this is actually wrong (and it took me quite some time to find this out). It’s one package with 2 bytes.
Now here comes a stupid thing. When I was doing this, at some point my status code when writing was STATUS_ERR_BAD_ADDRESS. This was because I had VCC to 3.3V, not to 5V! Light on the module was still on, but it didn’t work. So check that you have 5V input.
Who am I
Now check whether we can read registers from the sensor. There’s a ‘who am I’ register that will return 0x68.
Reading data
Now that we set this thing up we can start reading some data. First give it some time to boot up and then start reading.
And that’s it. Data now flows in and we can start consuming it. Here’s a nice photo of the sensor on a breadboard.