Search code examples
embeddedmbedopenocd

What happens on the background when mbed CLI flashes the chip?


Even though not every detail is relevant for this question, I will list my setup nonetheless:

To compile my code and flash the binary to the chip, I issue the following command in my cmd terminal:

> mbed compile -t GCC_ARM -m NUCLEO_F746ZG --flash

I get the following output:

...

+------------------+-------+-------+-------+
| Module           | .text | .data |  .bss |
+------------------+-------+-------+-------+
| [fill]           |   130 |     4 |    10 |
| [lib]\c.a        | 24965 |  2472 |    89 |
| [lib]\gcc.a      |  3120 |     0 |     0 |
| [lib]\misc       |   252 |    16 |    28 |
| mbed-os\drivers  |   658 |     4 |   100 |
| mbed-os\features |    74 |     0 | 12556 |
| mbed-os\hal      |  2634 |     4 |    66 |
| mbed-os\platform |  2977 |     4 |   270 |
| mbed-os\rtos     | 15887 |   168 |  5989 |
| mbed-os\targets  | 16013 |     4 |  1052 |
| source\main.o    |   244 |     4 |    84 |
| Subtotals        | 66954 |  2680 | 20244 |
+------------------+-------+-------+-------+
Total Static RAM memory (data + bss): 22924 bytes
Total Flash memory (text + data): 69634 bytes

Image: .\BUILD\NUCLEO_F746ZG\GCC_ARM\nucleo_f746zg_demo.bin
[mbed] Detected "NUCLEO_F746ZG" connected to "E:" and using com port "COM10"
        1 file(s) copied.

I'm particularly interested in the last lines, where the actual flashing of the chip takes place:

Image: .\BUILD\NUCLEO_F746ZG\GCC_ARM\nucleo_f746zg_demo.bin
[mbed] Detected "NUCLEO_F746ZG" connected to "E:" and using com port "COM10"
        1 file(s) copied.

I know from previous experience (before mbed CLI existed) that there is a lot going on to flash a binary to a chip. For example, I had to startup openocd, pass it a configuration file of the programmer (eg. stlink-v2-1.cfg) and a configuration file of the target board (eg. nucleo_f746zg.cfg). At last, I had to hand over the binary to openocd via a Telnet-session or a GDB-session. Everything is described extensively here: How to use the GDB (Gnu Debugger) and OpenOCD for microcontroller debugging - from the terminal?

Looking at mbed CLI flashing the chip, I get confused. What is happening on the background? Is mbed CLI secretly using openocd to connect to the chip? Or perhaps pyOCD? Or some other way?


Solution

  • mbed-cli is open source, you can find the repository here. If you search for "def compile_" you'll find the specific code for what is happening when you run mbed compile.

    mbed-cli uses mbed-ls to detect your board and htrun to flash it. htrun has a variety of plugins for copying to different boards, including pyocd but in the most basic case it just copies to the disk detected with mbed-ls.