Main Site Documentation

I2c locking required?


#1

When calling Write…do i have to do locking to avoid 2 thread writing at the same time?

        public void Write(byte reg, byte val)
        {
            int written;
            writeBuffer[0] = (byte)reg;
            writeBuffer[1] = val;
            writeTrans[0] = I2CDevice.CreateWriteTransaction(writeBuffer);
            written = _Device.Execute(writeTrans, 100);

            if (written != writeBuffer.Length)
            {
                if (Program.DebugLevel >= 1) Debug.Print(val + " has NOT been written to " + reg + "  I2C device[" + _Device.Config.Address + "].");
            }
            else
            {
                if (Program.DebugLevel >= 4) Debug.Print(val + " has been written to " + reg + "  I2C device[" + _Device.Config.Address + "].");
            }

        }

#2

lock( _Device )
{
writeBuffer[0] = (byte)reg;
writeBuffer[1] = val;
writeTrans[0] = I2CDevice.CreateWriteTransaction(writeBuffer);
written = _Device.Execute(writeTrans, 100);
}