1#include "../../../hardware_api.h"
4#include "../../../../common/foc_utils.h"
5#include "../../../../drivers/hardware_api.h"
6#include "../../../../drivers/hardware_specific/stm32/stm32_mcu.h"
7#include "../../../hardware_api.h"
8#include "../stm32_mcu.h"
9#include "../stm32_adc_utils.h"
14#define _ADC_VOLTAGE 3.3f
15#define _ADC_RESOLUTION 4096.0f
19uint32_t adc_val[5][4]={0};
21#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
22#define USE_ADC_INTERRUPT 1
24#define USE_ADC_INTERRUPT 0
28Stm32AdcInterruptConfig adc_interrupt_config[5] = {
29 {0, 0, USE_ADC_INTERRUPT},
30 {0, 0, USE_ADC_INTERRUPT},
31 {0, 0, USE_ADC_INTERRUPT},
32 {0, 0, USE_ADC_INTERRUPT},
33 {0, 0, USE_ADC_INTERRUPT}
39 Stm32CurrentSenseParams*
cs_params=
new Stm32CurrentSenseParams {
41 .adc_voltage_conv = (_ADC_VOLTAGE) / (_ADC_RESOLUTION)
50 STM32DriverParams* driver_params = (STM32DriverParams*)_driver_params;
51 Stm32CurrentSenseParams*
cs_params = (Stm32CurrentSenseParams*)_cs_params;
57 stm32_pause(driver_params);
60 int adc_index = _adcToIndex(
cs_params->adc_handle);
61 bool tim_interrupt = _initTimerInterruptDownsampling(
cs_params, driver_params, adc_interrupt_config[adc_index]);
64 SIMPLEFOC_DEBUG(
"STM32-CS: timer has no repetition counter, ADC interrupt has to be used");
69 LL_TIM_SetTriggerOutput(
cs_params->timer_handle->Instance, LL_TIM_TRGO_UPDATE);
72 if(HAL_ADCEx_Calibration_Start(
cs_params->adc_handle, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED) != HAL_OK){
73 #ifdef SIMPLEFOC_STM32_DEBUG
80 if(adc_interrupt_config[adc_index].use_adc_interrupt){
82 if(
cs_params->adc_handle->Instance == ADC1){
84 HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
85 HAL_NVIC_EnableIRQ(ADC_IRQn);
88 else if(
cs_params->adc_handle->Instance == ADC2) {
90 HAL_NVIC_SetPriority(ADC3_IRQn, 0, 0);
91 HAL_NVIC_EnableIRQ(ADC3_IRQn);
95 else if(
cs_params->adc_handle->Instance == ADC3) {
97 HAL_NVIC_SetPriority(ADC3_IRQn, 0, 0);
98 HAL_NVIC_EnableIRQ(ADC3_IRQn);
101 if(HAL_ADCEx_InjectedStart_IT(
cs_params->adc_handle) != HAL_OK){
102 #ifdef SIMPLEFOC_STM32_DEBUG
103 SIMPLEFOC_DEBUG(
"STM32-CS: ERR: cannot start injected channels in interrupt mode!");
108 if(HAL_ADCEx_InjectedStart(
cs_params->adc_handle) != HAL_OK){
109 #ifdef SIMPLEFOC_STM32_DEBUG
118 stm32_resume(driver_params);
129 uint8_t adc_index = (uint8_t)_adcToIndex(((Stm32CurrentSenseParams*)
cs_params)->adc_handle);
130 return _readADCInjectedChannelVoltage(pin, (
void*)
cs_params, adc_interrupt_config[adc_index], adc_val[adc_index]);
134 void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle){
135 uint8_t adc_index = (uint8_t)_adcToIndex(AdcHandle);
136 _handleInjectedConvCpltCallback(AdcHandle, adc_interrupt_config[adc_index], adc_val[adc_index]);
#define SIMPLEFOC_DEBUG(msg,...)
void * _driverSyncLowSide(void *driver_params, void *cs_params)
#define SIMPLEFOC_CURRENT_SENSE_INIT_FAILED
void * _configureADCLowSide(const void *driver_params, const int pinA, const int pinB, const int pinC=NOT_SET)
float _readADCVoltageLowSide(const int pinA, const void *cs_params)
const int const int const int pinC