LJP-TW
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: ARM Cortex-M4 description: tags: ARM, 底層 lang: zh_tw --- # ARM Cortex-M4 [TOC] ## 前言 本篇為學習 ARM Cortex-M4 的筆記 在 reverse arm 架構的程式的過程中 若我遇到不懂的,就會去查並記錄在此 ## On reset 在重置時, Cortex M3、M4 會 - 從位址 0x00000000 取出 MSP (Main Stack Point) 的初始值 - 從位址 0x00000004 取出 PC (Program Counter) 的初始值 - LSB 會 load 進 EPSR T-bit, reset 時需為 1 * Ref: * 註 [1] 章節 2.1.3, 第 2-4 頁 * 註 [2] 第 1 頁 ## Processor mode and privilege levels for software execution 在[註1](https://static.docs.arm.com/dui0553/a/DUI0553A_cortex_m4_dgug.pdf)的章節 2.1.1 - **Processor modes** - **Thread mode** 程式執行會在此 mode - **Handler mode** handle exceptions 時會在此 mode - **Privilege levels** - **Unprivileged** - 指令 **MSR**, **MRS** 用法有限制 - 指令 **CPS** 不能使用 - 不能存取 **system timer**, **NVIC**, **system control block** - 存取 Memory, 周邊設備時可能會被限制 - **Privileged** 所有指令, 設備皆能使用 在 Thread mode 時, Register **CONTROL** 決定了 Privilege level 在 Handler mode 時, Privilege level 都是 Privileged Privileged 程式能在 Thread mode 時修改 Register CONTROL Unprivileged 程式需要透過指令 **SVC** 才能修改 Register CONTROL ## Stack 每 push 一次, stack pointer 就**扣**一次 有兩種 stack, 各有不同的 register 記錄著, 分別為 - main stack - process stack 在 Thread mode 時, Register CONTROL 決定使用哪個 stack 在 Handler mode 時, 使用 main stack ## Core registers ![](https://i.imgur.com/lYkRQa1.png) | Register | APCS name | APCS role | |---|---|---| r0 | a1 | argument 1/scratch register/result r1 | a2 | argument 2/scratch register/result r2 | a3 | argument 3/scratch register/result r3 | a4 | argument 4/scratch register/result r4 | v1 | register variable r5 | v2 | register variable r6 | v3 | register variable r7 | v4 | register variable r8 | v5 | register variable r9 | sb/v6 | static base/register variable r10 | sl/v7 | stack limit/stack chunk handle/register variable r11 | fp/v8 | frame pointer/register variable r12 | ip | scratch register/new -sb in inter-link-unit calls r13 | sp | lower end of the current stack frame r14 | lr | link register/scratch register r15 | pc | program counter - **R0 ~ R12** 一般暫存器 - **Stack Pointer** 在 Thread mode, CONTROL bit[1] - 為 0: SP 為 Main Stack Pointer (MSP) - 為 1: SP 為 Process Stack Pointer (PSP) **reset 時, 處理器會 load address 0x00000000 的值進 MSP** - **Link Register** call function 時存放 return address **reset 時, 處理器會將 0xFFFFFFFF 放進 MSP** - **Program Counter** 記錄下一次 CPU 要執行哪邊的指令 **reset 時, 處理器會 load address 0x00000004 的值進 MSP** bit[0] 會被 load 到 EPSR T-bit, **一定要是 1** - **Program Status Register** PSR 為 32 bits 的 Register 其中的某些 bits 又被分成是 - Application PSR (APSR) - Interrupt PSR (IPSR) - Execution PSR (EPSR) ![](https://i.imgur.com/abhPfBw.png) 若要存取以上暫存器, 需用指令 **MSR**, **MRS**, 並將暫存器名稱作為參數 - e.g. - MRS, PSR // 讀取 PSR - MSR, APSR_nzcvq // 寫入 APSR N, Z, C, V, Q 若要一次存取 APSR, IPSR, EPSR 中任兩個 Register 有其他的代名詞 ![](https://i.imgur.com/HHevh1O.png) - **APSR** 儲存 condition flags ![](https://i.imgur.com/qV0b90E.png) - **IPSR** > The IPSR contains the exception type number of the current Interrupt Service Routine (ISR) > ![](https://i.imgur.com/6ATeb2J.png) - **EPSR** Cortex-M4 執行指令時, T-bit 需要為 1, 否則導致 fault 或 lockup - **Exception mask registers** TBD - **CONTROL regiser** TBD ## Parameter Passing ![](https://i.imgur.com/PiaaACp.png) ## Reference 1. [Cortex-M4 規格書](https://static.docs.arm.com/dui0553/a/DUI0553A_cortex_m4_dgug.pdf) 2. [stm32-prog.pdf](http://wiki.csie.ncku.edu.tw/embedded/Lab19/stm32-prog.pdf)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully