Embedded Systems · Timers, scheduling, and real-time constraints
Two 16-bit timers are cascaded to form a 32-bit timestamp
Problem
Two 16-bit timers are cascaded to form a 32-bit timestamp. \(\mathrm{TIM_L}\) is clocked at \(f_{\mathrm{L}}=1.000\,\mathrm{MHz}\), counts \(0,\ldots,65535\), and emits a TRGO pulse on each overflow. \(\mathrm{TIM_H}\) is clocked by that TRGO, so it increments once per \(65536\,\mu\mathrm{s}\). Software reads a 32-bit time by the protocol: read \(H_1=\mathrm{CNT}_H\), read \(L=\mathrm{CNT}_L\), read \(H_2=\mathrm{CNT}_H\). If \(H_2=H_1\), the timestamp is \((H_1\ll 16)\lor L\). If \(H_2\equiv H_1+1\pmod{2^{16}}\), an overflow of \(\mathrm{TIM_L}\) occurred during the read, and the following reconstruction is required: if \(L<32768\) the overflow preceded the read of \(L\), so use \((H_2\ll 16)\lor L\); if \(L\ge 32768\) the overflow followed the read of \(L\), so use \((H_1\ll 16)\lor L\). Pin \(\mathrm{PA0}\) is unused. Ignore TRGO insertion delay. (a) Compute the increment period of \(\mathrm{TIM_H}\) and the wrap period of the 32-bit pair, in seconds. (b) A coherent read returns \(H_1=\mathtt{0x00A1}\), \(L=\mathtt{0xFFF0}\), \(H_2=\mathtt{0x00A1}\). Compute the 32-bit tick count and the time in seconds. (c) A torn read returns \(H_1=\mathtt{0x00A1}\), \(L=\mathtt{0x0002}\), \(H_2=\mathtt{0x00A2}\). Reconstruct the timestamp. Then compute the incorrect timestamps obtained by blindly using \((H_1\ll 16)\lor L\) and by blindly using \((H_2\ll 16)\lor L\), and report each error in ticks. (d) Another torn read returns \(H_1=\mathtt{0x00A1}\), \(L=\mathtt{0xFFF8}\), \(H_2=\mathtt{0x00A2}\). Reconstruct the timestamp using the stated rule. Explain in one sentence why \(L\ge 32768\) selects \(H_1\) rather than \(H_2\).
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
- GPIOA on Anvil-M0 provides three 32-bit registers that affect the same 16 output…Microcontrollers, memory-mapped I/O, and peripherals
- A 12-bit ADC with V_REF=3.300 V and V_LSB=V_REF/4096 is read sixteen times in…Microcontrollers, memory-mapped I/O, and peripherals
- USART2 is clocked by f_CK=36.00 MHzInterrupts, concurrency, and shared state
- A UART RX FIFO is 16 bytes deepInterrupts, concurrency, and shared state
- A complementary PWM pair drives one leg of an H-bridgeTimers, scheduling, and real-time constraints
- A brushless-DC commutation computer runs two independent implicit-deadline periodic…Serial buses and communication protocols
- A single fully preemptive uniprocessor hosts three independent implicit-deadline…Serial buses and communication protocols
- Standard-mode I^2C requires t_LOW≥ 4.70 μs and t_HIGH≥ 4.00 μs on SCL, t_r≤ 1.00 μs…Memory, energy, and reliability budgets
- A two-door airlock is modelled by Boolean variables L and R, each 1 when that door is…State-machine verification and safety boundaries