Skip to main content

Embedded Systems · Timers, scheduling, and real-time constraints

Two 16-bit timers are cascaded to form a 32-bit timestamp

Problem

Embedded Systems practice problem

Two 16-bit timers are cascaded to form a 32-bit timestamp. TIML\mathrm{TIM_L} is clocked at fL=1.000 MHzf_{\mathrm{L}}=1.000\,\mathrm{MHz}, counts 0,…,655350,\ldots,65535, and emits a TRGO pulse on each overflow. TIMH\mathrm{TIM_H} is clocked by that TRGO, so it increments once per 65536 μs65536\,\mu\mathrm{s}. Software reads a 32-bit time by the protocol: read H1=CNTHH_1=\mathrm{CNT}_H, read L=CNTLL=\mathrm{CNT}_L, read H2=CNTHH_2=\mathrm{CNT}_H. If H2=H1H_2=H_1, the timestamp is (H1≪16)∨L(H_1\ll 16)\lor L. If H2≡H1+1(mod216)H_2\equiv H_1+1\pmod{2^{16}}, an overflow of TIML\mathrm{TIM_L} occurred during the read, and the following reconstruction is required: if L<32768L<32768 the overflow preceded the read of LL, so use (H2≪16)∨L(H_2\ll 16)\lor L; if L≥32768L\ge 32768 the overflow followed the read of LL, so use (H1≪16)∨L(H_1\ll 16)\lor L. Pin PA0\mathrm{PA0} is unused. Ignore TRGO insertion delay.

(a) Compute the increment period of TIMH\mathrm{TIM_H} and the wrap period of the 32-bit pair, in seconds.

(b) A coherent read returns H1=0x00A1H_1=\mathtt{0x00A1}, L=0xFFF0L=\mathtt{0xFFF0}, H2=0x00A1H_2=\mathtt{0x00A1}. Compute the 32-bit tick count and the time in seconds.

(c) A torn read returns H1=0x00A1H_1=\mathtt{0x00A1}, L=0x0002L=\mathtt{0x0002}, H2=0x00A2H_2=\mathtt{0x00A2}. Reconstruct the timestamp. Then compute the incorrect timestamps obtained by blindly using (H1≪16)∨L(H_1\ll 16)\lor L and by blindly using (H2≪16)∨L(H_2\ll 16)\lor L, and report each error in ticks.

(d) Another torn read returns H1=0x00A1H_1=\mathtt{0x00A1}, L=0xFFF8L=\mathtt{0xFFF8}, H2=0x00A2H_2=\mathtt{0x00A2}. Reconstruct the timestamp using the stated rule. Explain in one sentence why L≥32768L\ge 32768 selects H1H_1 rather than H2H_2.

Hint

Hint

Cascaded 16-bit counters form a 32-bit tick only if the high half is sampled consistently with the low half.

Check your work

Work the problem yourself first. Then open it in Training to check your answer and read the full worked solution.

The answer check and full solution for this problem come with ProofAnvil Practice membership ($19 USD monthly). See membership. Or start with the free Embedded Systems sample problem: Try the free sample problem.

More Embedded Systems practice problems

Back to Embedded Systems

An original ProofAnvil practice problem, written for this course. ProofAnvil is a practice course, not a homework-answer service.