cancel
Showing results for 
Search instead for 
Did you mean: 

Use USB Peripheral as UART with STM32L4.

MAndr.233
Associate III

Hi,

I'm currently developing a board where I use STM32L433RC as my main unit

of processment. This board will have an SD Card to store some data and later on

transfer it via UART. To achieve this, I must use an UART to Serial Port

converter.

After some research on MCU reference manual, I saw that it has an USB Peripheral Interface where I could connect D+ and D- directly to the MCU. Thus, if this worked, I wouldn’t need to have the converter in my board. However, I’ve some questions:

  1. Is it possible to use USB peripheral as a Serial Port?
  2. The learning curve of this peripheral it’s hard? Since I already have code to UART, which in fact is to simple, will I spend a lot of time doing the same thing to USB protocol?
  3. I’m not thinking about using and external clock. To use USB peripheral do I need one?
  4. Is it easy to change the Serial Port name? So, when I connect the MCU to the PC this custom name its used.

 Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions

1.> Is it possible to use USB peripheral as a Serial Port?

Generally, yes. On USB side you want to use CDC class. The level, how seamless it is, depends on the driver in the host. Supposedly, you don't want to write a USB driver. In Windows, only W10 provides standard class drivers for this working seamlessly, for older Win you and the users have to jump through some hoops. Lin users are used to the troubles and besides CDC usually works there fairly well (except the "is surely a modem" and the characteristic linuxy "we know better than the user how to handle stuff so we do magic" syndrome). OsX, Android are mostly Terra incognita.

2. >Learning curve

If you want to learn the hows and whys, it's hard, as USB is a relatively complex plus notoriously badly documented protocol in most of its aspects, and the existing implementations to which you want to plug notoriously violate that poorly defined spevification in minor but often important ways.

As a bonus, the USB peripherals in STM32 have their quirks, too, and especially the OTG module (which is in the higher-end L4, too) is very poorly documented.

But you may get lucky and the software generated by clicking in CubeMX, or extracted from the examples, may work for you. Apparently, it does so for many users out there.

There also third party USB libraries and solutions out there.

3. External clock

Does your STM32 have the "HSI48 which can be locked to USB" (CRS)? If yes, use it and you will be fine.

4. > it easy to change the Serial Port name?

No.

JW

View solution in original post

4 REPLIES 4

1.> Is it possible to use USB peripheral as a Serial Port?

Generally, yes. On USB side you want to use CDC class. The level, how seamless it is, depends on the driver in the host. Supposedly, you don't want to write a USB driver. In Windows, only W10 provides standard class drivers for this working seamlessly, for older Win you and the users have to jump through some hoops. Lin users are used to the troubles and besides CDC usually works there fairly well (except the "is surely a modem" and the characteristic linuxy "we know better than the user how to handle stuff so we do magic" syndrome). OsX, Android are mostly Terra incognita.

2. >Learning curve

If you want to learn the hows and whys, it's hard, as USB is a relatively complex plus notoriously badly documented protocol in most of its aspects, and the existing implementations to which you want to plug notoriously violate that poorly defined spevification in minor but often important ways.

As a bonus, the USB peripherals in STM32 have their quirks, too, and especially the OTG module (which is in the higher-end L4, too) is very poorly documented.

But you may get lucky and the software generated by clicking in CubeMX, or extracted from the examples, may work for you. Apparently, it does so for many users out there.

There also third party USB libraries and solutions out there.

3. External clock

Does your STM32 have the "HSI48 which can be locked to USB" (CRS)? If yes, use it and you will be fine.

4. > it easy to change the Serial Port name?

No.

JW

kurta999
Senior

We use USB CDC on L433 and it works without problems on windows (I haven't tested it on linux yet). Only my computer doesn't want to recognize the device, I need to disable & enable it in device manager to made it work.

You have to modify a bit in CubeMX generated code (or you had to do it before, I don't know if they fixed this but looks like they aren't)

Add this to MX_USB_DEVICE_Init: ( usb_device.c )

static void USB_DEVICE_MasterHardReset(void)

{

 osDelay(1000);

 GPIO_InitTypeDef GPIO_InitStruct;

 GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12;

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_PULLDOWN;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11 | GPIO_PIN_12, 0);

}

void MX_USB_DEVICE_Init(void)

{

 /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */

 USB_DEVICE_MasterHardReset();

/* other code.... -*/

}

Add this to CDC_Control_FS ( usbd_cdc_if.c )

  case CDC_GET_LINE_CODING:

   // Called when usb cable is plugged in (no terminal program)

   // Called when terminal program connects

   pbuf[0] = 0x20; // bits/second 115200

   pbuf[1] = 0xc2;

   pbuf[2] = 0x01;

   pbuf[3] = 0x00;

   pbuf[4] = 0x00; // 1 stop bit

   pbuf[5] = 0x00; // parity none

   pbuf[6] = 0x08; // 8 data bits

  break;

I found those solutions here in the forum.

Last note: USB can't receive more than 64bytes at a time, but this isn't a big problem because you can setup a timer and if eg. 10ms passed since last receive data then process every received 64byte chunk as one and done. If you need the code I can give it to you.

4. > it easy to change the Serial Port name?

No.

Why not? You can change it in CubeMX and it'll appear with that name when you plug your device.

> it'll appear with that name when you plug your device.

Where?

In the application where it is supposed to be used, e.g.a terminal program?

Maybe MAndr.233 should expand on that question and tell us, what does he mean by "name".

JW

> Where?

> In the application where it is supposed to be used, e.g.a terminal program?

In Windows you can see the device name from the USB descriptors in Control panel -> "Devices and printers"

A program can retrieve this name via certain API, as "bus-reported device description" property.

-- pa