Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samd21: pull all generic clock generators into clock.c #7208

Closed
photonthunder opened this issue Jun 19, 2017 · 4 comments
Closed

samd21: pull all generic clock generators into clock.c #7208

photonthunder opened this issue Jun 19, 2017 · 4 comments
Assignees
Labels
Platform: ARM Platform: This PR/issue effects ARM-based platforms State: stale State: The issue / PR has no activity for >185 days

Comments

@photonthunder
Copy link

The samd21 has 9 generic clock generators that can be tied to different clock sources and then tweaked for the users needs. Currently these are setup in clock.c, timer.c, rtt.c, and rtc.c. Wanted to open a discussion about cleaning this up. I recommend we pull all generic clock generator code back into clock.c and setup a specific clock generator for a specific clock. For example:

#if GEN1_1MHZ
    /* configure internal 8MHz oscillator to run without prescaler */
    SYSCTRL->OSC8M.bit.PRESC = 0;
    SYSCTRL->OSC8M.bit.ONDEMAND = 1;
    SYSCTRL->OSC8M.bit.RUNSTDBY = 0;
    SYSCTRL->OSC8M.bit.ENABLE = 1;
    while (!(SYSCTRL->PCLKSR.reg & SYSCTRL_PCLKSR_OSC8MRDY)) {}

    /* setup generic clock 1 as 1MHz */
    GCLK->GENDIV.reg =  (GCLK_GENDIV_DIV(8) | GCLK_GENDIV_ID(1));
    GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN |
                         GCLK_GENCTRL_SRC_OSC8M |
                         GCLK_GENCTRL_ID(1));
    GCLK->CLKCTRL.reg = (GCLK_CLKCTRL_GEN(1) |
                         GCLK_CLKCTRL_ID(1) |
                         GCLK_CLKCTRL_CLKEN);
    while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {}
#endif
    
#if GEN2_XOSC32
    /* configure 32kHz external oscillator */
    SYSCTRL->XOSC32K.reg = (SYSCTRL_XOSC32K_ONDEMAND |
                            SYSCTRL_XOSC32K_EN32K |
                            SYSCTRL_XOSC32K_XTALEN |
#if XOSC32_RUNSTDBY
                            SYSCTRL_XOSC32K_RUNSTDBY |
#endif
                            SYSCTRL_XOSC32K_STARTUP(6));
    
    /* Enable with Seperate Call */
    SYSCTRL->XOSC32K.bit.ENABLE = 1;
    
    /* setup generic clock 2 as 32 kHz */
    GCLK->GENDIV.reg =  (GCLK_GENDIV_ID(2) | GCLK_GENDIV_DIV(1));
    GCLK->GENCTRL.reg =  (GCLK_GENCTRL_GENEN |
                         GCLK_GENCTRL_ID(2) |
#if XOSC32_RUNSTDBY
                         GCLK_GENCTRL_RUNSTDBY |
#endif
                         GCLK_GENCTRL_SRC_XOSC32K);
    while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {}
#endif
    
#if GEN3_ULP32K
    /* Setup Clock generator 3 with divider 1 (32.768kHz) */
    GCLK->GENDIV.reg  = (GCLK_GENDIV_ID(3)  | GCLK_GENDIV_DIV(0));
    GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN |
                         GCLK_GENCTRL_ID(3) |
                         GCLK_GENCTRL_RUNSTDBY |
                         GCLK_GENCTRL_SRC_OSCULP32K);
    while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {}
#endif

Then in timer.c/rtt.c/rtc.c and so forth you just check that the clock is on then use it:

#if GEN2_XOSC32 == 0
#error Must turn on GEN2_XOSC32 to use RTT
#endif
    
    /* RTC uses GEN2_XOSC32 because OSC32K isn't accurate
     * enough (p1075/1138). Also keep running in standby. */
    GCLK->CLKCTRL.reg = GCLK_CLKCTRL_GEN_GCLK2 |
                        GCLK_CLKCTRL_CLKEN |
                        GCLK_CLKCTRL_ID(RTC_GCLK_ID);
    while (GCLK->STATUS.bit.SYNCBUSY) {}

See PR #7130 for motivation for this issue.

Thoughts, input?

@jnohlgard
Copy link
Member

Unassigning because I have just about zero experience with the Atmel SAM families.

@jnohlgard jnohlgard removed their assignment Jun 19, 2017
@aabadie aabadie added the Platform: ARM Platform: This PR/issue effects ARM-based platforms label Jun 22, 2017
@aabadie aabadie added this to the Release 2017.07 milestone Jun 22, 2017
@dylad
Copy link
Member

dylad commented Jun 26, 2017

This discussion can be expanded to SAML21 as its also have 9 generic clock module.
Personally, I think moving all those init into clock.c is a good idea.
And user should only modify the periph_conf.h file to obtain the desired behaviour regarding clock management without overwrite clock.c

@photonthunder
Copy link
Author

Submitted PR #7315 to try and move this forward. Once it is accepted then we can expand it to SAML21.

@stale
Copy link

stale bot commented Aug 10, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions.

@stale stale bot added the State: stale State: The issue / PR has no activity for >185 days label Aug 10, 2019
@stale stale bot closed this as completed Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: ARM Platform: This PR/issue effects ARM-based platforms State: stale State: The issue / PR has no activity for >185 days
Projects
None yet
Development

No branches or pull requests

5 participants