dmitrystu / libusb_stm32

Lightweight USB device Stack for STM32 microcontrollers
Apache License 2.0
721 stars 165 forks source link

PlatformIO support #49

Closed dzarda closed 2 years ago

dzarda commented 4 years ago

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?

dmitrystu commented 4 years ago

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

dzarda commented 4 years ago

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.

dmitrystu commented 4 years ago

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 commented 4 years ago

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

dzarda commented 4 years ago

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.

dmitrystu commented 4 years ago

Just followed CMSIS 5 guideline.

dmitrystu commented 4 years ago

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

dzarda commented 4 years ago
dmitrystu commented 4 years ago

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 ?

dzarda commented 4 years ago

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 commented 3 years ago

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.

jcw commented 2 years ago

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 commented 2 years ago

Ok, I think this will do the trick:

{
    "name": "libusb_stm32",
    "description": "Lightweight USB Device Stack",
    "version": "1.0.0",
    "keywords": "stm32, usb",
    "authors": {
        "name": "Dmitry Filimonchuk",
        "email": "dmitrystu@gmail.com"
    },
    "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.

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