Integrating a new SPI sensor in Zephyr

I have a SPI-interface DAC (digital-to-analog converter). I think the best way to represent this is as a Zephyr sensor. Do you guys agree? If so, is there a tutorial for how to integrate a new sensor in Zephyr? If you don't think that's the right approach, is there an nRF Connect SPI library I can use?

Here's a little code snippet from device.h, which defines a struct used in sensor.h:

/**
 * @brief Runtime device structure (in ROM) per driver instance
 */
struct device {
	/** Name of the device instance */
	const char *name;
	/** Address of device instance config information */
	const void *config;
	/** Address of the API structure exposed by the device instance */
	const void *api;
	/** Address of the common device state */
	struct device_state *state;
	/** Address of the device instance private data */
	void *data;
	/**
	 * Optional pointer to handles associated with the device.
	 *
	 * This encodes a sequence of sets of device handles that have some
	 * relationship to this node. The individual sets are extracted with
	 * dedicated API, such as device_required_handles_get().
	 */
	Z_DEVICE_HANDLES_CONST device_handle_t *handles;

#if defined(CONFIG_PM_DEVICE) || defined(__DOXYGEN__)
	/**
	 * Reference to the device PM resources (only available if
	 * @kconfig{CONFIG_PM_DEVICE} is enabled).
	 */
	struct pm_device *pm;
#endif
};

Note the extensive use of generic pointers to things like "api". No offense intended, but this is not a good way to create polymorphism in C. Any mistakes in type casting will have to be sorted at runtime and that's a huge debug task. Why not use C++, which is intended for inheritance and polymorphic classes?

So I'd rather access the SPI hardware more directly if there's some sort of basic API in Zephyr. If not, is there an example of accessing it at the register level?

Parents Reply Children
No Data
Related