STM32F0x1 comparator (COMP) and ADC on same pin simultaneously?
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.
The most relevant setup I could found is the following, from Using STM32F05xx analog comparators in application cases, (AN4112), page 4:
It says:
Figure 1 shows how to connect a sensor output (temperature sensor, pressure sensor, pyroelectric infrared detector, photodiode sensor) to an STM32F05xx device in an analog voltage monitoring application using comparator 2 (COMP2). COMP2 monitors the analog voltage in Stop mode while the ADC measures it in Run mode.
Here the ADC and the COMP work alternately but I think both are configured at the same time. If we continue in the document there is a block diagram about the COMP configuration.
Based on this image I think that the ADC and the COMP shares the same ADC Channel the only difference is the number of enabled Analog Threshold Sources.
The text under the Figure 5 in the question states that the
comparator input is an ADC channel
possibly the same as ADC_IN1.
The purpose of this whole configuration is to save power, the COMP triggers state changes of the MCU (STOP <--> RUN). So when the input is below a certain threshold the MCU and ADC are shut down and only the comparator works, and when the input is above the threshold the MCU and ADC wakes up the ADC measures the input voltage. But if the input falls below the threshold again the COMP sends the MCU to STOP state.
It is one way of using them together, I hope this would move the question forward.
I think you should ask on STM forum. There is also (STM32F1) UART RX and TIMx on the same pin, both inputs, but you can't use them together, like detecting end of transmission timer interrupt. Instead a different TIMx pin has to be connected in parallel to have the feature UART RX and timer interrupt. I guess just one function can be active at the same time.
Try to download MxCube and see what configuration can be built.