Electronic – STM32F0x1 comparator (COMP) and ADC on same pin simultaneously

stm32stm32f0

I am putting together a project that takes advantage of the comparator peripheral on the STM32F051 microcontroller. I have read in the documentation that for the comparator to work the input pin needs to be configured for analog… which is the same setup if you were using the pin as an ADC input.

Can I use a single pin, say PA1, as input and configure it as both a window comparator input and as an ADC input on ADC_IN1?

Are there any gotchas with this configuration such as additional noise that I should consider?

Datasheet (p 19)

Reference Manual (p 295)

An interesting reference, suggesting that this should be possible:
enter image description here
Taken from AN4232: Getting started with analog comparators for STM32F3 series

Best Answer

This works, you will get numbers back, as well as interrupts. I have checked it out, download from.

The accuracy and glitchiness are undetermined, as yet. But is well described in the Appnotes as indicated by @Bence Kaulics in this thread(see results.txt for additional AppNotes)

I believe it will be stable for the following reasons.

a) The circuit diagram in RM0091 shows a direct connection from comparator to pin. i.e. it is indicated that the comparator cannot be disconnected with a switch or MUX, and will thus work as soon as the pin is assigned 'analog in'.

b) The comparator is NOT dependent on a 'Clock' to enable it (it is alow power device).

c) ST would not be able to make this level of change without assigning a new part number.


The good news is that one can continue to use the comparator for high-speed events such as over current and emergency stop etc whilst still using the ADC analog window events for other software control.

This also works with the DAC, i.e. you can set the whole thing up internally with the DAC or externally with the DAC and a few resistors.

Just looking at the circuit you will be using the ADC in parallel to the comparator, essentially an amplifier. As we know these are very high impedance large gain devices. Glitches are not our friend.

Now we are going to bang a discharged cap onto that pin every so often to feed the ADC.

As we all know, we need to oversample the ADC and preferably reject the first reading after a mux switch event to eliminate skewing from and to adjacent channels.

The ADC on the STM have input impedance somewhat < 50K // 5pF dependent on how they are used. (DM00039193.pdf pg 76ff)

Table 53 gives 400->50KOhm, which is what I found it to be some time back when I calibrated my F373 ADC.

Pg 79 shows the ADC circuitry.

Pg 82 gives a short description of comparator pin, read in concert with the general description of analogue pins above (pg73ff)

Put that in parallel with your comparator input and ADC MUX and model it in spice. Remember to charge the ADC cap to a random voltage regularly.

Whatever else happens in the circuit and software you will get pretty reasonable glitches on your comparator input. BAD IDEA, even if you connect the pin to a low impedance follower and decoupling cap (on the moving line of a comparator input????).

The cap used by the ADC is the killer. Hopefully future devices will sample using a internal followers/isolators on both the ADC and the comparator. These may already be there as undocumented features(unlikely due to the mux crosstalk).

Like me, I think we get so involved with the digital side of things that when we move to analog and hybrid we forget the very basics.

How to measure small currents using current to voltage converter? is a discussion I had with someone else yesterday. I knew the answer cause I fell foul of it myself. Even at the best 3V into 50K we get an AVO of 16K/volt. When last did I use such an AVO / multimeter?

All that being said, a look at the circuitry of the F373 shows that ST and ARM seem to have intended to be able to get workable results from both comparator and adc being used simultaneously in a mixed device. The addition of opamps in the 150 and 300 series gives a clue as to the impedance isolation requirements.

I'm sure that someone more clever than I will be able to re-engineer the environment for which these internal interconnections were designed. I would think automotive or HVAC .... inverters and FOC. The FOC library may provide valuable insight.

Unless you are building a high speed, highly accurate instrument, such usage may be sufficiently stable to be used practically (within the caveats above). It certainly will save a lot of external circuitry. Rigorous testing at high speeds is probably best left as an exercise for the student (Tempt me to study).

Solution here.

Looks like the HAL ADC library is a bit broken for multi channel DMA. I have posted on the STM website to get an answer. Workarounds :-

a) IRQ both ADCs

b) Poll both ADC

c) DMA one channel and poll the other

d) Initialise the low level registers manually

This is yet another caveat to the solution where both comparators are used, till a better solution is found.