OS-less USB CDC on stm32f4

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
Post Reply
Octopoulpe
Posts: 1
Joined: Thu Aug 18, 2016 9:01 am

OS-less USB CDC on stm32f4

Post by Octopoulpe »

Hi !

I just started to use ChibiOS/HAL for a bare-metal application on STM32F405 (it has been great so far !).

I need to implement an interrupt-based USB CDC driver. The interrupts would call custom functions that fill a RX circular buffer and send data stored in a TX buffer.

I tried testhal/STM32/STM32F4xx/USB_CDC on my board and it worked (I just had to define BOARD_OTG_NOVBUSSENS because my board does not have VBUS connected). So the hardware is fine.
However this example uses RT, and when i copied the usbcfg.* files and USB initialisation code into my OS-less project, the board was not correctly detected by my linux host (it failed to read device descriptor and /dev/ttyACM0 was not created).

Can you point me to an example for USB CDC on OS-less application ?

Thanks!
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: OS-less USB CDC on stm32f4

Post by Giovanni »

Hi,

It has not been tested yet, the problem is that the OTG driver requires a thread that performs data-moving operations, without RTOS your only option is to use the main() to execute that code.

This means that your whole application would have to be interrupt/callback driven.

Giovanni
Matthias
Posts: 12
Joined: Fri Jul 29, 2016 3:23 pm
Has thanked: 1 time
Been thanked: 3 times

Re: OS-less USB CDC on stm32f4

Post by Matthias »

Hi,

I have stumbled over the same problem as Octopoulpe. The only difference is that I use a real time OS with static, non preemptive threads. As the polling function (usb_lld_pump) in the low level USB driver never terminates, it can not be used.

Dynamic, preemptive threads are a quite high requirement for a HAL driver.Thus I hope there is some interest in changing this.

My solution was to rewrite the polling function, so that it could be called cyclic. Another option is to use DMA for moving the data around. I did not investigate, but this is probably the better solution.
In the next days I will work on this topic. If wished I can give you the result.

@Giovanni was there some specific reason why polling is used?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: OS-less USB CDC on stm32f4

Post by Giovanni »

Hi,

It is not exactly polling, that thread is mostly waiting for a queue event than it wakes up and does the data copying.

The problem is that in the STM32 there are two OTG units (FS and HS), only HS has a DMA so it is not possible to write a driver using DMA for both units. The current implementation is a compromise.

I will look into unrolling that thread "work code" and make it invokable like a normal returning function but you would have to call it continuously.

Giovanni
Matthias
Posts: 12
Joined: Fri Jul 29, 2016 3:23 pm
Has thanked: 1 time
Been thanked: 3 times

Re: OS-less USB CDC on stm32f4

Post by Matthias »

Giovanni wrote:...
I will look into unrolling that thread "work code" and make it invokable like a normal returning function but you would have to call it continuously.

Giovanni


That is exactly what I am doing now.
tecnologic
Posts: 124
Joined: Tue Jan 10, 2012 8:42 am
Has thanked: 21 times
Been thanked: 5 times

Re: OS-less USB CDC on stm32f4

Post by tecnologic »

Hi All,

i am running USB CDC also with the os-less hal but on a STM32F303, so far it works with just changing usbcfg.c to calling osal functions instead of ch.. api. But when USB CDC runs for a while (5min) i got struck in a hardfault. In the Usage Fault Register the bit INVSTATE is set.

for me it seems that the usb driver is still using some kernel functionality. And sometimes the Stack from the Segger GDB Server shows
he was in the function usb_packet_write_from_buffer but this is just copying some bytes and i dont see any eroor or os function call.

[1] INVSTATE
Invalid state UsageFault:
0 = no invalid state UsageFault
1 = the processor has attempted to execute an instruction that makes illegal use of the EPSR.
When this bit is set to 1, the PC value stacked for the exception return points to the instruction that attempted the illegal use of the EPSR.
This bit is not set to 1 if an undefined instruction uses the EPSR.

source: http://infocenter.arm.com/help/index.js ... djcfc.html


Has any body tested USBv1 with os-less hal, i think it has the same status as the OTGv1 driver? If so does some one have a hint were to have a look at?


Some other useful info could be, that with litte traffic on the CDC it lasts longer. I thing in mostly gets Hardfaults when tranfering langer Packets.
I send every now an then around 16kb of data. There for i also increased SERIAL_USB_BUFFERS_SIZE to 2048 but this changed nothing.

regards

tec
tecnologic
Posts: 124
Joined: Tue Jan 10, 2012 8:42 am
Has thanked: 21 times
Been thanked: 5 times

Re: OS-less USB CDC on stm32f4

Post by tecnologic »

Just as an info. I activated rt in my Project even if i dont need it actually, but USB CDC is stable on STM32F3 with rt but not with os-less.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: OS-less USB CDC on stm32f4

Post by Giovanni »

Best way would be if you could make a very simple project showing the problem, just the minimum.

I suspect there is something the OS-less OSAL. Probably related to IRQs.

Are assertions enabled? is something caught?

Giovanni
Matthias
Posts: 12
Joined: Fri Jul 29, 2016 3:23 pm
Has thanked: 1 time
Been thanked: 3 times

Re: OS-less USB CDC on stm32f4

Post by Matthias »

Maybe the following patch help some of you guys. Its change the usb_lld_pump() in a cyclic callable function.
This way the USB driver should work without thread support, as long as you call usb_lld_pump() from time to time.

Cheers Matthias
Attachments
cyclic_usb_lld_pump.zip
(803 Bytes) Downloaded 551 times
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: OS-less USB CDC on stm32f4

Post by Giovanni »

Interesting, thanks.

Giovanni
Post Reply