Simvik

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.

Copiesout getsReturns*repair
both valid, identical datacopy 0NVM_REQ_OK-1 (none)
both valid, different datacopy 0: it is written first, so it is the newerNVM_REQ_REDUNDANCY_FAILED1
only copy 0 validcopy 0NVM_REQ_REDUNDANCY_FAILED1
only copy 1 validcopy 1NVM_REQ_REDUNDANCY_FAILED0
neither validdefaultsNVM_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 15

The 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 15

Learn it first

More AUTOSAR Classic for diagnostics engineers problems