Custom STM32F103C8T6
- RogerClark
- Posts: 8416
- Joined: Mon Apr 27, 2015 10:36 am
- Location: Melbourne, Australia
- Contact:
Re: Custom STM32F103C8T6
Yes
Use 8Mhz and you won't need to change any code.
I suspect to use 16Mhz, the change you would need to make is just 1 bit in one register, but I'd need to read the big manual on the STM32F103 to work out which bit enabled the DIV 2 on the input clock
Use 8Mhz and you won't need to change any code.
I suspect to use 16Mhz, the change you would need to make is just 1 bit in one register, but I'd need to read the big manual on the STM32F103 to work out which bit enabled the DIV 2 on the input clock
Re: Custom STM32F103C8T6
5x for a normal surface mount? Digikey pricing difference is 5 cents on an 85 cent part.Signal32 wrote: This is great! Now I can use 3225 16Mhz crystals which are ~5 times cheaper than the 8Mhz ones. Thanks!
Re: Custom STM32F103C8T6
I was talking specifically about 3225(3.2mm x 2.5mm) crystals -- China pricing.stevech wrote:5x for a normal surface mount? Digikey pricing difference is 5 cents on an 85 cent part.Signal32 wrote: This is great! Now I can use 3225 16Mhz crystals which are ~5 times cheaper than the 8Mhz ones. Thanks!
Digikey price difference in qty1 is only 2x, not 5x as with China pricing.
8MHz = 90c -- http://www.digikey.com/product-search/e ... ageSize=25
16Mhz = 46c -- http://www.digikey.com/product-search/e ... ageSize=25
Not to mention that you can get even smaller sizes for 16Mhz crystals (2016)
- RogerClark
- Posts: 8416
- Joined: Mon Apr 27, 2015 10:36 am
- Location: Melbourne, Australia
- Contact:
Re: Custom STM32F103C8T6
If you want to try to use your 16Mhz crystals
I think you'll need to modify
STM32F1\cores\maple\libmaple\rcc_f1.c
Specifically
Looking in the manual
http://www.st.com/content/ccc/resource/ ... 171190.pdf
on page 100
It looks like bit 17 is what you'd need to set in order to enabled the external prescaler "PLL XTPRE"
So you could change it in the boards_setup by changing the line
By OR'ing in
e.g. I think like this
But its difficult for me to test at the moment
I think you'll need to modify
STM32F1\cores\maple\libmaple\rcc_f1.c
Specifically
Code: Select all
void rcc_configure_pll(rcc_pll_cfg *pll_cfg) {
stm32f1_rcc_pll_data *data = pll_cfg->data;
rcc_pll_multiplier pll_mul = data->pll_mul;
uint32 cfgr;
/* Check that the PLL is disabled. */
ASSERT_FAULT(!rcc_is_clk_on(RCC_CLK_PLL));
cfgr = RCC_BASE->CFGR;
cfgr &= ~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLMUL);
cfgr |= pll_cfg->pllsrc | pll_mul;
RCC_BASE->CFGR = cfgr;
}
http://www.st.com/content/ccc/resource/ ... 171190.pdf
on page 100
It looks like bit 17 is what you'd need to set in order to enabled the external prescaler "PLL XTPRE"
So you could change it in the boards_setup by changing the line
Code: Select all
static stm32f1_rcc_pll_data pll_data = {BOARD_RCC_PLLMUL};
Code: Select all
(0x1 << 17)
Code: Select all
static stm32f1_rcc_pll_data pll_data = {BOARD_RCC_PLLMUL | (0x1 << 17) };
- GrumpyOldPizza
- Posts: 204
- Joined: Fri Apr 15, 2016 4:15 pm
- Location: Denver, CO
Re: Custom STM32F103C8T6
Interesting. Dragonfly is using a 16Mhz crystal over a 8MHz (https://www.tindie.com/products/onehors ... out-board/). I guess now I know whySignal32 wrote:I was talking specifically about 3225(3.2mm x 2.5mm) crystals -- China pricing.stevech wrote:5x for a normal surface mount? Digikey pricing difference is 5 cents on an 85 cent part.Signal32 wrote: This is great! Now I can use 3225 16Mhz crystals which are ~5 times cheaper than the 8Mhz ones. Thanks!
Digikey price difference in qty1 is only 2x, not 5x as with China pricing.
8MHz = 90c -- http://www.digikey.com/product-search/e ... ageSize=25
16Mhz = 46c -- http://www.digikey.com/product-search/e ... ageSize=25
Not to mention that you can get even smaller sizes for 16Mhz crystals (2016)

Anyway, I ended up coding clock setup code that would compute the PLL values: stm32l4_system_configure(), https://github.com/GrumpyOldPizza/ardui ... 4_system.c.
The idea is pretty simple, but the limits and divisors are different for STM32F1 (this is for STM32L4). For latter one it's complicated by the fact that you may want to use MSI @ 48MHz if HSE is not present at all, which means your input clock (post M-divisor) needs to be 8MHz for consistency (16MHz HSE divided by 2, 48MHz MSI divided by 6). Here a quick snipped with the gist of it:
Code: Select all
if (hseclk <= 48000000)
{
mout = hseclk / 8000000;
}
else
{
/* MSI with 48MHz */
mout = 6;
}
fclk = 8000000;
fpllout = 0;
nout = 0;
rout = 0;
for (r = 2; r <= 8; r += 2)
{
n = (sysclk * r) / fclk;
fvco = fclk * n;
if ((n >= 8) && (n <= 86) && (fvco <= 344000000) && (fvco >= 96000000))
{
fpll = fvco / r;
/* Prefer lower N,R pairs for lower PLL current. */
if (fpllout < fpll)
{
fpllout = fpll;
nout = n;
rout = r;
}
}
}
sysclk = fpllout;
}
At the end mout, nout, rout should be valid for the selecting input "sysclk" (which then is subdivided into hclk, pclk1 & pclk2). Ah I should not forget to mention that I had also tried to autodetect the HSE value by comparing it to LSE (32.678kHz). Worked nicely, but was not needed for the task at hand

Anyway it seems to be easier to compute this on the fly than having to hardcode all sorts of values.
Re: Custom STM32F103C8T6
Wouldn't a 16MHz crystal press the PLL stability and noise less than an 8MHz?
And, on the STM32F4 (and others?) the internal clock that runs at power up (HSI) and runs all the C startup code, is 16MHz. Switching to the HSE clock, stays at 16MHz until (or if) the PLL is used. Note that the C startup code at HSI speed has the problem that if you have a lot of BSS to be zero'd and/or a huge number of initialized variables (other than const), that all runs at HSI speed and can slow the startup process - if that's a factor. It is in one of my projects where the CPU has to get to the first line of code in main.c in < 10mSec.
I have an app on the '415 that runs at 16MHz for power up (hardware dictated) then on the PLL at 64MHz for most processing. When it's time to number-crunch, I switch to 168MHz for a few hundred mSec. Then back. For the switch, I reprogram some of the peripherals (debut UART, SPI) so they will stay at the same line speed no matter the speed switching to 168 then back to 64MHz.
Point being, the 8MHz crystal choice has other factors that may apply to some cases.
And, on the STM32F4 (and others?) the internal clock that runs at power up (HSI) and runs all the C startup code, is 16MHz. Switching to the HSE clock, stays at 16MHz until (or if) the PLL is used. Note that the C startup code at HSI speed has the problem that if you have a lot of BSS to be zero'd and/or a huge number of initialized variables (other than const), that all runs at HSI speed and can slow the startup process - if that's a factor. It is in one of my projects where the CPU has to get to the first line of code in main.c in < 10mSec.
I have an app on the '415 that runs at 16MHz for power up (hardware dictated) then on the PLL at 64MHz for most processing. When it's time to number-crunch, I switch to 168MHz for a few hundred mSec. Then back. For the switch, I reprogram some of the peripherals (debut UART, SPI) so they will stay at the same line speed no matter the speed switching to 168 then back to 64MHz.
Point being, the 8MHz crystal choice has other factors that may apply to some cases.
Re: Custom STM32F103C8T6
Thank you guys for all the help, the 8mhz crystal seems to be working fine.
But I still have some doubts about the programming. Today I tried to make the serial work (uploading thru ST Link and no bootloader) and I was not able to do it. My hardware uses the USART2 and maybe that was the problem. Can someone help me?
Im still very newbie with ARM controllers, i have just dealed with AVR devices.
Also had a problem with the PWM, I dont know why, but it seems to just turn the pin to a normal IO pin, I could not set the frequency for it, hope someone have an idea avout this also.
Thanks!
But I still have some doubts about the programming. Today I tried to make the serial work (uploading thru ST Link and no bootloader) and I was not able to do it. My hardware uses the USART2 and maybe that was the problem. Can someone help me?
Im still very newbie with ARM controllers, i have just dealed with AVR devices.
Also had a problem with the PWM, I dont know why, but it seems to just turn the pin to a normal IO pin, I could not set the frequency for it, hope someone have an idea avout this also.
Thanks!
- RogerClark
- Posts: 8416
- Joined: Mon Apr 27, 2015 10:36 am
- Location: Melbourne, Australia
- Contact:
Re: Custom STM32F103C8T6
I think STLink upload has USB serial enabled,
so Serial = USB Serial
Serial1 is Hardware serial 1
Serial2 is Hardware serial 2
etc
If you dont want USB serial, you will need to change boards.txt to remove the inline definitions for the USB serial stuff
or try uploading via Serial, as that upload mode does not have USB serial enabled
so Serial = USB Serial
Serial1 is Hardware serial 1
Serial2 is Hardware serial 2
etc
If you dont want USB serial, you will need to change boards.txt to remove the inline definitions for the USB serial stuff
or try uploading via Serial, as that upload mode does not have USB serial enabled
Re: Custom STM32F103C8T6
Ooh, that helps a lot, I will try it tomorrow and bring the result here. I dont know if this information helps, but im using a discovery board to upload.RogerClark wrote:I think STLink upload has USB serial enabled,
so Serial = USB Serial
Serial1 is Hardware serial 1
Serial2 is Hardware serial 2
etc
If you dont want USB serial, you will need to change boards.txt to remove the inline definitions for the USB serial stuff
or try uploading via Serial, as that upload mode does not have USB serial enabled
I have custom hardware, so its not so practical for me to upload via Serial, but i will try to change boards.txt and see if it works
Re: Custom STM32F103C8T6
win 7 here. ST-Link shows up in device manager USB list as "Universal Serial Bus Devices \ STMIcroelectronics STLink dongle."
Not in "Ports" serial ports
Not in "Ports" serial ports