giorgio.gross@robodev.eu [nuttx]
2018-01-09 15:25:47 UTC
Hi,
I aim to interface two identical proximity sensors via I2C (with identical I2C addresses) which are both connected to an I2C multiplexer (PCA9549BDP by NXP). I wrote drivers for both the multiplexer and the sensors, but I'm wondering how to select a sensor through the mux in a clean way:
Currently, the sensors are registered under "/dev/vcnl0"; reading one sensor requires opening the device, selecting the right port of the mux and then performing the read call. Reading the second sensor requires just to select the other mux port and then performing again a read call. Yet, the same file descriptor is used as before - it is not visible that the actual device read from is a different one (except that another mux port was selected).
I don't think this is a clean solution, it would be more convenient to have e.g. two sensors registered "/dev/vcnl0" and "/dev/vcnl1" and open/read from each one separately. But from my point of view that requires polluting the proximity sensor driver with code depending on the mux..
Another solution I can think of is to divide the sensor driver in upper and lower half. The upper half would then contain the mux port selection and the lower half would still be architecture independent. But I'm not sure if this is what the lower/upper half idea is intended for.
Is there a built in pattern / best practice to solve such problems? Please point me in the right direction if I have overlooked a post targeting that problem.
Thanks, Giorgio
I aim to interface two identical proximity sensors via I2C (with identical I2C addresses) which are both connected to an I2C multiplexer (PCA9549BDP by NXP). I wrote drivers for both the multiplexer and the sensors, but I'm wondering how to select a sensor through the mux in a clean way:
Currently, the sensors are registered under "/dev/vcnl0"; reading one sensor requires opening the device, selecting the right port of the mux and then performing the read call. Reading the second sensor requires just to select the other mux port and then performing again a read call. Yet, the same file descriptor is used as before - it is not visible that the actual device read from is a different one (except that another mux port was selected).
I don't think this is a clean solution, it would be more convenient to have e.g. two sensors registered "/dev/vcnl0" and "/dev/vcnl1" and open/read from each one separately. But from my point of view that requires polluting the proximity sensor driver with code depending on the mux..
Another solution I can think of is to divide the sensor driver in upper and lower half. The upper half would then contain the mux port selection and the lower half would still be architecture independent. But I'm not sure if this is what the lower/upper half idea is intended for.
Is there a built in pattern / best practice to solve such problems? Please point me in the right direction if I have overlooked a post targeting that problem.
Thanks, Giorgio