Skip to main content
Associate II
June 24, 2026
Solved

Comparateor inputs selection in CubeMx2

  • June 24, 2026
  • 5 replies
  • 122 views

Split from this post: 

Dear ST Technical Moderator,
(mEALLEm)

many thanks for your prompt answer !

You actually solved my problem by using CubeMX2 tool,
where one can assign selected peripheral pins (in my case COMP2 two inputs and one output)
to actual package IO pins.

As far as I see, CubeMX2 tool “offered” to make assignment of 

Comparator Plus input with one of four IO pins
COMP2_INP:
IO1: PA3
IO2: PB15
IO3: PC0
(I want to use PA3) !

and 
Comparator Minus input with one of four IO pins
COMP2_INM:
IO1: PC1
IO2: PC5
IO3: PA2
(I want to use PA2) !

it is stated in my text, in the very beginning !
... quote
In new design,  where planned MCU is STM32C542CCT6 device, (LQFP48 package),
I plan to use internal Comparator COMP2.
...
PA7 pin as Comparator2 Output
and pin PA2 as Comparator2 -INPut, 
and pin PA3 as Comparator2 +INPut.
... unquote


Well, 
the only problem is that I did not install SW development environment yet,
so,
 I did not use CubeMX2 tool,
 I was just reading the documentation, RefManual and device DS,
and I did not find any sentence related to IO correspondence you showed to me !

Besides, it seems to me that you also did not find such information in docs, as you mentioned in your reply
...
Meanwhile, I don’t see the I/O correspondence with COMPx-INy in the documentation.
...
??

 

However,
problem of STM32C5 MCU COMP2_IN Pins assignment
can be easily solved using CubeMX2,
but, there is not any specific instruction how to do that in docs !

Finally,
could you send me the code snippet as generated in CubeMX2
to see how the registers are filled to get the desired assignment ?


Once more,
Many thanks for your help !


Blažimir Miše,
BUCK
 

Best answer by T_Hamdi

Hello ​@blaziBUCK 

The Additional functions are usually analog connections. The GPIO pin is configured in analog mode, and the peripheral’s own register selects the connection.

For STM32C542, the detailed mapping is:

PA2 = COMP2_INM3   -> COMP2 negative input, IO3
PA3 = COMP2_INP1 -> COMP2 positive input, IO1

The datasheet table may show COMP2_INP and COMP2_INM without the selector number.

Manual configuration example :

/* 1. Enable GPIOA and COMP1/COMP2 clocks */
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
RCC->APB1HENR |= RCC_APB1HENR_COMP12EN;

/* 2. Configure PA2 and PA3 as analog inputs */
GPIOA->MODER &= ~((3UL << 4U) | (3UL << 6U));
GPIOA->MODER |= ((3UL << 4U) | (3UL << 6U));

GPIOA->PUPDR &= ~((3UL << 4U) | (3UL << 6U));

/* 3. Select comparator inputs in COMP2_CFGR1 */

/* PA3 = COMP2_INP1 = positive input IO1 */
COMP2->CFGR1 &= ~COMP_CFGR1_INPSEL;

/* PA2 = COMP2_INM3 = negative input IO3 */
COMP2->CFGR1 &= ~COMP_CFGR1_INMSEL;
COMP2->CFGR1 |= (7UL << COMP_CFGR1_INMSEL_Pos);

/* 4. Enable COMP2 */
COMP2->CFGR1 |= COMP_CFGR1_EN;

Registers used from RM0522

RCC_AHB2ENR.GPIOAEN :      Enable GPIOA clock
RCC_APB1HENR.COMP12EN :   Enable COMP1/COMP2 clock

GPIOA_MODER.MODE2:       PA2 analog mode
GPIOA_MODER.MODE3:       PA3 analog mode


COMP2_CFGR1.INMSEL  :     Select negative comparator input
COMP2_CFGR1.INPSEL  :     Select positive comparator input
COMP2_CFGR1.EN      :     Enable COMP2

Thus, PA2 is assigned to the COMP2 negative input through:

COMP2_CFGR1.INMSEL = IO3

and PA3 is assigned to the COMP2 positive input through:

COMP2_CFGR1.INPSEL = IO1

 

with regards,

Hamdi

5 replies

blaziBUCKAuthor
Associate II
June 25, 2026

Dear all,

if the problem of STM32C5 MCU COMP2_IN Pins assignment
can be easily/correctly solved using CubeMX2 ONLY  (sic),
what is the purpose of reading 2500+ page Document such is Reference Manual ?

(btw, there is not INFO about that)

 

Also,

where I could find some CODE examples of setting/assignments related to

“correspondence” between Comparators Pins (COMP2_INP, COMP2_INM)

and PORT pins (PA2, PA3) properly ?

 

Many thanks for your help !

T_Hamdi
ST Employee
August 13, 2026

Hello ​@blaziBUCK 

For the pin assignment, refer to the datasheet of the SoC STM32C542xx, not the reference manual. Go to Section 4: “Pinouts/ballouts, pin description, and alternate functions.” In this section, you can find the functions available for each pin. For example, for pin PA3, one of the possible functions is COMP1_OUT.

 about CODE examples You can refer to the examples delivered with the STM32CubeC5 package for additional guidance.

STM32CubeC5/examples/hal/comp/vref_it at main · STMicroelectronics/STM32CubeC5

 

with regards,

Hamdi

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question.Hamdi Teyeb"
blaziBUCKAuthor
Associate II
August 13, 2026

Dear Hamdi,

Many thanks for your reply trying to help me with Pins Assignment for the Comparator Inputs, but, You only confirmed what I mentioned in my original post!

Namely, let me remind that I plan to use Comparator2 in such way :

PA7 pin as Comparator2 Output
and pin PA2 as Comparator2 -INPut, 
and pin PA3 as Comparator2 +INPut.

In short,
it is not a problem to "assign" Comparator Output, COMP2_OUT to pin PA7, since I will declare AF14 = COMP2_OUT in appropriate GPIOA_AFRL register, see page 352 in RM0522 Reference manual, (RM)

but,

how to assign Comparator Inputs mentioned in “Additional Functions” in rightmost column in Table 12 in DS ?

In other words, how to assign, for example, pin PA2 to Comparator2 INPUT Minus ?

Is there some Register to do so ?

or the only way is to do that “assignment” in Software using CubeMX2 tool ?

(I did not find such statements in Code Examples you pointed...)

Best regards,

Blazi

T_Hamdi
T_HamdiBest answer
ST Employee
September 22, 2026

Hello ​@blaziBUCK 

The Additional functions are usually analog connections. The GPIO pin is configured in analog mode, and the peripheral’s own register selects the connection.

For STM32C542, the detailed mapping is:

PA2 = COMP2_INM3   -> COMP2 negative input, IO3
PA3 = COMP2_INP1 -> COMP2 positive input, IO1

The datasheet table may show COMP2_INP and COMP2_INM without the selector number.

Manual configuration example :

/* 1. Enable GPIOA and COMP1/COMP2 clocks */
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
RCC->APB1HENR |= RCC_APB1HENR_COMP12EN;

/* 2. Configure PA2 and PA3 as analog inputs */
GPIOA->MODER &= ~((3UL << 4U) | (3UL << 6U));
GPIOA->MODER |= ((3UL << 4U) | (3UL << 6U));

GPIOA->PUPDR &= ~((3UL << 4U) | (3UL << 6U));

/* 3. Select comparator inputs in COMP2_CFGR1 */

/* PA3 = COMP2_INP1 = positive input IO1 */
COMP2->CFGR1 &= ~COMP_CFGR1_INPSEL;

/* PA2 = COMP2_INM3 = negative input IO3 */
COMP2->CFGR1 &= ~COMP_CFGR1_INMSEL;
COMP2->CFGR1 |= (7UL << COMP_CFGR1_INMSEL_Pos);

/* 4. Enable COMP2 */
COMP2->CFGR1 |= COMP_CFGR1_EN;

Registers used from RM0522

RCC_AHB2ENR.GPIOAEN :      Enable GPIOA clock
RCC_APB1HENR.COMP12EN :   Enable COMP1/COMP2 clock

GPIOA_MODER.MODE2:       PA2 analog mode
GPIOA_MODER.MODE3:       PA3 analog mode


COMP2_CFGR1.INMSEL  :     Select negative comparator input
COMP2_CFGR1.INPSEL  :     Select positive comparator input
COMP2_CFGR1.EN      :     Enable COMP2

Thus, PA2 is assigned to the COMP2 negative input through:

COMP2_CFGR1.INMSEL = IO3

and PA3 is assigned to the COMP2 positive input through:

COMP2_CFGR1.INPSEL = IO1

 

with regards,

Hamdi

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question.Hamdi Teyeb"
blaziBUCKAuthor
Associate II
September 22, 2026

many thanks Hamdi !