Skip to content

Conversation

@guenzel-kinexon
Copy link
Contributor

The SAM0 ADC has an internal register to divide the accumulated results of oversampling the ADC by the amount of samples collected. This value has to be set by the driver and was missing.

Fixes #74607

@github-actions
Copy link

Hello @guenzel-kinexon, and thank you very much for your first pull request to the Zephyr project!
Our Continuous Integration pipeline will execute a series of checks on your Pull Request commit messages and code, and you are expected to address any failures by updating the PR. Please take a look at our commit message guidelines to find out how to format your commit messages, and at our contribution workflow to understand how to update your Pull Request. If you haven't already, please make sure to review the project's Contributor Expectations and update (by amending and force-pushing the commits) your pull request if necessary.
If you are stuck or need help please join us on Discord and ask your question there. Additionally, you can escalate the review when applicable. 😊

@guenzel-kinexon guenzel-kinexon marked this pull request as ready for review August 13, 2024 06:51
@zephyrbot zephyrbot added platform: Microchip SAM Microchip SAM Platform (formerly Atmel SAM) area: ADC Analog-to-Digital Converter (ADC) labels Aug 13, 2024
@decsny
Copy link
Member

decsny commented Aug 19, 2024

please address the compliance failures

@guenzel-kinexon
Copy link
Contributor Author

I'll try to get it done as soon as possible

@guenzel-kinexon guenzel-kinexon force-pushed the adjust-resolution-sam0-adc branch from 46a8e93 to 4015a33 Compare September 5, 2024 08:24
@guenzel-kinexon
Copy link
Contributor Author

please address the compliance failures

done

Copy link
Member

@nandojve nandojve left a comment

Choose a reason for hiding this comment

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

Hi @guenzel-kinexon,

You are doing far more than applying the fix. On Atmel we use 80 chars.
Split you PR doing first the coding formatting that you want.

Do no use clang!

Then add your change.

Copy link
Member

Choose a reason for hiding this comment

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

You shoud create an enum to store the oversampling value and do not add a default on the switch. Compiler will ensure that all valid entries defined on the enum will be on the switch case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The oversampling value is a numeric value from the DT which could ofc. be converted to an enum but I feel it would be an overkill to cast it just for the switch statement.

The default is modeled after how the peripheral works. All oversampling values above 4 have a constant adjustment value. An argument could be made that maybe a

if (oversampling < 4) {} else {} 

would be more suited here.

Copy link
Member

Choose a reason for hiding this comment

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

Hi @guenzel-kinexon ,

Let's forget for a moment the implementation details and focus on the fix.

I was looking on the datasheet and it is not clear to me what you want to achieve. Could you explain in details what you want to achieve?

I see main sections (below) to explain how to use those bits. They should be combined with AVGCTRL.SAMPLENUM[3:0] but you are not changing then.
45.6.2.9 Accumulation
45.6.2.10 Averaging
45.6.2.11 Oversampling and Decimation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ultimately I want to use the peripheral to do hardware-averaging. To make this work the registers for accumulation as well as averaging have to be set.

The accumulation is already done in the driver (line 319, right above the switch statement). What is missing, is the averaging configuration, which I added in this PR.
As described in Accumulation section of the datasheet (chapter 33.6.6 for the SAMD21 datasheet), up to 16 samples the hardware does no automatic division. For 32 samples it already adds a factor but that is expected that the user already set the correct factor in the ADJRES field (as described in the Averaging chapter).

The datasheet is not super clear about this imho. but with trial&error on real hardware I figured it out to be a manual+automatic scheme requiring the driver to set the adjustment factor from 0..4 for averaging up to 16 samples and then let the hardware handle additional division for averaging factors beyond that.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, I see now. Thank you for the fix.

Yes, you can useif (oversampling < 4) {} else {}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@guenzel-kinexon
Copy link
Contributor Author

Hi @guenzel-kinexon,

You are doing far more than applying the fix. On Atmel we use 80 chars. Split you PR doing first the coding formatting that you want.

Do no use clang!

Then add your change.

Doesn't the contribution guidelines enforce a Clang formatting of the code? I am a bit lost which formatting rules to follow here

@pdgendt
Copy link
Contributor

pdgendt commented Oct 1, 2024

Doesn't the contribution guidelines enforce a Clang formatting of the code? I am a bit lost which formatting rules to follow here

No clang format is informative, not blocking.

@kartben
Copy link
Contributor

kartben commented Nov 25, 2024

@guenzel-kinexon thanks for your first PR to the project :) Will you be able to come back to it or do you still have questions regarding next steps?

@guenzel-kinexon
Copy link
Contributor Author

I will try to fix it up asap.

The SAM0 ADC has an internal register to divide the accumulated results
of oversampling the ADC by the amount of samples collected. This value
has to be set by the driver and was missing.

Signed-off-by: Patrick Günzel <[email protected]>
@guenzel-kinexon guenzel-kinexon force-pushed the adjust-resolution-sam0-adc branch from 37e8e3a to 4939f44 Compare January 14, 2025 08:19
@nandojve nandojve added this to the v4.1.0 milestone Jan 14, 2025
@kartben kartben merged commit 66dceba into zephyrproject-rtos:main Jan 15, 2025
24 checks passed
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) platform: Microchip SAM Microchip SAM Platform (formerly Atmel SAM)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SAM adc driver not taking oversampling from the devicetree in the resolution calculation

6 participants