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

feat(Examples): Add RTOS Debug Features to MAX78000 FreeRTOSDemo #962

Merged
merged 13 commits into from
Mar 28, 2024

Conversation

Brandon-Hurst
Copy link
Contributor

Description

Added Debug features to the FreeRTOSDemo for MAX78000. These features are added in such a way as to be easily copyable to most other MCUs in the MSDK.

Summary:

  • A default configuration of TMR0 is provided in FreeRTOS_Debug.c and conditionally used based on the DEBUG flag. Numerous FreeRTOS configs related to debugging are also connected to this flag.
  • README.md details the inclusion of the TMR0 config based on DEBUG==1
  • A HardFaultHandler is defined with a helper function to grab the CPU registers from the failing code context in the event of a HardFault. This is recommended by the FreeRTOS documentation to assist with debugging Hard Faults.

Ideally this would be available at some point for all FreeRTOSDemo Examples or placed inside the MSDK FreeRTOS library. Exposing it inside an example may be preferable as an instructive inclusion for developers. This PR is meant to initiate a dialogue and add some more RTOS Debug features to MSDK. I am also happy to modify anything needed for this PR.

Regarding the HardFault Handler...

Credit to @BrentK-ADI for this portion of the example.
The code included is recommended in the FreeRTOS Documentation to improve debugging Hard Faults by grabbing the stack context from which the code failed. Debugging Hard Faults can be notoriously tricky without this information.
Link: https://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html

  • Within the handler itself, the first instruction checks bit 2 of the LR.
    • Per documentation "Upon exception entry, the active stack pointer is encoded in bit 2 of the EXC_RETURN value pushed to the link register. If the bit is set, the psp was active prior to exception entry, else the msp was active". This check determines which stack pointer the function causing the fault was using.
  • The next instruction, ite, is an assembly If/Else code, where the following mrs instructions are the if and else respectively, getting the appropriate stack address into R0. We then place getRegistersFromStack into R2. getRegistersFromStack is then called with bx.
  • The getRegistersFromStack function pulls all the relevant registers from the culprits stack. This includes the program counter (instruction) that caused the fault, as well as the LR (link register) determining which function called that block of code. R0-3 can also give context into the state of the calling function.

Testing

RTOS Stats Test

The build was completed and ran successfully on MAX78000FTHR, and RTOS Stats were gathered via the "RTOS Views" extension in VSCode, image below.
RTOS Stats

Worth noting is that FreeRTOS's kernel does not smartly handle Timer Overflow on the RTOS Stats Timer, so overflowing the provided counter will give inaccurate results. This is documented within FreeRTOS_Debug.c provided. The default 32-bit, 32 kHz configuration provides nearly 100 days before overflow.

HardFault Handler Test

Hard Fault Analysis was tested via a privileged memory read of address 0xFFFFFFFF inserted into the example.
image
image

The values shown are from the context which caused the HardFault, not from within the HardFault itself. This streamlines the process of finding the code which caused the fault (in this case located at address 0x1000_083e)

@github-actions github-actions bot added the MAX78000 Related to the MAX78000 (AI85) label Mar 21, 2024
Copy link
Contributor

@Jake-Carter Jake-Carter left a comment

Choose a reason for hiding this comment

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

Thanks @Brandon-Hurst @BrentK-ADI, looks like a great start. A few comments below.

Is there also some mechanism for determining which task/context is active from inside the hard fault handler? This would also be useful to narrow down which task is at fault

Examples/MAX78000/FreeRTOSDemo/FreeRTOS_Debug.c Outdated Show resolved Hide resolved
@Jake-Carter Jake-Carter merged commit a578236 into analogdevicesinc:main Mar 28, 2024
7 checks passed
EricB-ADI pushed a commit that referenced this pull request Aug 21, 2024
Revert "feat(Examples): Add RTOS Debug Features to MAX78000 FreeRTOSDemo (#962)"

This reverts commit a578236.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
MAX78000 Related to the MAX78000 (AI85)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants