Skip to content

Conversation

@anangl
Copy link
Member

@anangl anangl commented Feb 13, 2018

This is an initial proposal of a new API for the ADC driver, so that we could start discussion and conclude what is actually needed in the API, and what will be convenient to use.
I tried to keep similarity with the new SPI API (#5921).

A few questions/doubts that I have:

  1. Is there a need to use a bigger type than s16_t for sample values?
  2. How can gain/prescaling settings be effectively passed to the driver? I suppose that these settings will vary significantly between different controllers.
  3. Are there any other important configuration settings (of the ADC as a whole, or particular channels) that I haven't included yet?

@nashif @carlescufi @tbursztyka @MaureenHelm @andrewboie @galak

@galak galak mentioned this pull request Feb 13, 2018
33 tasks
@codecov-io
Copy link

codecov-io commented Feb 13, 2018

Codecov Report

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

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #6176   +/-   ##
=======================================
  Coverage   58.59%   58.59%           
=======================================
  Files         464      464           
  Lines       47483    47483           
  Branches     8796     8796           
=======================================
  Hits        27822    27822           
  Misses      15823    15823           
  Partials     3838     3838

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 597517c...c171e53. Read the comment docs.

Copy link
Contributor

@tbursztyka tbursztyka left a comment

Choose a reason for hiding this comment

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

samples value size will depend on resolution, so 8, 16, 24, 32, 64...?
Instead, I would make in void *, and depending on the resolution, the driver would fill it in accordingly.
Only thing would be that driver would check the buffer length vs resolution at first.
Just thinking out loud, I don't have much experience in ADCs (besides the awful adc_ti_adc108s102 I did back in the days)

include/adc.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

some description?

include/adc.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

so 0 would mean bare result?

include/adc.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

ticks is hw dependent. Wouldn't it be better to work on µs or ms? (Well, µs is only supported through busy wait loop)

Copy link
Member

Choose a reason for hiding this comment

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

I'm torn on this one. It's a pain to do the conversion in the driver and often inefficient due to divide operations, but I can see the value in having common units like us or ms for the application.

Copy link
Contributor

@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.

I get why adc_channel_setup() takes a dev pointer and the others dont, but the inconsistency is unfortunate.

include/adc.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

we should add defines & macros to access the fields your are defining.

Copy link
Member

Choose a reason for hiding this comment

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

Do these things ever change on a per-channel basis? They don't on NXP devices

include/adc.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

Wonder if sample_cfg should be separated from sample_results

Copy link
Member

Choose a reason for hiding this comment

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

It would be more consistent with spi to separate them

include/adc.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment seems incorrect.

Copy link
Contributor

Choose a reason for hiding this comment

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

We should document the following - is it required to call adc_channel_setup before adc_read*?

include/adc.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason channel_id isn't in adc_channel_cfg?

@nashif nashif added the area: API Changes to public APIs label Feb 13, 2018
include/adc.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

6 bits for the acquisition time looks a bit short, as 63 microseconds may be too small if you sample a high-impedance source or your ADC runs on a very slow clock.
On the other hand, 1us is rather high if you go high speed. For the STM32L4A6, I can setup Tacq as low as 30 nanoseconds.
So maybe we need a more flexible approach to specify the conversion time?

include/adc.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

s16_t is not enough, I used external 24-bit ADC in the past. And you need more bits when doing oversampling.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed

@vaussard
Copy link
Contributor

A missing feature is the ability to setup the trigger source. Here a software trigger is implied, but we have to use hardware triggers sometime. I do not see a straightforward way to setup it as it is usually hardware dependant, but maybe some kind of driver-specific API (like uart_drv_cmd() or Linux ioctl) ?

@anangl
Copy link
Member Author

anangl commented Feb 16, 2018

I will be on vacation all next week. I'll address all the comments (already existing ones and any new that may appear, and are welcome) when I'm back.

Copy link
Member

@MaureenHelm MaureenHelm left a comment

Choose a reason for hiding this comment

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

A missing feature is the ability to setup the trigger source. Here a software trigger is implied, but we have to use hardware triggers sometime. I do not see a straightforward way to setup it as it is usually hardware dependant,

Agreed

include/adc.h Outdated
Copy link
Member

Choose a reason for hiding this comment

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

It would be more consistent with spi to separate them

include/adc.h Outdated
Copy link
Member

Choose a reason for hiding this comment

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

I'm torn on this one. It's a pain to do the conversion in the driver and often inefficient due to divide operations, but I can see the value in having common units like us or ms for the application.

include/adc.h Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Agreed

include/adc.h Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Do these things ever change on a per-channel basis? They don't on NXP devices

include/adc.h Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Please add the dev pointer argument back and remove it from the adc_config struct

@anangl anangl force-pushed the adc_api_proposal branch from 0fdcbc6 to 3b03667 Compare March 27, 2018 14:11
@anangl
Copy link
Member Author

anangl commented Mar 27, 2018

Tried to address gathered feedback, to some extent at least.
Please consider it still as a work in progress, more a base for discussion than a real code.

include/adc.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

tiny left over from previous PR push

Copy link
Member Author

Choose a reason for hiding this comment

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

Right. I haven't tried asynchronous part yet. That's why I haven't got any compile error for this.

Copy link
Contributor

@tbursztyka tbursztyka left a comment

Choose a reason for hiding this comment

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

tiny changes requested. (mostly on struct alignment)

include/adc.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

to be consistent with other APIs, create function typedefs for the functions first

include/adc.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

I would reorder the attributes so these 2 u8_t would be at the end of the struct.

include/adc.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

I would move this attribute at the end of the struct

include/adc.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

move these 2 attributes at the beginning of the struct

include/adc.h Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Do they need to be unique? (no duplicates)

include/adc.h Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this should be channel_count? It's a little confusing to have "samplings" and "samples" be different things

Copy link
Member Author

Choose a reason for hiding this comment

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

I simplified the structure and used a bit-mask for specifying the channels to be included in the sampling. The previous proposal with the channel_ids pointer and sample_count was too confusing and probably troublesome in implementation (regarding duplicates, as you pointed out in the comment above, and order of the channels).

My idea was to define a sampling as reading of samples from one or more channels. Such samplings can be then arranged in sequences with optional triggers. I added more comments in the header file and changed some names to hopefully present the idea clearer. I am open to change the terminology to a better one. English is not my native language so the names I propose may not be the best (or even good enough) ones.

include/adc.h Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Are the triggers one-off or recurring (such as from a hardware timer)? Can you explain the difference between the DELAY triggers and the SAMPLE_RATE trigger?

Copy link
Member Author

Choose a reason for hiding this comment

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

I need to work more on these. The rough idea, just to start with something, was that the DELAY triggers will be done in software (with some sleeping or busy loop before doing next sampling) and the SAMPLE_RATE one (the name was rather a place holder) by hardware, e.g. some timer providing means to achieve accurate sampling intervals.

include/adc.h Outdated
Copy link
Member

Choose a reason for hiding this comment

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

A sampling is a group of channels?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. I added better, hopefully, descriptions of the structure and its fields.

include/adc.h Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Are these numbers non-integer gain factors? We will need more values in the enum, as the nxp adc supports gain factors of {1,2,4,8,16,32,64}

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, these are fractional factors. I added proper comments, and renamed "prescaling" to "gain".

@anangl anangl force-pushed the adc_api_proposal branch from 3b03667 to e12acbf Compare April 17, 2018 09:25
@anangl
Copy link
Member Author

anangl commented Apr 17, 2018

Updated the proposal according to the recent suggestions. Added more descriptions to make things clearer.
Some parts, like triggers, need more work. I continue to work on them and I will update the proposal when I got something worth presenting.

@anangl anangl force-pushed the adc_api_proposal branch from e12acbf to 433bf0b Compare April 17, 2018 12:09
@nashif nashif added the DNM This PR should not be merged (Do Not Merge) label Apr 21, 2018
This is a proposal of a new API for ADC driver to address reported
issues (zephyrproject-rtos#3980) and clarify confusing things, like the buffer length.
Asynchronous requests and channel configuration were added as well.

Signed-off-by: Andrzej Głąbek <[email protected]>
@anangl anangl force-pushed the adc_api_proposal branch from 433bf0b to d279047 Compare April 23, 2018 15:25
@anangl
Copy link
Member Author

anangl commented Apr 23, 2018

I updated the proposal once more. I got rid of the adc_sampling structure - its fields became members of the adc_sequence structure. I think this way it will be less confusing and simpler for example for single channel samplings, and the possibility of having a sequence of samplings each performed for a different sets of selected channels, which is no longer present, was actually not that much useful.
I added also actions that the application can pass from the callback to driver to alter the performed sequence.
And finally, I updated the adc_api test to show my vision of the usage of the proposed API.
Sample output:

***** Booting Zephyr OS v1.11.0-763-gd27904790 *****
Running test suite adc_basic_test
===================================================================
starting test - test_adc_sample_one_channel
Samples read: 0x034c 0x0000 0x0000 0x0000 0x0000 0x0000
PASS - test_adc_sample_one_channel.
===================================================================
starting test - test_adc_sample_two_channels
Samples read: 0x034b 0x034e 0x0000 0x0000 0x0000 0x0000
PASS - test_adc_sample_two_channels.
===================================================================
starting test - test_adc_sample_with_delay_trigger
delay_trigger_callback: sampling 0
delay_trigger_callback: sampling 1
delay_trigger_callback: sampling 2
delay_trigger_callback: sampling 3
delay_trigger_callback: sampling 4
Samples read: 0x034d 0x034b 0x034c 0x034b 0x034b 0x0000
PASS - test_adc_sample_with_delay_trigger.
===================================================================
starting test - test_adc_repeated_samplings
repeated_samplings_callback: done 1
Samples read: 0x034b 0x034b 0x0000 0x0000 0x0000 0x0000
repeated_samplings_callback: done 2
Samples read: 0x0348 0x034b 0x0000 0x0000 0x0000 0x0000
repeated_samplings_callback: done 3
Samples read: 0x034e 0x034b 0x0000 0x0000 0x0000 0x0000
repeated_samplings_callback: done 4
Samples read: 0x034b 0x034b 0x0000 0x0000 0x0000 0x0000
repeated_samplings_callback: done 5
Samples read: 0x034c 0x034c 0x0000 0x0000 0x0000 0x0000
repeated_samplings_callback: done 6
Samples read: 0x034b 0x034b 0x0000 0x0000 0x0000 0x0000
repeated_samplings_callback: done 7
Samples read: 0x034c 0x034c 0x0000 0x0000 0x0000 0x0000
repeated_samplings_callback: done 8
Samples read: 0x034c 0x034c 0x0000 0x0000 0x0000 0x0000
repeated_samplings_callback: done 9
Samples read: 0x034a 0x034a 0x0000 0x0000 0x0000 0x0000
repeated_samplings_callback: done 10
Samples read: 0x034c 0x034c 0x0000 0x0000 0x0000 0x0000
PASS - test_adc_repeated_samplings.
===================================================================
===================================================================
PROJECT EXECUTION SUCCESSFUL

Updated API calls and added a few new test cases so that the new
possibilities of the API could be presented and checked.

Signed-off-by: Andrzej Głąbek <[email protected]>
@anangl anangl force-pushed the adc_api_proposal branch from d279047 to c171e53 Compare April 23, 2018 16:35
@tbursztyka
Copy link
Contributor

Seems fine to me.
You seem to have forgotten the drivers update? (to get your test running)

@anangl
Copy link
Member Author

anangl commented May 21, 2018

Replaced by #7691.

@anangl anangl closed this May 21, 2018
@anangl anangl deleted the adc_api_proposal branch September 24, 2018 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: API Changes to public APIs DNM This PR should not be merged (Do Not Merge)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants