Author Topic: pfm3 : usb midi not working in 0.99b?  (Read 10362 times)

lokki

  • Sr. Member
  • ****
  • Posts: 383
    • View Profile
Re: usb midi not working in 0.99b?
« Reply #30 on: December 17, 2020, 08:38:51 AM »
Oh boy, thanks! Great to know, that I am not alone :) will try the firmware asap

lokki

  • Sr. Member
  • ****
  • Posts: 383
    • View Profile
Re: usb midi not working in 0.99b?
« Reply #31 on: December 17, 2020, 09:44:39 AM »
hey xavier, it seems stable now! great!! i rebooted about ten times, it worked every time.

curious, what did you change?

lokki

  • Sr. Member
  • ****
  • Posts: 383
    • View Profile
Re: usb midi not working in 0.99b?
« Reply #32 on: December 17, 2020, 09:56:49 AM »
ok i see it on GitHub, wow how did you manage to find that out so quickly? it seems like a really unobvious thing...or did you have an old unoptimised firmware that was running nicely and you took it from there?

Xavier

  • Administrator
  • Hero Member
  • *
  • Posts: 2257
    • View Profile
Re: usb midi not working in 0.99b?
« Reply #33 on: December 17, 2020, 10:29:23 AM »
Haha... It was interesting an i learnt many things :)

I said previously that my development pfm3 did not have this problem although my others have the problem.
I realized it's simply because my dev pfm3 has the debug version of the firmware (without full optimization).

I searched a bit on the web, and found interesting articles why optimizations can break drivers (by reordering or even skipping variable value assignation).
This one is very good (not specific about drivers though) :
https://blog.regehr.org/archives/28

My first try was to try to add the pragma line in the STM32 USB code :
Code: [Select]
#pragma GCC optimize ("-O1")
And It worked  :)
With O2 it also worked. with O3 it stopped working, and O3 is the default optimization flag for the release version of the firmware.
So the fix is simply this line :
https://github.com/Ixox/preenfm3/blob/v0.101/firmware/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_ll_usb.c#L48

--

Also I also wanted to come back to something i said :
The Midi drivers does not use the 8Mhz external crystal. It could, but in the preenfm3 the 48Mhz USB clock is wired on the HSI (high speed internal).
https://github.com/Ixox/preenfm3/blob/v0.101/firmware/Src/main.c#L263
It's good enough for the low speed USB involved.


Thanks again for pushing this problem.

lokki

  • Sr. Member
  • ****
  • Posts: 383
    • View Profile
Re: usb midi not working in 0.99b?
« Reply #34 on: December 17, 2020, 10:35:38 AM »
What is really strange about all this, is that it still sometimes worked before, so the code was only partially broken? or variable ordering is dynamic even after compile time? puzzling...

btw, teensy 4 use a similar chip, no? they use usb2 for midi communication, maybe that would be more reliable with -O3

Xavier

  • Administrator
  • Hero Member
  • *
  • Posts: 2257
    • View Profile
Re: usb midi not working in 0.99b?
« Reply #35 on: December 17, 2020, 10:45:01 AM »
What is really strange about all this, is that it still sometimes worked before, so the code was only partially broken? or variable ordering is dynamic even after compile time? puzzling...
btw, teensy 4 use a similar chip, no? they use usb2 for midi communication, maybe that would be more reliable with -O3

No there's not dynamic ordering at run time in the cortex-M7.
I assume it's a problem of race condition between the main USB interrups and other threads. So sometimes it worked, depending when the first USB interrupts are called during the init process.

Just had a look, the teensy 4 use a NXP iMXRT1062 (the pfm3 use a STM32H743)
The CPU/core part is also a ARM cortex-M7. But all the rest (hardware implementation, registers, interrupts etc...) is different.

Midi is SUPER SLOW (31Kbits/s). The slower USB is 1.5Mbits /s so we're good (50 times faster).
And i'm confidant that using -O2 on this low level driver won't have any perf impact.

But it could be interesting some day in the far future to understand why the USB access from computer is so slow.

« Last Edit: December 17, 2020, 10:56:24 AM by Xavier »

lokki

  • Sr. Member
  • ****
  • Posts: 383
    • View Profile
Re: usb midi not working in 0.99b?
« Reply #36 on: December 17, 2020, 11:19:24 AM »
DIN midi has a baudrate of 31250, true. But USB midi can run at up to 480mb (usb 2) and some newer synths and controllers use that! If you have lots of modulation that can make a big difference. Usb 1 is also not very tight, given it has a polling interval (at least 1ms I believe) and hence midi clock is not very precise from usb...

Xavier

  • Administrator
  • Hero Member
  • *
  • Posts: 2257
    • View Profile
Re: usb midi not working in 0.99b?
« Reply #37 on: December 17, 2020, 11:43:15 AM »
DIN midi has a baudrate of 31250, true. But USB midi can run at up to 480mb (usb 2) and some newer synths and controllers use that! If you have lots of modulation that can make a big difference. Usb 1 is also not very tight, given it has a polling interval (at least 1ms I believe) and hence midi clock is not very precise from usb...

Midi Din5 is even worse : 0.7ms per note on or midi CC. Even USB1 can send 64 bytes every ms so much more information.

I was talking about the slower USB 1 which is still much better than DIN5, not the one the preenfm3 is using.
The preenfm3 uses "USB 2.0  12Mbits Full Speed" specification  (with 1ms polling).

I would like someone to try sending 480mb/s of note on and midi CC to any synth :D
480mb/s works with a 0.125ms polling which is CPU intensive even when doing nothing...
« Last Edit: December 17, 2020, 11:46:18 AM by Xavier »

lokki

  • Sr. Member
  • ****
  • Posts: 383
    • View Profile
Re: usb midi not working in 0.99b?
« Reply #38 on: December 17, 2020, 02:07:20 PM »
I know I know. Still for clock DIN midi is better then usb, since a unsteady clock (caused by the 1ms polling) can mess with arps etc.

that aside, I am very happy the preenfm3 works over usb now and that it was such an easy fix! Thx.

forestcaver

  • Team member
  • *
  • Posts: 18
    • View Profile
Re: usb midi not working in 0.99b?
« Reply #39 on: December 17, 2020, 08:38:19 PM »
Unfortunately, I just tried with the latest fw posted on page 2 (0.100z) and it still isnt working for me.

lokki

  • Sr. Member
  • ****
  • Posts: 383
    • View Profile
Re: usb midi not working in 0.99b?
« Reply #40 on: December 17, 2020, 08:41:33 PM »
is your preenfm3 also always visible when in DFU mode?

Xavier

  • Administrator
  • Hero Member
  • *
  • Posts: 2257
    • View Profile
Re: usb midi not working in 0.99b?
« Reply #41 on: December 17, 2020, 09:22:04 PM »
Unfortunately, I just tried with the latest fw posted on page 2 (0.100z) and it still isnt working for me.

Please, confirm you see "0.100z" in the top right of the menu ?
In Menu > Config. Check Usb Midi is set to "In/Out".
After that double check you midi jack soldering.


forestcaver

  • Team member
  • *
  • Posts: 18
    • View Profile
Re: usb midi not working in 0.99b?
« Reply #42 on: December 17, 2020, 10:16:11 PM »
It was flashed ok in DFU mode (was visible each time I set the jumpers to pullup the boot to VCC) so I suspect the soldering of the USB socket is ok (I also checked it under a microscope after cleaning the flux when I built it). It would be odd to be a soldering issue if it can be flashed over USB, wouldnt it ?

I previously checked it was the 0.100z fw (as shown on the screen) when I tested it earlier tonight
I also checked USB Midi was set to In/Out when I tested it earlier.

I wont have time to take it out of the case and examine the soldering again until the weekend.

I have got a development build going with stm32cubeide and have an stlinkv2 so if I get time over the weekend I can also look at debugging but I will double check the soldering...

Xavier

  • Administrator
  • Hero Member
  • *
  • Posts: 2257
    • View Profile
Re: usb midi not working in 0.99b?
« Reply #43 on: December 17, 2020, 10:21:27 PM »
It would be odd to be a soldering issue if it can be flashed over USB, wouldnt it ?

Correct. If flashing over DFU works, then midi should work. No need to examine again.