I’m able to get some current readings.
This code was needed to enable the ADC clock as mentioned earlier.
__HAL_RCC_ADC12_CLK_ENABLE();
The following code enables the ADC for usage with an asynchronous clock.
// RCC_ADCCLKSOURCE_PLL enable: (optional: if asynchronous clock selected)
RCC_PeriphCLKInitTypeDef PeriphClkInit;
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC12;
PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_PLL;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
The following piece of DMA initialization code was missing (similarly for DMA1_Channel1)
static DMA_HandleTypeDef hdma_adc2;
/* Config DMA *****************************************************************/
hdma_adc2.Instance = DMA1_Channel2;
hdma_adc2.Init.Request = DMA_REQUEST_ADC2;
hdma_adc2.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_adc2.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_adc2.Init.MemInc = DMA_MINC_ENABLE;
hdma_adc2.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_adc2.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_adc2.Init.Mode = DMA_CIRCULAR;
hdma_adc2.Init.Priority = DMA_PRIORITY_LOW;
HAL_DMA_DeInit(&hdma_adc2);
HAL_DMA_Init(&hdma_adc2);
__HAL_LINKDMA(&hadc2, DMA_Handle, hdma_adc2);
The DMA interrupt handler needs to be defined within extern “C” blocks. (similarly for DMA1_Channel1)
extern "C" {
void DMA1_Channel2_IRQHandler(void) {
HAL_DMA_IRQHandler(&hdma_adc2);
}
}
Only the following define is needed for compilation
#define HAL_OPAMP_MODULE_ENABLED
With these adjustments and the code from:
src/current_sense/hardware_specific/stm32g4_hal.h
src/current_sense/hardware_specific/stm32g4_hal.cpp
src/current_sense/hardware_specific/stm32g4_mcu.cpp
src/drivers/hardware_specific/stm32_mcu.cpp
The readings themselves appear to be wrong, but that’s something for future work. At least I get some readings that are correlated to the motor voltage.
Where did you get the value of 64/7 located in examples/hardware_specific_examples/B-G431B-ESC1/B-G431B-ESC1_current_control/B-G431B-ESC1_current_control.ino? Using this scaling seems to result in the correct current readings, but I’m unable to find any documentation referring to this value.