5.5.1 · D4 · HinglishEmbedded Systems & Real-Time Software

ExercisesMicrocontroller architecture — ARM Cortex-M series (M0, M3, M4, M7)

4,103 words19 min read↑ Read in English

5.5.1 · D4 · Coding › Embedded Systems & Real-Time Software › Microcontroller architecture — ARM Cortex-M series (M0, M3,

Shuru karne se pehle, chalte hain un teen formulas ko re-earn karte hain jin par hum rely karenge, taaki koi bhi symbol bina explanation ke na aaye.

Neecha wali picture exactly dikhati hai kyun pipeline-fill formula hai aur nahi — ise Level 2 se pehle padho.

Figure — Microcontroller architecture — ARM Cortex-M series (M0, M3, M4, M7)

Level 1 — Recognition

Recall Solution 1.1

Seedha family table se:

  • M0+ stages, koi FPU nahi.
  • M3 stages, koi FPU nahi.
  • M4 stages, FPU optional (sirf "M4F" variant par), aur agar present hai toh yeh single-precision only hai.
  • M7 stages (dual-issue), FPU optional, aur ho sakta hai single + double precision.

"By default" kyun matter karta hai: M4F/M7 par bhi FPU reset par off hota hai — tumhe pehle CPACR mein CP10/CP11 bits set karne honge (Exercise 5.2 dekhо).

Recall Solution 1.2

NVIC (Nested Vectored Interrupt Controller). Interrupt par yeh automatically:

  1. 8 registers push karta hai (R0–R3, R12, LR, PC, xPSR) current stack par, aur
  2. Handler address look up karta hai vector table mein aur seedha wahan jump karta hai.

Dono ek fixed, known number of cycles mein hote hain — woh fixed-ness (zero nahi, lekin knowable) hi timing ko deterministic banata hai. Dekhо Interrupts and the NVIC.


Level 2 — Application

Recall Solution 2.1

Kya/kyun: pipeline-fill formula use karo kyunki hum cycles count kar rahe hain ek straight run of instructions ke liye. Pipe ko fill karne ke liye 1 extra cycle lagti hai (pehle ke baad sirf 1 stage), phir 50 instructions ek cycle mein stream out hoti hain.

Recall Solution 2.2

Cycles: cycles. Time: . Kyunki , woh hai . 168 se kyun divide karein: har cycle tak rehti hai, aur humare paas unki 12 hain.

Recall Solution 2.3

Key insight: iterations ke across pipe full rehti hai, toh tum fill cost ek baar pay karte ho, har loop par nahi. Nahi — woh galat hoga, pipe har pass re-fill hogi. Back-edge edge case, clearly stated: loop ki conditional branch pehle se un 10 instructions mein hai (woh mein counted hai), aur kyunki hum perfect branch prediction assume karte hain woh back-edge branch apna normal ek cycle cost karti hai bina flush ke. Agar, instead, back-edge har pass mein ek baar bhi mispredict kare, toh tum add karte flush cycles per pass, dete cycles. Toh loop overhead ya toh "free-ish" hai (predicted) ya ek per-pass tax (mispredicted) — kabhi silently ignore nahi hoti.

Recall Solution 2.4

Cycles cycles (round up — partial cycle nahi ho sakti). Kyun divide karein: IPC matlab instructions per cycle, toh cycles = instructions ÷ IPC.


Level 3 — Analysis

Figure — Microcontroller architecture — ARM Cortex-M series (M0, M3, M4, M7)
Recall Solution 3.1
  • (a) M3: cycles/sample.
  • (b) M4 MLA: cycles/sample.
  • (c) M4 SMLAD: do MACs per instruction cycles/sample.
  • Speedup M3 → SMLAD: — aur yeh per MHz hai, M4 ke higher clock se pehle jo aur help karta hai.

SIMD kyun half karta hai: ek 32-bit instruction do packed 16-bit lanes par ek saath operate karta hai (figure mein blue aur yellow lanes). Dekhо Fixed-point vs floating-point DSP.

Recall Solution 3.2
  • M3 soft-float: cycles.
  • M4F hardware: cycles.
  • Ratio: faster.
  • double trap: M4 FPU single-precision only hai, toh har double op silently software emulation par fall back karta hai — tum wapas near 200-cycle regime mein ho. float literals (3.14f) aur sinf use karo, sin nahi.
Recall Solution 3.3

Kyunki , woh hai . Har term kyun bounded hai: detect ek sync stage hai, stack ek fixed 12-cycle push hai, aur blocking term isliye bounded hai kyunki tum jaante ho longest higher-priority handler kya hai. Determinism = har term knowable, zero nahi.


Level 4 — Synthesis

Recall Solution 4.1

Cycles per sample: cycles. Deadline per sample: . Required clock: hume cycles ko mein fit karna hai, toh Toh ek modest ~2 MHz clock bhi technically isko meet karta hai — matlab FIR bottleneck nahi hai; interrupts aur dusre tasks dominate karte hain. (Real designs phir bhi ~100 MHz pick karte hain jitter aur dusre tasks ke against headroom ke liye. Dekhо RTOS task scheduling and context switching.)

Recall Solution 4.2

Cycles/second switching mein: cycles/s. Cycles/second available: . Fraction: . Tiny — yahi wajah hai ki auto-stacking NVIC + two-stack (MSP/PSP) design itna valued hai: switching overhead negligible rehta hai. Dekhо RTOS task scheduling and context switching.

Recall Solution 4.3
  • FFT / image stream → cacheable main memory. Yeh cache hits se benefit karta hai aur iski timing sirf on average good honi chahiye, worst-case-guaranteed nahi.
  • Servo loop code + data → TCM (tightly-coupled memory). TCM mein single-cycle, fixed access hoti hai koi cache misses nahi, toh uski worst-case timing bounded hoti hai — exactly wahi jo hard 10 µs deadline ko chahiye.

Servo ko cache kyun nahi karein? Kyunki ek cache miss access time ko data-dependent banata hai: ek hit fast hota hai, lekin ek miss core ko kai cycles ke liye stall karta hai jab woh slow main memory se fetch karta hai. Woh variability matlab hai tum ab prove nahi kar sakte ki servo worst case mein 10 µs ke andar finish hoga — aur hard-real-time worst case par judge hota hai, average par kabhi nahi. TCM poora gamble hata deta hai: har access ek fixed single cycle hai, toh deadline provable hai. Rule of thumb: deadline wali cheez ke liye determinism > average speed. Dekhо Cache vs TCM in real-time systems.


Level 5 — Mastery

Recall Solution 5.1

Design A: single-issue ⇒ IPC , toh cycles. Design B: , toh cycles cycles. Speedup: . Notice karo ki win do independent factors mein split hota hai: clock () aur IPC (), aur . Depth akele ne clock buy kiya; dual-issue ne extra 1.35 buy kiya.

Recall Solution 5.2

Cause: FPU ek coprocessor (CP10/CP11) hai jo reset par disabled hota hai. Tumhara compiled code real hardware FP instructions emit karta hai (kyunki -mfloat-abi=hard hai), lekin coprocessor disabled hone ke saath woh instructions undefined hain aur HardFault ke roop mein trap ho jaati hain. Fix: SystemInit (startup) mein CPACR register mein CP10/CP11 access bits set karo — commonly: SCB->CPACR |= (0xF << 20); (CP10 aur CP11 ko full access deta hai), phir ek DSB/ISB barrier. Yahan koi arithmetic nahi, lekin yeh sabse common "meri float crash karti hai" mystery hai.

Recall Solution 5.3

Kya stacking wasted hoti hai? Nahi. Cortex-M late-arrival use karta hai: jab stacking ke dauraan ek higher-priority IRQ appear hota hai, CPU woh same register push reuse karta hai aur simply higher-priority handler par vector karta hai — woh second time push nahi karta. Toh hum stacking ek baar count karte hain, do baar nahi.

Timeline ko order mein walk karo:

  • Pending interrupt ko detect karo: cycles.
  • 8 registers stack karo (shared, exactly once done late-arrival ki wajah se): cycles.
  • High-priority handler body completion tak run karta hai: cycles.
  • Exception-return / unstack path servo body start hone se pehle: cycles.

Kyunki , woh hai .

Mastery point: naive answer push ko double count karta hai jaise cycles. Late-arrival (aur uska sibling tail-chaining) exactly woh hardware mechanism hai jo duplicate 12-cycle push remove karta hai, worst-case latency ko bounded aur knowable rakhta hai nested preemption ke under bhi. Dekhо Interrupts and the NVIC.


Recall Self-test checklist (finish karne ke baad reveal karo)

Pipeline fill ek baar paid per run? ::: Haan — ek baar add karo, har loop nahi (assume karo koi branch flushes nahi). IPC formula direction? ::: Cycles = Instructions ÷ IPC (IPC matlab instructions per cycle). M4F par double? ::: Software emulation — slow. Sirf single-precision float hardware mein. Cycles → time? ::: Cycles ko clock in MHz se divide karo µs paane ke liye; µs ko 1000 se multiply karo ns mein padhne ke liye. Nested IRQ stacking kitni baar count hoti hai? ::: Ek baar (tail-chain / late-arrival reuse), har level par nahi. Ideal kya break karta hai? ::: Branch mispredictions, data/memory stalls, exceptions — har ek re-fill/bubble penalty add karta hai.