A couple of people had requested that I give some example code on using the I2C peripheral in an interrupt controlled manner. By using it in this manner, the processor can achieve other tasks while also doing I2C transactions giving a multi-tasking feel to a program.
In this example, I combine using the DRDY output of the HMC5883L magnetometer as an edge triggered interrupt to trigger the transfer of 6x bytes through I2C representing the magnetometer data for each axis: X, Y and Z. The DRDY output can be configured to have a variable speed and in this case, it is set to work at the maximum rate of 75Hz. The X, Y and Z values are calculated in the interrupt as this only required a shift and or so its not particularly taxing. I tested this using Semihosting and a small magnet to affect the readings.
Wiring up the HMC5883L magnetometer. All components to the right of row 30 are unrelated to this project!
The actual pinouts for the HMC5883L are in the code which will be posted on my Github. SDA and SCL are connected to PB6 and PB7 meaning I2C1 is the hardware component used. All information required for the sensor can be found in the datasheet.
Testing the Semihosting output with a magnet close to the magnetometer. The sensitivity was a little bit too high here so going too close caused saturation!
The code for this project can be found on my Github.