Skip to content

STM32F1 USBDevice #13406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 17, 2020
Merged

STM32F1 USBDevice #13406

merged 2 commits into from
Sep 17, 2020

Conversation

Allmoz
Copy link
Contributor

@Allmoz Allmoz commented Aug 10, 2020

Summary of changes

As requested in #13350

Assignation of the USB low priority interrupts for the USB interrupt for the STM32F1 target family. This should enable USB capability in F103 ( and hopefully in F102, and F105/F107 also).

Modified MCU_STM32F103x8 target files to add the needed peripheral definitions and clock configuration (same as NUCLEO_STM32F103RB now) to enable USBDEVICE on MCU_STM32F103x8 based boards.

Tested on a STM32F103C8 and STM32F103RE.

Tested on a NUCLEO_F103RB by @jeromecoutant

Impact of changes

Migration actions required

Documentation

None that I know


Pull request type

[x] Patch update (Bug fix / Target update / Docs update / Test update / Refactor)
[] Feature update (New feature / Functionality change / New API)
[] Major update (Breaking change E.g. Return code change / API behaviour change)

Test results

[] No Tests required for this change (E.g docs only update)
[x] Covered by existing mbed-os tests (Greentea or Unittest)
[] Tests / results supplied as part of this PR

Reviewers

@jeromecoutant I'm not sure if adding it to the full STM32F1 is ok since I only tested it for STM32F103C8 and STM32F103RE.
I suppose "Test results" is "Covered by existing mbed-os tests", please confirm.

Best regards

@ciarmcom
Copy link
Member

@Allmoz, thank you for your changes.
@jeromecoutant @ARMmbed/mbed-os-maintainers please review.

@adbridge
Copy link
Contributor

@Allmoz as this is actually a target update please change the PR type to patch. Also please fill in an actual summary for the PR rather than just quoting something else, thank you.

@Allmoz
Copy link
Contributor Author

Allmoz commented Aug 12, 2020

Thanks @adbridge , done.

Copy link
Collaborator

@jeromecoutant jeromecoutant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with NUCLEO-F103RB:

  • USB-DP connected to PA12
  • USB-DM connected to PA11
  • 1.5KOhm pull up to USB-DP PA12

@@ -1280,7 +1280,8 @@
"device_has_add": [
"CAN",
"SERIAL_ASYNCH",
"FLASH"
"FLASH",
"USBDEVICE"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we can't add it there, as USBDEVICEmeans you have an USB connector.
This can be only defined in each target (in you custom_target.json)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted

@jeromecoutant
Copy link
Collaborator

Maybe you could also check and add my commit in your PR ? :-)
jeromecoutant@f096704

@Allmoz
Copy link
Contributor Author

Allmoz commented Aug 14, 2020

@jeromecoutant , ohhh i wanna test it so bad. does it fix that the USB issue that is not recognized after programming unless you connect->disconnect->connect?

Sure I will add it.

@mergify mergify bot added the needs: work label Aug 14, 2020
@mergify
Copy link

mergify bot commented Aug 14, 2020

This PR cannot be merged due to conflicts. Please rebase to resolve them.

@mergify mergify bot removed the needs: review label Aug 14, 2020
@Allmoz Allmoz force-pushed the master branch 2 times, most recently from af73fb5 to a22f3e5 Compare August 14, 2020 18:39
@Allmoz
Copy link
Contributor Author

Allmoz commented Aug 14, 2020

Maybe you could also check and add my commit in your PR ? :-)
jeromecoutant@f096704

Awesome, it works on the STM32F103C8 and STM32F103RE.

To achieve that behavior i was using:

serial.connect();
serial.disconnect();
ThisThread::sleep_for(1s);
serial.connect();

Checked and added :)

@adbridge with @jeromecoutant commit this turns also into a "[x] Feature update" ?

0xc0170
0xc0170 previously approved these changes Aug 31, 2020
@mergify mergify bot added needs: CI and removed needs: work labels Aug 31, 2020
@0xc0170
Copy link
Contributor

0xc0170 commented Aug 31, 2020

@adbridge with @jeromecoutant commit this turns also into a "[x] Feature update" ?

The feature is already there, this is just enabling it for a target - it is a patch update.

Thanks @Allmoz for the contribution.

@DesktopMan
Copy link

Will these changes apply to STM32F103CB as well?

@jeromecoutant
Copy link
Collaborator

Will these changes apply to STM32F103CB as well?

I think so
Custom target with STM32F103CB should inherit from MCU_STM32F103xB

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 1, 2020

CI started

@mergify mergify bot added needs: work and removed needs: CI labels Sep 1, 2020
@mbed-ci
Copy link

mbed-ci commented Sep 1, 2020

Jenkins CI Test : ❌ FAILED

Build Number: 1 | 🔒 Jenkins CI Job | 🌐 Logs & Artifacts

CLICK for Detailed Summary

jobs Status
jenkins-ci/mbed-os-ci_unittests ✔️
jenkins-ci/mbed-os-ci_build-ARM
jenkins-ci/mbed-os-ci_build-GCC_ARM

@jeromecoutant
Copy link
Collaborator

Jenkins CI Test : ❌ FAILED

@Allmoz
Build failure is my fault...
Remove my commit, then let's re-start CI, and merge.
I will then propose the updates in a new PR
Sorry

@0xc0170 0xc0170 added needs: CI release-type: patch Indentifies a PR as containing just a patch labels Sep 10, 2020
@0xc0170
Copy link
Contributor

0xc0170 commented Sep 10, 2020

I'll schedule CI soon

@mbed-ci
Copy link

mbed-ci commented Sep 10, 2020

Jenkins CI Test : ✔️ SUCCESS

Build Number: 2 | 🔒 Jenkins CI Job | 🌐 Logs & Artifacts

CLICK for Detailed Summary

jobs Status
jenkins-ci/mbed-os-ci_unittests ✔️
jenkins-ci/mbed-os-ci_build-ARM ✔️
jenkins-ci/mbed-os-ci_build-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_greentea-test ✔️
jenkins-ci/mbed-os-ci_dynamic-memory-usage ✔️
jenkins-ci/mbed-os-ci_cloud-client-pytest ✔️

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 15, 2020

Once we release the next Mbed OS version, we'll merge this.

@0xc0170 0xc0170 merged commit 3801f6e into ARMmbed:master Sep 17, 2020
@mergify mergify bot removed the ready for merge label Sep 17, 2020
@wubaojun1982
Copy link

still can't use USBSerial on bluepill (STM32F103C8T6) board.

@Allmoz
Copy link
Contributor Author

Allmoz commented Sep 17, 2020

still can't use USBSerial on bluepill (STM32F103C8T6) board.

you need to have the device declared in your custom_target.json:

    "device_has_add": [
        "USBDEVICE"
    ]

the pin map in your custom PeripheralPins.c :

//*** USBDEVICE ***

MBED_WEAK const PinMap PinMap_USB_FS[] = {
{PA_11, USB_FS, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)}, // USB_DM
{PA_12, USB_FS, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)}, // USB_DP
{NC, NC, 0}
};

and the pin names in the custom PinNames.h

//USB pins
USB_DM = PA_11,
USB_DP = PA_12,

@wubaojun1982
Copy link

https://os.mbed.com/users/hudakz/code/mbed-os-bluepill/shortlog/
can you do it like this, using the USBSerial with bluepill(stm32f103c8t6).

@wubaojun1982
Copy link

it can work successful, I added the clock source configuration in the med_app.json like this:
"target_overrides": {
"*": {
"target.clock_source": "USE_PLL_HSE_XTAL",
"target.clock_source_usb": "1",
"target.device_has_add": ["USBDEVICE"]
}
}

@JojoS62
Copy link
Contributor

JojoS62 commented Sep 23, 2020

has this been tested with ARMC6 compiler? A testprogram with USBMIDI is running on a Bluepill when compiled with gcc, but when I compile it MbedStudio, it produces a hardfault.

@masoudr
Copy link
Contributor

masoudr commented Sep 25, 2020

I'm having trouble to get it to work with STM32F103RB target. I get no result in my UART port and USBSerial doesn't work either.
mbed_app.json

{
    "macros": [
        "MBED_HEAP_STATS_ENABLED=1",
        "MBED_STACK_STATS_ENABLED=1",
        "MBED_MEM_TRACING_ENABLED=1"
    ],
    "target_overrides": {
        "*": {
            "target.clock_source": "USE_PLL_HSE_EXTC|USE_PLL_HSI",
            "target.macros_add": ["HSE_VALUE=8000000"],
            "target.components_add": [
                "SPIF"
            ],
            "target.device_has_add": ["USBDEVICE"],
            "platform.stdio-baud-rate": 115200,
            "target.stdio_uart_tx": "PB_6",
            "target.stdio_uart_rx": "PB_7"
        }
    }
}

@jeromecoutant Have you tested it with this target?
Thanks.

@jeromecoutant
Copy link
Collaborator

@jeromecoutant Have you tested it with this target?

Yes... as said in a previous comment:

  • USB-DP connected to PA12
  • USB-DM connected to PA11
  • 1.5KOhm pull up to USB-DP PA12
  • "target.device_has_add": ["USBDEVICE"] in mbed_app.json

@jeromecoutant
Copy link
Collaborator

and #13522

@masoudr
Copy link
Contributor

masoudr commented Sep 25, 2020

@jeromecoutant Thanks for the update. I've used the USB6B1 chip but I haven't connected the pull-up resistor. But the code crashes for me. Also, the console doesn't work either to read the details of the exception. Now I will try to add the pull-up resistor and see if it works and report here.

@masoudr
Copy link
Contributor

masoudr commented Sep 25, 2020

I added the Pull-up resistor and it works fine. I have two questions:

  1. The below code doesn't start (LED won't blink) until I open the USB serial port. Is there any way that I can prevent this?
#include "mbed.h"
#include "USBSerial.h"
//Virtual serial port over USB
USBSerial usbserial;

DebugSerial pc;

DigitalOut LED(PC_8);

bool value = false;

int main(void)
{
    printf("USBSerial test\n");
    while (1) {
        if(usbserial.writable())
            usbserial.printf("I am a virtual serial port\r\n");
        LED  = value;
        value = !value;
        thread_sleep_for(1000);
    }
}
  1. Is there any way that I can use it with a bare-metal profile? I get a linker error for USBSerial class.
{
    "requires": ["bare-metal", "spif-driver", "mbed-trace"],
    "macros": [
        "MBED_HEAP_STATS_ENABLED=1",
        "MBED_STACK_STATS_ENABLED=1",
        "MBED_MEM_TRACING_ENABLED=1"
    ],
    "target_overrides": {
        "*": {
            "target.c_lib": "small",
            "target.printf_lib": "minimal-printf",
            "platform.minimal-printf-enable-floating-point": false,
            "target.clock_source": "USE_PLL_HSE_EXTC|USE_PLL_HSI",
            "target.macros_add": ["HSE_VALUE=8000000"],
            "target.components_add": [
                "SPIF"
            ],
            "target.device_has_add": ["USBDEVICE"]
        }
    }
}

@JojoS62
Copy link
Contributor

JojoS62 commented Sep 25, 2020

you can use USB with bare-metal, just add "drivers-usb" to the requires list.

The USB device classes have defaults in the constructor, the first is 'connect_blocking'.

USBSerial usbserial(false);

but you have to call usbserial.connect() later to connect.

But this should be discussed in the Mbed forum.

@wubaojun1982
Copy link

still can't use USBSerial on bluepill (STM32F103C8T6) board.

you need to have the device declared in your custom_target.json:

    "device_has_add": [
        "USBDEVICE"
    ]

the pin map in your custom PeripheralPins.c :

//*** USBDEVICE ***

MBED_WEAK const PinMap PinMap_USB_FS[] = {
{PA_11, USB_FS, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)}, // USB_DM
{PA_12, USB_FS, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)}, // USB_DP
{NC, NC, 0}
};

and the pin names in the custom PinNames.h

//USB pins
USB_DM = PA_11,
USB_DP = PA_12,

After mbed6.4 released, the USBSerial con't work on bluepill (stm32f103c8t6). All configration don't changed

@Allmoz
Copy link
Contributor Author

Allmoz commented Oct 31, 2020

Hi,

still can't use USBSerial on bluepill (STM32F103C8T6) board.

you need to have the device declared in your custom_target.json:

    "device_has_add": [
        "USBDEVICE"
    ]

the pin map in your custom PeripheralPins.c :
//*** USBDEVICE ***
MBED_WEAK const PinMap PinMap_USB_FS[] = {
{PA_11, USB_FS, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)}, // USB_DM
{PA_12, USB_FS, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)}, // USB_DP
{NC, NC, 0}
};
and the pin names in the custom PinNames.h

//USB pins
USB_DM = PA_11,
USB_DP = PA_12,

After mbed6.4 released, the USBSerial con't work on bluepill (stm32f103c8t6). All configration don't changed

Its true for me too, but this should on its own issue. could you make that new issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.