STM32H503CB : use comparator → compilator error

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
trimarco232
Posts: 53
Joined: Wed Jul 12, 2023 11:09 am
Answers: 1

STM32H503CB : use comparator → compilator error

Post by trimarco232 »

Arduino IDE 2.3.6
STM32duino 2.12.0
Hi have add the hal_conf_extra.h file with : #define HAL_COMP_MODULE_ENABLED
the code comes from CubeMX (HAL) :
hcomp1.Instance = COMP1;
hcomp1.Init.InputMinus = COMP_INPUT_MINUS_1_2VREFINT;
hcomp1.Init.InputPlus = COMP_INPUT_PLUS_IO3;
etc.
→ Compilation error: 'hcomp1' was not declared in this scope
had a look , but didn't find why ?
fpiSTM
Posts: 2001
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 109
Location: Le Mans
Contact:

Re: STM32H503CB : use comparator → compilator error

Post by fpiSTM »

Hcomp1 variable is not declared. It is probably defined globally in cubemx project.
trimarco232
Posts: 53
Joined: Wed Jul 12, 2023 11:09 am
Answers: 1

Re: STM32H503CB : use comparator → compilator error

Post by trimarco232 »

thanks , so what have I to do in order to make this thing to work ?
fpiSTM
Posts: 2001
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 109
Location: Le Mans
Contact:

Re: STM32H503CB : use comparator → compilator error

Post by fpiSTM »

Add the variable declaration in your sketch....
trimarco232
Posts: 53
Joined: Wed Jul 12, 2023 11:09 am
Answers: 1

Re: STM32H503CB : use comparator → compilator error

Post by trimarco232 »

well , it wasn't that simple , I had to :
- set manually the COMPEN bit in the APB1LENREN register
- declare hcomp1 as an COMP_HandleTypeDef
here is the code (it compiles and updates the registers , but I didn't check the comparator working yet)

Code: Select all

void comp_setup(void) {                // PB1←in- ;  PB2←in+  ; PB15→out
  RCC->APB1LENR |= RCC_APB1LENR_COMPEN; // comparator clock enable
  COMP_HandleTypeDef hcomp1;           // déclaration expresse
  hcomp1.Instance = COMP1;
  hcomp1.Init.InputMinus = COMP_INPUT_MINUS_1_2VREFINT;
  hcomp1.Init.InputPlus = COMP_INPUT_PLUS_IO3;
  hcomp1.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED;
  hcomp1.Init.Hysteresis = COMP_HYSTERESIS_NONE;
  hcomp1.Init.BlankingSrce = COMP_BLANKINGSRC_NONE;
  hcomp1.Init.Mode = COMP_POWERMODE_HIGHSPEED;
  hcomp1.Init.TriggerMode = COMP_TRIGGERMODE_NONE;
  HAL_COMP_Init(&hcomp1);
  COMP1->CFGR1 |= 1; /// HAL_COMP_Start(&hcomp1); ← ne marche pas
  Serial.print("RCC_APB1LENR:");
  Serial.println(RCC->APB1LENR, BIN);  /// debug : verify
  Serial.print("COMP1_CFGR1:");
  Serial.println(COMP1->CFGR1, BIN);  /// debug : verify

  LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_2, LL_GPIO_MODE_ANALOG);     // PB2 analog = COMP1_INP
  LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_9, LL_GPIO_MODE_ALTERNATE);  // PB15 is alternate
  LL_GPIO_SetAFPin_8_15(GPIOB, LL_GPIO_PIN_9, GPIO_AF12_COMP1);      // PB15 is COMP1_OUT
}
Post Reply

Return to “General discussion”