min-protocol / min

The MIN protocol specification and reference implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

STM32Cube USB VCP

Tyrn opened this issue · comments

commented

Hi,

Is there any experience with MIN protocol outside Arduino? I'm having trouble adapting it to the middleware supplied by STM32CubeIDE. Shouldn't be too difficult; unfortunately, I'm a newbie.

Sorry, I did some Google research, didn't find anything.

Yep, used extensively on a PyBoard (STM32F405) with USB VCP. Also used on an RP2040 Raspberry Pi Pico with USB. Check out this video:

https://youtu.be/o2vqKgQ-v1k

Source code for the MicroPython firmware changes:

https://github.com/kentindell/canhack/tree/master/pico/micropython

The monitor framework has been used to build the CANPicoCtrl GUI: the host is all in Python, talking to the Pico, with its monitor in Python.

https://kentindell.github.io/2022/05/11/canpicoctrl/

The monitor software for host and target is included in the branch mincan in this repo:

https://github.com/min-protocol/min/tree/mincan

I'll merge this and tidy everything up when I've got the Linux implementation super robust (right now the pyserial hangs on close() if there's a write timeout, so I'm changing the discovery of a monitor to a ping, with a timeout). The framework should be very useful for anyone wanting to do command/response from a host app.

commented

Oh, how nice of you! Thanks!

commented

Yep, used extensively on a PyBoard (STM32F405) with USB VCP.

Are there any public repositories with this?

No, but it's obsolete compared to the Pico implementation using TinyUSB.

commented

I have to manage STM32F103C8T6 using STM32CubeIDE.

Take a look at how MIN is stitched in to the MicroPython Pico framework: it doesn't touch anything but the callbacks, and if your IDE has USB support it should be straightforward to glue it together in a similar way.

commented

I'm overwhelmed, actually :(

usbd_cdc_if.c suggests its own set of callbacks and nothing else whatever, which I have no idea how to marry with half the MIN callbacks.

A Cube version of sketch_echo

If you look in rp2_min.c you'll see a couple of static functions: usb_write and usb_read. These push bytes to/from USB. In the Pico case, it uses the TinyUSB library that's instantiated by MicroPython (the Pico SDK has TinyUSB support too, and I guess the STM32 SDK also does, so it will look similar).

The MIN callbacks are also in there: min_tx_space just says "yeah, 1K free", min_tx_byte pushes a byte into a local buffer, min_tx_start clears the local buffer, and min_tx_finished pushes the local buffer into TinyUSB (via the local push function).

The function min_poll is called from the Python recv() code, which pulls the bytes from the USB drivers, which is how MIN is driven.

The interesting callback is min_application_handler, which in this case creates a bunch of Python objects on the heap to pass into Python-world. But that's where your application code would go to handle incoming MIN frames.

MIN is entirely polled, so no interrupts. And the TinyUSB interface is polled too (there are interrupts in the background that feed the USB API but I don't go anywhere near those and let MicroPython deal with it all).