Skip to content

cpu/stm32f[2|4|7]: unify stmclk and improve PLL configuration#7477

Merged
kaspar030 merged 4 commits intoRIOT-OS:masterfrom
OTAkeys:pr/fix_stm32_clk
Aug 29, 2017
Merged

cpu/stm32f[2|4|7]: unify stmclk and improve PLL configuration#7477
kaspar030 merged 4 commits intoRIOT-OS:masterfrom
OTAkeys:pr/fix_stm32_clk

Conversation

@vincent-d
Copy link
Copy Markdown
Member

When initializing PLL, input frequency was hard-coded to 2Mhz, leading to invalid output frequency when the crystal is not an even frequency (25MHz for instance).

Now, PLL input frequency can be configured (PLL_IN_FREQ) and PLL M and N factors are checked.

@vincent-d vincent-d added Platform: ARM Platform: This PR/issue effects ARM-based platforms Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) labels Aug 16, 2017
@vincent-d vincent-d added this to the Release 2017.10 milestone Aug 16, 2017
@smlng
Copy link
Copy Markdown
Member

smlng commented Aug 16, 2017

side questions: as both files look (very) similar can (or should) this go into cpu/stm32_common somehow?

@smlng
Copy link
Copy Markdown
Member

smlng commented Aug 16, 2017

@MichelRottleuthner might be interesting for you, too?

@vincent-d vincent-d changed the title cpu/stm32f[2|4]: improve PLL configuration cpu/stm32f[2|4|7]: unify stmclk and improve PLL configuration Aug 16, 2017
@vincent-d
Copy link
Copy Markdown
Member Author

@smlng I unified stm32f2,4,7 stmclk files in stm32_common. It might be adapted to other stm32...

Changed PR title accordingly

@smlng
Copy link
Copy Markdown
Member

smlng commented Aug 16, 2017

nice work! Hope, it doesn't break existing stuff 🤞 We only have very few STM nucleos available for testing, to hopefully @aabadie can help on that, too.

@smlng smlng added CI: needs squashing Commits in this PR need to be squashed; If set, CI systems will mark this PR as unmergable Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Aug 16, 2017
@vincent-d
Copy link
Copy Markdown
Member Author

I think it would need some more documentation on how to configure everything, does someone have an idea where is the best place to write that?

@smlng
Copy link
Copy Markdown
Member

smlng commented Aug 16, 2017

though this needs some squashing, still Murdock can have a first look on this.

@smlng
Copy link
Copy Markdown
Member

smlng commented Aug 16, 2017

where is the best place to write that

how about cpu/stm32_common/include/stmclk.h or create a doc.txt in cpu/stm32_common and add it there?

@vincent-d
Copy link
Copy Markdown
Member Author

Added coreclock max value check and documentation (doc.txt in cpu/stm32_common)

@vincent-d
Copy link
Copy Markdown
Member Author

Removed coreclock check as RCC_MAX_FREQUENCY seems to be defined only for f4.

@vincent-d
Copy link
Copy Markdown
Member Author

I split the configuration in one header per PLL. Now, main PLL, PLLI2S and PLLSAI can be configured.

Copy link
Copy Markdown
Contributor

@aabadie aabadie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments and question. Otherwise this is very good job. I'll be able to test that on hardware next week.

#define _PLL_N_FINISHED 1
#endif
#if !_PLL_N_FINISHED
#undef P
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are those lines needed, since the macro are redefined just below ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes because P could be already defined above, which would cause a preprocessor error on the (re)define below.

#define PLLI2S_SRC RCC_PLLI2SCFGR_PLLI2SSRC
#else
#define PLLI2S_IN PLL_IN
#define PLLI2S_SRC 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use ()

/**
* @ingroup cpu
* @defgroup cpu_stm32_common STM32 common
* @{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this block @{ is required.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, I think the block is not needed, too - though it doesn't hurt either. Still I would re-order that to:

/**
 * @defgroup cpu_stm32_common STM32 common
 * @ingroup cpu
 * @brief STM32 common configurations
[ * @{ ] <- optional
 *

the @brief should be above @{ (if still used)

@smlng
Copy link
Copy Markdown
Member

smlng commented Aug 17, 2017

I split the configuration in one header per PLL.

does it simplify [edit] things/configuration [/edit], because looks like you introduced lots of duplicate code/macros and more LOCs now than with the single header before,

@vincent-d
Copy link
Copy Markdown
Member Author

does it simplify think, because looks like you introduced lots of duplicate code/macros and more LOCs now than with the single header before,

There is more features now. A lot can be calculated automatically. And the problems with macro are:

  • there is no loop structures
  • it's hard to reuse (I'm not 100% sure with some macro evaluation, though. maybe it could be refactored a bit)

@vincent-d
Copy link
Copy Markdown
Member Author

Refactored some macros

@haukepetersen
Copy link
Copy Markdown
Contributor

a big +1 for the changes in general. However, the macro-code is very hard to read and will be extremely hard to maintain in the future!

Without having tried this: but couldn't we solve a lot of this functionality using inline functions? As all parameters are constants, the compiler should actually be able reduce those inline functions to constant expressions, right? Especially when building with LTO=1, the code size increase for these functions should be more or less non-existent but the readability/maintainability should be vastly improved.

@vincent-d
Copy link
Copy Markdown
Member Author

vincent-d commented Aug 18, 2017

However, the macro-code is very hard to read

I can only agree with that ;) even if I tried my best to have something easy to read.

but couldn't we solve a lot of this functionality using inline functions?

I don't know if it's really possible. I can give a try. But I would like to avoid any runtime computation, as the clock init is called when waking up where saving time is important.
I'm open to any help and suggestions on this!

@vincent-d
Copy link
Copy Markdown
Member Author

Another idea would be to write that in an external tool (in python for instance) and generate the right constants.
But:

  • I don't know how to read properly preprocessor macro (you need at least CLOCK_HSE and CLOCK_CORECLOCK plus some constants from vendor header) in a python tool
  • I don't know how to add such a tool in the build process

@haukepetersen
Copy link
Copy Markdown
Contributor

Another idea would be to write that in an external tool (in python for instance) and generate the right constants.

I think I would like that. How about something simple in C like I did for the computation of SPI clock frequencies -> cpu/stm32_common/dist/spi_divtable/spi_divtable.c? Or we might even want to add the functionality into a single tool, which computes all the relevant things for clocking a board. This would not even have to be part of the build-system, but it could simply statically dump the new configuration which then has to be copied manually into the periph_conf.h file.

@vincent-d
Copy link
Copy Markdown
Member Author

the size and efficiency of the tool are second order to clarity and maintainability

I know, when I wrote smaller, I meant, less line duplication (same init value -> line duplication) ;)

I think I found a way. I'll try to push something this afternoon.

@haukepetersen
Copy link
Copy Markdown
Contributor

nice!

@vincent-d
Copy link
Copy Markdown
Member Author

Refactored clk_conf. Config is now in header file with a structure and an array.

@haukepetersen
Copy link
Copy Markdown
Contributor

nice, I like it!

@vincent-d
Copy link
Copy Markdown
Member Author

Fixed some wrong APB values, fixed spaces, updated boards.

Should be OK now!

@vincent-d
Copy link
Copy Markdown
Member Author

vincent-d commented Aug 28, 2017

Removed stm32f0 code from clk_conf (will be added in #7500)

Copy link
Copy Markdown
Contributor

@haukepetersen haukepetersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some minor remarks left, otherwise I think this PR is good to go.

puts("");

printf("/**\n"
" * @name Clock settings\n"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indention slipped off, should be tab aligned -> @name Clock settings

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't understand

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all the comments in the perihp_conf.h have the equivalent spaces to a tab character after the @brief/@name -> leading to 3 spaces after a @brief and 4 spaces after a @name. So this should be also outputted by the clk_conf tool so that the comment style is consistent in the periph_conf.h files.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I didn't know that. Will fix.

printf("/**\n"
" * @name Clock settings\n"
" *\n"
" * @note This is auto-generated from\n"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

"#define CLOCK_HSE (%uU)\n", pll_src ? pll_in : 0);
printf("/* 0: no external low speed crystal available,\n"
" * 1: external crystal available (always 32.768kHz) */\n"
"#define CLOCK_LSE (%d)\n", is_lse);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about (%dU) to make it consistent with CLOCK_HSE?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not

@haukepetersen
Copy link
Copy Markdown
Contributor

one more thing I just noticed: could you add .gitignore file to /cpu/stm32_common/dist, stating

clk_conf
spi_divtable

Thanks!


/* Print constants */
puts("==============================================================");
puts("Please copy the following code into your board's periph_conf.h");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make it somehow possible to do this automatically. Either by using stderr for all other output, or by (maybe optionally) writing the actual code into a file.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(we can do that as followup. @haukepetersen feel free to dismiss my review if you're happy.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used stderr for evrything but code. Is it OK?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! thanks!

puts("");

printf("/**\n"
" * @name Clock settings\n"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, still one space missing.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crap, I can't count up to 4! :D

Should be OK now

Copy link
Copy Markdown
Contributor

@haukepetersen haukepetersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good -> ACK from my side

@haukepetersen haukepetersen added the CI: needs squashing Commits in this PR need to be squashed; If set, CI systems will mark this PR as unmergable label Aug 28, 2017
@vincent-d
Copy link
Copy Markdown
Member Author

Oops, maybe I squashed a bit quickly. @kaspar030 any objections?

@vincent-d vincent-d added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: needs squashing Commits in this PR need to be squashed; If set, CI systems will mark this PR as unmergable CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Aug 28, 2017
@aabadie
Copy link
Copy Markdown
Contributor

aabadie commented Aug 29, 2017

@kaspar030, your comment has been addressed. Are you fine with the change ?

@kaspar030
Copy link
Copy Markdown
Contributor

&go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Platform: ARM Platform: This PR/issue effects ARM-based platforms Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants