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

cpu/samd21: fix NVM wait states #6875

Merged
merged 1 commit into from
Apr 19, 2017
Merged

Conversation

dylad
Copy link
Member

@dylad dylad commented Apr 8, 2017

While looking in cpu/samd21 I found a small bug. See commit.
Moreover, I've noticed another bug but I don't know how to fix it. I opened an issue because it also related to Atmel CMSIS see #6874
Cheers !

@aabadie aabadie 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 Apr 10, 2017
@aabadie aabadie added this to the Release 2017.04 milestone Apr 10, 2017
Copy link
Contributor

@keestux keestux left a comment

Choose a reason for hiding this comment

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

Wow. Good catch! The change loos good to me, and I guess (hope) you've tested it.

But I'm a bit puzzled. The old code was starting/stopping the APBA(!) clock for the SYSCTRL, leaving the SYSCTRL stopped. So, how could it have worked???

@dylad
Copy link
Member Author

dylad commented Apr 11, 2017

Unfortunately I don't own SAMR21-Xpro board so I cannot test it. I was looking in this file for my SAMD20-Xpro port to RIOT.

Maybe no one ever try to get a CORECLOCK > 24 Mhz ? But I agree with you, this is weird.

@keestux
Copy link
Contributor

keestux commented Apr 11, 2017

My Autonomo board runs at 48MHz. I will try to test it on it.

@dylad
Copy link
Member Author

dylad commented Apr 11, 2017

Many thanks @keestux !

@keestux
Copy link
Contributor

keestux commented Apr 11, 2017

It's good that I tested it. The patch is not OK :-)

But the original code is wrong too. It just happened to be somewhat harmless. Let's look at the code once more.

PM->APBAMASK.reg |= PM_AHBMASK_NVMCTRL;
NVMCTRL->CTRLB.reg |= NVMCTRL_CTRLB_RWS(1);
PM->APBAMASK.reg &= ~PM_AHBMASK_NVMCTRL;

Given the code above there are three things mixed up. APBA mask, APBB mask and AHB mask. (My head starts to spin with all these PM registers.)

The define PM_AHBMASK_NVMCTRL is meant for AHB mask, not the APBB mask. (( The define is for bit 4, and so we did a start/stop of the WDT APB clock. ))

Now, there is also an enable for the NVMCTRL APB Clock (notice the APB). The question is, which one did we try to start/stop? And, is it really needed? ArduinoCore does do it. They simply do this at startup.

/* Set 1 Flash Wait State for 48MHz, cf tables 20.9 and 35.27 in SAMD21 Datasheet */
NVMCTRL->CTRLB.bit.RWS = NVMCTRL_CTRLB_RWS_HALF_Val ;

Digging a little deeper in ASF, there is a driver/nvm.c. In that code they do start/stop of the APB clock when they modify the RWS field of the NVMCTRL. From that I conclude that our code must be:

PM->APBBMASK.reg |= PM_APBBMASK_NVMCTRL;
NVMCTRL->CTRLB.reg |= NVMCTRL_CTRLB_RWS(1);
PM->APBBMASK.reg &= ~PM_APBBMASK_NVMCTRL;

I've tried it with hello world and can confirm that it still works.

@dylad
Copy link
Member Author

dylad commented Apr 12, 2017

Thank you for the time you spent on it @keestux.
I absolutely didn't notice there were two clocks for NVMCTRL.
I'll change the code.

@keestux
Copy link
Contributor

keestux commented Apr 12, 2017

Can you squash the two commits?

Signed-off-by: dylad <dylan.laduranty@mesotic.com>
@dylad dylad force-pushed the fix_samd21_cpu_bug branch from be3cb62 to 2b1a1e7 Compare April 12, 2017 18:24
@dylad
Copy link
Member Author

dylad commented Apr 12, 2017

Squashed.

@keestux
Copy link
Contributor

keestux commented Apr 12, 2017

ACK

@aabadie aabadie added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Apr 18, 2017
Copy link
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.

proxying @keestux's ACK

@aabadie
Copy link
Contributor

aabadie commented Apr 19, 2017

All green. Merging. Should this one be backported ?

@aabadie aabadie merged commit a884640 into RIOT-OS:master Apr 19, 2017
@dylad dylad deleted the fix_samd21_cpu_bug branch May 7, 2017 09:06
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)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants