5 years, 4 months ago.

Is there any USB CDC libraries for STM32?

I have checked all USB device libraries from mbed. All of them have been ported to LPC1768/LPC11U35/KL25Z/46Z. Is there any USB libraries dedicated for STM32 micro?

2 Answers

5 years, 3 months ago.

Hi Zoltan i tried your usbdevice_stm32F103 for printf() and it work.

But if i try the same code with scanf() it completely block the mcu. Is it possible use scanf() in your lib ?

I used it in this way:

uint8_t buf[128];

if(usbSerial.scanf("%s", buf)>0) { usbSerial.printf("\nRicevuto: %s", buf); }

scanf shall work with any valid serial device. It reads (waits for) subsequent characters until a whitespace is received. (This may appear like blocking the mcu.) Whitespace characters are considered to be blank, newline and tab. To avoid loosing characters the length of string sent in one write operation over the serial connection should not exceed the length of buffer. On the other hand, to prevent exceeding the buffer's capacity you shall specify the maximum number of characters to be read in one reading operation (scanf("%maxcharss", buf). When specifying maxchars, please account for a null char ('\0') which is automatically appended by scanf to the string received. The following code worked for me:

#include "stm32f103c8t6.h"
#include "mbed.h"
#include "USBSerial.h"
DigitalOut  led(LED1);
int main() {
    confSysClock();     //Configure system clock (72MHz HSE clock, 48MHz USB clock)
    USBSerial usbSerial;
    volatile char buf[128]; 
    while(1) {
        usbSerial.scanf("%127s", buf);  //waits until a whitespace char or max. number of chars is received 
        usbSerial.printf("%s\r\n", buf);
        led = !led;
posted by Zoltan Hudak 04 Oct 2016