Will this link work?
Add up both ends' clock errors on a UART and compare them with the frame's budget. An easy embedded C problem (Stuck buses, stuck pins and drifting clocks),…
easy · Stuck buses, stuck pins and drifting clocks · uart · clocks · arithmetic
A UART receiver samples each bit in its middle, timed from the start bit's edge, so the clock error between the two ends accumulates over the frame. For a frame of bits bits (start, data, parity, stop) the receiver reaches the middle of the last bit bits - 0.5 bit times after the edge, and fails when that is off by half a bit:
limit % = 50 / (bits - 0.5)
Write link_ok(tx_error, rx_error, bits). The errors are each end's baud-rate error in percent (positive is fast). The error that counts is the difference between them. Return 1 if its size is at most half the limit (the margin real edges and sampling need), 0 otherwise.
Starter code
int link_ok(double tx_error, double rx_error, int bits) {
double limit = 50.0 / (bits - 0.5);
return tx_error + rx_error <= limit / 2;
}Example tests
Both ends close, then both off the same way
show(0.08, 0.0, 10);
show(2.0, 1.9, 10);Expected output:
+0.08 +0.00 10 bits: 1
+2.00 +1.90 10 bits: 1Errors in opposite directions
show(2.1, -1.5, 10);Expected output:
+2.10 -1.50 10 bits: 0Learn it first
- Stuck buses, stuck pins and drifting clocks · Freeing a locked I2C bus, detecting stuck pins, UART clock budgets and a timeout on every wait.
More Stuck buses, stuck pins and drifting clocks problems
- The bus recovery sequence · Clock SCL until a stuck slave lets go of SDA, at most nine times, then make a STOP.