Example #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello.");
            Console.WriteLine("arg count: " + args.Length);
            for (int j = 0; j < args.Length; j++)
            {
                Console.WriteLine(args[j]);
            }


            double new_freq;

            if (args[0].Equals("RESET"))
            {
                new_freq = 0;
            }
            else
            {
                new_freq = (double)float.Parse(args[0]);
            }

            IntPtr usb_dev_handle = IntPtr.Zero;

            try
            {
                usb_dev_handle = USB.InitFindAndOpenDevice(OZY_VID, OZY_PID);
                Console.WriteLine("Device handle is: " + usb_dev_handle.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e.Message);
                return;
            }



            do
            {
                byte[] bytes = new byte[2];



                if (new_freq == 0.0)
                {
                    bytes[0] = 135;  // reset/mem ctl reg
                    bytes[1] = 0x80; // recall bit
                    if (!(OZY.Write_I2C(usb_dev_handle, SI570_ADDR, bytes)))
                    {
                        Console.WriteLine("Failed to write reset to device");
                        break;
                    }
                    Console.WriteLine("Device Reset!");
                    break;
                }
                bytes[0] = 135; // reset/mem ctl reg
                bytes[1] = 0x1; // recall bit
                if (!(OZY.Write_I2C(usb_dev_handle, SI570_ADDR, bytes)))
                {
                    Console.WriteLine("Failed to read NVM to RAM!");
                    break;
                }


                bytes    = new byte[1];
                bytes[0] = 0x7;  // want to start reading at addr 7;

                if (!(OZY.Write_I2C(usb_dev_handle, SI570_ADDR, bytes)))
                {
                    Console.WriteLine("Failed to write to I2C addr 0x55");
                    break;
                }

                bytes = new byte[6];   // need to read 6 bytes
                bool rc;
                rc = OZY.Read_I2C(usb_dev_handle, SI570_ADDR, ref bytes);
                Console.WriteLine("Read_I2C returned: " + rc);
                for (int i = 0; i < bytes.Length; i++)
                {
                    Console.WriteLine("data[" + i + "]: " + bytes[i]);
                }

                int hs_div = (bytes[0] >> 5) & 7;
                int n1     = (bytes[0] & 0x1f) << 2;
                n1 |= (bytes[1] >> 6) & 3;

                Console.WriteLine("HS_DIV divider: " + HS_MAP[hs_div] + " (" + hs_div + ")");
                Console.WriteLine("N1: " + n1);

                // see the si570 data sheet for info on this -- ref freq is 38 bits -- 10 bits whole number, 28 bits
                // fractional
                int ref_freq = (bytes[1] & 0x3f) << 4;
                ref_freq |= (bytes[2] >> 4) & 0xf;

                int ref_lo = (bytes[5] & 0xff);
                ref_lo |= (bytes[4] & 0xff) << 8;
                ref_lo |= (bytes[3] & 0xff) << 16;
                ref_lo |= (bytes[2] & 0xf) << 24;


                Console.WriteLine("ref_whole: " + ref_freq + " reff_fractional: " + ref_lo);
                double reff = ref_lo / TWO_POWER28;
                reff += ref_freq;

                Console.WriteLine("Ref Freq: " + reff);

                double fxtal = (FREQ_NOMINAL * (1 + n1) * HS_MAP[hs_div]) / reff;


                Console.WriteLine("fxtal: " + fxtal);



                Console.WriteLine("new_freq: " + new_freq);
                double total_divide_max = FOSC_MAX / new_freq;
                double total_divide_min = FOSC_MIN / new_freq;

                // given the new freq, find a HS * N1 we can live with
                // between total_dive_min and total_divide_max

                int min_divide = (int)Math.Ceiling(total_divide_min);
                int max_divide = (int)Math.Floor(total_divide_max);


                int new_hs    = -1;
                int total_div = 0;

                for (int i = min_divide; i <= max_divide; i++)
                {
                    total_div = i;
                    new_hs    = findGoodHS(total_div);
                    if (new_hs > 0)
                    {
                        break;
                    }
                }

                if (new_hs <= 0)
                {
                    Console.WriteLine("could not find good HS divider!!\n");
                    break;
                }

                int new_n1 = total_div / new_hs;
                Console.WriteLine("hs: " + new_hs + " n1: " + new_n1);

                double f_osc = new_freq * new_n1 * new_hs;

                double new_reff = f_osc / fxtal;

                Console.WriteLine("f_osc: " + f_osc + " new_reff: " + new_reff);

                // debug stuff
                // new_reff = 43.3639223910868;
                // new_hs = 4;
                // new_n1 = 22;

                // calculate reffreq integer
                Console.WriteLine("new_reff (float): " + new_reff);
                uint new_reff_whole = (uint)Math.Floor(new_reff);
                Console.WriteLine("new_reff_whole: " + new_reff_whole);
                ulong reff_bytes = ((ulong)new_reff_whole) << 28;
                Console.WriteLine("reff bytes (whole): " + reff_bytes);
                double new_reff_fraction = new_reff - (double)new_reff_whole;
                Console.WriteLine("new_reff_fraction: " + new_reff_fraction);
                uint fractional_bytes = (uint)Math.Truncate((double)new_reff_fraction * (double)TWO_POWER28);
                Console.WriteLine("fractional bytes: " + fractional_bytes);
                reff_bytes |= fractional_bytes;
                Console.WriteLine("reff bytes: " + reff_bytes);



                byte[] new_regs = new byte[6];

                new_regs[5] = (byte)(reff_bytes & 0xff);
                new_regs[4] = (byte)((reff_bytes >> 8) & 0xff);
                new_regs[3] = (byte)((reff_bytes >> 16) & 0xff);
                new_regs[2] = (byte)((reff_bytes >> 24) & 0xff);

                new_regs[1] = (byte)(((ulong)(reff_bytes >> 32)) & 0x3f);
                --new_n1;
                new_regs[1] |= (byte)((new_n1 & 3) << 6);

                new_regs[0] = (byte)((new_n1 >> 2) & 0x1f);
                int new_hs_bits = -1;
                for (int j = 0; j < HS_MAP.Length; j++)
                {
                    if (HS_MAP[j] == new_hs)
                    {
                        new_hs_bits = j;
                        break;
                    }
                }
                if (new_hs_bits == -1)
                {
                    Console.WriteLine("Bad HS: " + new_hs);
                    break;
                }
                new_regs[0] |= (byte)(new_hs_bits << 5);

                for (int j = 0; j < new_regs.Length; j++)
                {
                    Console.WriteLine("newRegs[" + j + "]: " + new_regs[j]);
                }

                bytes    = new byte[2];
                bytes[0] = 137;  // freezo dco register
                bytes[1] = 0x10; // assert freeze bit
                if (!(OZY.Write_I2C(usb_dev_handle, SI570_ADDR, bytes)))
                {
                    Console.WriteLine("Failed to Freeze DCO!");
                    break;
                }


                bytes    = new byte[7];
                bytes[0] = 0x7; // starting addr
                for (int j = 0; j < 6; j++)
                {
                    bytes[1 + j] = new_regs[j];
                }
                if (!(OZY.Write_I2C(usb_dev_handle, SI570_ADDR, bytes)))
                {
                    Console.WriteLine("Failed to Write new regs!");
                    break;
                }



                bytes    = new byte[2];
                bytes[0] = 137; // freezo dco register
                bytes[1] = 0x0; // deassert freeze bit
                if (!(OZY.Write_I2C(usb_dev_handle, SI570_ADDR, bytes)))
                {
                    Console.WriteLine("Failed to defrost DCO!");
                    break;
                }

                bytes[0] = 135;  // new freq reg
                bytes[1] = 0x40; // deassert freeze bit
                if (!(OZY.Write_I2C(usb_dev_handle, SI570_ADDR, bytes)))
                {
                    Console.WriteLine("Failed to assert new freq!");
                    break;
                }
            } while (false);

            Console.WriteLine("Closing USB device...");
            libUSB_Interface.usb_close(usb_dev_handle);
            Console.WriteLine("done...");
        }
Example #2
0
        static void Main(string[] args)
        {
            if ((args.Length < 5) || (args.Length == 0))
            {
                Console.WriteLine("usage: write_I2C <VID> <PID> <i2c_address in hex> <count in hex> <bytes in hex...>");
                return;
            }

            if (args[0].Length > 2)
            {
                if (args[0].Substring(0, 2) != "0x")
                {
                    Console.WriteLine("You must specify VID in Hex (0x0)");
                    return;
                }
                else
                {
                    args[0] = args[0].Substring(2);
                }
            }
            else
            {
                Console.WriteLine("You must specify VID in Hex (0x0)");
                return;
            }


            int vid = int.Parse(args[0], NumberStyles.HexNumber);

            if (args[1].Length > 2)
            {
                if (args[1].Substring(0, 2) != "0x")
                {
                    Console.WriteLine("You must specify PID in Hex (0x0)");
                    return;
                }
                else
                {
                    args[1] = args[1].Substring(2);
                }
            }
            else
            {
                Console.WriteLine("You must specify PID in Hex (0x0)");
                return;
            }

            int pid = int.Parse(args[1], NumberStyles.HexNumber);

            if (args[2].Length > 2)
            {
                if (args[2].Substring(0, 2) != "0x")
                {
                    Console.WriteLine("You must specify i2c_addr in Hex (0x0)");
                    return;
                }
                else
                {
                    args[2] = args[2].Substring(2);
                }
            }
            else
            {
                Console.WriteLine("You must specify i2c_addr in Hex (0x0)");
                return;
            }

            int i2c_addr = int.Parse(args[2], NumberStyles.HexNumber);

            if (args[3].Length > 2)
            {
                if (args[3].Substring(0, 2) != "0x")
                {
                    Console.WriteLine("You must specify count in Hex (0x0)");
                    return;
                }
                else
                {
                    args[3] = args[3].Substring(2);
                }
            }
            else
            {
                Console.WriteLine("You must specify count in Hex (0x0)");
                return;
            }

            int count = int.Parse(args[3], NumberStyles.HexNumber);

            byte[] bytes = new byte[args.Length - 4];

            for (int i = 4; i < args.Length; i++)
            {
                if (args[i].Length > 2)
                {
                    if (args[i].Substring(0, 2) != "0x")
                    {
                        Console.WriteLine("You must specify bytes in Hex (0x0)");
                        return;
                    }
                    else
                    {
                        args[i] = args[i].Substring(2);
                    }
                }
                else
                {
                    Console.WriteLine("You must specify bytes in Hex (0x0)");
                    return;
                }

                bytes[i - 4] = byte.Parse(args[i], NumberStyles.HexNumber);
            }

            IntPtr usb_dev_handle = IntPtr.Zero;

            try
            {
                usb_dev_handle = USB.InitFindAndOpenDevice(vid, pid);
                Console.WriteLine("Device handle is: " + usb_dev_handle.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e.Message);
                return;
            }

            if ((OZY.Write_I2C(usb_dev_handle, i2c_addr, bytes)))
            {
                Console.WriteLine("Wrote to address: " + i2c_addr);
            }
            else
            {
                Console.WriteLine("Failed to write address: " + i2c_addr);
            }
            Console.WriteLine("Closing device...");
            libUSB_Interface.usb_close(usb_dev_handle);
            Console.WriteLine("done...");
        }
Example #3
0
        public override void SetMicGain()
        {
            // This is used to set the MicGain and Line in when Ozy/Magister is used
            // The I2C settings are as follows:

            //    For mic input and boost on/off
            //    1E 00 - Reset chip
            //    12 01 - set digital interface active
            //    08 15 - D/A on, mic input, mic 20dB boost
            //    08 14 - ditto but no mic boost
            //    0C 00 - All chip power on
            //    0E 02 - Slave, 16 bit, I2S
            //    10 00 - 48k, Normal mode
            //    0A 00 - turn D/A mute off
            //    00 00 - set Line in gain to 0

            //    For line input
            //    1E 00 - Reset chip
            //    12 01 - set digital interface active
            //    08 10 - D/A on, line input
            //    0C 00 - All chip power on
            //    0E 02 - Slave, 16 bit, I2S
            //    10 00 - 48k, Normal mode
            //    0A 00 - turn D/A mute off
            //    00 00 - set Line in gain to 0

            if ((MainForm.PenneyPresent || MainForm.PennyLane) && (MainForm.Penny_version != 0) && (MainForm.KK_on == true))  // update mic gain on Penny or PennyLane TLV320
            {
                byte[] Penny_TLV320      = new byte[2];
                byte[] Penny_TLV320_data = new byte[16];

                // need to select the config data depending on the Mic Gain (20dB) selected
                if (MainForm.MicGain20dB)
                {
                    Penny_TLV320_data = new byte[] { 0x1e, 0x00, 0x12, 0x01, 0x08, 0x15, 0x0c, 0x00, 0x0e, 0x02, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00 }
                }
                ;
                else if (MainForm.LineIn)
                {
                    Penny_TLV320_data = new byte[] { 0x1e, 0x00, 0x12, 0x01, 0x08, 0x10, 0x0c, 0x00, 0x0e, 0x02, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00 }
                }
                ;

                else
                {
                    Penny_TLV320_data = new byte[] { 0x1e, 0x00, 0x12, 0x01, 0x08, 0x14, 0x0c, 0x00, 0x0e, 0x02, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00 }
                };

                // set the I2C interface speed to 400kHZ
                if (!(OZY.Set_I2C_Speed(hdev, 1)))
                {
                    MessageBox.Show("Unable to set I2C speed to 400kHz", "System Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // send the configuration data to the TLV320 on Penelope or PennyLane
                for (int x = 0; x < 16; x += 2)
                {
                    Penny_TLV320[0] = Penny_TLV320_data[x]; Penny_TLV320[1] = Penny_TLV320_data[x + 1];
                    if (!(OZY.Write_I2C(hdev, 0x1b, Penny_TLV320)))
                    {
                        MessageBox.Show("Unable to configure TLV320 on Penelope via I2C", "System Eror!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        // break out of the configuration loop
                        break;
                    }
                }
            }
        }