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

Re: Introducing USPi - A bare metal USB driver written in C

Fri Oct 16, 2015 8:45 am

Seems to be a time critical application. I suppose your network protocol does not have retransmissions and the rendering may take relatively long? That's difficult.

I don't know if it is possible to have some "poll receiver" function which gets called at several places in your main loop also at some places in the rendering code and while waiting for the timer. This function should also call USPiReceiveFrame() as long there are frames available. The Ethernet device can buffer some received frames but not endlessly.

The problem is that there is no easy "frame has arrived" interrupt. The Ethernet device has to be polled over a USB bulk-in endpoint and each poll has to be started triggered by something and may end with "no frame available". I could only use the system timer for this. But unfortunately I'm not convinced that this would result in a better performance and reliability in this case.

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Introducing USPi - A bare metal USB driver written in C

Thu Jan 28, 2016 7:43 am

Thank you again for taking the time to write this great USB Driver. I have learned a good deal from it, as it is done a lot simpler than my previous experiences with USB, I wish I could have learned as much about USB before I wrote modules for USB before (I made it way more complex than needed then).

I had played with your USPi a bit before having moved, when I lost my RPi. As soon as my new RPi2 arrives I will be playing with it again, and taking many more notes than before.

I hope to eventually do three things, largely thanks to what I am learning about USB from USPi, and a couple of other easier ways around USB. These three things are, in chronological order:
  • [li]Modify USPi, with a little added code for better support of some HW, to compile into a RISC OS module replacing the existing USB modules on the RPi.[/li]
    [li]Use the notes I take, and have taken to implement a new RISC OS USB module, as well as a couple of USB class driver modules from the ground up, under a true open source license [that is with unreasonable restriction, eg NOT GPL]. Likely to be either a MIT license or a two clause BSD license.[/li]
    [li]Implement a bare metal USB stack under a true open source license that is a lot easier to interface to, for any language, though especially for those of us that like ARM Assembly.[/li]
All things I would have never dreamed of doing again if not for USPi. So thank you again. I will likely end up using some of the code I wrote in the past, at least for a few parts (that were coded simply enough).

Also thank you fort the updates and examples you have provided in USPi, as these have been quite helpful as well.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

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

Re: Introducing USPi - A bare metal USB driver written in C

Thu Jan 28, 2016 12:15 pm

Thank you and good luck!

ydk
Posts: 6
Joined: Mon Feb 25, 2013 11:22 am

Re: Introducing USPi - A bare metal USB driver written in C

Sat Feb 13, 2016 5:43 pm

I built the USPi. It built the kernel.img in the sample/keyboard, sample/storage, sample/ethernet etc

I tried to test the keyboard, so I copied the kernel.img from sample/keyboard in the SD card and then powered on the raspberry pi.

I have connected my raspberry pi to my TV using HDMI, but then nothing happens if I press any key using my USB keyboard.

Can i expect some prints on the TV (like some logs etc which I see in loggerWrite2() routine finally calling screen.c file) ?

Please correct if my understanding is wrong. Please let me know how do I test this and where to see the prints ?

note: my raspberry pi + TV set + HDMI works perfectly fine with the standard raspbian image so no problems in my setup.

Regards
YK

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

Re: Introducing USPi - A bare metal USB driver written in C

Sun Feb 14, 2016 12:11 pm

Did you copy the firmware files bootcode.bin, fixup.dat and start.elf to the SD card along with kernel.img? That's required.

There must be some logging output to the screen (top left corner) from the sample even if the keyboard is not detected.

If you use a RPi2B you have to set "RASPPI ?= 2" in the file Rules.mk before building.

ydk
Posts: 6
Joined: Mon Feb 25, 2013 11:22 am

Re: Introducing USPi - A bare metal USB driver written in C

Sun Feb 14, 2016 12:38 pm

Hi rst,

thanks for the reply.. I got it working, the problem was with the HDMI output format.

For raspbian my config.txt worked without hdmi_Safe=1, but now when I enable hdmi_safe=1, I was able to get the output for 480p and able to detect and display the key presses.

Many thanks

Rgards
Yk

ydk
Posts: 6
Joined: Mon Feb 25, 2013 11:22 am

Re: Introducing USPi - A bare metal USB driver written in C

Sun Feb 21, 2016 5:40 pm

Hi Rst,

I want to try to port the USPi to freertos. I tried integrating the USPi source into bitthunder OS(based on freertos) by James Walmsley. I am finding it difficult to get it compiled because USPi uses the built in environment which initializes the raspberry pi system (like timers, interrupts, etc) and there are many function calls in USPi source which use this environment setup e.g. (SetPowerStateOn(), ConnectInterrupt() etc).

By any chance have you ported USPi to any standard rtos like freertos or other?

regards
ydk

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

Re: Introducing USPi - A bare metal USB driver written in C

Mon Feb 22, 2016 11:41 am

ydk wrote:I am finding it difficult to get it compiled because USPi uses the built in environment which initializes the raspberry pi system (like timers, interrupts, etc) and there are many function calls in USPi source which use this environment setup e.g. (SetPowerStateOn(), ConnectInterrupt() etc).
The environment is only provided as example and as a base for the samples. You have to implement the functions declared in include/uspios.h in your system and provide them to the USPi library. Most systems have own functions for memory allocation, interrupt handling etc. and you have to provide a wrapper to them.
By any chance have you ported USPi to any standard rtos like freertos or other?
I tested the USPi library with the provided environment only but I know that other people use the library with their own system.

Please note that it can be difficult to use the library in a RTOS because such systems have requirements according to interrupt latency which the library cannot meet.

ydk
Posts: 6
Joined: Mon Feb 25, 2013 11:22 am

Re: Introducing USPi - A bare metal USB driver written in C

Mon Feb 22, 2016 5:22 pm

ok thanks for the reply. let me try and see if i can be able to get it working.

regards
yk

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

Re: Introducing USPi - A bare metal USB driver written in C

Tue Mar 22, 2016 9:56 pm

USPi has been updated to run on the Raspberry Pi 3 in 32-bit mode. You have to define

Code: Select all

RASPPI = 3
in Rules.mk or Config.mk to build this version.

The USPi library itself is unchanged but the USPi Environment has been updated.

tarkshya
Posts: 47
Joined: Tue Nov 03, 2015 7:28 pm

Re: Introducing USPi - A bare metal USB driver written in C

Thu Apr 07, 2016 9:38 pm

Any idea how we can figure out when a device is connected or disconnected.

There is this interrupt DWHCI_CORE_INT_MASK_DISCONNECT, but enabling it resets the processor itself (when a device is disconnected/reconnected). Some portion of the code in USPI is commented that uses this interrupt, i tried enabling it, doesn't seem to work though.

Tried some jugglery with DWHCI_CORE_INT_MASK_PORT_INTR, doesn't work either, Any guidance/pointers will be appreciated.



Thanks in advance

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

Re: Introducing USPi - A bare metal USB driver written in C

Fri Apr 08, 2016 7:15 am

tarkshya wrote:Any idea how we can figure out when a device is connected or disconnected.
You cannot. This is not implemented in USPi. USB devices have to be already connected on boot and must not be removed while the system is running. The comments were for testing only.

tarkshya
Posts: 47
Joined: Tue Nov 03, 2015 7:28 pm

Re: Introducing USPi - A bare metal USB driver written in C

Fri Apr 08, 2016 3:57 pm

Hello rst, any idea, how the linux driver subsystem allows it? like USB hotplugging, is it a timer based approach or interrupt driven ? can't we attempt something like that in USPI?


Also i am seeing a lot of interrupts, even when the system is not doing anything

long back there was this thread

viewtopic.php?f=28&t=7866&start=125

which mentions 8000 interrupts/sec (that happens to be that USB 2.0 125 ms frame thing).

One of the suggestions there was to route the interrupt to an FIQ and in the FIQ check if there was any activity in the ports, and if so call the IRQ handler. Probably a kernel patch was added to do that, but i am not able to find that, any idea what they might have done? Or if you could tell, how to check USB activity, does DWG provide any registers or something like that ?


The xinu os which has a simpler and clean implementation (for this synopsys controller) neither seems to implement hotplugging nor the interrupt reduction thing.

Any idea on how to approach both these problems (or i have to read the entire linux usb driver before attempting anything) ?

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

Re: Introducing USPi - A bare metal USB driver written in C

Fri Apr 08, 2016 4:55 pm

tarkshya wrote:Hello rst, any idea, how the linux driver subsystem allows it? like USB hotplugging, is it a timer based approach or interrupt driven ?
There is a host port register which has bits for connect and disconnect. You can enable an interrupt if a status change on this port occurs. This only works on the models A, A+ or Pi Zero. When a hub is connected to the root port the connect/disconnect events are handled by the hub driver. There is an interrupt endpoint which has to poll the hub continuously for status changes.
Also i am seeing a lot of interrupts, even when the system is not doing anything
With USPi there should only be USB interrupts if there is some device attached which has an interrupt endpoint (e.g. keyboard, mouse). It is simply polled for events.
which mentions 8000 interrupts/sec (that happens to be that USB 2.0 125 ms frame thing).
I think this is still the case with Raspbian on the RPi 1 and Zero. RPi 2 and 3 have more hardware support to avoid this. USPi has a different approach for the "micro-frame scheduler" which doesn't use that much interrupts but may disable the IRQ for longer if low- or full-speed devices are attached.
The xinu os which has a simpler and clean implementation (for this synopsys controller) neither seems to implement hotplugging nor the interrupt reduction thing.
Getting rid of hotplugging was a design decision to reduce complexity also in USPi.
Any idea on how to approach both these problems (or i have to read the entire linux usb driver before attempting anything) ?
For hotplugging it may be the best reference. I'm not sure if Ultibo supports hotplugging.
can't we attempt something like that in USPI?
Not by myself. But of course you can! ;-)

User avatar
Ultibo
Posts: 160
Joined: Wed Sep 30, 2015 10:29 am
Location: Australia
Contact: Website

Re: Introducing USPi - A bare metal USB driver written in C

Sat Apr 09, 2016 2:25 am

rst wrote:I'm not sure if Ultibo supports hotplugging.
Yes, Ultibo core does support hotplugging (connect / disconnect) for all USB devices.

For tarkshya, if you are interested in adding this support to USPi there are sort of two halves to the question.

First the connect and disconnect events which (as rst says) are generated by any hubs attached to the USB. For each hub you need to register a status change request with the IN interrupt endpoint and keep resubmitting it at the polling interval specified in the descriptors. When one of these requests completes successfully then the data will contain a bitmap of which ports on that hub have status change events pending, you can then ask the hub for the port status and determine what the change was (eg connected, disconnected, over current etc).

The second half of the work is in responding to those changes and either allocating a new USB device structure and passing it to available drivers to try binding to or (for a disconnect) informing drivers of an unbind and then deallocating the device and cleaning up.

The DWHCI_CORE_INT_MASK_PORT_INTR is the correct interrupt for the root port itself but is only really needed on an A/A+/Zero because all the other models have a hard wired hub attached on the board.

Even though it is written in pascal the Ultibo core code might be simpler to follow than other sources ;) (any C programmer should be able to read pascal without much trouble).

The relevant files are:

USB core and hub driver

https://github.com/ultibohub/Core/blob/ ... re/usb.pas

DWCOTG host driver

https://github.com/ultibohub/Core/blob/ ... dwcotg.pas

Both of these rely on some infrastructure from other sub systems like threads, semaphores, locks and queues but they are standard sorts of mechanisms that could be implemented in any project.
Ultibo.org | Make something amazing
https://ultibo.org

Threads, multi-core, OpenGL, Camera, FAT, NTFS, TCP/IP, USB and more in 3MB with 2 second boot!

tarkshya
Posts: 47
Joined: Tue Nov 03, 2015 7:28 pm

Re: Introducing USPi - A bare metal USB driver written in C

Sat Apr 09, 2016 3:49 pm

Thanks a lot to both rst and ultibo for the pointers, will look into it.


Thanks
Tarkshya

mausebar
Posts: 1
Joined: Tue May 17, 2016 2:39 pm

Re: Introducing USPi - A bare metal USB driver written in C

Tue May 17, 2016 2:59 pm

Hi,
I'm using this library in a project.
I need to give the arriving ethernet frame a timestamp.
Some hardware support this kind of time stamp, which means at the time the ethernet frame arrives(e.g. at MAC), hardware will give it a time stamp. Pi hardware doesn't support.
If there is only a ethernet controller, then it is fine, because when a frame arrives, ethernert controller can trigger an interrupt to arm core, in the interrupt handler i can give a time stamp to the frame.
But since on Pi ethernet is connected with arm core via USB, it is not possible that ethernet automatically triggers an interrupt.
I am thinking about let usb host controller poll ethernet device periodically(interrupt endpoint?), but it seems there is only bulk endpoint implemented in USPi. Is there way i can add interrupt endpoint to ethernet device?
And more, any hint on how i can achieve the mentioned time stamping with USPi?
I'm new to the whole usb stuff...
thanks

vtrucco
Posts: 9
Joined: Fri Mar 25, 2016 2:14 pm

Re: Introducing USPi - A bare metal USB driver written in C

Fri Jun 17, 2016 6:48 pm

no text
Last edited by vtrucco on Thu Jun 30, 2016 10:42 pm, edited 1 time in total.

User avatar
quarkjuggler
Posts: 164
Joined: Wed Nov 27, 2013 4:37 am
Location: Aransas Pass, Texas, U.S.A.

Re: Introducing USPi - A bare metal USB driver written in C

Fri Jun 17, 2016 11:13 pm

I don't know much -- just enough to know that what you have done is quite remarkable. Thank you. Now...

I took your link and found "Dowloads<cr><lf>Source code (zip). When I click on that, I get a screen that shows an item, but I don't know what to do next. It even shows where I wanted to put it. Do I click on it? Does some option make it download? --I'm new to github, so what am I doing on the bare metal forum?
Statements resembling facts are IMO only. "he" means "he or she", etc. "Mother Nature" is an abstraction, not a real spirit.
John

User avatar
quarkjuggler
Posts: 164
Joined: Wed Nov 27, 2013 4:37 am
Location: Aransas Pass, Texas, U.S.A.

Re: Introducing USPi - A bare metal USB driver written in C

Sat Jun 18, 2016 12:12 am

I think I got that one. It had already downloaded and had opened the zip file. I didn't know it was my computer originating the screen. So I looked in my downloads file and found it.

I clicked on an item and got the message "The following problems were found during validation of compiler set "TDM-GCC 4.9.2 32-bit Release". The following libraries don't exist: C:\Program Files\Dev-Cpp\MinGW64\lib32 <CR><LF>Would you like Dev-C++ to remove them...? ....

Can I use a C++ compiler to compile a C program like yours? Should I be trying to compile the top level, which I assume is "USBDevice"?
Statements resembling facts are IMO only. "he" means "he or she", etc. "Mother Nature" is an abstraction, not a real spirit.
John

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

Re: Introducing USPi - A bare metal USB driver written in C

Sat Jun 18, 2016 11:58 am

vtrucco wrote:Hey rst, great work, thanks for sharing.

Already did some tests and works great, but I can´t figured out how to configure a FIQ (I need to listen for a falling edge in GPIO24).

My hardware is already working in my bare metal app, but I would like to include mouse support.

Thanks!
Thanks! Unfortunately there isn't GPIO (and FIQ) support in USPi. The function of the USPi environment was reduced to the features needed to get the USB support running. Other features have to be implemented by yourself.

Perhaps you could have a look at Circle which is a bare metal environment which includes both USB and GPIO support. It is implemented in C++.

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

Re: Introducing USPi - A bare metal USB driver written in C

Sat Jun 18, 2016 12:07 pm

Thanks!
quarkjuggler wrote:I clicked on an item and got the message "The following problems were found during validation of compiler set "TDM-GCC 4.9.2 32-bit Release". The following libraries don't exist: C:\Program Files\Dev-Cpp\MinGW64\lib32 <CR><LF>Would you like Dev-C++ to remove them...? ....

Can I use a C++ compiler to compile a C program like yours? Should I be trying to compile the top level, which I assume is "USBDevice"?
Normally the GNU C++ compiler is able to build C code too.

I think you are doing this on Windows. Unfortunately I have no experience building USPi there. So I cannot help you much with your toolchain. But are you sure your toolchain is installed and configured right? The error message above looks like there is something wrong with it.

You should try to build the files in the lib/ subdirectory first. That's the USB code itself. In the env/ subdirectory there is the USPi environment which is needed to get the samples running.

EyeJay
Posts: 1
Joined: Tue Jun 28, 2016 1:14 am

Re: Introducing USPi - A bare metal USB driver written in C

Tue Jun 28, 2016 1:22 am

Hello,
Please excuse a newbie's question, starting out with a Pi Zero bare metal. Does the library implement a USB CDC serial interface? I would like the Pi to appear as a virtual serial port when connected via the USB port. If not, would you be able to point me in the right direction?
Many Thanks

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

Re: Introducing USPi - A bare metal USB driver written in C

Tue Jun 28, 2016 10:08 am

EyeJay wrote:Does the library implement a USB CDC serial interface? I would like the Pi to appear as a virtual serial port when connected via the USB port.
No, the USPi library implements an USB host controller interface driver with some function drivers for specific USB devices. It cannot be used to let the Pi Zero act as an USB device on another USB host controller.
If not, would you be able to point me in the right direction?
Have a look at this. I don't know if this is still up-to-date.

Return to “Bare metal, Assembly language”