Custom STM32F103C8T6

If you made your own board, post here, unless you built a Maple or Maple mini clone etc
Dallasta
Posts: 6
Joined: Thu Mar 03, 2016 11:43 am

Custom STM32F103C8T6

Post by Dallasta » Fri May 20, 2016 7:06 pm

Hey guys!

I recently designed a custom STM32 board for an RS485 aplication. I was using an Atmega 168 before and I was having trouble with speed, so I decided it was time to move.
The board is simply a STM32F103C8T6 with an AS5048A magnetic encoder and a SN65HVD78 transceiver. It reads the encoder via SPI and sends the information to a dynamixel servo network, using the dynamixel protocol. I have the SWD pins to program it.
As I already have the code for the arduino IDE so I was thinking what should I modify on my code to port it. Another question I have is how I set the clock on the microcontroller (on AVR devices you use the memory fuses)? I'm using a 16Mhz crystal to reach the 72Mhz.
If you want, I can send the codes, the libs and the board schematic.
Here is a picture of the boards (I have two different models to fit in different places). I'm now soldering them and waiting for a few components to arrive.
Image
I would apreciate a lot if someone could help me here.
Thanks!

User avatar
Slammer
Posts: 241
Joined: Tue Mar 01, 2016 10:35 pm
Location: Athens, Greece

Re: Custom STM32F103C8T6

Post by Slammer » Fri May 20, 2016 9:19 pm

Normally STM boards using 8MHz XTAL and a programmable PLL creates the 72MHz. There is a complex clock system compared with AVR, that clocks different peripherals. Arduino core makes everything for setting up the clock, but as I said, it is better to use 8MHz crystal.
I suggest to read at least the first chapters of Geoffrey Brown's book from here: http://www.cs.indiana.edu/~geobrown/book.pdf
It is very important to know something about MCU, everything will be more easy.
As for Arduino compatibility, I can say that most of the libraries not related with hardware directly, will work almost untouchable. Everything is here, Serial, I2C, SPI. Some code with external Interrupts and hardware timers it is possible to need some rework.

Dallasta
Posts: 6
Joined: Thu Mar 03, 2016 11:43 am

Re: Custom STM32F103C8T6

Post by Dallasta » Sat May 21, 2016 12:28 pm

Slammer wrote:Normally STM boards using 8MHz XTAL and a programmable PLL creates the 72MHz. There is a complex clock system compared with AVR, that clocks different peripherals. Arduino core makes everything for setting up the clock, but as I said, it is better to use 8MHz crystal.
I suggest to read at least the first chapters of Geoffrey Brown's book from here: http://www.cs.indiana.edu/~geobrown/book.pdf
It is very important to know something about MCU, everything will be more easy.
As for Arduino compatibility, I can say that most of the libraries not related with hardware directly, will work almost untouchable. Everything is here, Serial, I2C, SPI. Some code with external Interrupts and hardware timers it is possible to need some rework.
Thanks for the help, but my doubt is how I tell the microcontroller that I'm using 8Mhz or 16Mhz? Now I'm considering changing the crystal, because the first time I calculated it on STM32Cube, it told me I could not reach the 72Mhz with an 8Mhz crystal, but now it seems to accept it. Dont know why :? .
Probably the libs i'm using will work with no problem then. I think the Dynamixel protocol uses SoftwareSerial, but that's not a problem, I can easily change it to normal Serial.

User avatar
martinayotte
Posts: 1182
Joined: Mon Apr 27, 2015 1:45 pm

Re: Custom STM32F103C8T6

Post by martinayotte » Sat May 21, 2016 1:18 pm

In the case of F4xxx, it is quite explicit, the code is located in cores/maples/libmaple/rccF2.c :

Code: Select all

        /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
#ifdef ARDUINO_STM32F4_NETDUINO2PLUS
        int PLL_M = 25; // The NETDUINO has a 25MHz external oscillator
#else
        int PLL_M = 8;
#endif
        int PLL_N = 336;

        /* SYSCLK = PLL_VCO / PLL_P */
        int PLL_P = 2;

        /* USB OTG FS, SDIO and RNG Clock =  PLL_VCO / PLLQ */
        int PLL_Q = 7;
But for F1xx, it is a bit more "hidden", for exemple, in variants/maple/wirish/boards_setup.cpp :

Code: Select all

#define BOARD_RCC_PLLMUL RCC_PLLMUL_9
or here for the GD32, in variants/generic_gd32f103c/wirish/boards_setup.cpp :

Code: Select all

// Generic GD32 boards seem to have a 12Mhz crystal rather than the 8Mhz common on STM32 boards, hence the PLL multiplier settings are different.
// Additionally the GD32 has a 4 USB PLL divider settings, rather than the 2 settings in the STM32, which allow it to operate on frequencies of 48,72,96 and 120Mhz and still have USB functionin

#ifndef BOARD_RCC_PLLMUL
#if F_CPU==120000000
        #define BOARD_RCC_PLLMUL RCC_PLLMUL_10
#elif F_CPU==96000000
        #define BOARD_RCC_PLLMUL RCC_PLLMUL_8
#elif F_CPU==72000000
        #define BOARD_RCC_PLLMUL RCC_PLLMUL_6
#endif

#endif

User avatar
RogerClark
Posts: 6118
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Custom STM32F103C8T6

Post by RogerClark » Sat May 21, 2016 1:52 pm

Like Martin says..

Its defined for each variant on the F1.

AFIK, no one else uses 16Mhz. Most boards use 8Mhz, though at least 1 STM32 board uses 12Mhz, and the GD32 boards I have use 12Mhz.

Just to get the board working with your existing crystal, just hack the generic f130c board variant file
https://github.com/rogerclarkmelbourne/ ... _setup.cpp

However I dont know how you setup a multipler of 16 to give you 72mhz. Youd need mult of 4.5

This may be possible, but I'm not sure

The values are defined in

https://github.com/rogerclarkmelbourne/ ... ries/rcc.h

But you'd need to look in the main reference for the F103 (google RM0008) or click here http://www2.st.com/resource/en/referenc ... 171190.pdf

And look to see if its supported and if so , how you set it.

But in the long term, get some 8Mhz crystals ;-)

User avatar
Vassilis
Posts: 295
Joined: Thu May 21, 2015 6:42 am
Location: Thessaloniki, Greece
Contact:

Re: Custom STM32F103C8T6

Post by Vassilis » Sat May 21, 2016 2:23 pm

@Dallasta
Open the MXBluePillF103C8.ioc file with STM32CubeMX and see how the 8 MHz clock configuration is set on STM32F103C8 MCU (Clock Configuration tab).
Attachments
MXBluePillF103C8.zip
(2.21 KiB) Downloaded 62 times

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

Re: Custom STM32F103C8T6

Post by Pito » Sun May 22, 2016 6:15 am

There is a prescaler by 2 in front of PLL so you may divide a 16MHz crystal to get 8MHz and use the PLL setting as is. In order to switch the prescaler on:
PLLXTPRE: HSE divider for PLL entry
Set and cleared by software to divide HSE before PLL entry. This bit can be written only
when PLL is disabled.
0: HSE clock not divided
1: HSE clock divided by 2
Pukao Hats Cleaning Services Ltd.

Signal32
Posts: 26
Joined: Sun May 08, 2016 12:19 am

Re: Custom STM32F103C8T6

Post by Signal32 » Sun May 22, 2016 12:43 pm

Pito wrote:There is a prescaler by 2 in front of PLL so you may divide a 16MHz crystal to get 8MHz and use the PLL setting as is. In order to switch the prescaler on:
PLLXTPRE: HSE divider for PLL entry
Set and cleared by software to divide HSE before PLL entry. This bit can be written only
when PLL is disabled.
0: HSE clock not divided
1: HSE clock divided by 2
This is great! Now I can use 3225 16Mhz crystals which are ~5 times cheaper than the 8Mhz ones. Thanks!

User avatar
RogerClark
Posts: 6118
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Custom STM32F103C8T6

Post by RogerClark » Sun May 22, 2016 1:23 pm

can you post a code snippet when you get it working using the libmaple based core , of what you did to make it work

Dallasta
Posts: 6
Joined: Thu Mar 03, 2016 11:43 am

Re: Custom STM32F103C8T6

Post by Dallasta » Sun May 22, 2016 6:38 pm

Thanks for the help guys!
Now i'm really considering using 8Mhz crystals...
What's the default configuration for the clock? I really dont want to hack things and hope for them to work. I dont have much time to finish this projetc (Brace yourselves, Robocup is coming!). If I use an 8Mhz crystal, the prescalers are already set to reach 72Mhz?

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest