Skip to content

Commit 1bc1eb1

Browse files
gmarullcarlescufi
authored andcommitted
drivers: blink: add custom out-of-tree driver class
Add a new out-of-tree custom driver class to demonstrate all aspects of custom driver class creation. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent fb84d3f commit 1bc1eb1

File tree

6 files changed

+149
-0
lines changed

6 files changed

+149
-0
lines changed

CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
# This CMake file is picked by the Zephyr build system because it is defined
55
# as the module CMake entry point (see zephyr/module.yml).
66

7+
8+
# This is needed so that custom driver classes using system calls are taken into
9+
# account
10+
list(APPEND SYSCALL_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include)
11+
set(SYSCALL_INCLUDE_DIRS ${SYSCALL_INCLUDE_DIRS} PARENT_SCOPE)
12+
713
zephyr_include_directories(include)
814

915
add_subdirectory(drivers)

drivers/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Copyright (c) 2021 Nordic Semiconductor ASA
22
# SPDX-License-Identifier: Apache-2.0
33

4+
# Out-of-tree drivers for custom classes
5+
add_subdirectory_ifdef(CONFIG_BLINK blink)
6+
7+
# Out-of-tree drivers for existing driver classes
48
add_subdirectory_ifdef(CONFIG_SENSOR sensor)

drivers/Kconfig

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
menu "Drivers"
5+
rsource "blink/Kconfig"
56
rsource "sensor/Kconfig"
67
endmenu

drivers/blink/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) 2024 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
zephyr_library()

drivers/blink/Kconfig

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) 2024 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
menuconfig BLINK
5+
bool "Blink device drivers"
6+
help
7+
This option enables the blink custom driver class.
8+
9+
if BLINK
10+
11+
config BLINK_INIT_PRIORITY
12+
int "Blink device drivers init priority"
13+
default KERNEL_INIT_PRIORITY_DEVICE
14+
help
15+
Blink device drivers init priority.
16+
17+
module = BLINK
18+
module-str = blink
19+
source "subsys/logging/Kconfig.template.log_config"
20+
21+
endif # BLINK

include/app/drivers/blink.h

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)