Malban
Posts: 21
Joined: Thu Aug 01, 2019 6:16 pm
Location: Germany
Contact: Website

[solved] USB access on a PiZero

Sat Apr 17, 2021 1:52 pm

Hi,
the subject probably came up before - but I have not found anything I could use.
I am programming baremetal on a PiZero - I would like to use a USB keyboard. So far I have had no success.

I tried:
- 2 different logitech keyboards
- one no name keyboard
- one cherry keyboard
- one apple keyboard

I tried this with:
- chadwicks precompiled kernel.img
- based on chadwick: Arm32_64_USB, a precompiled version and an own built
- Circle versions: 8, 22, 41, and the most current 43.3


Error message with: Arm32_64_USB

Code: Select all

HCD: Hardware: OT2.80a (BCM2708a).
HCD: Attach Device USB Root Hub. Address:1 Class:9 USB:2.0, 1 configuration(s), 1 interface(s).
HCD:  -Product:       FAKED Root Hub (tm).
HCD:  -Configuration: FAKE config string.
Physical host power on
Setting split control, addr: 1 port: 0, packetSize: PacketSize: 8

Result: -8 Action: 0x10000001 tempInt: 0x00000082 tempSplit: 0x80000080 Bytes sent: 0

HCD: SETUP packet to device: 0 req: 0x6 req Type: 0x80 Speed: 2 PacketSize: 0 LowNode: 1 LowPort: 0 Error: -10

Enumeration: Step 1 on device 2 failed, Result: 0.

 +-USB Root Hub id: 1 port: 0 speed: Full packetsize: 64 
    ¿-New Device (Not Ready) id: 0 port: 0 speed: Low packetsize: 8 
Which basically means the lib has no idea what is wrong.

Latest circle Version gives:
Image


Is there some sort of magic spell I have to cast in order for it to work?
Did anyone succeed in accessing the USB port with a PiZero (apart from Linux) - If so, could you please share your experience?

I know there is at least one other implementation which I did not try (Ultibo) - but I shied away from Pascal...

Thx

Malban
Last edited by Malban on Sat Apr 17, 2021 5:34 pm, edited 1 time in total.

rst
Posts: 509
Joined: Sat Apr 20, 2013 6:42 pm
Location: Germany

Re: USB access on a PiZero

Sat Apr 17, 2021 5:18 pm

This is unusual. There may be USB keyboards, which do not work with Circle, but one of these devices, you have tried, should work at least. Have you tried this hardware configuration with Raspberry Pi OS for comparison? You may try the setting usbpowerdelay=2000 in the file cmdline.txt on the SD card, but I'm unsure, if this will help. Which firmware version are you using? On Raspberry Pi OS you can request it using:

Code: Select all

strings /boot/start.elf | fgrep VC_BUILD

Malban
Posts: 21
Joined: Thu Aug 01, 2019 6:16 pm
Location: Germany
Contact: Website

Re: USB access on a PiZero

Sat Apr 17, 2021 5:34 pm

Thx for your answer.
I have been examining further.

Although I had tried the Chadwick code with different PiZeros - I didn't try circle with different PiZeros.
It seems that one of my Pi Zeros (naturally the one I am developing with) has a faulty USB port.

I tried two other piZeros, which I had lying around - and circle is working now.

I have compiled the uspi library and will try to configure it with my own setup.
While we are "talking", ... within my own baremetal stuff, I make heavily use of interrupts, which are not allowed to be interrupted by anything else.

The description of the uspi library says something about different modes asynchronous, bulk and synchronous modes.
I guess that is dependent on the usb device you are connecting.

I am not very experienced when it comes to USB programming - is there a way to use the USB keyboard without using interrupts?

The old Chadwick library seemed to suggest, that you could get away with polling - but than again, I did not get that to work ;-).

Malban

PS.
If interested in what I am doing:
http://vide.malban.de/pitrex
http://vide.malban.de/pitrex/pitrex-baremetal-download

rst
Posts: 509
Joined: Sat Apr 20, 2013 6:42 pm
Location: Germany

Re: USB access on a PiZero

Sun Apr 18, 2021 8:10 am

You are welcome. Your project is interesting and has hard real-time requirements. For a similar project I was asked before, if I could rewrite the USB driver to work with polling. Personally I'm not a friend of this, because it makes the design more difficult, especially for a general purpose library. Also the timing can be a problem. But as CSUD proves it is possible. Unfortunately CSUD has not been developed further since 2014 by Alex Chadwick. There are a number of forks, but AFAIK only for the RPi 3.

BTW. Bulk is one type of an endpoint of an USB device, which is the target of transfers on USB. Transfers to/from bulk endpoints are reliable, but without an asserted bandwidth. In synchronous mode, a function returns, when the transfer is done. In asynchronous mode, a function starts a transfer and returns. On completion a callback routine is invoked later.

Malban
Posts: 21
Joined: Thu Aug 01, 2019 6:16 pm
Location: Germany
Contact: Website

Re: [solved] USB access on a PiZero

Sun Apr 18, 2021 7:33 pm

@rst

Question - do you think there will come anything bad out of the following?
(at the moment I am using only USB - Keyboard!)

I have implemented USPI into my program, and sort of did a small environment header like described in uspios.h.
Thus at the moment I can compile and use the complete uspi.

I run the "normal" setup routine, and it sets everything up fine. I can use the keyboard (yeah!)

But as said - I don't want it to disturb my own interrupts. So after the first initialization, I withdraw all interrupt handling and sort of
"disable" uspi that way.

During my own program I call now the "old" interrupt handler, as was set with the "ConnectInterrupt()" routine, approximately 50 times per second (I do not "wait" for an interrupt, I just keep calling the handler, as I have seen via output, that the USB device calls it also heaps and heaps of time without providing any information).

So effectively I am "polling" (or pushing???) the interrupt handler - without using an interrupt at all.
... and "oh wonder" - with this simple polling mechanism, I still am able to use the keyboard, since the called "handler" still "catches" all keystrokes and calls the set keyPressedHandler().

That was actually nice and easy :-).

Do you think there will be side effects to this approach, which I can not see with a short and ugly test program that I am currently running?

Thx for you time and input...

Cheers
Malban

rst
Posts: 509
Joined: Sat Apr 20, 2013 6:42 pm
Location: Germany

Re: [solved] USB access on a PiZero

Mon Apr 19, 2021 4:43 am

@Malban Interesting idea to poll by simply calling the interrupt handler! The interrupt handler checks, if an interrupt condition is asserted and if not, it does nothing. So this works. But the "virtual" interrupt latency may be as bad as 20ms in this case (50 polls per second). This will probably work with many keyboards, but there may be devices, which will not work.

BTW. The keyboard is polled by the USB driver all the time in an interval of some milliseconds. That's why the interrupt handler is called that often. There is no other way to fetch the key pressed info from the device in reasonable time.

Return to “Bare metal, Assembly language”