Thanks for posting!
I'm not sure that this is what you mean, though:
regs->SMPR1 = ADC_SMPR1_SMP17; // sample rate for VREFINT ADC channel
While this code seems to work if you're only reading V_REFINT, that's not what the ADC_SMPRx_SMPn
bits are for. (You can read more about register bit definitions in the libmaple overview.)
The ADC_SMPR1_SMP17
register bit definition is for extracting or clearing the SMP17 bits in ADC_SMPR1; it's not meant to be used as a value to set.
Here's its definition:
#define ADC_SMPR1_SMP17 (0x7 << 21)
Looking at ST's docs for ADC_SMPR1 (RM0008 section 11.12.4), setting regs->SMPR1
to ADC_SMPR1_SMP17
has the following effects:
1. Sets the sample time for channel 17 (the V_REFINT channel) to 0x7, or 239.5 ADC cycles (the longest available).
2. Sets the ADC sample time for channels 10 through 16 to 0x0, or 1.5 ADC cycles (the shortest available).
The second effect in particular is worrisome. It will speed up ADC conversion on channels 10-16 greatly, but drastically lowers the acceptable maximum impedance of your ADC input (if the impedance is too high for the sample time, you'll get inaccurate readings). We deliberately set the ADC sample time much higher during init()
to allow for high impedance inputs:
https://github.com/leaflabs/libmaple/blob/v0.0.12/wirish/boards.cpp#L122
If you really want to set the channel 17 sample time to 239.5 ADC cycles, you should instead use a read-modify-write sequence, like this:
uint32 smpr = regs->SMPR1; // read SMPR1 register's current value
smpr &= ~ADC_SMPR1_SMP17; // clear the SMP17 bits, but leave the other bits alone
smpr |= (0x7 << 21); // set the SMP17 bits to 0x7, meaning 239.5 ADC cycles
regs->SMPR1 = smpr; // write the modified value back to SMPR1
You can also globally set the sample time for every channel on ADC1 to 239.5 ADC cycles with adc_set_sample_rate(ADC1, ADC_SMPR_239_5)
. The Wirish default is 55.5 cycles, or ADC_SMPR_55_5
. The available sample rates for this function are here (heads up: they're different on Maple 2):
http://leaflabs.com/docs/libmaple/api/adc.html#project0adc_8h_1ace843f98737343a7f8d1ef7ac8f9b519