Read a redundant NvM block
Pick the valid copy of a block stored twice, say which copy needs repair, and fall back to defaults. A medium embedded C problem (AUTOSAR Classic for…
medium · AUTOSAR Classic for diagnostics engineers · autosar · automotive · nvm · crc · robustness
Data that an ECU must not lose (a calibration, the odometer, a security counter) is kept by AUTOSAR's NvM as a redundant block: two copies, each with its own CRC. A power cut during a write damages at most one of them.
Write nvm_read(copies, defaults, out, repair). A copy is valid when its stored crc equals crc8 of its data.
| Copies | out gets | Returns | *repair |
|---|---|---|---|
| both valid, identical data | copy 0 | NVM_REQ_OK | -1 (none) |
| both valid, different data | copy 0: it is written first, so it is the newer | NVM_REQ_REDUNDANCY_FAILED | 1 |
| only copy 0 valid | copy 0 | NVM_REQ_REDUNDANCY_FAILED | 1 |
| only copy 1 valid | copy 1 | NVM_REQ_REDUNDANCY_FAILED | 0 |
| neither valid | defaults | NVM_REQ_RESTORED_FROM_ROM | -1 |
*repair is the index of the copy the caller must rewrite from out.
Starter code
nvm_result_t nvm_read(const nvm_copy_t copies[2], const uint8_t *defaults, uint8_t *out, int *repair) {
(void)defaults;
memcpy(out, copies[0].data, BLOCK_LEN);
*repair = -1;
return NVM_REQ_OK;
}Example tests
Both copies good
read(make(10), make(10));Expected output:
OK, repair -1, data 10 11 12 13 14 15The first copy was damaged by a power cut
nvm_copy_t a = make(10), b = make(10);
a.data[2] ^= 0x40;
read(a, b);Expected output:
REDUNDANCY_FAILED, repair 0, data 10 11 12 13 14 15Learn it first
- AUTOSAR Classic for diagnostics engineers · The layered architecture, the path of a diagnostic request through CanIf, CanTp, PduR, Dcm and Dem, and NvM.
More AUTOSAR Classic for diagnostics engineers problems
- Route a PDU through the stack · A PDU router: deliver each received PDU to every destination its routing table names.