Signal32 wrote:stevech wrote:Signal32 wrote:This is great! Now I can use 3225 16Mhz crystals which are ~5 times cheaper than the 8Mhz ones. Thanks!

5x for a normal surface mount? Digikey pricing difference is 5 cents on an 85 cent part.

I was talking specifically about 3225(3.2mm x 2.5mm) crystals -- China pricing.

Digikey price difference in qty1 is only 2x, not 5x as with China pricing.

8MHz = 90c --

http://www.digikey.com/product-search/e ... ageSize=2516Mhz = 46c --

http://www.digikey.com/product-search/e ... ageSize=25Not to mention that you can get even smaller sizes for 16Mhz crystals (2016)

Interesting. Dragonfly is using a 16Mhz crystal over a 8MHz (

https://www.tindie.com/products/onehors ... out-board/). I guess now I know why

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.