Пример #1
0
        public static async Task <int> IncrementOrDecrementStrain(SgAdjust sgAdjust)
        {
            if (_deviceCount == 0)
            {
                await Init();
            }


            // set the SPI xFer params for I/O expander
            uint baudRate2      = 250000; //1000000
            uint idleCsVal2     = 0x1ff;
            uint activeCsVal2   = 0x1fd;  //GP1 CS PIN, remaining pins are GP //0x1ee GP4 and GP0 set as active low CS
            uint csToDataDly2   = 0;
            uint dataToDataDly2 = 0;
            uint dataToCsDly2   = 0;
            uint txFerSize2     = 3;    // I/O expander xFer size set to 4
            byte spiMd2         = 0;
            uint csMask4        = 0x02; //GP1 as CS  // 0x10 set GP4 as CS

            byte[] txData = new byte[3], rxData = new byte[3];
            switch (sgAdjust)
            {
            // set the expander config params
            case SgAdjust.Increment:
                txData[0] = 0x60;
                break;

            case SgAdjust.Decrement:
                txData[0] = 0xE0;
                break;

            case SgAdjust.Save:
                txData[0] = 0x20;
                //SmartLog.WriteLine(
                //    $"txData:{txData.ToHex()}\nrxData:{rxData.ToHex()}\nbaudRate2:{baudRate2}\ntxFerSize2:{txFerSize2}\ncsMask4:{csMask4}\nidleCsVal2:{idleCsVal2}\nactiveCsVal2:{activeCsVal2}\ncsToDataDly2:{csToDataDly2}\ndataToCsDly2:{dataToCsDly2}\ndataToDataDly2:{dataToDataDly2}\nspiMd2:{spiMd2}");
                break;
            }

            txData[1] = 0x00;
            txData[2] = 0x00;
            // send the data
            // use the extended SPI xFer API first time in order to set all the parameters
            // the subsequent xFer with the same device may use the simple API in order to save CPU cycles
            _response = MCP2210.M_Mcp2210_xferSpiDataEx(_deviceHandle, txData, rxData, ref baudRate2, ref txFerSize2,
                                                        csMask4, ref idleCsVal2, ref activeCsVal2, ref csToDataDly2,
                                                        ref dataToCsDly2, ref dataToDataDly2, ref spiMd2);
            if (_response != MCP2210.M_E_SUCCESS)
            {
                MCP2210.M_Mcp2210_Close(_deviceHandle);
                SmartLog.WriteErrorLine($"Sg ({sgAdjust.ToString()}) transfer error: " + _response);
                _deviceCount = 0;
                return(_response);
            }

            SmartLog.WriteLine($"Sg ({sgAdjust.ToString()}) transfer response: " + _response);
            return(0);
        }
Пример #2
0
        //Disconnect function
        private void Disconnect(string Device)
        {
            int DeviceNumber; //Device number used to select which device will be disconnected

            //Use input string to select Device number
            if (Device == "SevenSeg")
            {
                DeviceNumber = SevenSeg.SelectedIndex;
            }
            else if (Device == "ADC")
            {
                DeviceNumber = ADC.SelectedIndex;
            }
            else if (Device == "Temp")
            {
                DeviceNumber = Temp.SelectedIndex;
            }
            else if (Device == "Motor")
            {
                DeviceNumber = Motor.SelectedIndex;
            }
            else
            {
                DeviceNumber = DacDevice.SelectedIndex;
            }

            //Disconnect command
            MCP2210.M_Mcp2210_Close(deviceHandle[DeviceNumber]);
            //Get error
            error = MCP2210.M_Mcp2210_GetLastError();
            //Update error and connection status
            if (error != MCP2210.M_E_SUCCESS)
            {
                LastError.Items.Add(error.ToString());
                LastError.Items.Add("Device Error!");
                LastError.SelectedIndex = LastError.Items.Count - 1;
                LastError.ScrollIntoView(LastError.SelectedItem);
            }
            else
            {
                LastError.Items.Add(error.ToString());
                LastError.Items.Add("Device Disconnected!");
                LastError.SelectedIndex = LastError.Items.Count - 1;
                LastError.ScrollIntoView(LastError.SelectedItem);
            }
        }
Пример #3
0
        //Timer setup and communication for Temperature sensor
        private void dispatcherTimerTemp_Tick(object sender, EventArgs e)
        {
            int  DeviceNumber     = Temp.SelectedIndex; //Device chosen by user on GUI
            uint pbaudRate77      = 1000000;            //baud rate
            uint pidleCsVal77     = 0x1ff;              //Bit is 1 for clock idle low
            uint pactiveCsVal77   = 0x1ef;              //GP4 set as active low CS
            uint pcsToDataDly77   = 0;                  //time delay from CS to data, quanta of 100us
            uint pdataToDataDly77 = 0;                  //time delay from data to data, quanta of 100us
            uint pdataToCsDly77   = 0;                  //time delay from data to CS, quanta of 100us
            uint ptxferSize77     = 2;                  //TC77 txfer size set to 2 bytes
            byte pspiMd77         = 0;                  //SPI mode from connected devices datasheet
            uint csmask77         = 0x10;               //1 represents CS, set GP4 as CS

            //Setup transfer and receive bytes
            byte[] txData77 = new byte[2], rxData77 = new byte[2];
            txData77[0] = 0x00;
            txData77[1] = 0x00;
            rxData77[0] = 0x00;
            rxData77[1] = 0x00;

            //Tx and Rx data from TC77
            error = MCP2210.M_Mcp2210_xferSpiDataEx(deviceHandle[DeviceNumber], txData77, rxData77, ref pbaudRate77, ref ptxferSize77, csmask77, ref pidleCsVal77, ref pactiveCsVal77,
                                                    ref pcsToDataDly77, ref pdataToCsDly77, ref pdataToDataDly77, ref pspiMd77);
            if (error != MCP2210.M_E_SUCCESS)
            {
                MCP2210.M_Mcp2210_Close(deviceHandle[DeviceNumber]);
                LastError.Items.Add(" Transfer error: " + error);
                LastError.SelectedIndex = LastError.Items.Count - 1;
                LastError.ScrollIntoView(LastError.SelectedItem);
            }

            //Setting last three bits to zero as they are don't cares from datasheet
            rxData77[1] = (byte)(rxData77[1] & 0xF8);

            byte[] temper = new byte[1];
            temper[0]   = rxData77[1];
            rxData77[1] = rxData77[0];
            rxData77[0] = temper[0];

            //Calculations and display
            temp  = BitConverter.ToInt16(rxData77, 0);
            temp /= 128;
            //GUI string display
            Temperature.Text = temp.ToString("0.00");
            //7-Seg display setup
            temp1     = (int)temp;
            DigitTemp = temp1.ToString().ToCharArray();
            if (DigitTemp.Length > 1)
            {
                DigitOneTemp = DigitTemp[0];
                DigitTwoTemp = DigitTemp[1];
            }
            else if (DigitTemp.Length > 0)
            {
                DigitOneTemp = '0';
                DigitTwoTemp = Digit[0];
            }

            if (CharToSeg.ContainsKey(DigitOneTemp) && CharToSeg.ContainsKey(DigitTwoTemp))
            {
                digitOneTemp = (uint)(0x100 | CharToSeg[DigitOneTemp]);
                digitTwoTemp = (uint)(0x080 | CharToSeg[DigitTwoTemp]);
            }
            else
            {
                digitOneTemp = (uint)(0x100 | CharToSeg['0']);
                digitTwoTemp = (uint)(0x080 | CharToSeg['0']);
            }

            //GUI bar display
            if (temp < 15)
            {
                temp = 15;
            }
            else if (temp > 40)
            {
                temp = 40;
            }
            temp          = (temp - 15) * 100 / 25;
            TempBar.Value = temp;

            //Forcing the CommandManager to raise the RequerySuggested event
            CommandManager.InvalidateRequerySuggested();
        }
Пример #4
0
 public static void Close()
 {
     MCP2210.M_Mcp2210_Close(_deviceHandle);
     _deviceCount = 0;
 }
Пример #5
0
        static int Main(string[] args)
        {
            // Default SPI parameters
            bool   shouldShowHelp  = false;
            uint   deviceIndex     = 0;
            uint   pbaudRate2      = 1000000;
            uint   pidleCsVal2     = 0xfff; // Idle value for CS
            uint   pactiveCsVal2   = 0x000; // Active value for CS
            uint   pcsToDataDly2   = 0;     // CS to data delay
            uint   pdataToCsDly2   = 0;     // Last data byte to CS delay
            uint   pdataToDataDly2 = 0;     // Delay between bytes
            uint   ptxferSize2     = 4;     // Bytes to Transfer per SPI Transaction
            byte   pspiMd2         = 0;     // SPI Mode
            uint   csIndex         = 0;
            String gpio            = null;
            String data            = null;

            // Set the command line parser
            var options = new OptionSet {
                { "i|index=", $"index of the MCP2210 device (defaults to {deviceIndex})", (uint i) => deviceIndex = i },
                { "b|baud=", $"baud rate in MHz (defaults to {pbaudRate2})", (uint b) => pbaudRate2 = b },
                { "csToDataDly=", $"delay between CS assertion and data send in units of 100 us (defaults to {pcsToDataDly2})", (uint d) => pcsToDataDly2 = d },
                { "dataToCsDly=", $"delay between the last byte sent and CD deassert in units of 100 us (defaults to {pdataToCsDly2})", (uint d) => pdataToCsDly2 = d },
                { "dataToDataDly=", $"delay between bytes in units of 100 us (defaults to {pdataToDataDly2})", (uint d) => pdataToDataDly2 = d },
                { "m|mode=", $"SPI mode (defaults to {pspiMd2})", (byte m) => pspiMd2 = m },
                { "cs=", $"CS index (defaults to {csIndex})", (uint i) => csIndex = i },
                { "d|data=", "Array of bytes to send in hex format separated by commas", s => data = s },
                { "gpioW=[number][1|0]", "Write 1 or 0 to the specified GPIO (example: gpioW=21, write 1 to GPIO 2)", s => gpio = s },
                { "h|help", "show this message and exit", h => shouldShowHelp = h != null },
            };

            try
            {
                // Parse the command line
                options.Parse(args);

                // Show help menu
                if (shouldShowHelp)
                {
                    options.WriteOptionDescriptions(Console.Out);
                    return(0);
                }

                // We always need data
                if (data == null)
                {
                    return(0);
                }
            }
            catch (OptionException e)
            {
                // output some error message
                Console.Write("greet: ");
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `greet --help' for more information.");
            }

            // Search connected MCP2210 devices
            int devCount = MCP2210.M_Mcp2210_GetConnectedDevCount(DEFAULT_VID, DEFAULT_PID);

            if (DEBUG)
            {
                Console.WriteLine(devCount + " devices found");
            }

            if (devCount > 0)
            {
                // Connect to the MCP2210
                StringBuilder path         = new StringBuilder();
                IntPtr        deviceHandle = new IntPtr();
                int           res;

                // Open the device first
                deviceHandle = MCP2210.M_Mcp2210_OpenByIndex(DEFAULT_VID, DEFAULT_PID, deviceIndex, path);
                res          = MCP2210.M_Mcp2210_GetLastError();
                if (res != MCP2210.M_E_SUCCESS)
                {
                    Console.WriteLine("Failed to open connection");
                    return(-1);
                }

                // Check if any GPIO must be written
                if (gpio != null && gpio.Length == 2)
                {
                    uint   gpioNumber = uint.Parse(gpio[0].ToString());
                    uint   value      = uint.Parse(gpio[1].ToString());
                    byte[] pins       = new byte[MCP2210.M_MCP2210_GPIO_NR];

                    uint gpioValue = (uint)(1 << (int)gpioNumber);

                    // Set everything as GPIO
                    for (int n = 0; n < pins.Length; ++n)
                    {
                        pins[n] = (byte)MCP2210.M_MCP2210_PIN_DES_GPIO;
                    }

                    // Set GPIO
                    MCP2210.M_Mcp2210_SetGpioConfig(deviceHandle, (byte)MCP2210.M_MCP2210_VM_CONFIG, pins, gpioValue, 0, (byte)MCP2210.M_MCP2210_REMOTE_WAKEUP_DISABLED,
                                                    (byte)MCP2210.M_MCP2210_INT_MD_CNT_NONE, (byte)MCP2210.M_MCP2210_SPI_BUS_RELEASE_DISABLED);
                }

                // Convert the data string to bytes to send
                string[] hexStrings = data.Split(',');
                byte[]   txData     = new byte[hexStrings.Length], rxData = new byte[hexStrings.Length];
                for (int n = 0; n < hexStrings.Length; ++n)
                {
                    txData[n] = Convert.ToByte(hexStrings[n], 16);
                }

                // Create the CS mask
                uint csmask = (uint)((int)1 << (int)csIndex);

                // Set the SPI transaction length to the number of bytes that will be sent
                ptxferSize2 = (uint)(txData.Length > 65535 ? 65535 : txData.Length);

                // Configure the MCP2210 and send the data, everything in one call
                res = MCP2210.M_Mcp2210_xferSpiDataEx(deviceHandle, txData, rxData, ref pbaudRate2, ref ptxferSize2, csmask, ref pidleCsVal2, ref pactiveCsVal2, ref pcsToDataDly2,
                                                      ref pdataToCsDly2, ref pdataToDataDly2, ref pspiMd2);
                if (res != MCP2210.M_E_SUCCESS)
                {
                    MCP2210.M_Mcp2210_Close(deviceHandle);
                    Console.WriteLine(" Transfer error: " + res);
                    return(res);
                }

                // Write the output data in hex format
                var txString = new StringBuilder(">TxData: ");
                var rxString = new StringBuilder(">RxData: ");
                for (int n = 0; n < txData.Length; ++n)
                {
                    txString.Append(txData[n].ToString("X"));
                    txString.Append(",");
                    rxString.Append(rxData[n].ToString("X"));
                    rxString.Append(",");
                }

                // Remove last comma
                txString.Length -= 1;
                rxString.Length -= 1;

                Console.WriteLine(txString);
                Console.WriteLine(rxString);

                MCP2210.M_Mcp2210_Close(deviceHandle);
            }

            return(0);
        }