Programming for Robotics with Arduino C++ 4, Challenge 3 solution (which values tear?) Idea: model the read as one load per byte, lowest byte first, and let the interrupt add 1 to the counter before the load of byte `gap`. For every possible counter value of a 2-byte and a 3-byte counter (65,536 and 16.8 million values, an exhaustive test) and every gap, compare the value read with the two true values, before and after the add (wrapping at the counter size), and count those that match neither. The disassembly of the two read functions comes from the s_isr sketch of the chapter. CODE: // x3.cpp -- Challenge 3: exactly which counter values can be read wrongly? Exhaustive on the byte-load model (host build, not run on a board). // A counter of B bytes is read one byte at a time, lowest first. An interrupt adds 1 between two loads. For every counter value and every gap, // compare the value read with the two true values (before / after the interrupt) and count the torn ones. #include #include static uint32_t torn_read(uint32_t counter, int bytes, int isr_after) { uint32_t out = 0; for (int b = 0; b < bytes; ++b) { if (b == isr_after) counter += 1; out |= ((counter >> (8 * b)) & 0xFFu) << (8 * b); } return out; } int main() { std::printf("exhaustive count of torn reads (interrupt adds 1 between two byte loads):\n"); std::printf(" counter bytes values tried gap after load torn values fraction predicted 1/256^gap worst error\n"); const int sizes[] = {2, 3}; for (int bytes : sizes) { uint32_t total = 1u << (8 * bytes); for (int g = 1; g < bytes; ++g) { uint32_t torn = 0; long worst = 0; for (uint32_t v = 0; v < total; ++v) { uint32_t r = torn_read(v, bytes, g); uint32_t after = (v + 1) & (total - 1); if (r != v && r != after) { ++torn; long e1 = (long)r - (long)v; if (e1 < 0) e1 = -e1; long e2 = (long)r - (long)after; if (e2 < 0) e2 = -e2; long e = e1 < e2 ? e1 : e2; if (e > worst) worst = e; } } double pred = 1.0; for (int i = 0; i < g; ++i) pred /= 256.0; std::printf(" %13d %12u %14d %11u %8.6f %19.6f %11ld\n", bytes, (unsigned)total, g, (unsigned)torn, (double)torn / total, pred, worst); } } std::printf("\nthe AVR code for the two reads of a 32-bit counter, from the compiled sketch (avr-objdump):\n"); std::printf(" read_left_unsafe: 4 x lds + ret = 5 instructions, 12 cycles, interrupts never off\n"); std::printf(" read_left_atomic: in sreg, cli, 4 x lds, out sreg, ret = 8 instructions, 15 cycles, interrupts off for cli..out = 1 + 4 x 2 + 1 = 10 cycles = 0.6 us\n"); std::printf(" the interrupt routine takes about 94 cycles = 5.9 us, so the atomic read delays an edge by at most about a tenth of one routine\n"); return 0; } END CODE OUTPUT: exhaustive count of torn reads (interrupt adds 1 between two byte loads): counter bytes values tried gap after load torn values fraction predicted 1/256^gap worst error 2 65536 1 256 0.003906 0.003906 255 3 16777216 1 65536 0.003906 0.003906 255 3 16777216 2 256 0.000015 0.000015 65535 the AVR code for the two reads of a 32-bit counter, from the compiled sketch (avr-objdump): read_left_unsafe: 4 x lds + ret = 5 instructions, 12 cycles, interrupts never off read_left_atomic: in sreg, cli, 4 x lds, out sreg, ret = 8 instructions, 15 cycles, interrupts off for cli..out = 1 + 4 x 2 + 1 = 10 cycles = 0.6 us the interrupt routine takes about 94 cycles = 5.9 us, so the atomic read delays an edge by at most about a tenth of one routine END OUTPUT Reading the output. A read is torn only if the add carries out of the bytes already loaded: gap 1 tears when the lowest byte is 0xFF, gap 2 when the lowest two bytes are 0xFFFF, so the fractions are exactly 1/256 and 1/65,536 (the 2-byte counter: 256 of 65,536 values, the 3-byte counter: 65,536 of 16,777,216 for gap 1 and 256 for gap 2). The worst error is the size of the carry that was missed: 255 for a one-byte gap, 65,535 for a two-byte gap. A torn read is therefore rare for any one value, but a counter that counts up passes through every one of these values, and the error is huge (a wheel that seems to have turned 65,000 ticks in 20 ms). The cost of the cure, from the disassembly: the plain read is 4 lds and a ret (5 instructions, 12 cycles); the atomic read adds in (save the status register), cli and out (restore it), so 8 instructions and 15 cycles, and interrupts are off from the cli to the out, about 1 + 4 x 2 + 1 = 10 cycles or 0.6 microseconds at 16 MHz. The interrupt routine itself is about 94 cycles, so an edge that arrives during the atomic read waits a tenth of one routine at most. ATOMIC_RESTORESTATE saves and restores the interrupt flag instead of forcing it on, so the read is also safe inside code that already runs with interrupts off. Check on method: this is a model of byte loads, not a run on a board; the instruction listing is the compiler's for the Uno and the cycle counts are from the data sheet. The model assumes one interrupt, adding exactly 1; a faster edge rate, or an interrupt that adds more, tears more often and by other amounts. It also assumes the compiler loads the bytes in this order, which the listing confirms for this build only; another compiler or optimisation level could order them differently, and the same bug would tear in a different pattern.