Simvik

Infusion pump: read the pressure, with a timeout

Start a conversion, poll its done flag, and give up safely if the converter never answers. A medium embedded C problem (Registers and memory-mapped I/O), run…

medium · Registers and memory-mapped I/O · polling · timeout · adc · product

Product: Infusion pump (Medical)

Requirement. The pump measures line pressure to detect a blocked tube (an occlusion). If the pressure sensor's converter ever stops answering, the pump must raise an alarm instead of freezing.

Write pressure_read: start a conversion (write ADC_START to the control register), poll the status register until ADC_EOC is set, then return the data register. Poll at most 1000 times; if EOC never comes, return -1.

Starter code

int32_t pressure_read(void) {
  while (!(adc_read_sr() & ADC_EOC)) {
  }
  return adc_read_dr();
}

Example tests

A normal conversion

hw_setup(3, 2048);
printf("%d\n", pressure_read());

Expected output:

2048

A converter that never answers

hw_setup(-1, 0);
printf("%d\n", pressure_read());

Expected output:

-1

Learn it first

  • Registers, volatile and const · How C talks to hardware: memory-mapped registers, what volatile and const promise, register structs and read-modify-write.

More Registers and memory-mapped I/O problems