|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#ifndef APP_DRIVERS_BLINK_H_ |
| 7 | +#define APP_DRIVERS_BLINK_H_ |
| 8 | + |
| 9 | +#include <zephyr/device.h> |
| 10 | +#include <zephyr/toolchain.h> |
| 11 | + |
| 12 | +/** |
| 13 | + * @defgroup drivers_blink Blink drivers |
| 14 | + * @ingroup drivers |
| 15 | + * @{ |
| 16 | + * |
| 17 | + * @brief A custom driver class to blink LEDs |
| 18 | + * |
| 19 | + * This driver class is provided as an example of how to create custom driver |
| 20 | + * classes. It provides an interface to blink an LED at a configurable rate. |
| 21 | + * Implementations could include simple GPIO-controlled LEDs, addressable LEDs, |
| 22 | + * etc. |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * @defgroup drivers_blink_ops Blink driver operations |
| 27 | + * @{ |
| 28 | + * |
| 29 | + * @brief Operations of the blink driver class. |
| 30 | + * |
| 31 | + * Each driver class tipically provides a set of operations that need to be |
| 32 | + * implemented by each driver. These are used to implement the public API. If |
| 33 | + * support for system calls is needed, the operations structure must be tagged |
| 34 | + * with `__subsystem` and follow the `${class}_driver_api` naming scheme. |
| 35 | + */ |
| 36 | + |
| 37 | +/** @brief Blink driver class operations */ |
| 38 | +__subsystem struct blink_driver_api { |
| 39 | + /** |
| 40 | + * @brief Configure the LED blink period. |
| 41 | + * |
| 42 | + * @param dev Blink device instance. |
| 43 | + * @param period_ms Period of the LED blink in milliseconds, 0 to |
| 44 | + * disable blinking. |
| 45 | + * |
| 46 | + * @retval 0 if successful. |
| 47 | + * @retval -EINVAL if @p period_ms can not be set. |
| 48 | + * @retval -errno Other negative errno code on failure. |
| 49 | + */ |
| 50 | + int (*set_period_ms)(const struct device *dev, unsigned int period_ms); |
| 51 | +}; |
| 52 | + |
| 53 | +/** @} */ |
| 54 | + |
| 55 | +/** |
| 56 | + * @defgroup drivers_blink_api Blink driver API |
| 57 | + * @{ |
| 58 | + * |
| 59 | + * @brief Public API provided by the blink driver class. |
| 60 | + * |
| 61 | + * The public API is the interface that is used by applications to interact with |
| 62 | + * devices that implement the blink driver class. If support for system calls is |
| 63 | + * needed, functions accessing device fields need to be tagged with `__syscall` |
| 64 | + * and provide an implementation that follows the `z_impl_${function_name}` |
| 65 | + * naming scheme. |
| 66 | + */ |
| 67 | + |
| 68 | +/** |
| 69 | + * @brief Configure the LED blink period. |
| 70 | + * |
| 71 | + * |
| 72 | + * @param dev Blink device instance. |
| 73 | + * @param period_ms Period of the LED blink in milliseconds. |
| 74 | + * |
| 75 | + * @retval 0 if successful. |
| 76 | + * @retval -EINVAL if @p period_ms can not be set. |
| 77 | + * @retval -errno Other negative errno code on failure. |
| 78 | + */ |
| 79 | +__syscall int blink_set_period_ms(const struct device *dev, |
| 80 | + unsigned int period_ms); |
| 81 | + |
| 82 | +static inline int z_impl_blink_set_period_ms(const struct device *dev, |
| 83 | + unsigned int period_ms) |
| 84 | +{ |
| 85 | + const struct blink_driver_api *api = |
| 86 | + (const struct blink_driver_api *)dev->api; |
| 87 | + |
| 88 | + return api->set_period_ms(dev, period_ms); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * @brief Turn LED blinking off. |
| 93 | + * |
| 94 | + * This is a convenience function to turn off the LED blinking. It also shows |
| 95 | + * how to create convenience functions that re-use other driver functions, or |
| 96 | + * driver operations, to provide a higher-level API. |
| 97 | + * |
| 98 | + * @param dev Blink device instance. |
| 99 | + * |
| 100 | + * @return See blink_set_period_ms(). |
| 101 | + */ |
| 102 | +static inline int blink_off(const struct device *dev) |
| 103 | +{ |
| 104 | + return blink_set_period_ms(dev, 0); |
| 105 | +} |
| 106 | + |
| 107 | +#include <syscalls/blink.h> |
| 108 | + |
| 109 | +/** @} */ |
| 110 | + |
| 111 | +/** @} */ |
| 112 | + |
| 113 | +#endif /* APP_DRIVERS_BLINK_H_ */ |
0 commit comments