Optimizing ADC Performance for InlineCurrentSense on STM32 in SimpleFOC

You can probably just use the LowsideCurrentSense class. The inline hardware doesn’t require synchronization with the PWM, but it doesn’t hurt. Inline hall effect current sensors are noisy and need oversampling, but INA240 should be clean enough to use a single sample.

If you want to write your own code anyway, read the ADC section of RM0368 https://www.st.com/resource/en/reference_manual/rm0368-stm32f401xbc-and-stm32f401xde-advanced-armbased-32bit-mcus-stmicroelectronics.pdf

And DMA as well if you want oversampling, since F401 doesn’t have hardware oversampling (have the DMA write into a circular buffer of however many samples you want, and make a custom _readADCVoltageInline that adds up the values in it).

F401’s ADC and DMA are much less complicated than G4’s, so it’s a good one to learn on.

Personally I find it easier to work with the hardware registers directly rather than the LL/HAL libraries. The register definitions are in …\packages\STMicroelectronics\hardware\stm32\2.3.0\system\Drivers\CMSIS\Device\ST\STM32F4xx\Include

At startup, you need to enable the peripherals in the RCC registers, RCC_APB2ENR |= RCC_APB2ENR_ADC1EN; and RCC_AHB1ENR |= RCC_AHB1ENR_DMA2EN; if using DMA. Or call whatever LL/HAL functions do the same thing (though I was not able to find them easily, hence why I prefer registers).