Exemplo n.º 1
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Nusbio initialization");
            var serialNumber = Nusbio.Detect();

            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("nusbio not detected");
                return;
            }

            var  sclPin            = NusbioGpio.Gpio0; // White
            var  sdaPin            = NusbioGpio.Gpio1; // Green
            byte MCP23008_I2C_ADDR = 0x20;             // Microship MCP 23008 = 8 gpios

            // Gpio 0 and 1 are reserved for I2C Bus
            var standardOutputGpios = new List <NusbioGpio>()
            {
                NusbioGpio.Gpio2,
                NusbioGpio.Gpio3,
                NusbioGpio.Gpio4,
                NusbioGpio.Gpio5,
                NusbioGpio.Gpio6,
                NusbioGpio.Gpio7
            };
            var extendedOutputGpios = new List <string>()
            {
                NusbioGpioEx.Gpio8,
                NusbioGpioEx.Gpio9,
                NusbioGpioEx.Gpio10,
                NusbioGpioEx.Gpio11,
                NusbioGpioEx.Gpio12,
                NusbioGpioEx.Gpio13,
                NusbioGpioEx.Gpio14,
#if !GPIO_15_AS_INPUT
                NusbioGpioEx.Gpio15, // Configured as input
#endif
            };

            using (var nusbio = new Nusbio(serialNumber)) // ,
            {
                _mcp = new MCP23008(nusbio, sdaPin, sclPin);
                _mcp.Begin(MCP23008_I2C_ADDR);

                Cls(nusbio);

                #if GPIO_15_AS_INPUT
                var inputGpio = NusbioGpioEx.Gpio15;
                _mcp.SetPinMode(inputGpio, PinMode.InputPullUp);
                #endif

                while (nusbio.Loop())
                {
                    #if GPIO_15_AS_INPUT
                    if (_mcp.GPIOS[inputGpio].DigitalRead() == PinState.High)
                    {
                        ConsoleEx.Write(0, 2, string.Format("[{0}] Button Down", DateTime.Now), ConsoleColor.Cyan);
                    }
                    else
                    {
                        ConsoleEx.Write(0, 2, string.Format("[{0}] Button Up     ", DateTime.Now), ConsoleColor.Cyan);
                    }
                    #endif

                    foreach (var eg in extendedOutputGpios)
                    {
                        _mcp.GPIOS[eg].State = !_mcp.GPIOS[eg].State;
                        _mcp.GPIOS[eg].DigitalWrite(_mcp.GPIOS[eg].State);
                    }
                    //foreach (var sg in standardOutputGpios)
                    //{
                    //    nusbio.GPIOS[sg].State = !nusbio.GPIOS[sg].State;
                    //    nusbio.GPIOS[sg].DigitalWrite(nusbio.GPIOS[sg].State);
                    //}
                    TimePeriod.Sleep(500);

                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;
                        if (k == ConsoleKey.A)
                        {
                            Cls(nusbio);
                        }
                        if (k == ConsoleKey.Q)
                        {
                            _mcp.AllOff();
                            break;
                        }
                    }
                }
            }
            Console.Clear();
        }
Exemplo n.º 2
0
        static void ReadAndVerifyEEPROMPage_BatchRead(int numberOfPageToRead, int expectedSingleValue = -1)
        {
            Console.Clear();

            var batchPageSize = 128; // Transfer batch of 32page x 256byte = 8k

            ConsoleEx.TitleBar(0, string.Format("Performance test reading all {0}k in {1} pages batch mode", _eeprom.MaxKByte, batchPageSize));
            ConsoleEx.Gotoxy(0, 2);

            var t = Stopwatch.StartNew();

            EEPROM_25AA1024_UnitTests.PerformanceTest_ReadPage(_eeprom, batchPageSize,
                                                               (pageIndex, maxPage) => ConsoleEx.WriteLine(0, 3, string.Format("{0:000} % done", 1.0 * pageIndex / maxPage * 100.0), ConsoleColor.Yellow));
            t.Stop();

            Console.WriteLine("Data:{0}kb, Time:{1}ms, {2:0.00} kb/s", _eeprom.MaxKByte, t.ElapsedMilliseconds, _eeprom.MaxByte * 1.0 / t.ElapsedMilliseconds);
            Console.WriteLine("Hit enter key");
            Console.ReadLine();
        }
Exemplo n.º 3
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Nusbio initialization");

            Nusbio.ActivateFastMode();

            var serialNumber = Nusbio.Detect();

            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("nusbio not detected");
                return;
            }

            //Nusbio.BaudRate = Nusbio.BaudRate / 16;

            using (var nusbio = new Nusbio(serialNumber))
            {
                _eeprom = new EEPROM_25AA1024(
                    nusbio: nusbio,
                    clockPin: NusbioGpio.Gpio0,
                    mosiPin: NusbioGpio.Gpio1,
                    misoPin: NusbioGpio.Gpio2,
                    selectPin: NusbioGpio.Gpio3
                    );

                if (nusbio.Type == NusbioType.NusbioType1_Light)
                {
                    _eeprom._spi.SoftwareBitBangingMode = true;
                }

                _eeprom.Begin();

                // Noticed that we cannot detect if the EEPROM is connected or
                // not based on SPI command. The SPI command does not fail.
                // But return a buffer with garbage mostly all byte set to 255
                // So based on the expected data of the first page we could
                // detect of the EEPROM is connected or not
                var r = _eeprom.ReadPage(0, _eeprom.PAGE_SIZE);
                if (r.Succeeded)
                {
                    if (r.Buffer[0] == 255)
                    {
                        Console.WriteLine("SPI EEPROM 25AA1024 not detected, hit enter to continue");
                        Console.ReadLine();
                    }
                }

                Cls(nusbio);

                while (nusbio.Loop())
                {
                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;

                        if (k == ConsoleKey.W)
                        {
                            var a = ConsoleEx.Question(23, string.Format("Execute Write/Read/Write Test {0}k now Y)es, N)o", _eeprom.MaxKByte), new List <char>()
                            {
                                'Y', 'N'
                            });
                            if (a == 'Y')
                            {
                                Console.Clear(); ConsoleEx.TitleBar(0, "Writing EEPROM");

                                var testValue = 1 + 4 + 16 + 64;
                                _eeprom.WriteAll(0, EEPROM_25AA1024.MakeBuffer((byte)testValue, _eeprom.MaxByte), (pageIndex, maxPage) => ConsoleEx.WriteLine(0, 2, string.Format("{0:000} % done", 1.0 * pageIndex / maxPage * 100.0), ConsoleColor.Yellow));

                                Console.Clear(); ConsoleEx.TitleBar(0, "Reading EEPROM");
                                var dataRead = _eeprom.ReadAll(16, (pageIndex, maxPage) => ConsoleEx.WriteLine(0, 2, string.Format("{0:000} % done", 1.0 * pageIndex / maxPage * 100.0), ConsoleColor.Yellow));
                                for (var z = 0; z < _eeprom.MaxByte; z++)
                                {
                                    if (dataRead[z] != testValue)
                                    {
                                        throw new ArgumentException("Writing/Reading error");
                                    }
                                }
                                WriteEEPROMPage(512);
                                ReadAndVerifyEEPROMPage_BatchRead(512);
                            }
                        }

                        if (k == ConsoleKey.R)
                        {
                            ReadAndVerifyEEPROMPage(10);
                            Cls(nusbio);
                        }

                        if (k == ConsoleKey.A)
                        {
                            if (nusbio.Type != NusbioType.NusbioType1_Light)
                            {
                                ReadAndVerifyEEPROMPage_BatchRead(512);
                            }
                            //ReadAndVerifyEEPROMPage(512);
                        }
                        if (k == ConsoleKey.B)
                        {
                            // This mode is slow, only read the first 64 pages out of 512
                            ReadAndVerifyEEPROMOnyByteAtTheTime(64);
                        }

                        if (k == ConsoleKey.Q)
                        {
                            break;
                        }

                        Cls(nusbio);
                    }
                }
            }
            Console.Clear();
        }
Exemplo n.º 4
0
        static void ReadAndVerifyEEPROMPage(int numberOfPageToRead, int expectedSingleValue = -1)
        {
            Console.Clear();
            ConsoleEx.TitleBar(0, string.Format("Performance test reading all {0}k no batch standard SPI code", _eeprom.MaxKByte));
            ConsoleEx.Gotoxy(0, 2);

            var totalErrorCount = 0;
            var t = Stopwatch.StartNew();

            byte[] buf;

            for (var p = 0; p < numberOfPageToRead; p++)
            {
                if (p % 50 == 0 || p <= 10)
                {
                    Console.WriteLine("Reading page {0}", p);
                }

                // The method ReadPage, use the default SPI method to transfer data
                var r = _eeprom.ReadPage(p * _eeprom.PAGE_SIZE, _eeprom.PAGE_SIZE);
                if (r.Succeeded)
                {
                    buf = r.Buffer;
                    for (var i = 0; i < _eeprom.PAGE_SIZE; i++)
                    {
                        var expected = i;
                        if (p == 2)
                        {
                            expected = NEW_WRITTEN_VALUE_1;
                        }
                        if (p == 3)
                        {
                            expected = NEW_WRITTEN_VALUE_2;
                        }

                        if (expectedSingleValue != -1)
                        {
                            expected = expectedSingleValue;
                        }

                        if (buf[i] != expected)
                        {
                            Console.WriteLine("Failed Page:{0} [{1}] = {2}, expected {3}", p, i, buf[i], expected);
                            totalErrorCount++;
                        }
                    }
                }
                else
                {
                    Console.WriteLine("ReadBuffer failure");
                }
            }
            t.Stop();
            Console.WriteLine("{0} error(s), Data:{1}kb, Time:{2}, {3:0.00} b/s, {4:0.00} Kb/s",
                              totalErrorCount,
                              _eeprom.PAGE_SIZE * numberOfPageToRead / 1024.0,
                              t.ElapsedMilliseconds,
                              _eeprom.PAGE_SIZE * numberOfPageToRead * 1.0 / t.ElapsedMilliseconds * 1000,
                              _eeprom.PAGE_SIZE * numberOfPageToRead * 1.0 / t.ElapsedMilliseconds * 1000 / 1024);
            Console.WriteLine("Hit enter key");
            Console.ReadLine();
        }