Linux: flashing own STM32F103C8T6 generic USB bootloader not much luck

STM32duino bootloader aka Maple bootloader
Hypercube
Posts: 6
Joined: Fri Apr 28, 2017 10:28 pm

Linux: flashing own STM32F103C8T6 generic USB bootloader not much luck

Post by Hypercube » Fri Apr 28, 2017 11:36 pm

Hi,

I built my own STM32F103C8T6 board and used Linux tools under Ubuntu 14.04 to flash the board
~/Arduino/hardware/Arduino_STM32/tools/linux/stlink$ ./st-flash write generic_boot20_pc13.bin 0x08000000
Seems to flash OK, I tried a few more binaries from here:
https://github.com/rogerclarkmelbourne/ ... 1/binaries

But after flashing, the USB does not register as ttyACM0 when it is plugged in.
I know the USB works because I have some C software I built using ST's AC6 compiler
and when I flash it with that, then it does register as ttyACM0, and I can see regular serial data coming
out of it in the Arduino IDE serial monitor.

(As an aside, I can also program Arduino blink project to the board using STLink upload method
and it works! Amazing! So many moving parts come together to make it happen! :shock: )

So where can I start / what I can do to make STM32duino-bootloader work?

Is it possible I misunderstood something - i.e. that I need to do something to the board to activate
the USB loader software by setting boot pins?

victor_pv
Posts: 1650
Joined: Mon Apr 27, 2015 12:12 pm

Re: Linux: flashing own STM32F103C8T6 generic USB bootloader not much luck

Post by victor_pv » Sat Apr 29, 2017 12:56 am

The bootloader will not enumerate as a serial port, only as a DFU device.
When you load a sketch and the sketch starts, the sketch code will emulate a serial device.

User avatar
Pito
Posts: 1525
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Linux: flashing own STM32F103C8T6 generic USB bootloader not much luck

Post by Pito » Sat Apr 29, 2017 5:50 am

Do you have got the d+ usb switch (via 1k5 to Vcc) on your board?
Pukao Hats Cleaning Services Ltd.

ag123
Posts: 740
Joined: Thu Jul 21, 2016 4:24 pm

Re: Linux: flashing own STM32F103C8T6 generic USB bootloader not much luck

Post by ag123 » Sat Apr 29, 2017 6:23 am

compile and install a sketch from roger's f1 core https://github.com/rogerclarkmelbourne/Arduino_STM32
usb is initialized and enumerated by the sketch itself
make sure that the define SERIAL_USB is there in platforms.txt specified as a compile flag

run lsusb

with a sketch in a normal boot you would see 1eaf:0004 - serial usb device
if you connect into the dfu or 'perpetual boot loader mode' you would see 1eaf:0003 dfu boot loader
in the dfu mode you can install a sketch using dfu-util
http://www.stm32duino.com/viewtopic.php?f=22&t=1958
http://www.stm32duino.com/viewtopic.php ... 0&start=10

Hypercube
Posts: 6
Joined: Fri Apr 28, 2017 10:28 pm

Re: Linux: flashing own STM32F103C8T6 generic USB bootloader not much luck

Post by Hypercube » Sat Apr 29, 2017 8:16 am

Pito wrote:Do you have got the d+ usb switch (via 1k5 to Vcc) on your board?
Yes the resistor is present and the USB is working with ST's free AC6 Linux Eclipse compiler built project with code written in C.

Hypercube
Posts: 6
Joined: Fri Apr 28, 2017 10:28 pm

Solved! Linux: flashing own STM32F103C8T6 generic USB bootloader not much luck

Post by Hypercube » Sat Apr 29, 2017 8:51 am

victor_pv wrote:The bootloader will not enumerate as a serial port, only as a DFU device.
When you load a sketch and the sketch starts, the sketch code will emulate a serial device.
I am able to load projects through STLink interface,
such as Arduino/hardware/Arduino_STM32/examples/Sensors/HardTimerAsEncoder/HardTimerAsEncoder.ino
example project, and see LED flashing so I know the code is running.
But when I connect the USB, it won't enumerate the USB:

[ 920.378361] usb 1-1.4: new full-speed USB device number 7 using ehci-pci
[ 920.450359] usb 1-1.4: device descriptor read/64, error -32
....
[ 922.114434] usb 1-1-port4: unable to enumerate USB device

The I try Tools > CPU Speed > 72 MHz and then Tools > CPU Speed > 48MHz - Slow with USB
Made no difference.

Then I spot in the Arduino IDE:

Build options changed, rebuilding all
~/Arduino/hardware/Arduino_STM32/STM32F1/cores/maple/libmaple/usb/stm32f1/usb_cdcacm.c:65:2: warning: #warning USB CDC ACM relies on LeafLabs board-specific configuration. You may have problems on non-LeafLabs boards. [-Wcpp]
#warning USB CDC ACM relies on LeafLabs board-specific configuration.\
^

:idea: All this got me thinking - how does that clock tree get set?
The board I built is using 12MHz xtal and the clock tree has been set up with SYSCLK running
at 72MHz and other prescalers set up correctly to make USB work at 48MHz
using STM32CubeMX, and so thats probably why the C based code works.

Checking the LeafLabs schematic, the maplemini is 8MHz.
:
Did some more research :shock: managed to solve it :D

1. Ran STM32CubeMX and changed frequency from 12MHz to 8MHz and noted that only PLLMUL need to change in value from 6 to 9 to make everything work at 8MHz. So working in reverse there should be a setting for PLLMUL that is 9 that needs to be changed to 6 to make it work at 12MHz
2. Did ~/Arduino/hardware$ grep -R PLLMUL > ~/stduino.txt and searching for PLLMUL in stduino.txt see lots of RCC_PLLMUL_9 and RCC_PLLMUL_6.
3. Attention is drawn to file ~/Arduino/hardware/Arduino_STM32/STM32F1/variants/generic_stm32f103c/wirish/boards_setup.cpp

#ifndef BOARD_RCC_PLLMUL
#if F_CPU==72000000
#define BOARD_RCC_PLLMUL RCC_PLLMUL_9
#elif F_CPU==48000000
#define BOARD_RCC_PLLMUL RCC_PLLMUL_6
#endif
#endif

So the code seems to set BOARD_RCC_PLLMUL RCC_PLLMUL_9 if CPU is running at 72000000.
I just just changed that line to #define BOARD_RCC_PLLMUL RCC_PLLMUL_6
Then on the Arduino IDE, making sure Tools > CPU speed is set to 72MHz, I programmed it.

Hey Presto! It works!!! :D :D
I guess I am going to need to create some new board definition to make it all work
in a user friendly way or change the xtal.

In total this project is impressive. I am ever so thankful to the guys that built this empire.
Last edited by Hypercube on Sat Apr 29, 2017 10:23 am, edited 4 times in total.

User avatar
Pito
Posts: 1525
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Linux: flashing own STM32F103C8T6 generic USB bootloader not much luck

Post by Pito » Sat Apr 29, 2017 9:32 am

Where in the stm32duino environment did you a change to reflect the 12MHz Xtal?
Pukao Hats Cleaning Services Ltd.

Hypercube
Posts: 6
Joined: Fri Apr 28, 2017 10:28 pm

Re: Linux: flashing own STM32F103C8T6 generic USB bootloader not much luck

Post by Hypercube » Sat Apr 29, 2017 11:04 am

Pito wrote:Where in the stm32duino environment did you a change to reflect the 12MHz Xtal?
The file ~/Arduino/hardware/Arduino_STM32/STM32F1/variants/generic_stm32f103c/wirish/boards_setup.cpp is

#ifndef BOARD_RCC_PLLMUL
#if F_CPU==72000000
#define BOARD_RCC_PLLMUL RCC_PLLMUL_9
#elif F_CPU==48000000
#define BOARD_RCC_PLLMUL RCC_PLLMUL_6
#endif
#endif

I changed:

#ifndef BOARD_RCC_PLLMUL
#if F_CPU==72000000
#define BOARD_RCC_PLLMUL RCC_PLLMUL_6
#elif F_CPU==48000000
#define BOARD_RCC_PLLMUL RCC_PLLMUL_6
#endif
#endif

The serial port now registers as ttyACM0, but alas, nothing gets printed through it :(

I tried Serial.begin(115200); in setup(), and Serial.print("Hi"); delay(1000); in loop(), but nothing
I also tried to change from Serial to SerialUSB, but that causes error SerialUSB' was not declared in this scope.
Any ideas?
:
Ignore that - it suddenly starts working - :)

victor_pv
Posts: 1650
Joined: Mon Apr 27, 2015 12:12 pm

Re: Linux: flashing own STM32F103C8T6 generic USB bootloader not much luck

Post by victor_pv » Sat Apr 29, 2017 7:56 pm

Hypercube wrote:
Pito wrote:Where in the stm32duino environment did you a change to reflect the 12MHz Xtal?
The file ~/Arduino/hardware/Arduino_STM32/STM32F1/variants/generic_stm32f103c/wirish/boards_setup.cpp is

#ifndef BOARD_RCC_PLLMUL
#if F_CPU==72000000
#define BOARD_RCC_PLLMUL RCC_PLLMUL_9
#elif F_CPU==48000000
#define BOARD_RCC_PLLMUL RCC_PLLMUL_6
#endif
#endif

I changed:

#ifndef BOARD_RCC_PLLMUL
#if F_CPU==72000000
#define BOARD_RCC_PLLMUL RCC_PLLMUL_6
#elif F_CPU==48000000
#define BOARD_RCC_PLLMUL RCC_PLLMUL_6
#endif
#endif

The serial port now registers as ttyACM0, but alas, nothing gets printed through it :(

I tried Serial.begin(115200); in setup(), and Serial.print("Hi"); delay(1000); in loop(), but nothing
I also tried to change from Serial to SerialUSB, but that causes error SerialUSB' was not declared in this scope.
Any ideas?
:
Ignore that - it suddenly starts working - :)
If you select STLink as upload method then the Serial device is the USART1, not the USB.
Try loading the bootloader to the board, then upload using bootloader method, otherwise try SerialUSB.begin, SerialUSB.print etc.

Hypercube
Posts: 6
Joined: Fri Apr 28, 2017 10:28 pm

Re: Linux: flashing own STM32F103C8T6 generic USB bootloader not much luck

Post by Hypercube » Sun Apr 30, 2017 12:07 am

victor_pv wrote: If you select STLink as upload method then the Serial device is the USART1, not the USB.
Try loading the bootloader to the board, then upload using bootloader method, otherwise try SerialUSB.begin, SerialUSB.print etc.
I think today STLink upload sets the serial device to USB by default and that bit is now working.

The problems were down to Arduino IDE grabbing the serial port and not letting it go when the device disconnects (Ubuntu 14.04).
What the serial monitor needs is a restart, disconnect and connect button at the bottom of the window with a option to set the relevant port.
If I close serial monitor, unplug the device, connect it back in and open serial monitor, the thing is OK, but hey
I don't want to keep doing this wearing out the connector.

The other half of the problem is that I have a 12MHz xtal on my board. Which means the default 8MHz clock tree settings for the maple board won't work in this new board until BOARD_RCC_PLLMUL is set correctly everywhere. And then the boot loader needs to be compiled - don't know how to do that :(

As a shortcut I tried to run maple_mini_bootloaer_updater.ino but after you press Y, the USB will not work because of the clock tree settings
being incorrect again somewhere.

Post Reply