How to handle multiple devices of the same kind in device driver code?

Hi, I’m writing a RIOT device driver for the ST A3G4250D sensor. How can I set up my code to work with multiple ST A3G4250D devices?

Hi,

that’s cool! Looking forward for your PR :slight_smile:

You could orient your code e.g. on the SHT3x driver. Basically, you have “device handle”, which is a struct containing all the state. The functions operate on this device handle. Because no state is store elsewhere but in the device handle, the driver will be able to handle as many devices as you can allocate device handles (practically CPU time, I/O pins, busses etc. are typically the limiting factor, not memory for device handles).

You can open a PR early on and add an WIP: in front of the title, if you gave questions while the driver is not yet completed. Feel free to mention me in such a PR if you have any questions.

For writing drivers, this doc is pretty handy.

Any of the existing drivers in RIOT should serve as a good example. I often look at the ds18 driver as an example. It uses a pointer to a struct as a device handle which is preferred in my opinion. Some drivers use an index number as a device handle instead. See the soft_uart and soft_spi drivers for such examples.