Skip to content

drivers/periph: reworked the ADC driver#4430

Merged
miri64 merged 31 commits intoRIOT-OS:masterfrom
haukepetersen:opt_periph_adc
Mar 14, 2016
Merged

drivers/periph: reworked the ADC driver#4430
miri64 merged 31 commits intoRIOT-OS:masterfrom
haukepetersen:opt_periph_adc

Conversation

@haukepetersen
Copy link
Copy Markdown
Contributor

This is a proposal for a reworked and simplified ADC driver interface, introducing the concept of logical ADC lines. This should separate the former used logical channels and the CPU internal channels now clearly from each other, leading to less confusion when implementing this interface.

This remodeled interface has exactly the same feature set than the former interface, so only support for non-sequential, one-shot conversions for now.

Enhancements are:

  • plain ADC lines, no dev/chan combinations anymore
  • resolution is now specified for each conversion
  • removed the power_xx functions from interface (made them implicit now)
  • better documentation...

A sample implementation for the stm32f4(discovery) is attached, also the test application is already adapted.

@haukepetersen haukepetersen added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation Platform: ARM Platform: This PR/issue effects ARM-based platforms Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR Impact: major The PR changes a significant part of the code base. It should be reviewed carefully Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care. labels Dec 7, 2015
@haukepetersen haukepetersen added this to the Release 2016.03 milestone Dec 7, 2015
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please note that if you disable the ADC clock you will reset it's configuration to reset state. This means the ADC will be unconfigured next time you call adc_sample. Don't know if that was intended?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

are you sure about this? As far as I know does the configuration stay if you disable the clock

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe you are right and am I confused with the RCC RST register.

@DipSwitch
Copy link
Copy Markdown
Member

Clean solution :) This would also work for the L1, F0, F1 and maybe the F3 (don't have that one)

@haukepetersen
Copy link
Copy Markdown
Contributor Author

backported the gpio_init_analog from #4431.

@DipSwitch, @PeterKietzmann: are you in line with these changes?

@DipSwitch
Copy link
Copy Markdown
Member

DipSwitch commented Dec 9, 2015 via email

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just asking: Wasn't there also APB1ENR for some ports or pins?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

just re-checked: all F4s supported by RIOT currently have the ADC periphs connected to the APB1 bus

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For the F(1|3)'s this is also the case, and probably also for the F0 and L series it's some sort of a business model :p

@PeterKietzmann
Copy link
Copy Markdown
Member

@haukepetersen the change looks reasonable and "similar" to other periph driver optimizations. I did not review or test in detail right now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe you would also like to calculate the maximum allowed clock speed compile time.

/* calculate the maximum allowed ADC clock speed (12Mhz) */
uint32_t devider = 2;
for (; devider < 8; devider += 2) {
    if ((CLOCK_CORECLOCK / devider) <= 12000000) {
        break;
    }
}
devider = ((devider / 2) - 1) << 16;

/* set clock prescaler */
RCC->CCR &= ~(ADC_CCR_ADCPRE);
RCC->CCR |= devider;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hm, I would rather configure the preferred divider in the periph_conf.h to save code size

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since CLOCK_CORECLOCK is a constant, the compiler optimized the for loop away and replaces it with a constant ;)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure about that? Recently I started to doubt the compile a little bit when doing some code size analyses... But there is only one way to find out - I will give this a try. If optimization works as expected, this patch would definitely make sense

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Jep, you were right, added this to the code.

@DipSwitch
Copy link
Copy Markdown
Member

  • gpio_init_analog() is never called.
  • Maybe it would be nice to automatically add channel 16, 17 and 18 to the line configuration:
  1. Temperature
  2. Internal Vref
  3. Battery Charge

To enable 16 and 17 you need to set ADC_CCR_TSVREFE to ADC->CCR and for channel 18 you need to set ADC_CCR_VBATE to ADC->CCR

  • Maybe increase the default sample time a bit to reduce measurement noise. ADC->SMPR[12]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/* check if the linenel is valid */
if (line >= ADC_NUMOF) {
    return -1;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

nope. The paradigm for the periph drivers is: check the device for correctnes in the init function and assume it to be correct in all other functions. This saves quite some instructions and memory...

DipSwitch added a commit to DipSwitch/RIOT that referenced this pull request Dec 13, 2015
@haukepetersen
Copy link
Copy Markdown
Contributor Author

About the internal ADC values and sample time: Let's discuss this in another PR...

@haukepetersen
Copy link
Copy Markdown
Contributor Author

squashed

@haukepetersen haukepetersen added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Mar 14, 2016
@DipSwitch
Copy link
Copy Markdown
Member

Sorry missed that, killed the previous travis build (which is still building) and make it build the new commit :)

@DipSwitch
Copy link
Copy Markdown
Member

Nope, there are still some s MSG commits, I think you want to squash those as well?

@haukepetersen
Copy link
Copy Markdown
Contributor Author

I can't see them, did you update your view?

@DipSwitch
Copy link
Copy Markdown
Member

I did, but probably there was a delay in github, they are gone now and travis builds the latest version. ^_^

@DipSwitch
Copy link
Copy Markdown
Member

Murdock agree's, do we wait for Travis?

@haukepetersen
Copy link
Copy Markdown
Contributor Author

As decided this morning -> if Murdock is good, go...

miri64 added a commit that referenced this pull request Mar 14, 2016
drivers/periph: reworked the ADC driver
@miri64 miri64 merged commit d66625b into RIOT-OS:master Mar 14, 2016
@miri64
Copy link
Copy Markdown
Member

miri64 commented Mar 14, 2016

(I took @DipSwitch's comment as implicit ACK if not, I ack'd it implicitly by merging.)

@haukepetersen haukepetersen deleted the opt_periph_adc branch March 14, 2016 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Impact: major The PR changes a significant part of the code base. It should be reviewed carefully Platform: ARM Platform: This PR/issue effects ARM-based platforms Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care. Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants