Page 1 of 1

Nucleo G431KB enable OPAMP and COMP

Posted: Fri Oct 16, 2020 12:30 am
by OmegaDryan
Hi guys,

Can someone explain how to enable the opamp in the G4 board?

I found this wiki, and it looks like I need to create a file "variant.h" to enable the OPAMP peripheral. I really don't know where to start here. I know how to do it using CubeMX but I have never done it in Arduino.

https://github.com/stm32duino/wiki/wiki ... ion--150-1

I would like to do the same thing for the COMP peripheral.

Best,

Alessandro

Re: Nucleo G431KB enable OPAMP and COMP

Posted: Fri Oct 16, 2020 7:46 am
by OmegaDryan
I was able to make it work!

In the Arduino make a new tap (create a file named "hal_conf_extra.h") and paste the following code:

Code: Select all

#if !defined(HAL_OPAMP_MODULE_DISABLED)
  #define HAL_OPAMP_MODULE_ENABLED
#else
  #undef HAL_OPAMP_MODULE_ENABLED
#endif
The main Arduino code:

Code: Select all

OPAMP_HandleTypeDef hopamp2;

void setup() {
  // put your setup code here, to run once:


MX_OPAMP2_Init();
HAL_OPAMP_MspInit(&hopamp2);
HAL_OPAMP_Start(&hopamp2);
}

void loop() {
  // put your main code here, to run repeatedly:

}

static void MX_OPAMP2_Init(void)
{

  /* USER CODE BEGIN OPAMP2_Init 0 */

  /* USER CODE END OPAMP2_Init 0 */

  /* USER CODE BEGIN OPAMP2_Init 1 */

  /* USER CODE END OPAMP2_Init 1 */
  hopamp2.Instance = OPAMP2;
  hopamp2.Init.PowerMode = OPAMP_POWERMODE_NORMAL;
  hopamp2.Init.Mode = OPAMP_PGA_MODE;
  hopamp2.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0;
  hopamp2.Init.InternalOutput = DISABLE;
  hopamp2.Init.TimerControlledMuxmode = OPAMP_TIMERCONTROLLEDMUXMODE_DISABLE;
  hopamp2.Init.PgaConnect = OPAMP_PGA_CONNECT_INVERTINGINPUT_NO;
  hopamp2.Init.PgaGain = OPAMP_PGA_GAIN_4_OR_MINUS_3;
  hopamp2.Init.UserTrimming = OPAMP_TRIMMING_FACTORY;
  if (HAL_OPAMP_Init(&hopamp2) != HAL_OK)
  {
    //Error_Handler();
  }
}

void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef* hopamp)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hopamp->Instance==OPAMP2)
  {
  /* USER CODE BEGIN OPAMP2_MspInit 0 */

  /* USER CODE END OPAMP2_MspInit 0 */

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**OPAMP2 GPIO Configuration
    PA6     ------> OPAMP2_VOUT
    PA7     ------> OPAMP2_VINP
    */
    GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  }

}

Re: Nucleo G431KB enable OPAMP and COMP

Posted: Fri Oct 16, 2020 8:11 am
by fpiSTM
Simply add a hal_conf_extra.h file with:

Code: Select all

#define HAL_OPAMP_MODULE_ENABLED
For ref:
https://github.com/stm32duino/wiki/wiki ... definition

Re: Nucleo G431KB enable OPAMP and COMP

Posted: Fri Oct 16, 2020 4:19 pm
by OmegaDryan
fpiSTM wrote: Fri Oct 16, 2020 8:11 am Simply add a hal_conf_extra.h file with:

Code: Select all

#define HAL_OPAMP_MODULE_ENABLED
For ref:
https://github.com/stm32duino/wiki/wiki ... definition
That's only one step in the whole process.

I used CubeMX to generate the code for the main and the msp. Then copied it to Arduino with some small modification. Last but not least you need to start the peripheral otherwise nothing will work!

HAL_OPAMP_Start(&hopamp2);

Even CubeMX doesn't start the peripheral by default, so you need to add that command by yourself!

Re: Nucleo G431KB enable OPAMP and COMP

Posted: Fri Oct 16, 2020 6:22 pm
by fpiSTM
I only comment on HAL OPAMP enable way. As there is no dedicated support for it you have to manage it by yourself like you've made 😉