Do not write back an old status byte with only TF forced to zero. In Control/status 2 at 01h, both AF and TF clear on a zero write and are unchanged by a one write. If AF was zero when read and an alarm matches before the write, copying that old zero clears the new event. Write one to AF when clearing only TF and handle the mode, enables and unused fields according to their own rules.
Read values are not necessarily safe write values
The PT7C4363BQ uses an external 32.768 kHz crystal and has both a calendar alarm and countdown timer. Register 01h puts TI/TP at bit 4, AF at bit 3, TF at bit 2, AIE at bit 1 and TIE at bit 0. Bits 7 through 5 are unused and are written as zero. AF and TF report the alarm and timer events when read. On a write, zero clears the corresponding flag; one leaves the current state unchanged. It does not force the flag high or restore a cached state.
This changes normal read-modify-write practice. Reading and modifying a software-owned field may preserve other configuration. For a hardware flag that can change between bus operations, an old read is not its present value. Explicitly supply the no-clear value for the other flag when handling a single event. Define ownership of the mode and enables separately. A one write protecting a flag does not prevent two software writers from racing over TI/TP, AIE or TIE.
| Current intention | AF / TF fields written | Treatment of other fields |
|---|---|---|
| Clear only TF | 1 / 0 | Retain the confirmed mode and enables |
| Clear only AF | 0 / 1 | Retain the confirmed mode and enables |
| Deliberately clear both | 0 / 0 | Preserve evidence of both events first |
| Change configuration only | 1 / 1 | Write intended settings and zero unused bits |
How a newly arriving event can be lost
Suppose a service routine reads TF at one and AF at zero. The calendar alarm then matches before the write and sets AF. Writing back the old AF zero while clearing TF requests two clears. A later read may show neither flag active and provide no evidence of the alarm. If the write puts one in AF and zero in TF, an already-set AF survives and a still-clear AF remains clear. This example illustrates the race under the documented semantics; it does not report a measured lost event on a particular board.
For example, with TI/TP, AIE and TIE assumed to be zero, one and one, a write that clears TF and preserves AF would yield 0Bh. A different control configuration needs a different full byte. Do not copy 0Bh to other products or modes. Construct the write from intended control settings, clear actions and required unused values rather than from a magic constant. Keep the old snapshot and the written byte in traces so a reviewer can identify whether a bit was a deliberate preserve command or a copied old value.
Read again without treating one flag as a queue
Capture initial status, identify which event has entered processing and then clear the relevant bit under the authorized design. Read again after completing the transaction and handle any flag that remains active as appropriate. Do not change alarm conditions to suppress a software race, or clear both flags before preserving the first observed state. For a disputed transition, keep the initial read, clear write and following read, correlated with INT or software markers. A final zero does not mean nothing happened earlier.
A single set flag does not count occurrences. Multiple countdown completions during one service interval cannot be reconstructed from TF alone. Nor does this method preserve another occurrence of the same flag you are deliberately clearing. The datasheet does not specify detailed simultaneous-event priority between the internal event and that bus write. A system requiring every event needs separately designed counts or trusted timing evidence. Protection of the other flag is not a blanket no-loss guarantee.
Test two kinds of close timing
First replay an offline transaction where TF is read and AF arrives before the clear. Verify that the driver generates the preserve command without changing alarms on running equipment. Then use an approved bench assembly with alarm and timer events close enough to exercise the race. Preserve the actual timeline and vary event order or firmware timing one at a time without changing the crystal and other configuration. A run with no overlap does not cover the race.
Record interrupt mode separately. A set flag is active according to its read semantics; whether the host received a notification also depends on enables, configuration and the pin. An application event count alone cannot tell whether hardware lost a flag or software failed to log it. Keep hardware reads and writes, pin captures and application outcomes in separate records.
Describe each flag's write behavior in the driver contract. A clear function should take an explicit event intent rather than treating a register read as a configuration template. Define ownership and order for concurrent access, so one routine does not protect AF only for another to clear it with a stale value. Regress all three paths: clearing both intentionally, clearing only one, and modifying enables without clearing either.
Audit three timestamps: pre-write observation, completion and application consumption. If the alarm was eligible before a clear-only-TF transaction, check whether a different routine serviced its flag instead of attributing every discrepancy to hardware. With multiple modules responding to one physical interrupt, one shared entry point can own the status register, distribute captured events and clear deliberately. Whatever the notification order, retain the raw value and requested clear action.
Give the tester two assertions for the preserve field: writing one when the flag was zero must not invent an event, and writing one when it was high must not clear it. If using an emulator, verify its no-change behavior rather than modeling the flag as ordinary assignment. Restore the approved alarm configuration after bench testing. Do not claim coverage of simultaneous internal events that the test did not actually exercise.
FAQ
Does writing one to AF create an alarm event?
No. One preserves the existing flag; zero clears it. If AF becomes set just afterward, check for a real alarm match and inspect the bus trace instead of attributing it to the preserve operation.
Can I copy the original upper bits while clearing only TF?
Not indiscriminately. Write zeros to unused bits as specified, use the authorized configuration for mode and enables, and write one to AF to preserve it. Those fields follow different write rules.
