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

adc: add support for channels 16 and 17 in ADC1 #67

Merged
merged 5 commits into from
Jun 11, 2019
Merged

adc: add support for channels 16 and 17 in ADC1 #67

merged 5 commits into from
Jun 11, 2019

Conversation

geomatsi
Copy link
Contributor

Add reading methods to read from ADC1 special channels:

  • channel 16: temperature sensor
  • channel 17: internal reference voltage

Note that these methods should be implemented only for ADC1 block
since in ADC2 and ADC3 those channels are connected to Vss.

Signed-off-by: Sergey Matyukevich geomatsi@gmail.com

@TheZoq2
Copy link
Member

TheZoq2 commented Jun 2, 2019

Thanks for the PR!

The code and docs look very nice, though the docs mention that "The recommended sampling time for the temperature sensor is 17.1 μs" and as far as I can tell, that isn't done here. With that addressed, i'd say this can be merged

@geomatsi
Copy link
Contributor Author

geomatsi commented Jun 3, 2019

Hi @TheZoq2

Good point. However current ADC implementation in this crate has no knowledge about its clock settings. So one possible option would be to ask user to set proper sampling time from the application, where ADC clock settings are known.

On the other hand I agree that it is not convenient and it make a lot of sense to configure and then restore sampling time in read_temp function. I tried to use maximum sampling time (239.5 cycles) as a conservative setting that could fit all the ADC frequencies. But it didn't work for some reason. So I will take a further look and push more commits as soon as I figure out the solution.

Regards,
Sergey

@therealprof
Copy link
Member

@geomatsi The functions can take the current clocks a parameter.

@geomatsi
Copy link
Contributor Author

geomatsi commented Jun 5, 2019

@therealprof Sure, but I would rather suggest to add current clock parameter into ADC constructor. IIUC we should instantiate ADC after RCC clock configuration is frozen, so it is possible to pass current clock value only once.

Add methods to read from ADC1 special channels:
- channel 16: temperature sensor
- channel 17: internal reference voltage

Note that these methods should be implemented only for ADC1 block
since in ADC2 and ADC3 those channels are connected to Vss.

Signed-off-by: Sergey Matyukevich <geomatsi@gmail.com>
Recommended ADC sampling time for temperature sensor is 17.1 usec.
So use approximate sampling time settings to support the
whole range of possible ADC frequencies.

Signed-off-by: Sergey Matyukevich <geomatsi@gmail.com>
Update existing ADC example according to ADC API changes.
Add new ADC example to read ambient temperature using ADC1 CH16.

Signed-off-by: Sergey Matyukevich <geomatsi@gmail.com>
@geomatsi
Copy link
Contributor Author

geomatsi commented Jun 9, 2019

Hi @TheZoq2 , @therealprof , and all,
PR has been updated: more changes (see review comments) and more examples. Could you please take a look ?

Regards,
Sergey

@TheZoq2
Copy link
Member

TheZoq2 commented Jun 9, 2019

Looks good to me, but I think I prefer having adc not take clocks as that would make this a non-breaking change which would be nice. Or do we anticipate adc requiring clock data in more situations?

@therealprof
Copy link
Member

Taking clocks looks fine for me.

@TheZoq2
Copy link
Member

TheZoq2 commented Jun 9, 2019

In that case, i'm fine with merging this, unless you have any other concerns @therealprof

Copy link
Member

@TheZoq2 TheZoq2 left a comment

Choose a reason for hiding this comment

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

On second thought, I do have a few comments about the example

}

#[exception]
fn HardFault(ef: &ExceptionFrame) -> ! {
Copy link
Member

Choose a reason for hiding this comment

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

Most examples use panic_halt rather than semihosting, which removes the need for defining these exceptions. Is there a specific need for them here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. I will remove them.

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

//let clocks = rcc.cfgr.use_hse(8.mhz()).sysclk(36.mhz()).pclk1(12.mhz()).adcclk(6.mhz()).freeze(&mut flash.acr);
//let clocks = rcc.cfgr.use_hse(8.mhz()).sysclk(32.mhz()).pclk1(16.mhz()).adcclk(8.mhz()).freeze(&mut flash.acr);
//let clocks = rcc.cfgr.use_hse(8.mhz()).sysclk(40.mhz()).pclk1(20.mhz()).adcclk(10.mhz()).freeze(&mut flash.acr);
//let clocks = rcc.cfgr.use_hse(8.mhz()).sysclk(48.mhz()).pclk1(24.mhz()).adcclk(12.mhz()).freeze(&mut flash.acr);
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 comments here useful?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, these are just several examples of clock settings that can be done with external oscillator. My main purpose was to demonstrate more or less the whole range of ADC frequencies. Besides, I think the example for use_hse is helpful regardless of adc usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed those extra examples in comments. One use_hse example should be enough.

Copy link
Member

Choose a reason for hiding this comment

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

I think it may be useful to document the relationships between the possible frequencies for the different registers in case there are some restrictions to the accepted values. This would clarify what you have to do if you want to set the ADC clock to 12 MHz, for example.

Going a bit further, I could also think it would be nice to document these value relationships in general here somewhere as a shortcut to some paragraph about prescaler config in the datasheet.

Copy link
Member

Choose a reason for hiding this comment

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

I agree, but i'm not sure if this example is the right way to do this. I'd say it should either be in the documentation or a separate example.

Signed-off-by: Sergey Matyukevich <geomatsi@gmail.com>
Signed-off-by: Sergey Matyukevich <geomatsi@gmail.com>
@TheZoq2
Copy link
Member

TheZoq2 commented Jun 11, 2019

Looks good to merge, thanks for yet another great PR :)

@TheZoq2 TheZoq2 merged commit df8a6e5 into stm32-rs:master Jun 11, 2019
@geomatsi geomatsi deleted the adc-vref branch June 11, 2019 14:00
@TheZoq2 TheZoq2 mentioned this pull request Jul 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants