private bool SendAndReceive(string[] commands, out Frame[] result)
        {
            int  i;
            bool success = true;

            spiPort.reset();
            for (i = 0; i < commands.Length; i++)
            {
                spiPort.writeHex(commands[i]);
            }
            // append an extra command to read result of the last command
            spiPort.writeHex(READ_ID);
            // wait for the result of all commands to come
            int expectedLength = commands.Length * 4;

            while (spiPort.read_avail() < expectedLength)
            {
                string errmsg = "";
                YAPI.Sleep(3, ref errmsg);
            }
            string hexstr = spiPort.readHex(2 * expectedLength);

            // Parse result
            result = new Frame[commands.Length];
            for (i = 0; i < commands.Length; i++)
            {
                Frame query = new Frame(commands[i]);
                result[i] = new Frame(hexstr.Substring(8 + 8 * i, 8));
                if (result[i].crc_error || result[i].addr != query.addr)
                {
                    success = false;
                }
                _chip_ready = (result[i].rs == 1);
            }

            return(success);
        }
        public override bool Setup()
        {
            spiPort = YSpiPort.FirstSpiPort();
            if (spiPort == null)
            {
                Console.WriteLine("No Yocto-Spi detected");
                return(false);
            }

            YModule      module       = spiPort.get_module();
            string       errmsg       = "";
            string       serialNumber = module.get_serialNumber();
            YPowerOutput powerOutput  = YPowerOutput.FindPowerOutput(serialNumber + ".powerOutput");

            powerOutput.set_voltage(YPowerOutput.VOLTAGE_OUT3V3);
            spiPort.set_voltageLevel(YSpiPort.VOLTAGELEVEL_TTL3V);
            spiPort.set_spiMode("2000000,0,msb");
            spiPort.set_protocol("Frame:1ms");
            spiPort.set_ssPolarity(YSpiPort.SSPOLARITY_ACTIVE_LOW);
            module.saveToFlash();
            YAPI.Sleep(25, ref errmsg);
            spiPort.writeHex(SET_MODE_4);
            YAPI.Sleep(5, ref errmsg);

            string[] commands = { READ_STATUS, READ_STATUS, READ_STATUS, READ_ID, SET_ANGLES };
            Frame[]  result;
            if (!SendAndReceive(commands, out result))
            {
                Console.WriteLine("Failed to initialize SCL3300 (communication error)");
                return(false);
            }
            if (!_chip_ready)
            {
                Console.WriteLine("SCL3300 startup failed (rs={4})", result[2].rs);
                return(false);
            }
            if ((result[3].data & 0xff) != 0xc1)
            {
                Console.WriteLine("Unexpected SCL3300 identification (WHOAMI={0})", (result[3].data & 0xff));
                return(false);
            }
            if (!DecodeStatus(result[2]))
            {
                Console.WriteLine("SCL3300 Status bad, chip reset is required");
                return(false);
            }
            Console.WriteLine("SCL3300 is ready");
            return(true);
        }