Skip to content

An example of writing and reading to a software I2C bus using an Azure Sphere

Notifications You must be signed in to change notification settings

Workshopshed/AzureSphereBitBangI2C

Repository files navigation

Azure Sphere Bit Bang I2C

An example of writing and reading to an I2C device (HMC5883 Digital compass) using an Azure Sphere.

Note that since this example was completed the API for the hardware I2C has been made available and it would make sense to use that in preference to software.

https://docs.microsoft.com/en-us/azure-sphere/reference/applibs-reference/applibs-i2c/i2c-overview

We needed to port this software protocol (bitbang) as the hardware version was not yet available. Based on the work of https://github.com/AlaskaResearchCubeSat/bit-bang-I2C

Capture of the I2C protocol0

Usage

For the full details of use see main.c

Create a structure to hold the file handles and define the pins used,

static i2cbus_t i2cbus1 = {
    .sclFd = -1,
    .scl = MT3620_RDB_HEADER2_PIN13_GPIO,
    .sdaFd = -1,
    .sda = MT3620_RDB_HEADER2_PIN11_GPIO
};

Setup the bus

i2c_bb_setup(&i2cbus1);

Writing a value "0" a register HMC5883_REGISTER_MAG_MR_REG_M using i2c_bb_tx

const unsigned char msgEnable[2] = { HMC5883_REGISTER_MAG_MR_REG_M, 0x00 }; // Enable Magnetometer, Continuous mode, Low Speed I2C 
i2c_bb_tx(&i2cbus1, HMC5883_ADDRESS_MAG, &msgEnable[0], 2);

Reading 6 bytes from a register HMC5883_REGISTER_MAG_OUT_X_H_M using i2c_bb_rx

const unsigned char msgSelectMagRegister[1] = { HMC5883_REGISTER_MAG_OUT_X_H_M }; // Get mag reading

unsigned char rec[6];
short ret;

//Write register to read
i2c_bb_tx(&i2cbus1, HMC5883_ADDRESS_MAG, &msgSelectMagRegister[0], 1);

//Read 6 bytes
ret = i2c_bb_rx(&i2cbus1, HMC5883_ADDRESS_MAG, &rec[0], 6);

About

An example of writing and reading to a software I2C bus using an Azure Sphere

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages