USB Performance

Whenever time permits can someone over at GHI look into the performance hit USB causes? If I connect USB mouse and keyboard (without even using them) I see screen renders drop by an order of at least 10.

Perhaps there’s a constant poll going on? A missing sleep? Something is definitely using way more resources that one would think reasonable.

@ Skewworks - Could you give up a quick example that shows the performance problem?

Sorry, I totally fell asleep on this one. The sample is below and it’s really easy to work and see the issue.

Startup the program on any device with screen attached and already configured. Do NOT plug in USB yet. Watch the lovely scroll test for a second or two. OK, now plug in a USB mouse and watch it grind to a near halt.

using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Presentation.Media;

using GHI.Premium.USBHost;
using GHI.Premium.System;

namespace USBDelay
{
    public class Program
    {

        #region Variables

        static Bitmap screen, bmp;

        static USBH_Keyboard _keyboard;
        static USBH_Mouse _mouse;

        #endregion

        public static void Main()
        {
            int w, h, bpp, rot;

            // Get screen size
            HardwareProvider.HwProvider.GetLCDMetrics(out w, out h, out bpp, out rot);

            // Create bitmps
            screen = new Bitmap(w, h);
            bmp = new Bitmap(w, h);
            DrawPattern(bmp);

            // Enable USB
            USBHostController.DeviceConnectedEvent += USBHostController_DeviceConnectedEvent;
            USBHostController.DeviceDisconnectedEvent += USBHostController_DeviceDisconnectedEvent;

            // Begin scrolling
            scroll();
        }

        static void DrawPattern(Bitmap dest)
        {
            int yStart = 0;

            for (int y = 0; y < dest.Height; y += 8)
            {
                for (int x = yStart; x < dest.Width; x += 16)
                {
                    dest.DrawRectangle(0, 0, x, y, 8, 8, 0, 0, Color.White, 0, 0, Color.White, 0, 0, 256);
                }
                if (yStart == 0)
                    yStart = 8;
                else
                    yStart = 0;
            }
        }

        static void scroll()
        {
            int x = 0;
            while (true)
            {
                x++;
                if (x <= -screen.Width)
                    x += screen.Width;
                if (x > 0)
                    x = x - screen.Width;
                screen.DrawImage(x, 0, bmp, 0, 0, screen.Width, screen.Height);
                if (x != 0)
                    screen.DrawImage(screen.Width + x, 0, bmp, 0, 0, -x, screen.Height);

                screen.Flush();
            }
        }

        static void USBHostController_DeviceConnectedEvent(USBH_Device device)
        {
            switch (device.TYPE)
            {
                case USBH_DeviceType.Keyboard:
                        _keyboard = new USBH_Keyboard(device);
                        Debug.Print("Keyboard connected");
                    return;
                case USBH_DeviceType.Mouse:
                        _mouse = new USBH_Mouse(device);
                        Debug.Print("Mouse connected");
                    return;
            }
        }

        static void USBHostController_DeviceDisconnectedEvent(USBH_Device device)
        {
            switch (device.TYPE)
            {
                case USBH_DeviceType.Keyboard:
                    _keyboard = null;
                    Debug.Print("Keyboard disconnected");
                    break;
                case USBH_DeviceType.Mouse:
                    _mouse = null;
                    Debug.Print("Mouse disconnected");
                    break;
            }
        }

    }
}

@ Skewworks - We will take a look. Have you tried it on 4.3?

Just a thought but does it throw the DeviceConnected event repeatedly or only once? It sounds like it may be continuing to throw the event as long as something is attached. Does it resume normal speed after you disconnect?

That’s not the issue. Normal speed does resume after disconnect.

Not yet, but I should.

I’ll be honest, I popped open the assemblies. :-[
I’m pretty sure I know what’s causing the 4.2 delay and it does look like it will work much better on 4.3.

Guess it’s time to upgrade to 4.3. I was hoping to put that off a bit as I have had connection and deployment issues with it.

@ Skewworks - What sort of connection issues are you having? We’d like to have the next SDK coming for 2015 be as stable as possible.

For the Raptor for example it keeps changing it’s name between G400_G400 and G400_Gadgeteer. Why, I don’t know but it does it all on its own after deploys. So I have to either go in every time and select the target or hit deploy 2-3x.

BTW, testing so far shows an enormous improvement on USB. I need to add more code to make sure USB is actually connecting (none of my graphics stuff is in 4.3 so I have no cursor to show me the mouse).

Ah, OK. Not as good as I thought. Forgot you have to call .Start() now on USB. Still a definite improvement.

This is exactly the symptom I had when I thought I had bricked my raptor… I would load the new firmware with fez config, and the device would become a “G400_G400”, and none of my projects would deploy from VS. After 2 or 3 retries it became G400_Gadgeteer (or perhaps I manually set the friendly name to Gadgeteer to cause that, I don’t remember)

@ Skewworks - Could you show the steps you follow that have the Raptor alternate between G400 and Gadgeteer?

Hi,

Where i can get distrib?
for Fez Spider (on MF 4.3)

containing

using GHI.Premium.USBHost;
using GHI.Premium.System;

you download GHI’s SDK from the “Support” link above.

The premium libraries no longer exist in 4.3

They are there, just not called premium.

Offtopic:
Canyou describe how to implement base usb device with MF 4.3 (before it will be keyb/mouse or etc) on usb-host?
Something like

i want try do some interesting thing
https://www.ghielectronics.com/community/forum/topic?id=18369

https://www.ghielectronics.com/docs/36/usb-host describes it perfectly…

Brett, before my question i already learn all samples from direct links
and try api calls with spider already.

I need more low level access for this features.

then the next phase is you looking at the API library. https://www.ghielectronics.com/downloads/man/Library_Documentation_v4.3/html/fb58d3ab-e16b-ece3-b245-481d52eab3b9.htm

And if you still need to understand more about USB, you’re going to exceed what can be learnt here.

Please let’s talk into https://www.ghielectronics.com/community/forum/topic?id=18369
not here…

p.s. i dont look api or they source - i have it and samples with api
i not beginning member of GHI devices and understand how it realised.
i just try find way for use generic usbDevice handler (NOT Keyb/Mouse or etc relisation, just generic for it’s all)

p.p.s.
if you detaled see in this topic sample has

but in samples for UsbHost (https://www.ghielectronics.com/docs/36/usb-host)
it’s just

I mean USBHostController != Controller realisation