التصحيح والدمج
مُقدَّم
7:37
المعالجات الدقيقة والأنظمة المدمجة
The digital logic course built gates and flip-flops; this course builds the machine that runs programs. Embedded work is verified from three directions.…
سرد باللغة الإنجليزية · ترجمة نصية بالإنجليزية و中文 يمكنك التبديل بينهما
في هذا الدرس
| English | المعنى |
|---|---|
| watchdog/ˈwɒtʃdɒɡ/ | مراقب |
| debounce/ˈdebaʊns/ | إزالة الارتجاف |
"It froze twice last night"
- Every firmware bug starts as a vague report. Firmware is tested on the exact hardware it ships on, and the fault rarely appears when someone is watching.
- Without a method you often only move a bug instead of fixing it. This lesson gives that method.
The debug loop
- Reproduce the fault with the smallest input. Isolate it at a boundary with an LED, a breakpoint or one UART print.
- Fix one thing, then re-verify. Two changes at once leave you unsure which one helped.

رتب حلقة التصحيح في الترتيب الصحيح.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
بدون إعادة الإنتاج، لا يمكنك معرفة ما إذا كان الإصلاح قد نجح؛ وإصلاح أكثر من شيء يخفي أيهما كان السبب.
Bisection
- Put a probe in the middle of the suspect code. Whether the probe fires before the fault tells you which half holds the bug.
- Each probe halves the range, so $\log_{2}(n)$ probes find one instruction among $n$: ten for 1024.
بحث التقسيم الثنائي يفحص 512 تعليمات مشتبه بها. كم عدد القياسات التي يحتاجها كحد أقصى؟
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
كل قياس ينصف النطاق: $\log_{2} 512 = 9$.
منطقة مشتبه بها تحتوي على 4096 تعليمات. كم عدد دورات التقسيم الثنائية اللازمة لإيجاد الخاطئة؟
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
$2^{12} = 4096$، وبالتالي تستلزم 12 دورة. مضاعفة الكود تضيف دورة واحدة فقط.
The watchdog
- A watchdog 看门狗 is a counter that resets the chip if it runs out. The firmware must "kick" it (restart the count) regularly.
- A hung system then reboots by design instead of freezing. Kick it only from the main loop, never from an ISR.
لماذا يعد تفريغ watchdog داخل ISR أمر خاطئ؟
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
إذا قام ISR بإعادة تشغيل المراقب، قد يتجمد الحلقة الرئيسية بينما تستمر المقاطعات في تغذيته، ولن يحدث إعادة التشغيل أبداً.
Debounce
- A pressed button bounces: its contact opens and closes for a few milliseconds. Debounce 消抖 accepts a new state only after N equal readings in a row.
- It combines a timer tick, a GPIO read and a state decision: the whole course in one routine.
Worked example: a debounce trace
- Samples every 10 ms read 1, 0, 0, 1. Rule: two equal readings in a row.
- At 10 ms there is one 0; at 20 ms a second 0, so a stable 0 is declared. The 1 at 30 ms is a single sample and is ignored.
One contrary reading does not erase the qualified state
$N\text{ equal sampled readings span }(N-1)\Delta t$
Step through the worked 1,0,0,1 trace at 10 ms intervals with two equal readings required. The first qualified 0 appears at 20 ms; the later single 1 does not reverse it. Edit samples, compare a one-sample pulse and an alternating input, and change N to distinguish input chatter from accepted output changes. Startup remains unqualified until a state earns enough consecutive readings.
تصل العينات 1، 0، 0، 1 كل 10 مللي ثانية، بدءاً من 0 مللي ثانية. مع قاعدة الاثنتين المتتاليتين، متى يُعلن عن حالة مستقرة؟
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
عند 10 ms يوجد واحد 0 ولا حكم؛ عند 20 ms الثاني 0 يجعل 0 مستقراً. 1 عند 30 ms هو عينة فردية.
تتطلب القاعدة ثلاث قراءات متساوية متتالية، مأخوذة كل 5 مللي ثانية بدءاً من 0 مللي ثانية. القراءات هي 0، 1، 0، 1، 1، 1. في أي وقت، بالمللي ثانية، يُعلن عن مستقر 1؟
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
قيم 1 عند 15 و20 و25 مللي ثانية هي الثلاث الأولى متتالية، لذا يأتي الحكم عند 25 مللي ثانية.
Worked example: bisecting the freeze
- The suspect region is 1024 instructions. Make instruction 512 light the LED: dark at the freeze means the first half; lit means the second.
- Each run halves the region: 512, 256, 128 … after ten runs, one instruction is left, because $2^{10} = 1024$.
Three common slips
- Fixing two things at once. One helps, one breaks something else, and now there are three bugs.
- Testing on another board. Debug on the same board revision that failed.
- Turning off the watchdog "for now". The field failure it would have survived is exactly the one that ships.
أثناء تصحيح الأخطاء، من الممارسات الجيدة إيقاف المراقب والاحتفاظ به معطلاً بعد إصلاح الخطأ.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
منتج تم شحنه بدون مراقب يتجمد في الموقع بدلاً من إعادة التشغيل. إذا كان يجب أن يكون معطلاً خلال جلسة، قم بتفعيله مجدداً قبل الإصدار.
Recap
- Reproduce, bisect, fix one thing, re-verify.
- $\log_{2}(n)$ probes find the fault among $n$ instructions:
| Instructions $n$ | 1024 | about 1 million |
|---|---|---|
| Probes | 10 | 20 |
- Debounce needs N equal readings in a row; kick the watchdog from the main loop only.
ما المشكلة؟ لا يرى ذلك سوى فريق الموقع.