2016-02-27 22:31:44 +01:00
|
|
|
.. _device_drivers:
|
|
|
|
|
2019-02-01 15:48:43 +01:00
|
|
|
Device Driver Model
|
2016-02-27 22:31:44 +01:00
|
|
|
###############################
|
|
|
|
|
|
|
|
Introduction
|
|
|
|
************
|
2018-03-26 13:42:38 +02:00
|
|
|
The Zephyr kernel supports a variety of device drivers. Whether a
|
|
|
|
driver is available depends on the board and the driver.
|
2016-07-08 21:51:25 +02:00
|
|
|
|
2016-02-27 22:31:44 +01:00
|
|
|
The Zephyr device model provides a consistent device model for configuring the
|
2016-07-08 21:51:25 +02:00
|
|
|
drivers that are part of a system. The device model is responsible
|
|
|
|
for initializing all the drivers configured into the system.
|
2016-02-27 22:31:44 +01:00
|
|
|
|
2019-08-15 00:16:55 +02:00
|
|
|
Each type of driver (e.g. UART, SPI, I2C) is supported by a generic type API.
|
2016-02-27 22:31:44 +01:00
|
|
|
|
2016-07-08 21:51:25 +02:00
|
|
|
In this model the driver fills in the pointer to the structure containing the
|
2016-02-27 22:31:44 +01:00
|
|
|
function pointers to its API functions during driver initialization. These
|
2016-07-08 21:51:25 +02:00
|
|
|
structures are placed into the RAM section in initialization level order.
|
|
|
|
|
2019-08-15 00:16:55 +02:00
|
|
|
.. image:: device_driver_model.svg
|
|
|
|
:width: 40%
|
|
|
|
:align: center
|
|
|
|
:alt: Device Driver Model
|
|
|
|
|
2016-07-08 21:51:25 +02:00
|
|
|
Standard Drivers
|
|
|
|
****************
|
|
|
|
|
|
|
|
Device drivers which are present on all supported board configurations
|
|
|
|
are listed below.
|
|
|
|
|
|
|
|
* **Interrupt controller**: This device driver is used by the kernel's
|
|
|
|
interrupt management subsystem.
|
|
|
|
|
|
|
|
* **Timer**: This device driver is used by the kernel's system clock and
|
|
|
|
hardware clock subsystem.
|
|
|
|
|
|
|
|
* **Serial communication**: This device driver is used by the kernel's
|
|
|
|
system console subsystem.
|
|
|
|
|
|
|
|
* **Random number generator**: This device driver provides a source of random
|
|
|
|
numbers.
|
|
|
|
|
|
|
|
.. important::
|
2016-02-27 22:31:44 +01:00
|
|
|
|
2019-08-15 00:16:55 +02:00
|
|
|
Certain implementations of the random number generator device driver
|
|
|
|
do not generate sequences of values that are truly random.
|
2016-02-27 22:31:44 +01:00
|
|
|
|
2016-06-13 23:54:07 +02:00
|
|
|
Synchronous Calls
|
|
|
|
*****************
|
|
|
|
|
2016-08-11 22:50:45 +02:00
|
|
|
Zephyr provides a set of device drivers for multiple boards. Each driver
|
2016-06-13 23:54:07 +02:00
|
|
|
should support an interrupt-based implementation, rather than polling, unless
|
|
|
|
the specific hardware does not provide any interrupt.
|
|
|
|
|
2019-08-15 00:16:55 +02:00
|
|
|
High-level calls accessed through device-specific APIs, such as
|
|
|
|
:file:`i2c.h` or :file:`spi.h`, are usually intended as synchronous. Thus,
|
|
|
|
these calls should be blocking.
|
2016-06-13 23:54:07 +02:00
|
|
|
|
2016-02-27 22:31:44 +01:00
|
|
|
Driver APIs
|
|
|
|
***********
|
|
|
|
|
|
|
|
The following APIs for device drivers are provided by :file:`device.h`. The APIs
|
|
|
|
are intended for use in device drivers only and should not be used in
|
|
|
|
applications.
|
|
|
|
|
|
|
|
:c:func:`DEVICE_INIT()`
|
|
|
|
create device object and set it up for boot time initialization.
|
|
|
|
|
2016-04-14 18:28:33 +02:00
|
|
|
:c:func:`DEVICE_AND_API_INIT()`
|
|
|
|
Create device object and set it up for boot time initialization.
|
|
|
|
This also takes a pointer to driver API struct for link time
|
|
|
|
pointer assignment.
|
|
|
|
|
2016-02-27 22:31:44 +01:00
|
|
|
:c:func:`DEVICE_NAME_GET()`
|
|
|
|
Expands to the full name of a global device object.
|
|
|
|
|
|
|
|
:c:func:`DEVICE_GET()`
|
|
|
|
Obtain a pointer to a device object by name.
|
|
|
|
|
|
|
|
:c:func:`DEVICE_DECLARE()`
|
|
|
|
Declare a device object.
|
2016-08-23 21:19:29 +02:00
|
|
|
|
2019-10-04 21:24:41 +02:00
|
|
|
.. _device_struct:
|
|
|
|
|
2016-08-23 21:19:29 +02:00
|
|
|
Driver Data Structures
|
|
|
|
**********************
|
|
|
|
|
|
|
|
The device initialization macros populate some data structures at build time
|
|
|
|
which are
|
|
|
|
split into read-only and runtime-mutable parts. At a high level we have:
|
|
|
|
|
|
|
|
.. code-block:: C
|
|
|
|
|
|
|
|
struct device {
|
|
|
|
struct device_config *config;
|
|
|
|
void *driver_api;
|
|
|
|
void *driver_data;
|
|
|
|
};
|
|
|
|
|
2018-03-26 15:26:50 +02:00
|
|
|
struct device_config {
|
|
|
|
char *name;
|
|
|
|
int (*init)(struct device *device);
|
|
|
|
const void *config_info;
|
|
|
|
[...]
|
|
|
|
};
|
|
|
|
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
The ``config`` member is for read-only configuration data set at build time. For
|
2016-08-23 21:19:29 +02:00
|
|
|
example, base memory mapped IO addresses, IRQ line numbers, or other fixed
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
physical characteristics of the device. This is the ``config_info`` structure
|
|
|
|
passed to the ``DEVICE_*INIT()`` macros.
|
2016-08-23 21:19:29 +02:00
|
|
|
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
The ``driver_data`` struct is kept in RAM, and is used by the driver for
|
2016-08-23 21:19:29 +02:00
|
|
|
per-instance runtime housekeeping. For example, it may contain reference counts,
|
|
|
|
semaphores, scratch buffers, etc.
|
|
|
|
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
The ``driver_api`` struct maps generic subsystem APIs to the device-specific
|
2016-08-23 21:19:29 +02:00
|
|
|
implementations in the driver. It is typically read-only and populated at
|
|
|
|
build time. The next section describes this in more detail.
|
|
|
|
|
|
|
|
|
|
|
|
Subsystems and API Structures
|
|
|
|
*****************************
|
|
|
|
|
2018-03-26 13:42:38 +02:00
|
|
|
Most drivers will be implementing a device-independent subsystem API.
|
2016-08-23 21:19:29 +02:00
|
|
|
Applications can simply program to that generic API, and application
|
|
|
|
code is not specific to any particular driver implementation.
|
|
|
|
|
|
|
|
A subsystem API definition typically looks like this:
|
|
|
|
|
|
|
|
.. code-block:: C
|
|
|
|
|
|
|
|
typedef int (*subsystem_do_this_t)(struct device *device, int foo, int bar);
|
|
|
|
typedef void (*subsystem_do_that_t)(struct device *device, void *baz);
|
|
|
|
|
|
|
|
struct subsystem_api {
|
|
|
|
subsystem_do_this_t do_this;
|
|
|
|
subsystem_do_that_t do_that;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline int subsystem_do_this(struct device *device, int foo, int bar)
|
|
|
|
{
|
|
|
|
struct subsystem_api *api;
|
|
|
|
|
|
|
|
api = (struct subsystem_api *)device->driver_api;
|
|
|
|
return api->do_this(device, foo, bar);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void subsystem_do_that(struct device *device, void *baz)
|
|
|
|
{
|
|
|
|
struct subsystem_api *api;
|
|
|
|
|
|
|
|
api = (struct subsystem_api *)device->driver_api;
|
|
|
|
api->do_that(device, foo, bar);
|
|
|
|
}
|
|
|
|
|
|
|
|
A driver implementing a particular subsystem will define the real implementation
|
|
|
|
of these APIs, and populate an instance of subsystem_api structure:
|
|
|
|
|
|
|
|
.. code-block:: C
|
|
|
|
|
|
|
|
static int my_driver_do_this(struct device *device, int foo, int bar)
|
|
|
|
{
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
static void my_driver_do_that(struct device *device, void *baz)
|
|
|
|
{
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct subsystem_api my_driver_api_funcs = {
|
|
|
|
.do_this = my_driver_do_this,
|
|
|
|
.do_that = my_driver_do_that
|
|
|
|
};
|
|
|
|
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
The driver would then pass ``my_driver_api_funcs`` as the ``api`` argument to
|
|
|
|
``DEVICE_AND_API_INIT()``, or manually assign it to ``device->driver_api``
|
|
|
|
in the driver init function.
|
2016-08-23 21:19:29 +02:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
Since pointers to the API functions are referenced in the ``driver_api``
|
2016-08-23 21:19:29 +02:00
|
|
|
struct, they will always be included in the binary even if unused;
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
``gc-sections`` linker option will always see at least one reference to
|
2016-08-23 21:19:29 +02:00
|
|
|
them. Providing for link-time size optimizations with driver APIs in
|
|
|
|
most cases requires that the optional feature be controlled by a
|
|
|
|
Kconfig option.
|
|
|
|
|
2019-08-01 18:54:31 +02:00
|
|
|
Device-Specific API Extensions
|
|
|
|
******************************
|
|
|
|
|
|
|
|
Some devices can be cast as an instance of a driver subsystem such as GPIO,
|
|
|
|
but provide additional functionality that cannot be exposed through the
|
|
|
|
standard API. These devices combine subsystem operations with
|
|
|
|
device-specific APIs, described in a device-specific header.
|
|
|
|
|
|
|
|
A device-specific API definition typically looks like this:
|
|
|
|
|
|
|
|
.. code-block:: C
|
|
|
|
|
|
|
|
#include <drivers/subsystem.h>
|
|
|
|
|
|
|
|
typedef int (*specific_do_this_t)(struct device *device, int foo);
|
|
|
|
|
|
|
|
struct specific_api {
|
|
|
|
subsystem_driver_api subsystem_api; /* this must be first */
|
|
|
|
specific_do_this_t do_this;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline int specific_do_this(struct device *device, int foo)
|
|
|
|
{
|
|
|
|
struct specific_api *api = (struct specific_api*)device->driver_api;
|
|
|
|
|
|
|
|
return api->do_this(device, foo);
|
|
|
|
}
|
|
|
|
|
|
|
|
A driver implementing extensions to the subsystem will define the real
|
|
|
|
implementation of both the subsystem API and the specific APIs:
|
|
|
|
|
|
|
|
.. code-block:: C
|
|
|
|
|
|
|
|
static int generic_do_whatever(struct device *device, void *arg)
|
|
|
|
{
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
static int specific_do_this(struct device *device, int foo)
|
|
|
|
{
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct specific_api driver_api = {
|
|
|
|
.subsystem_api = {
|
|
|
|
.do_whatever = generic_do_whatever,
|
|
|
|
},
|
|
|
|
.do_this = specific_do_this,
|
|
|
|
};
|
|
|
|
|
|
|
|
Applications use the device through both the subsystem and specific
|
|
|
|
APIs. The subsystem APIs will directly access the subsystem part of the
|
|
|
|
specific API structure.
|
|
|
|
|
2016-08-23 21:19:29 +02:00
|
|
|
Single Driver, Multiple Instances
|
|
|
|
*********************************
|
|
|
|
|
|
|
|
Some drivers may be instantiated multiple times in a given system. For example
|
|
|
|
there can be multiple GPIO banks, or multiple UARTS. Each instance of the driver
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
will have a different ``config_info`` struct and ``driver_data`` struct.
|
2016-08-23 21:19:29 +02:00
|
|
|
|
|
|
|
Configuring interrupts for multiple drivers instances is a special case. If each
|
|
|
|
instance needs to configure a different interrupt line, this can be accomplished
|
|
|
|
through the use of per-instance configuration functions, since the parameters
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
to ``IRQ_CONNECT()`` need to be resolvable at build time.
|
2016-08-23 21:19:29 +02:00
|
|
|
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
For example, let's say we need to configure two instances of ``my_driver``, each
|
|
|
|
with a different interrupt line. In ``drivers/subsystem/subsystem_my_driver.h``:
|
2016-08-23 21:19:29 +02:00
|
|
|
|
|
|
|
.. code-block:: C
|
|
|
|
|
|
|
|
typedef void (*my_driver_config_irq_t)(struct device *device);
|
|
|
|
|
|
|
|
struct my_driver_config {
|
2017-04-19 20:02:23 +02:00
|
|
|
u32_t base_addr;
|
2016-08-23 21:19:29 +02:00
|
|
|
my_driver_config_irq_t config_func;
|
|
|
|
};
|
|
|
|
|
|
|
|
In the implementation of the common init function:
|
|
|
|
|
|
|
|
.. code-block:: C
|
|
|
|
|
|
|
|
void my_driver_isr(struct device *device)
|
|
|
|
{
|
|
|
|
/* Handle interrupt */
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
int my_driver_init(struct device *device)
|
|
|
|
{
|
2016-10-06 16:41:22 +02:00
|
|
|
const struct my_driver_config *config = device->config->config_info;
|
2016-08-23 21:19:29 +02:00
|
|
|
|
|
|
|
/* Do other initialization stuff */
|
|
|
|
...
|
|
|
|
|
|
|
|
config->config_func(device);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Then when the particular instance is declared:
|
|
|
|
|
|
|
|
.. code-block:: C
|
|
|
|
|
|
|
|
#if CONFIG_MY_DRIVER_0
|
|
|
|
|
|
|
|
DEVICE_DECLARE(my_driver_0);
|
|
|
|
|
2017-09-19 00:34:28 +02:00
|
|
|
static void my_driver_config_irq_0(void)
|
2016-08-23 21:19:29 +02:00
|
|
|
{
|
|
|
|
IRQ_CONNECT(MY_DRIVER_0_IRQ, MY_DRIVER_0_PRI, my_driver_isr,
|
|
|
|
DEVICE_GET(my_driver_0), MY_DRIVER_0_FLAGS);
|
|
|
|
}
|
|
|
|
|
2016-10-21 10:07:12 +02:00
|
|
|
const static struct my_driver_config my_driver_config_0 = {
|
2017-09-19 00:34:28 +02:00
|
|
|
.base_addr = MY_DRIVER_0_BASE_ADDR,
|
|
|
|
.config_func = my_driver_config_irq_0
|
2016-08-23 21:19:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct my_driver_data_0;
|
|
|
|
|
|
|
|
DEVICE_AND_API_INIT(my_driver_0, MY_DRIVER_0_NAME, my_driver_init,
|
2018-12-03 18:02:32 +01:00
|
|
|
&my_driver_data_0, &my_driver_config_0, POST_KERNEL,
|
2016-08-23 21:19:29 +02:00
|
|
|
MY_DRIVER_0_PRIORITY, &my_driver_api_funcs);
|
|
|
|
|
|
|
|
#endif /* CONFIG_MY_DRIVER_0 */
|
|
|
|
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
Note the use of ``DEVICE_DECLARE()`` to avoid a circular dependency on providing
|
2016-08-23 21:19:29 +02:00
|
|
|
the IRQ handler argument and the definition of the device itself.
|
|
|
|
|
|
|
|
Initialization Levels
|
|
|
|
*********************
|
|
|
|
|
|
|
|
Drivers may depend on other drivers being initialized first, or
|
|
|
|
require the use of kernel services. The DEVICE_INIT() APIs allow the user to
|
|
|
|
specify at what time during the boot sequence the init function will be
|
2019-08-17 18:45:32 +02:00
|
|
|
executed. Any driver will specify one of four initialization levels:
|
2016-08-23 21:19:29 +02:00
|
|
|
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
``PRE_KERNEL_1``
|
2016-08-23 21:19:29 +02:00
|
|
|
Used for devices that have no dependencies, such as those that rely
|
|
|
|
solely on hardware present in the processor/SOC. These devices cannot
|
2017-11-04 15:56:53 +01:00
|
|
|
use any kernel services during configuration, since the kernel services are
|
2016-08-23 21:19:29 +02:00
|
|
|
not yet available. The interrupt subsystem will be configured however
|
|
|
|
so it's OK to set up interrupts. Init functions at this level run on the
|
|
|
|
interrupt stack.
|
|
|
|
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
``PRE_KERNEL_2``
|
2016-08-23 21:19:29 +02:00
|
|
|
Used for devices that rely on the initialization of devices initialized
|
2018-12-03 18:02:32 +01:00
|
|
|
as part of the ``PRE_KERNEL_1`` level. These devices cannot use any kernel
|
2017-02-27 22:15:43 +01:00
|
|
|
services during configuration, since the kernel services are not yet
|
2016-08-23 21:19:29 +02:00
|
|
|
available. Init functions at this level run on the interrupt stack.
|
|
|
|
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
``POST_KERNEL``
|
2016-11-08 20:06:55 +01:00
|
|
|
Used for devices that require kernel services during configuration.
|
|
|
|
Init functions at this level run in context of the kernel main task.
|
2016-08-23 21:19:29 +02:00
|
|
|
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
``APPLICATION``
|
2016-08-23 21:19:29 +02:00
|
|
|
Used for application components (i.e. non-kernel components) that need
|
|
|
|
automatic configuration. These devices can use all services provided by
|
|
|
|
the kernel during configuration. Init functions at this level run on
|
2016-11-08 20:06:55 +01:00
|
|
|
the kernel main task.
|
2016-08-23 21:19:29 +02:00
|
|
|
|
|
|
|
Within each initialization level you may specify a priority level, relative to
|
|
|
|
other devices in the same initialization level. The priority level is specified
|
|
|
|
as an integer value in the range 0 to 99; lower values indicate earlier
|
|
|
|
initialization. The priority level must be a decimal integer literal without
|
|
|
|
leading zeroes or sign (e.g. 32), or an equivalent symbolic name (e.g.
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
``\#define MY_INIT_PRIO 32``); symbolic expressions are *not* permitted (e.g.
|
|
|
|
``CONFIG_KERNEL_INIT_PRIORITY_DEFAULT + 5``).
|
2016-08-23 21:19:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
System Drivers
|
|
|
|
**************
|
|
|
|
|
2018-03-26 15:28:26 +02:00
|
|
|
In some cases you may just need to run a function at boot. Special ``SYS_*``
|
|
|
|
macros exist that map to ``DEVICE_*INIT()`` calls.
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
For ``SYS_INIT()`` there are no config or runtime data structures and there
|
|
|
|
isn't a way
|
2016-08-23 21:19:29 +02:00
|
|
|
to later get a device pointer by name. The same policies for initialization
|
|
|
|
level and priority apply.
|
|
|
|
|
2018-03-26 15:28:26 +02:00
|
|
|
For ``SYS_DEVICE_DEFINE()`` you can obtain pointers by name, see
|
doc: fix uses of back quotes in documentation
ReST defines interpreted text roles where text enclosed by single quotes
can be "intrepreted", for example :ref:`some name` becomes a link to
a label anywhere in the doc set named "some name", :c:func:`funcname()`
becomes a link to the API documentation for "funcname", and
:option:`CONFIG_NAME` becomes a link to, in our case, the documentation
for the generated Kconfig option.
This patch fixes uses of `some name` (without a role) by either adding
an explicit role, or changing to ``some name``, which indicates inline
code block formatting (most likely what was intended).
This is a precursor to changing the default behavior of interpreted
text to treat `some name` as :any:`some name` (as configured in
doc/conf.py), which would attempt to create a link to any available
definition of "some name".
We may not change this default role behavior, but it becomes an option
after the fixes in this patch. In any case, this patch fixes incorrect
uses of single-quoted text (possibly introduced because GitHub's
markdown language uses single-quoted text for inline code formatting).
Jira: ZEP-2414
Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2017-08-02 00:20:20 +02:00
|
|
|
:ref:`power management <power_management>` section.
|
2016-08-23 21:19:29 +02:00
|
|
|
|
|
|
|
:c:func:`SYS_INIT()`
|
|
|
|
|
2018-03-26 15:28:26 +02:00
|
|
|
:c:func:`SYS_DEVICE_DEFINE()`
|
2018-03-26 14:03:02 +02:00
|
|
|
|
|
|
|
Error handling
|
|
|
|
**************
|
|
|
|
|
|
|
|
In general, it's best to use ``__ASSERT()`` macros instead of
|
|
|
|
propagating return values unless the failure is expected to occur
|
|
|
|
during the normal course of operation (such as a storage device
|
|
|
|
full). Bad parameters, programming errors, consistency checks,
|
|
|
|
pathological/unrecoverable failures, etc., should be handled by
|
|
|
|
assertions.
|
|
|
|
|
|
|
|
When it is appropriate to return error conditions for the caller to
|
2019-08-15 00:16:55 +02:00
|
|
|
check, 0 should be returned on success and a POSIX :file:`errno.h` code
|
2018-03-26 14:03:02 +02:00
|
|
|
returned on failure. See
|
|
|
|
https://github.com/zephyrproject-rtos/zephyr/wiki/Naming-Conventions#return-codes
|
|
|
|
for details about this.
|
|
|
|
|
2019-01-25 19:29:25 +01:00
|
|
|
API Reference
|
|
|
|
**************
|
|
|
|
|
|
|
|
.. doxygengroup:: device_model
|
|
|
|
:project: Zephyr
|