Skip to content

feat/cm_backtrace: integrate cm_backtrace for MCU fault.#292

Merged
sunzhen222 merged 14 commits intomainfrom
feat/cm_backtrace
Apr 16, 2025
Merged

feat/cm_backtrace: integrate cm_backtrace for MCU fault.#292
sunzhen222 merged 14 commits intomainfrom
feat/cm_backtrace

Conversation

@sunzhen222
Copy link
Copy Markdown
Contributor

@sunzhen222 sunzhen222 commented Apr 15, 2025

Summary by CodeRabbit

  • New Features
    • Integrated advanced backtrace and fault diagnosis for ARM Cortex-M microcontrollers, including detailed runtime error reporting and call stack analysis.
    • Added multilingual support for error and debug messages (English and Simplified Chinese, including UTF-8).
    • Introduced persistent error logging to eMMC storage with automatic log file management.
  • Improvements
    • Enhanced firmware initialization to include backtrace support.
    • Updated fault handlers for improved error shutdown and simplified maintenance.
    • Added a brief delay after display clear during error shutdown for better user experience.
  • Documentation
    • Added comprehensive README and language documentation for the new backtrace feature.
  • Chores
    • Updated build scripts and linker files to support new backtrace integration.

@sunzhen222 sunzhen222 requested a review from a team as a code owner April 15, 2025 07:29
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 15, 2025

Walkthrough

This update integrates the CmBacktrace library for Cortex-M fault diagnosis and backtrace support. It adds new source files, headers, and configuration for the library, including language support in English and Chinese. The build system is updated to include these sources. Fault handler logic is refactored: detailed handlers are replaced with simple display functions, and new assembly handlers are introduced for GCC. The linker script now marks code boundaries. The error shutdown flow now includes a short delay after clearing the display.

Changes

Files/Paths Change Summary
core/SConscript.firmware Adds CmBacktrace sources and include paths to firmware build for model 'H'.
core/embed/cm_backtrace/cm_backtrace.c
core/embed/cm_backtrace/cm_backtrace.h
Adds CmBacktrace implementation and public API header.
core/embed/cm_backtrace/cmb_cfg.h
core/embed/cm_backtrace/cmb_def.h
Adds configuration and definitions headers for CmBacktrace.
core/embed/cm_backtrace/cmb_user_cfg.c
core/embed/cm_backtrace/cmb_user_cfg.h
Adds user config source and header for error file handling and custom print.
core/embed/cm_backtrace/Languages/README.md Adds README about CmBacktrace language support.
core/embed/cm_backtrace/Languages/en-US/cmb_en_US.h Adds English language strings for CmBacktrace.
core/embed/cm_backtrace/Languages/zh-CN/cmb_zh_CN.h
core/embed/cm_backtrace/Languages/zh-CN/cmb_zh_CN_UTF8.h
Adds Chinese (GB18030 and UTF-8) language strings for CmBacktrace.
core/embed/cm_backtrace/fault_handler/gcc/cmb_fault.S Adds GCC assembly fault handlers for Cortex-M exceptions.
core/embed/firmware/main.c Replaces detailed fault handlers with simple display functions; integrates CmBacktrace init.
core/embed/firmware/memory_H.ld Adds _stext and _etext symbols to mark code section boundaries.
core/embed/trezorhal/common.c Adds a 3 ms delay after clearing display in error shutdown.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 7bfe04a and edbdd91.

📒 Files selected for processing (1)
  • core/embed/trezorhal/common.c (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • core/embed/trezorhal/common.c
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Style check
  • GitHub Check: Defs check
  • GitHub Check: Gen check

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (6)
core/embed/firmware/delay.c (1)

120-131: Add comment explaining the delay approximation

The function calculates delay based on clock speed assumptions.

void software_delay_ms(uint32_t delay)
{
+    // This is an approximate delay based on CPU cycles
    uint32_t sysClockHz = HAL_RCC_GetSysClockFreq();
    // 4 cycles per loop, 1000 loops per ms
    uint32_t loops_per_ms = sysClockHz / (4 * 1000);
core/embed/cm_backtrace/Languages/README.md (1)

1-2: Title language inconsistency

Title in Chinese, content in English.

-# CmBacktrace: ARM Cortex-M 系列 MCU 错误追踪库
+# CmBacktrace: ARM Cortex-M Series MCU Error Tracking Library
core/embed/trezorhal/common.c (1)

180-180: Purpose of delay unclear

Add comment explaining why 3ms delay before display.

  display_clear();
+  // Short delay to ensure display is fully cleared before drawing
  software_delay_ms(3);
  display_image(9, 50, 46, 40, toi_icon_warning + 12,
core/embed/cm_backtrace/cm_backtrace.c (2)

137-137: Typo in variable name.


600-601: Typo in function name.

core/embed/cm_backtrace/cmb_def.h (1)

271-271: Spelling mistake.

- // Indicates a stack overflow error has occured
+ // Indicates a stack overflow error has occurred
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 94f2b46 and 623fb2b.

📒 Files selected for processing (16)
  • core/SConscript.firmware (3 hunks)
  • core/embed/cm_backtrace/Languages/README.md (1 hunks)
  • core/embed/cm_backtrace/Languages/en-US/cmb_en_US.h (1 hunks)
  • core/embed/cm_backtrace/Languages/zh-CN/cmb_zh_CN.h (1 hunks)
  • core/embed/cm_backtrace/Languages/zh-CN/cmb_zh_CN_UTF8.h (1 hunks)
  • core/embed/cm_backtrace/cm_backtrace.c (1 hunks)
  • core/embed/cm_backtrace/cm_backtrace.h (1 hunks)
  • core/embed/cm_backtrace/cmb_cfg.h (1 hunks)
  • core/embed/cm_backtrace/cmb_def.h (1 hunks)
  • core/embed/cm_backtrace/cmb_user_cfg.c (1 hunks)
  • core/embed/cm_backtrace/cmb_user_cfg.h (1 hunks)
  • core/embed/cm_backtrace/fault_handler/gcc/cmb_fault.S (1 hunks)
  • core/embed/firmware/delay.c (1 hunks)
  • core/embed/firmware/main.c (3 hunks)
  • core/embed/firmware/memory_H.ld (2 hunks)
  • core/embed/trezorhal/common.c (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
core/embed/trezorhal/common.c (1)
core/embed/firmware/delay.c (1)
  • software_delay_ms (120-131)
core/embed/cm_backtrace/cmb_user_cfg.c (1)
core/embed/cm_backtrace/cmb_user_cfg.h (2)
  • delete_err_info_file (7-7)
  • cmb_user_println (8-8)
core/embed/firmware/main.c (5)
core/embed/cm_backtrace/cm_backtrace.h (1)
  • cm_backtrace_init (38-38)
core/embed/cm_backtrace/cm_backtrace.c (1)
  • cm_backtrace_init (145-175)
core/embed/trezorhal/hardware_version.c (2)
  • hw_ver_to_str (13-41)
  • get_hw_ver (65-78)
core/embed/trezorhal/hardware_version.h (2)
  • hw_ver_to_str (30-30)
  • get_hw_ver (33-33)
core/embed/trezorhal/common.h (1)
  • error_shutdown (85-86)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Style check
🔇 Additional comments (15)
core/embed/firmware/memory_H.ld (1)

62-62: Added symbols for CmBacktrace library

These symbols define the boundaries of executable code in flash memory, essential for accurate backtrace and fault analysis by CmBacktrace.

Also applies to: 75-75

core/embed/cm_backtrace/cmb_user_cfg.h (1)

1-10: Function declarations look good.

The header file correctly defines interfaces for error file management and formatted printing, with proper include guards.

core/SConscript.firmware (3)

457-461: CM_BACKTRACE source files integration looks good.

The source files for the CmBacktrace library are properly added to the build system.


706-706: Include path correctly added.

The CmBacktrace include path is properly integrated into the build configuration.


1094-1095: CM_BACKTRACE object files properly included.

The CmBacktrace object files are correctly added to the program object list.

core/embed/cm_backtrace/cmb_cfg.h (4)

1-27: License header is well-formatted.

The MIT license header is properly formatted and includes all necessary information.


29-37: Configuration looks appropriate.

The configuration correctly sets up user config inclusion and defines the print function macro.


38-48: Platform configuration looks good.

The bare metal platform is enabled and CPU is correctly configured for Cortex-M7.


49-63: Fallback configuration is well-defined.

The fallback configuration provides reasonable defaults if user config is not enabled.

core/embed/cm_backtrace/Languages/en-US/cmb_en_US.h (3)

1-29: License header is appropriate.

The MIT license header is correctly formatted with proper attribution and usage notes.


31-41: Error and information messages are clear.

The general error and information messages are well-written and provide useful diagnostic information.


42-72: Fault descriptors are comprehensive.

The fault descriptors provide detailed explanations for various fault types and causes, helping with debugging.

core/embed/cm_backtrace/Languages/zh-CN/cmb_zh_CN.h (1)

1-72: LGTM! Chinese (GB18030) language strings look correct.

The strings provide good error descriptions for fault conditions. The encoding is properly documented.

core/embed/cm_backtrace/Languages/zh-CN/cmb_zh_CN_UTF8.h (1)

1-72: LGTM! UTF-8 encoded Chinese strings match GB18030 version.

Good to have UTF-8 encoding option alongside GB18030 for better compatibility.

core/embed/cm_backtrace/cm_backtrace.h (1)

1-49: LGTM! Well-structured header with clear API.

The public interface provides necessary functions for backtrace and fault handling.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (8)
core/embed/cm_backtrace/cmb_def.h (2)

138-138: Typo. "statck" → "stack"


296-296: Spelling. "occured" → "occurred"

core/embed/cm_backtrace/Languages/en-US/cmb_en_US.h (2)

86-86: Grammar error. Redundant phrase "is caused by indicates"


93-93: Grammar error. Redundant phrase "is caused by Indicates"

core/embed/cm_backtrace/cm_backtrace.c (3)

620-620: Typo. "statck_del_fpu_regs"


621-621: Typo. "statck_has_fpu_regs"


623-623: Typo. "statck_has_fpu_regs"

core/embed/firmware/main.c (1)

240-259: New simplified fault handlers.

Each handler just shows error code. No context about why they're called from assembly.

Add brief comment stating these are called from assembly handlers after context capture.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 623fb2b and 1ecd1bc.

📒 Files selected for processing (10)
  • core/embed/cm_backtrace/Languages/en-US/cmb_en_US.h (1 hunks)
  • core/embed/cm_backtrace/Languages/zh-CN/cmb_zh_CN.h (1 hunks)
  • core/embed/cm_backtrace/Languages/zh-CN/cmb_zh_CN_UTF8.h (1 hunks)
  • core/embed/cm_backtrace/cm_backtrace.c (1 hunks)
  • core/embed/cm_backtrace/cm_backtrace.h (1 hunks)
  • core/embed/cm_backtrace/cmb_cfg.h (1 hunks)
  • core/embed/cm_backtrace/cmb_def.h (1 hunks)
  • core/embed/cm_backtrace/cmb_user_cfg.c (1 hunks)
  • core/embed/cm_backtrace/cmb_user_cfg.h (1 hunks)
  • core/embed/firmware/main.c (3 hunks)
✅ Files skipped from review due to trivial changes (1)
  • core/embed/cm_backtrace/cmb_user_cfg.c
🚧 Files skipped from review as they are similar to previous changes (5)
  • core/embed/cm_backtrace/Languages/zh-CN/cmb_zh_CN_UTF8.h
  • core/embed/cm_backtrace/cmb_cfg.h
  • core/embed/cm_backtrace/cmb_user_cfg.h
  • core/embed/cm_backtrace/Languages/zh-CN/cmb_zh_CN.h
  • core/embed/cm_backtrace/cm_backtrace.h
🧰 Additional context used
🧬 Code Graph Analysis (1)
core/embed/cm_backtrace/cm_backtrace.c (4)
core/embed/cm_backtrace/cm_backtrace.h (5)
  • cm_backtrace_init (38-39)
  • cm_backtrace_firmware_info (40-40)
  • cm_backtrace_call_stack (41-41)
  • cm_backtrace_assert (42-42)
  • cm_backtrace_fault (43-43)
core/embed/cm_backtrace/cmb_def.h (18)
  • cmb_get_sp (400-400)
  • cmb_get_sp (400-400)
  • cmb_get_sp (412-412)
  • cmb_get_sp (429-432)
  • cmb_get_sp (429-429)
  • cmb_get_sp (445-445)
  • cmb_get_psp (399-399)
  • cmb_get_psp (399-399)
  • cmb_get_psp (407-407)
  • cmb_get_psp (425-428)
  • cmb_get_psp (425-425)
  • cmb_get_psp (440-440)
  • cmb_get_msp (398-398)
  • cmb_get_msp (398-398)
  • cmb_get_msp (402-402)
  • cmb_get_msp (421-424)
  • cmb_get_msp (421-421)
  • cmb_get_msp (435-435)
core/embed/cm_backtrace/cmb_user_cfg.h (1)
  • delete_err_info_file (7-7)
core/embed/cm_backtrace/cmb_user_cfg.c (1)
  • delete_err_info_file (9-9)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Style check
  • GitHub Check: Defs check
  • GitHub Check: Gen check
🔇 Additional comments (2)
core/embed/firmware/main.c (2)

76-77: Added includes for cm_backtrace.


142-142: Initialized cm_backtrace after memory setup.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

♻️ Duplicate comments (1)
core/embed/cm_backtrace/cmb_user_cfg.c (1)

32-33: ⚠️ Potential issue

Write result not verified.

No error checking after file write.

🧹 Nitpick comments (1)
core/embed/cm_backtrace/cmb_user_cfg.c (1)

13-14: Buffer size hard-coded.

Fixed 512-byte buffer.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 1ecd1bc and 228541c.

📒 Files selected for processing (1)
  • core/embed/cm_backtrace/cmb_user_cfg.c (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
core/embed/cm_backtrace/cmb_user_cfg.c (1)
core/embed/cm_backtrace/cmb_user_cfg.h (2)
  • delete_err_info_file (7-7)
  • cmb_user_println (8-8)
🪛 Cppcheck (2.10-2)
core/embed/cm_backtrace/cmb_user_cfg.c

[error] 30-30: va_list 'args' was opened but not closed by va_end().

(va_end_missing)

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Style check
  • GitHub Check: Gen check
  • GitHub Check: Defs check

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between c28bc21 and 0af2481.

📒 Files selected for processing (1)
  • core/embed/cm_backtrace/cm_backtrace.c (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
core/embed/cm_backtrace/cm_backtrace.c (4)
core/embed/cm_backtrace/cm_backtrace.h (5)
  • cm_backtrace_init (38-39)
  • cm_backtrace_firmware_info (40-40)
  • cm_backtrace_call_stack (41-41)
  • cm_backtrace_assert (42-42)
  • cm_backtrace_fault (43-43)
core/embed/cm_backtrace/cmb_def.h (18)
  • cmb_get_sp (400-400)
  • cmb_get_sp (400-400)
  • cmb_get_sp (412-412)
  • cmb_get_sp (429-432)
  • cmb_get_sp (429-429)
  • cmb_get_sp (445-445)
  • cmb_get_psp (399-399)
  • cmb_get_psp (399-399)
  • cmb_get_psp (407-407)
  • cmb_get_psp (425-428)
  • cmb_get_psp (425-425)
  • cmb_get_psp (440-440)
  • cmb_get_msp (398-398)
  • cmb_get_msp (398-398)
  • cmb_get_msp (402-402)
  • cmb_get_msp (421-424)
  • cmb_get_msp (421-421)
  • cmb_get_msp (435-435)
core/embed/cm_backtrace/cmb_user_cfg.c (1)
  • delete_err_info_file (9-9)
core/embed/cm_backtrace/cmb_user_cfg.h (1)
  • delete_err_info_file (7-7)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Defs check
  • GitHub Check: Style check
  • GitHub Check: Gen check

@sunzhen222 sunzhen222 merged commit 3f76732 into main Apr 16, 2025
8 checks passed
@sunzhen222 sunzhen222 deleted the feat/cm_backtrace branch April 16, 2025 03:16
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.

3 participants