📜 ⬆️ ⬇️

Filling firmware in STM32 via USB

image

In my project, I use the microcontroller STM32F103C8 and the stm32duino framework . This Arduino clone offers a special bootloader that allows you to upload firmware via USB, without using external components like ST-Link or USB-UART adapter.

Today I needed to work with a bare controller from under CooCox and without stm32duino. But that's the problem. Even a simple blinker poured through this bootloader does not work.
')
Let's figure it out. Perhaps my calculations seem to someone commonplace. But I am just starting to study the STM32 controllers and, in search of a problem, I killed at least half a day. Suddenly this article will shorten the development time.

I have nothing against ST-Link and other debuggers. But it won't be in my finished device, but it will definitely be USB. Why not immediately lay the opportunity to update the firmware via USB? Personally, I find this method convenient. the more so that all the same I have already connected a cord through which the power and USB Serial are going.

Let's see how the bootloader works. First, using the example of AVR controllers. Why did I remember about him? I switched from Arduino and subconsciously expected the same behavior. But the STM32 turned out to be different. Because I want to talk about the difference between these two microcontrollers.

So. In AVR ATMega microcontrollers, you can reserve a certain amount of memory under the bootloader near the end of the flash. With the help of fuse bits, you can adjust from which address the program will start. If there is no bootloader, the program starts from the address 0x0000. If there is a bootloader, it starts from some other address (for example, in ATMega32 with 0x3C00, if the bootloader size is 2k).

image

When the bootloader has done its work, it transfers control to the main program from the address 0x0000. Those. the program always starts at address 0x0000. The compiler and linker work taking into account the fact that the code will be located at the beginning of the address space.

In STM32 microcontrollers, this is not the case. All programs start at address 0x0800000. The bootloader is not so special. This is the same program that starts from the same starting address. In the process of operation, the bootloader can receive firmware (via USB or UART, read from a flash drive, receive from a satellite, get from a whatever, ...) and write it to addresses higher than the bootloader itself. And, of course, at the end of their work, to transfer control to the main program.

image

So when compiling the firmware, you need to know where the bootloader will write the firmware and adjust the addresses accordingly.

On this with the theory of everything. We proceed to practice. Below is a step-by-step instruction on how to screw the USB bootloader to the STM32F1xx series microcontrollers, and maybe to some others too.

There are, however, some limitations on circuitry. Here I, unfortunately, is not strong. ITP needs a pull-up resistor 1.5k for the PA12 port (also known as USB D +). This allows the bootloader to connect and disconnect from USB at the right times.

Instruction:


Another bit of nuance. Before uploading the firmware, you need to start the bootloader. The easiest way is to press the reset button. After that, the bootloader will start and the firmware will wait for a few seconds. If at that moment nobody launched maple_upload, the loader will transfer control to the main firmware.

In order not to press resets each time, motherboards based on libmaple / stm32duino use a trick. They listen to usb serial port. If a DTR signal appears there and a key sequence of bytes is transmitted, the microcontroller is reloaded into a bootloader. See rxHook () function .

This may cause inconvenience. If the microcontroller zaglyuchil and hung, then he no longer listens to the port. Therefore, he can not hear the key sequence and reboot into the bootloader. Then only reset to help.

That's all. I hope my article will shed light on how the bootloader works in the STM32 and how you can download the firmware via the USB port. Unfortunately, the threshold of entry is still high, but suddenly someone my article will help him overcome.

Source: https://habr.com/ru/post/403007/


All Articles