libusb_stm32 icon indicating copy to clipboard operation
libusb_stm32 copied to clipboard

PlatformIO support

Open dzarda opened this issue 5 years ago • 13 comments

Major kudos for the library.

Simply including the repo in the PlatformIO project manifest doesn't work out of the box because of different inc vs include convention:

[env]
lib_deps =
    dmitrystu/libusb_stm32

We can create a library.json manifest in the repo to enable this. What do you think?

dzarda avatar Apr 07 '20 19:04 dzarda

No problem. Send the PR. It will be merged.

dmitrystu avatar Apr 07 '20 23:04 dmitrystu

Thank you. It will require somehow handling the dependencies. E.g stm32.h uses a long path in its includes as opposed to only having "stm32fxx.h" (PlatformIO wants that for ststm32). I'll think about a clean way around this.

For now I have simply copy pasted the thing into my project.

dzarda avatar Apr 13 '20 20:04 dzarda

This is a simply wrapper for the generic CMSIS includes provided by STM. I didn't find a solution like #include<avr/io.h> for the AVR, so i made it.

dmitrystu avatar Apr 13 '20 23:04 dmitrystu

There are some utility macro's in this include. I can move them to the library code.

dmitrystu avatar Apr 13 '20 23:04 dmitrystu

Why is this full path needed? "STM32F1xx/Include/stm32f1xx.h" I've never seen it used like this. From my experience -I always points to Include/.

Anyway having it part of libusb OOTB would be slightly easier not only for PIO consumers but everyone.

dzarda avatar Apr 14 '20 07:04 dzarda

Just followed CMSIS 5 guideline.

dmitrystu avatar Apr 14 '20 12:04 dmitrystu

I use CMSIS 5 with the includes from the ST git repo. Hmm. Looks they's can be got as submodules now.

dmitrystu avatar Apr 14 '20 12:04 dmitrystu

  • AFAICS the CMSIS guide makes no statement about -I location (sadly).
  • PlatformIO (ststm32) points the-I to the stm32f1xx.h directly

dzarda avatar Apr 22 '20 12:04 dzarda

Why? Looks like there is a pretty good described structure:

<cmsis_root>
    + CMSIS
    + Device
       + <Vendor>
          +<Device>
            + Include
            + Source
               + ARM
               + GCC
               + IAR

BTW, Which define/attribute/other_mark usually used in the PlatformIO to determine what kind of device-specific code must be used ?

dmitrystu avatar Apr 22 '20 16:04 dmitrystu

The structure is specified well, but they don't tell you where to point the includePath.

PlatformIO expects this to work: #include "stm32f1xx.h"

The ST determination macros are available in ststm32, that's all fine: #if defined(STM32F0)

dzarda avatar Apr 26 '20 21:04 dzarda

For completeness - support would require selecting the appropriate port source based on the STMXXX macros. Back then I got sorta stuck on that, but should be doable.

dzarda avatar May 17 '21 21:05 dzarda

Old issue, but allow me to chime in:

As far as I can tell, the defines set in PlatformIO for STM32 boards are a bit of a mystery, what I always end up doing in my projects is to define the STM32F1 macro etc myself, using build_flags = -DSTM32F1 in my platformio.ini files.

As for stm32.h, I think the following alternative version would work with PIO - from this:

#elif defined(STM32F1)
    #include "STM32F1xx/Include/stm32f1xx.h"

... to:

#elif defined(STM32F1)
    #include "stm32f1xx.h"

(for all chip families, of course)

When framework = cmsis is set in platformio.ini, then the include path will automatically be set by PIO, and point to the CMSIS headers which it manages itself in ~/.platformio/packages/framework-cmsis-STM32F1/ etc.

The only changes involved, as far as I can tell, are to: 1) add a library.json file (see docs), 2) include a modified stm32.h for PIO use, and 3) publish this library so PIO will find it. Then, all anyone has to do is to add lib_deps = libusb_stm32 to their own platformio.ini file and this library will get auto-installed from this git repository, built, and linked-in.

I'm new to libusb_stm32, but I'll see if I can get a demo working for the Blue Pill.

jcw avatar Jan 28 '22 19:01 jcw

Ok, I think this will do the trick:

  • unpack this zip file in repo root, this creates a demo-pio/ folder: demo-pio.zip
  • create a library.json file in repo root with this content:
{
    "name": "libusb_stm32",
    "description": "Lightweight USB Device Stack",
    "version": "1.0.0",
    "keywords": "stm32, usb",
    "authors": {
        "name": "Dmitry Filimonchuk",
        "email": "[email protected]"
    },
    "repository": {
        "type": "git",
        "url": "https://github.com/dmitrystu/libusb_stm32.git"
    },
    "frameworks": [ "cmsis", "stm32cube" ],
    "platforms": [ "ststm32" ],
    "headers": "usb.h",
    "build": { "includeDir": "inc" }
}

That's basically it, but if you don't have PlatformIO, you also need to install that. The simplest is the command-line version, see this page. Then from the shell (I'm assuming MacOS or Linux, have no experience with Windows), type the following:

cd demo-pio
pio run -t upload

The upload step will only succeed if a Blue Pill with ST-Link is attached, evidently.

  • I had to alter the demo code so that SystemInit() is not redefined, as it's already in the CMSIS code, and sets up some basics (not the clock, just stack and FPU, IIRC) - my solution was to copy the cdc_*.c into one cdc_main.c and change the main init sequence slightly
  • library.json has all the info needed to define libusb_stm32 as a library which PIO understands - it's not published or installed anywhere, but the demo-pio project loads the library as if it was (see platformio.ini)
  • if you want to publish this in the PIO way, you'll need to use that pio publish command I mentioned earlier (which requires setting up an account)
  • ... but there's no need: even as it is, people can request this library from GitHub, by adding lib_deps = dmitrystu/libusb_stm32 to their own platformio.ini project file - this will work once the library.json file is present on GitHub

There are probably some details I missed, but I think this library would be very nice to have available in any PIO project.

jcw avatar Jan 28 '22 21:01 jcw