Skip to content

Update Flash Examples, Add API for Critical Sections #357

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

Merged
merged 45 commits into from
Feb 1, 2023

Conversation

Jake-Carter
Copy link
Contributor

@Jake-Carter Jake-Carter commented Jan 24, 2023

This PR resolves #287 by fixing and simplifying all micro's Flash examples. It also adds a purely inline API for entering and exiting a critical section of code to all micro's mxc_sys.h.

Flash examples key changes:

  • Simplify overall program flow and functions
  • Execute code from Flash instead of RAM like a standard example.
  • Execute flash writes from a critical section of code to enable the point above
  • Remove misleading flash clock divider

Critical Section API key changes:

  • Add MXC_SYS_Crit_Enter/Exit and MXC_SYS_In_Crit_Section functions to mxc_sys.h (supports ARM and RISC-V)
  • Add MXC_CRITICAL(...) macro to mxc_sys.h
  • Modify Libraries/CMSIS/5.9.0/Core/Include/core_rv32.h to undefine __CORTEX_M when building for RISC-V

Critical Section Example Usage:

MXC_CRITICAL(myfunction())

or...

MXC_CRITICAL(
    int myvar = 0;
    myvar = myfunction();
    // more critical code...
    )

This macro is equivalent to:

    MXC_SYS_Crit_Enter();
    int myvar = 0;
    myvar = myfunction();
    // more critical code...
    MXC_SYS_Crit_Exit();

How to Review:
Check out the fix/flash-examples branch locally and open the Flash example in your preferred IDE. Review the readme and run through the example on your preferred micro.

Optionally review the critical API implementation in mxc_sys.h for your preferred micro.

@Jake-Carter
Copy link
Contributor Author

Note to self: reverted some commits that accidentally made it from fix/csi2

static inline void MXC_SYS_Crit_Enter(void)
{
_mxc_crit_get_state();
if (_state.ie_status)
Copy link
Contributor

Choose a reason for hiding this comment

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

Add curly brackets to these if statements.

* @brief Exit a critical section of code, re-enabling interrupts if they
* were previously.
*/
static inline void MXC_SYS_Crit_Exit(void)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why these are inline functions? Not saying it's wrong or needs to be changed, just curious.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Inline is used to give the lowest possible timing overhead. Since this API is essentially just a wrapper around an assembly function, it doesn't make sense to add the 2-4uS overhead associated with a normal function call. Especially because the code needs to enter the critical section as soon as possible to avoid getting interrupted on its way there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's "static inline" as well because that is a forced requirement for accessing other static member variables and using static function calls.

#define TESTSIZE 8192 //2 pages worth so we can do erase functions

#define MXC_FLASH_MEM_SIZE_TEST MXC_FLASH_MEM_SIZE
#define TEST_ADDRESS 0x0003E000
Copy link
Contributor

Choose a reason for hiding this comment

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

Define TEST_ADDRESS with MXC_FLASH_ defines. (MXC_FLASH_MEM_SIZE - <page_num>*MXC_FLASH_PAGE_SIZE)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I like that. Done in 9be3fd4

}
printf("Done!\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the write be validated here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it makes sense to validate again here. Added in 6c58802

printf("----------------\n");
)
// clang-format on
if (err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Add curly brackets. Same for lines 283, 293, and 296.

@sihyung-maxim
Copy link
Contributor

The max32672.h changes were merged into main. And hopefully with the inclusion of Libraries/CMSIS/5.9.0/Core/Include/core_cm4.h, the build examples action will pass. I'll review when it's ready!

@Jake-Carter
Copy link
Contributor Author

@sihyung-maxim build issues are fixed, ready for your review

Copy link
Contributor

@sihyung-maxim sihyung-maxim left a comment

Choose a reason for hiding this comment

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

Nice. I like these new Flash examples.

Only thing I noticed was a small typo present in all the READMEs and main.c printfs.

Press Push Button 1 (PB1/SW1) to continue...

---(Critical)---
Sucessfully erased page 64 of flash (addr 0x1007e000)
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo for Successfully.

if (retval != E_NO_ERROR) {
return retval;
}
printf("Sucessfully verified test pattern!\n\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo for successfully.

}

retval = MXC_FLC_Write(end, end_len, buffer);
printf("Sucessfully verified test pattern!\n\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo for successfully

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flash / Flash_CLI Examples Have Misleading CLKDIV
3 participants