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

sam0 flashpage: wait for READY bit in INTFLAG after write command #10880

Merged
merged 3 commits into from Feb 1, 2019

Conversation

fedepell
Copy link
Contributor

Contribution description

While debugging on an issue with RWWEE (see devel mailing list) I saw in the SAML manual:

Procedure for Manual Page Writes (CTRLB.MANW=1)
The row to be written to must be erased before the write command is given.
• Write to the page buffer by addressing the NVM main address space directly
• Write the page buffer to memory: CTRL.CMD='Write Page' and CMDEX
• The READY bit in the INTFLAG register will be low while programming is in progress, and access
through the AHB will be stalled

And noticed that the last point, waiting for the READY bit in INTFLAG, was actually missing in current code (while it is done correctly for example for the erase cycle).
While I didn't notice myself any problem until now with the code without this READY bit waiting, I believe it may be more correct to add this code and prevent possible race conditions and strange errors.

Testing procedure

On SAMx boards you can run the automated test in tests/periph_flashpage.

@fedepell
Copy link
Contributor Author

From datasheet also:

Any commands written while INTFLAG.READY is low will be ignored.

@aabadie aabadie requested a review from dylad January 27, 2019 09:06
@aabadie aabadie added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation Platform: ARM Platform: This PR/issue effects ARM-based platforms Area: drivers Area: Device drivers Area: cpu Area: CPU/MCU ports labels Jan 27, 2019
@fedepell
Copy link
Contributor Author

As an additional information: I've put also a counter inside that while to see if it gets indeed triggered and for how much time. On my saml21-xpro for the main flash it usually passes inside just once, for the RWWEE about 300 times. So while for main flash it doesn't seem to be somehow vital, for RWWEE it seems to be. Anyhow given it's in the specs it may be still wise to always use it probably.

@dylad
Copy link
Member

dylad commented Jan 28, 2019

On my saml21-xpro for the main flash it usually passes inside just once, for the RWWEE about 300 times. So while for main flash it doesn't seem to be somehow vital, for RWWEE it seems to be

This is a bit weird, isn't it ? I mean, according to the datasheet this is supposed to be the same flash but smaller.
I'm wondering if it's not related to the wait states. Changing CPU frequency or Power-level have an impact to the flash.

I'll try to test your PR on SAML21 and SAML10/SAML11 soon. I'll also check with different wait states to see if there is a difference but I'll be quite busy this week.

Anyway, I agreed with you. This check must be there even "if it works" now.

@fedepell
Copy link
Contributor Author

I was quite surprised by the difference (almost instant vs 300) I must admit!

While I don't know how it is internally managed, I could try to explain to myself the difference by the fact that the RWWEE must have a quite more complex logic (to handle this "read while write" thing, so must somehow be allowing the main flash to be reading at the same time if needed) or also by the fact that the main flash has a cache while this one not.
Of course they are just conjectures to try to explain the measurement, will try to dig into the datasheet more (even if from what I looked so far there were no details on these things)

@keestux
Copy link
Contributor

keestux commented Jan 29, 2019

Something else I noticed in the SAMD manual.

To issue a command, the CTRLA.CMD bits must be written along with the CTRLA.CMDEX value. When a command is issued, INTFLAG.READY will be cleared until the command has completed. Any commands written while INTFLAG.READY is low will be ignored.

That raises the question whether we should worry about the INTFLAG.READY bit for any CMDEX command, including PBC.

@keestux
Copy link
Contributor

keestux commented Jan 29, 2019

Have a look at the nvm driver in ASF, sam0/drivers/nvm/nvm.c. They do a wait for NVM ready after every CMDEX command.

BTW. I suggest we create a static inline function that waits for the ready bit. That way there is only one place where we need the #ifdef and the while

@keestux
Copy link
Contributor

keestux commented Jan 29, 2019

For example

static inline bool nvm_is_ready() __attribute__((always_inline));
static inline bool nvm_is_ready()
{
#ifdef CPU_SAML1X
    return _NVMCTRL->STATUS.bit.READY;
#else
    return _NVMCTRL->INTFLAG.bit.READY;
#endif
}

And to use it:

    _NVMCTRL->CTRLA.reg = (NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_WP);
    while (!nvm_is_ready()) {}

@fedepell
Copy link
Contributor Author

Thanks for the hint, that looks good! I would even put the while then in the inline so it becomes even cleaner maybe, try to check my lastest commit.

I also added the wait after PBC as you suggest as the ASF does.

@keestux keestux self-requested a review January 31, 2019 19:20
Copy link
Member

@dylad dylad left a comment

Choose a reason for hiding this comment

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

Tested lastest changes on SAML10-XPRO.
You have my ACK too.

@dylad dylad added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Feb 1, 2019
@dylad dylad merged commit 3995fd8 into RIOT-OS:master Feb 1, 2019
@danpetry danpetry added this to the Release 2019.04 milestone Mar 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: cpu Area: CPU/MCU ports Area: drivers Area: Device drivers 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: 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.

None yet

5 participants