Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce reworked API for ADC drivers #7691

Merged
merged 21 commits into from
Aug 29, 2018

Conversation

anangl
Copy link
Member

@anangl anangl commented May 21, 2018

This replaces #6176 and introduces a new API for ADC drivers basing on the feedback gathered there.
Implementations of drivers for ADC peripherals present in nRF chips are included, as well as updated and extended adc_api test to present the way the API is supposed to be used.

Marked as DNM, since existing ADC drivers (and "grove" sensors) need to be updated for this one to be merged.

List of drivers to be converted

  • nrfx
  • dw/qmsi
  • sam_afec
  • ti_adc108s102
  • mcux

Fixes #3980

@codecov-io
Copy link

codecov-io commented May 21, 2018

Codecov Report

Merging #7691 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #7691   +/-   ##
=======================================
  Coverage   52.12%   52.12%           
=======================================
  Files         212      212           
  Lines       25939    25939           
  Branches     5590     5590           
=======================================
  Hits        13521    13521           
  Misses      10177    10177           
  Partials     2241     2241

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b0c3b35...47dfd81. Read the comment docs.

@anangl anangl force-pushed the adc_api_rework branch 2 times, most recently from c657298 to 7a19df3 Compare May 22, 2018 07:52
include/adc.h Outdated
u8_t channel_id : 5;

/** Channel type: single-ended or differential. */
bool differential : 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep u8_t here, I am not entirely sure compiler will properly pack it with previous byte.

@nashif nashif added this to the v1.13.0 milestone May 29, 2018
@anangl anangl force-pushed the adc_api_rework branch 2 times, most recently from 9878e45 to 1a8b1f1 Compare June 4, 2018 13:37
@MaureenHelm MaureenHelm added area: ADC Analog-to-Digital Converter (ADC) area: API Changes to public APIs labels Jun 19, 2018
@anangl
Copy link
Member Author

anangl commented Jun 26, 2018

  • refactored the test a bit, added one more test case for asynchronous calls
  • added an atomic variable to handle triggering of another sampling while the previous one is still in progress
  • added the possibility to perform multiple samplings with zero interval (as fast as possible)

Copy link
Collaborator

@galak galak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use DT for IRQ info, should have DT nodes for adc controllers.

return -EBUSY;
}

IRQ_CONNECT(ADC_IRQn, CONFIG_ADC_0_IRQ_PRI,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should get IRQ info from DT

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

static nrfx_adc_channel_t m_channels[CONFIG_ADC_NRFX_ADC_CHANNEL_COUNT];


static int adc_nrfx_channel_setup(struct device *dev,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use nrfx_adc_ as a function prefix, its a bit confusing when seeing adc_ and thinking maybe that's an adc core function, and not a driver specific one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the nrfx_ prefix is used for stuff that comes from nrfx, and in particular all API functions of the nrfx driver for ADC peripheral are prefixed with nrfx_adc_. So that would be confusing as well. On the other hand, adc_nrfx_ is consistent with the scheme we use for naming implementation files.
And as I see such approach is commonly used. See for example: spi_dw_transceive, i2c_mcux_configure, uart_stm32_poll_in. Thus, I would prefer to stay with this naming.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, that makes sense.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess my comment is about being consistent in this file, so either everything should be prefixed adc_nrfx_ or not.

nrf_saadc_int_enable(NRF_SAADC_INT_END);
NRFX_IRQ_ENABLE(SAADC_IRQn);

IRQ_CONNECT(SAADC_IRQn, CONFIG_ADC_0_IRQ_PRI,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IRQ num and priority should come from DT.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@anangl
Copy link
Member Author

anangl commented Jun 29, 2018

  • rebased on the latest master to get the support for nRF52810 and update its "ADC affected" files in this PR
  • added DTS entries as requested by @galak
  • minor editorial changes: used BIT and BIT_MASK macros instead of shifting in ADC_ACQ_TIME macros, renamed ADC_DEV_NAME TO ADC_DEVICE_NAME in the "adc_api" test

@anangl
Copy link
Member Author

anangl commented Jul 2, 2018

  • removed syscalls from the API since read functions cannot use them, because of the callback that is executed in the ISR context, and there is a little sense in having syscall only for channel configuration

@anangl
Copy link
Member Author

anangl commented Jul 3, 2018

Rebased on master to resolve a conflict in "boards/arm/nrf52_pca20020/Kconfig.board".

@galak galak requested a review from fallrisk July 3, 2018 18:04
Copy link
Collaborator

@galak galak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments on the DTS support and minor comments on code style for the NRF drivers.

@@ -6,4 +6,5 @@

config BOARD_NRF51_PCA10028
bool "nRF51 PCA10028"
select HAS_DTS_ADC
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

select HAS_DTS_ADC, should be with the driver, not the board.

@@ -6,5 +6,6 @@

config BOARD_NRF52810_PCA10040
bool "nRF52810 PCA10040"
select HAS_DTS_ADC
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this, just do the select in the driver's Kconfig.

static nrfx_adc_channel_t m_channels[CONFIG_ADC_NRFX_ADC_CHANNEL_COUNT];


static int adc_nrfx_channel_setup(struct device *dev,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess my comment is about being consistent in this file, so either everything should be prefixed adc_nrfx_ or not.

*/
channel_id = 0;
do {
if (channels & 1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like the 1 here could be replaced w/define or some support. Is this an enable bit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits set to 1 in this bit-mask indicate that the corresponding channels are selected for the sampling sequence.
I refactored a bit this part of code (in both implementations). Hopefully, it will be clearer now.

@@ -13,10 +13,16 @@ menuconfig ADC
bool
prompt "ADC drivers"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add select HAS_DTS_ADC so that all ADC drivers require DTS support.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what would be the point in having HAS_DTS_ADC at all then? Right now it only excludes from Kconfig the options providing information that can be taken from DT, and these options are dependent on the same menuconfig entry that would select HAS_DTS_ADC. They should be rather removed then.
And this would be inconsistent with what we've got in other drivers. Is this a new approach that we want to take?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As agreed on the API call, I'll add another commit with this change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

@dbkinder dbkinder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc change LGTM. Commit message should be updated (still talks about being marked as DNM).

@MaureenHelm
Copy link
Member

Commit message should be updated (still talks about being marked as DNM).

Which commit?

@mnkp
Copy link
Member

mnkp commented Aug 29, 2018

I am aware that I am very late to the review, unfortunately I was not able to devote much time to the Zephyr project in last months.

The new ADC API is definitely the step in the right direction, however its design has some problems. The main one is the lack of support for triggers. ADC hardware comes in an overwhelming variety of design variants often with a distinctively different architectures. It makes it difficult to design universal API, there are however some fairly common features. One of such features is the concept of a trigger. Once the ADC module is configured - using the nomenclature of this API - the sequence is defined, the measurement can be started by a trigger whereas trigger is typically:

  • a software trigger
  • a timer trigger
  • a pin
  • a trigger of some internal trigger subsystem

of those the new API supports software trigger and makes an attempt to support timer trigger. The pin trigger and subsystem trigger is missing. The subsystem trigger is popular on many low power MCUs where it is possible to start ADC measurement on some internal, predefined condition without waking up the CPU.

Unfortunately this API can not be easily extended. The next time a project needs to trigger ADC measurement on the pin level change the ADC API would need to be completely reworked.

In Linux the ADC driver is handled by Industrial I/O driver (IIO) subsystem. Its API is build around the concept of triggers.

anangl and others added 21 commits August 29, 2018 11:06
This commit adds ADC nodes to DTS files for nRF SoCs and introduces
corresponding  bindings for these nodes.

Signed-off-by: Andrzej Głąbek <[email protected]>
ADC nodes are enabled in DTS for the following nRF development kits:
- nrf51_pca10028
- nrf52_pca10040
- nrf52_pca20020
- nrf52810_pca10040
- nrf52840_pca10056

Signed-off-by: Andrzej Głąbek <[email protected]>
This commit replaces the API for ADC drivers with a reworked one.
As requested in the issue zephyrproject-rtos#3980, the adc_enable/adc_disable functions
are removed. Additionaly, some new features are introduced, like:
- asynchronous calls
- configuration of channels
- multi-channel sampling

Common parts of code that are supposed to appear in each implementation
of the driver (like locking, synchronization, triggering of consecutive
samplings) are provided in the "adc_context.h" file to keep consistency
with the SPI driver. Syscalls are no longer present in the API because
the functions starting read requests cannot use them, since they can be
provided with a callback that is executed in the ISR context, and there
is no point in supporting syscalls only for the channels configuration.

"adc_api" test is updated and extended with additional test cases,
with intention to show how the API is supposed to be used.
"adc_simple" test is removed as it does not seem to add much value.

Signed-off-by: Andrzej Głąbek <[email protected]>
This commit adds translation layers to make nrfx drivers for the nRF
ADC (nRF51 series) and SAADC (nRF52 series) peripherals accessible via
the Zephyr's API. The SAADC peripheral is accessed using nrfx HAL only
as it turns out that usage of the nrfx driver in this case would be
inconvenient and would unnecessarily complicate the shim.

Signed-off-by: Andrzej Głąbek <[email protected]>
This adds to the adc_api test board-specific configurations
for the following boards:
- nrf51_pca10028
- nrf52_pca10040
- nrf52840_pca10056

"adc" is indicated as supported in the yaml files for these boards
so that they are included in sanitycheck.

Signed-off-by: Andrzej Głąbek <[email protected]>
To require relevant DTS entries on all platforms providing the ADC
driver.

Signed-off-by: Andrzej Głąbek <[email protected]>
Major rework of the mcux adc16 driver to convert it to the new adc api.
Currently supports a subset of the api features including synchronous
and asynchronous reads, and consecutive reads triggered by the kernel
timer.

Does not yet support some of the channel configuration options such as
gain and reference voltage because the hardware only allows these
options to be configured by peripheral instance rather than by channel.
The values are currently hardcoded in the driver, but in the future we
could introduce some flexibility per instance via device tree
attributes.

Does not yet support consecutive reads triggered by a hardware timer.

Signed-off-by: Maureen Helm <[email protected]>
Adds board-specific macros to enable the adc_api test to run on kinetis
boards. This restores the test to support all the nxp boards that were
supported before the new adc api was introduced.

Signed-off-by: Maureen Helm <[email protected]>
Updated to work with new ADC API.

Signed-off-by: Justin Watson <[email protected]>
Remove adc_qmsi_ss to use designware driver for sensor subsystem

Signed-off-by: Punit Vara <[email protected]>
Get required data from dts tree.

Signed-off-by: Punit Vara <[email protected]>
Enable ADC node in dts.

Signed-off-by: Punit Vara <[email protected]>
Replace new ADC apis for designware driver

Signed-off-by: Punit Vara <[email protected]>
Remove Kconfig which are replaced by dts and unnecessary macros which is
not useful anymore.

Signed-off-by: Punit Vara <[email protected]>
Replace old ADC apis with new ones.

Signed-off-by: Punit Vara <[email protected]>
Replace old ADC driver APIs with new ones.

Signed-off-by: Punit Vara <[email protected]>
Add ARC related parameters and modified test case to work
this test case on arc sensor subsystem.

Signed-off-by: Punit Vara <[email protected]>
check return value for sample fetch. Conditional printk
added to print adc value.

Signed-off-by: Punit Vara <[email protected]>
Use Kconfig to get device name. Make LIBC conditional to get
output.

Signed-off-by: Punit Vara <[email protected]>
Remove ADC support from d2000 as it is not migrated to
new ADC apis.

Signed-off-by: Punit Vara <[email protected]>
Remove unnecessary sample width scenario.

Signed-off-by: Punit Vara <[email protected]>
@nashif nashif merged commit 108b539 into zephyrproject-rtos:master Aug 29, 2018
@anangl anangl deleted the adc_api_rework branch December 3, 2018 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: ADC Analog-to-Digital Converter (ADC) area: API Changes to public APIs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants