SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
stm32_adc_utils.h
Go to the documentation of this file.
1#ifndef STM32_ADC_UTILS_HAL
2#define STM32_ADC_UTILS_HAL
3
4#include "Arduino.h"
5
6#if defined(_STM32_DEF_)
7
8#define _TRGO_NOT_AVAILABLE 12345
9
10
11#include "../../../common/foc_utils.h"
12#include "../../../communication/SimpleFOCDebug.h"
13#include "../../../drivers/hardware_specific/stm32/stm32_mcu.h"
14#include "stm32_mcu.h"
15
16/* Exported Functions */
17/**
18 * @brief Return ADC HAL channel linked to a PinName
19 * @param pin: PinName
20 * @param adc: ADC_TypeDef a pointer to the ADC handle
21 * @retval Valid HAL channel
22 */
23uint32_t _getADCChannel(PinName pin, ADC_TypeDef* adc = NP);
24uint32_t _getADCInjectedRank(uint8_t ind);
25
26// timer to injected TRGO - architecure specific
27uint32_t _timerToInjectedTRGO(TIM_HandleTypeDef* timer);
28
29// timer to regular TRGO - architecure specific
30uint32_t _timerToRegularTRGO(TIM_HandleTypeDef* timer);
31
32// function returning index of the ADC instance
33int _adcToIndex(ADC_HandleTypeDef *AdcHandle);
34int _adcToIndex(ADC_TypeDef *AdcHandle);
35
36// functions helping to find the best ADC channel
37int _findIndexOfFirstPinMapADCEntry(int pin);
38int _findIndexOfLastPinMapADCEntry(int pin);
39ADC_TypeDef* _findBestADCForInjectedPins(int num_pins, int pins[], ADC_HandleTypeDef adc_handles[]);
40ADC_TypeDef* _findBestADCForRegularPin(int pin, ADC_HandleTypeDef adc_handles[]);
41
42// Structure to hold ADC interrupt configuration per ADC instance
43struct Stm32AdcInterruptConfig {
44 bool needs_downsample = 0;
45 uint8_t tim_downsample = 0;
46 bool use_adc_interrupt = 0;
47};
48
49// returns 0 if no interrupt is needed, 1 if interrupt is needed
50uint32_t _initTimerInterruptDownsampling(Stm32CurrentSenseParams* cs_params, STM32DriverParams* driver_params, Stm32AdcInterruptConfig& adc_interrupt_config);
51// returns 0 if no downsampling is needed, 1 if downsampling is needed, 2 if error
52uint8_t _handleInjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle, Stm32AdcInterruptConfig& adc_interrupt_config, uint32_t adc_val[4]);
53// reads the ADC injected voltage for the given pin
54// returns the voltage
55// if the pin is not found in the current sense parameters, returns 0
56float _readADCInjectedChannelVoltage(int pin, void* cs_params, Stm32AdcInterruptConfig& adc_interrupt_config, uint32_t adc_val[4]);
57
58
59#endif
60#endif