Darien Figueroa / Mbed OS Final_Program

Dependencies:   USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2cDevice.cs Source File

I2cDevice.cs

00001 /*******************************************************************************
00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All rights Reserved.
00003 * 
00004 * This software is protected by copyright laws of the United States and
00005 * of foreign countries. This material may also be protected by patent laws
00006 * and technology transfer regulations of the United States and of foreign
00007 * countries. This software is furnished under a license agreement and/or a
00008 * nondisclosure agreement and may only be used or reproduced in accordance
00009 * with the terms of those agreements. Dissemination of this information to
00010 * any party or parties not specified in the license agreement and/or
00011 * nondisclosure agreement is expressly prohibited.
00012 *
00013 * The above copyright notice and this permission notice shall be included
00014 * in all copies or substantial portions of the Software.
00015 *
00016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00017 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00018 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00019 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00020 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00021 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00022 * OTHER DEALINGS IN THE SOFTWARE.
00023 *
00024 * Except as contained in this notice, the name of Maxim Integrated
00025 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00026 * Products, Inc. Branding Policy.
00027 *
00028 * The mere transfer of this software does not imply any licenses
00029 * of trade secrets, proprietary technology, copyrights, patents,
00030 * trademarks, maskwork rights, or any other form of intellectual
00031 * property whatsoever. Maxim Integrated Products, Inc. retains all
00032 * ownership rights.
00033 *******************************************************************************
00034 */
00035 
00036 using System;
00037 using System.Collections.Generic;
00038 using System.Linq;
00039 using System.Text;
00040 
00041 namespace RPCSupport.Devices
00042 {
00043     public class I2cDevice : ClientDevice
00044     {
00045         int i2cSpeed = 1; // default to 400kHz
00046         const string CLASSNAME = "I2c";
00047         public I2cDevice(RPCClient client)
00048             : base(client)
00049         {
00050         }
00051         public void SetI2cSpeed(int speed)
00052         {
00053             i2cSpeed = speed;
00054         }
00055         public byte ReadReg(byte instance, byte slaveAddress, byte addr)
00056         {
00057             byte[] dataToWrite = new byte[] { addr };
00058             byte numberToRead = 1;
00059             byte[] data = WriteRead(instance, slaveAddress, dataToWrite, numberToRead);
00060             return data[0];
00061             //string reply1 = Call(CLASSNAME, "Init", instance.ToString("X2"), i2cSpeed.ToString("X2"));
00062             //string reply2 = Call(CLASSNAME, "Write", slaveAddress.ToString("X2"), "01", addr.ToString("X2"));
00063             //string reply3 = Call(CLASSNAME, "Read", slaveAddress.ToString("X2"), "01");
00064             //return client.pipeline.StringToByte(reply3);
00065         }
00066         public void WriteReg(byte instance, byte slaveAddress, byte addr, byte data)
00067         {
00068             byte[] dataToWrite = new byte[] { addr, data };
00069             byte numberToRead = 0;
00070             WriteRead(instance, slaveAddress, dataToWrite, numberToRead);
00071             //Call(CLASSNAME, "WriteReg", instance.ToString("X2"), slaveAddress.ToString("X2"), addr.ToString("X2"), data.ToString("X2"));
00072         }
00073         public byte[] ReadMultiReg(byte instance, byte slaveAddress, byte addr, byte numBytes)
00074         {
00075             byte[] dataToWrite = new byte[] { addr };
00076             byte numberToRead = (byte)numBytes;
00077             byte[] data = WriteRead(instance, slaveAddress, dataToWrite, numberToRead);
00078             return data;
00079             //string reply = Call(CLASSNAME, "ReadMultiReg", instance.ToString("X2"), slaveAddress.ToString("X2"), addr.ToString("X2"), numBytes.ToString("X2"));
00080             //return client.pipeline.StringToMultiBytes(reply, numBytes);
00081         }
00082 
00083         public void WriteMultiReg(byte instance, byte slaveAddress, byte addr, byte numberOf, byte[] data)
00084         {
00085             List<byte> dataToWrite = new List<byte>();
00086             dataToWrite.Add(addr);
00087             dataToWrite.AddRange(data);
00088             WriteRead(instance, slaveAddress, dataToWrite.ToArray(), 0);
00089             //StringBuilder sb = new StringBuilder();
00090             //for (int i = 0; i < numberOf; i++)
00091             //{
00092             //    sb.Append(data[i].ToString("X2"));
00093             //    if (i != numberOf - 1)
00094             //    {
00095             //        sb.Append(" ");
00096             //    }
00097             //}
00098             //Call(CLASSNAME, "WriteMultiReg", instance.ToString("X2"), slaveAddress.ToString("X2"), addr.ToString("X2"), numberOf.ToString("X2"), sb.ToString());
00099         }
00100 
00101         public void ReadReg(DeviceSupport.DeviceDetails deviceDetails, DeviceSupport.RegisterInfo reg)
00102         {
00103             byte[] dataToWrite = new byte[] { (byte)reg.address };
00104             byte numberToRead = (byte)reg.numBytes;
00105             byte[] data = WriteRead((byte)deviceDetails.i2cInstance, (byte)deviceDetails.i2cSlaveAddress, dataToWrite, numberToRead);
00106             for (int i = 0; i < data.Length; i++)
00107             {
00108                 reg.data[i] = (int)data[i];
00109             }            
00110             //if (reg.numBytes == 1)
00111             //{
00112             //    byte data = ReadReg((byte)deviceDetails.i2cInstance, (byte)deviceDetails.i2cSlaveAddress, (byte)reg.address);
00113             //    reg.data[0] = (int)data;
00114             //}
00115             //else
00116             //{
00117             //    byte[] data = ReadMultiReg((byte)deviceDetails.i2cInstance, (byte)deviceDetails.i2cSlaveAddress, (byte)reg.address, (byte)reg.numBytes);
00118             //    for (int i = 0; i < data.Length; i++)
00119             //    {
00120             //        reg.data[i] = (int)data[i];
00121             //    }
00122             //}
00123         }
00124 
00125         internal void WriteReg(DeviceSupport.DeviceDetails deviceDetails, DeviceSupport.RegisterInfo reg)
00126         {
00127             byte[] dataToWrite;
00128             byte numberToRead = 0;
00129 
00130             dataToWrite = new byte[reg.numBytes + 1];
00131             dataToWrite[0] = (byte)reg.address;
00132             for (int i = 0; i < reg.numBytes; i++)
00133             {
00134                 dataToWrite[i + 1] = (byte)(reg.data[i]);
00135             }
00136 
00137             WriteRead((byte)deviceDetails.i2cInstance, (byte)deviceDetails.i2cSlaveAddress, dataToWrite, numberToRead);
00138             //if (reg.numBytes == 1)
00139             //{
00140             //    WriteReg((byte)deviceDetails.i2cInstance, (byte)deviceDetails.i2cSlaveAddress, (byte)reg.address, (byte)reg.data[0]);
00141             //}
00142             //else
00143             //{
00144             //    byte[] data = new byte[reg.numBytes];
00145             //    for (int i = 0; i < data.Length; i++)
00146             //    {
00147             //        data[i] = (byte)reg.data[i];
00148             //    }
00149             //    WriteMultiReg((byte)deviceDetails.i2cInstance, (byte)deviceDetails.i2cSlaveAddress, (byte)reg.address, (byte)reg.numBytes, data);
00150             //}
00151         }
00152 
00153         public byte[] WriteRead(byte instance, byte slaveAddress, byte[] dataToWrite, byte numberToRead)
00154         {
00155             string dataToWriteStr = "";
00156             StringBuilder sb = new StringBuilder();
00157             for (int i = 0; i < dataToWrite.Length; i++)
00158             {
00159                 sb.Append(dataToWrite[i].ToString("X2") + " ");
00160             }
00161             dataToWriteStr = sb.ToString().Trim();
00162             string reply = Call(CLASSNAME, "WriteRead", instance.ToString("X2"), slaveAddress.ToString("X2"), dataToWrite.Length.ToString("X2"),dataToWriteStr,  numberToRead.ToString("X2"));
00163             return client.pipeline.StringToMultiBytes(reply, numberToRead);
00164         }
00165     }
00166 }