# Can I program an MCU directly from USB if it supports USB? [closed]

If a microcontroller has USB functionality, can I program it directly from a USB port? Can I design the PCB without a USB to serial converter? What are the precautions I should take? The example that came to my mind was STM32F070 series.

• You can if it has a USB boot-loader. – Ron Beyer Aug 19 '19 at 20:43
• Of course that depends on the part and what might already be flashed into it. Note that even if you plan to use USB, it's really best to include a place where you could get at signals from a debug UART, and also from whatever direct programming the part supports (SWD, ISP, JTAG, whatever)... these will be extremely helpful if you find your USB implementation (even in application firmware) is not yet working as desired. – Chris Stratton Aug 19 '19 at 20:51

If a microcontroller has USB functionality, can I program it directly from a USB port?

That depends entirely on the microcontroller.

In the case of the STM32F070, the answer is "no". While this part supports USB, its bootloader only supports programming over the USART. Another option to consider for programming is SWD.

• are you sure? the stm32duino bootloader doesn't run on STM32F070? – Juraj Aug 20 '19 at 12:35
• @Juraj one can of course put a third-party bootloader in part of the flash at the cost of linking projects to accommodate that and relocate the vector table, the point is that the factory ROM bootloader does not support USB - in contrast to some later/higher end STM32 parts where it supports USB as well as the serial modes that the ROM bootloader of simpler chips like this support. – Chris Stratton Aug 20 '19 at 13:28
• @ChrisStratton Putting any kind of bootloader on a Cortex-M0 part (like the F070) is tricky, as the M0 doesn't have a VTOR register -- the vector table isn't relocatable. This can be worked around but it's nontrivial. – duskwuff -inactive- Aug 20 '19 at 16:43
• Yes, that is specifically what I was referring to – Chris Stratton Aug 20 '19 at 18:16
• @duskwuff that seems like an over complicated way to achieve it, you can remap the memory at 0x00000000 at run time using the syscfg->cfgr1 register to point at ram, all the startup code for your application needs to do is copy the vector table to the start of ram and then change 0x00000000 to point at sram. – Colin Aug 20 '19 at 18:36

The bootloader needs to receive the application binary and save it to flash. It is always possible to create a bootloader that receives the binary over USB or any other communication channel.

EDIT: my answer implies that you don't need an USB to TTL Serial adapter on your PCB. But make sure you can connect one externally to pins of the factory bootloader. Or that you can connect a programmer to SWD pins of the MCU. Because as easy the bootloader can write the application binary, the application can accidentally delete the bootloader.

• Yes, but when that is not a feature of the factory ROM bootloader, you first have to have a way to get your custom bootloader in there. In theory, you could do that in a socket before soldering, in practice it means you need SWD connections or similar on the board. Besides one would have to be extremely confident and experienced and building something chip-scale tiny to justify not bringing those out to at least pogo pads on the back. If the asker makes a board supporting nothing but USB, what they've made is an addition to the "mistakes I've made" gallery hanging on their wall. – Chris Stratton Aug 20 '19 at 13:29
• The problem is that while what you stated it true in isolation, as an answer to the question that was actually asked about board design, it is false and terrible advice. – Chris Stratton Aug 20 '19 at 13:33
• @ChrisStratton, I ansered the title of the Question. In the Question there is nothing about pins of the ROM bootloader not being accessible. Why should the PCB have an on-board USB to TTL Serial converter? the BluePill doesn't have it – Juraj Aug 20 '19 at 14:18
• You are failing to actually understand the intent of the question - you are implying that a board with only the native USB would be workable, when in fact for 99% of people it would not be, as the only way you could use that is if you have a means of pre-programming the chips before soldering them. That doesn't mean you need to put a USB-serial chip on the board, but it does mean that it is required to have a way to access the non-USB ROM bootloader and/or SWD - practically speaking, it's absurd unwise not to bring out both the UART and SWD signals to a place where they can be accessed. – Chris Stratton Aug 20 '19 at 15:04
• @ChrisStratton, the question is if they need an usb to ttl adapter on board. this is because of ROM bootolader, but they don't need the adapter on board. I edited the Answer – Juraj Aug 20 '19 at 16:55