Skip to content

Commit 10ab753

Browse files
authored
Merge pull request #210 from sDessens/B-G431B-current-sense-fixes
B g431 b current sense fixes
2 parents b1a5e4e + 60acc40 commit 10ab753

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

src/current_sense/hardware_specific/esp32/esp32_mcu.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ int adc_read_index[2]={0};
6868

6969
// function reading an ADC value and returning the read voltage
7070
float _readADCVoltageLowSide(const int pin, const void* cs_params){
71-
7271
mcpwm_unit_t unit = ((ESP32MCPWMCurrentSenseParams*)cs_params)->mcpwm_unit;
7372
int buffer_index = ((ESP32MCPWMCurrentSenseParams*)cs_params)->buffer_index;
7473
float adc_voltage_conv = ((ESP32MCPWMCurrentSenseParams*)cs_params)->adc_voltage_conv;

src/current_sense/hardware_specific/stm32/b_g431/b_g431_hal.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#include "../../../hardware_api.h"
22
#if defined(ARDUINO_B_G431B_ESC1)
33

4+
#include "communication/SimpleFOCDebug.h"
5+
46
#include "stm32g4xx_hal.h"
57
#include "stm32g4xx_ll_pwr.h"
68
#include "stm32g4xx_hal_adc.h"
79
#include "b_g431_hal.h"
10+
811
// From STM32 cube IDE
912
/**
1013
******************************************************************************
@@ -101,15 +104,15 @@ void MX_ADC1_Init(ADC_HandleTypeDef* hadc1)
101104

102105
if (HAL_ADC_Init(hadc1) != HAL_OK)
103106
{
104-
Error_Handler();
107+
SIMPLEFOC_DEBUG("HAL_ADC_Init failed!");
105108
}
106109

107110
/** Configure the ADC multi-mode
108111
*/
109112
multimode.Mode = ADC_MODE_INDEPENDENT;
110113
if (HAL_ADCEx_MultiModeConfigChannel(hadc1, &multimode) != HAL_OK)
111114
{
112-
Error_Handler();
115+
SIMPLEFOC_DEBUG("HAL_ADCEx_MultiModeConfigChannel failed!");
113116
}
114117
/** Configure Regular Channel
115118
*/
@@ -121,15 +124,15 @@ void MX_ADC1_Init(ADC_HandleTypeDef* hadc1)
121124
sConfig.Offset = 0;
122125
if (HAL_ADC_ConfigChannel(hadc1, &sConfig) != HAL_OK)
123126
{
124-
Error_Handler();
127+
SIMPLEFOC_DEBUG("HAL_ADC_ConfigChannel failed!");
125128
}
126129
/** Configure Regular Channel
127130
*/
128131
sConfig.Channel = ADC_CHANNEL_3; // ADC1_IN3 = PA2 = OP1_OUT
129132
sConfig.Rank = ADC_REGULAR_RANK_2;
130133
if (HAL_ADC_ConfigChannel(hadc1, &sConfig) != HAL_OK)
131134
{
132-
Error_Handler();
135+
SIMPLEFOC_DEBUG("HAL_ADC_ConfigChannel failed!");
133136
}
134137
/* USER CODE BEGIN ADC1_Init 2 */
135138

@@ -173,7 +176,7 @@ void MX_ADC2_Init(ADC_HandleTypeDef* hadc2)
173176

174177
if (HAL_ADC_Init(hadc2) != HAL_OK)
175178
{
176-
Error_Handler();
179+
SIMPLEFOC_DEBUG("HAL_ADC_Init failed!");
177180
}
178181
/** Configure Regular Channel
179182
*/
@@ -185,7 +188,7 @@ void MX_ADC2_Init(ADC_HandleTypeDef* hadc2)
185188
sConfig.Offset = 0;
186189
if (HAL_ADC_ConfigChannel(hadc2, &sConfig) != HAL_OK)
187190
{
188-
Error_Handler();
191+
SIMPLEFOC_DEBUG("HAL_ADC_ConfigChannel failed!");
189192
}
190193
/* USER CODE BEGIN ADC2_Init 2 */
191194

src/current_sense/hardware_specific/stm32/b_g431/b_g431_mcu.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Arduino.h"
77
#include "../stm32_mcu.h"
88
#include "../../../../drivers/hardware_specific/stm32_mcu.h"
9+
#include "communication/SimpleFOCDebug.h"
910

1011
#define _ADC_VOLTAGE 3.3f
1112
#define _ADC_RESOLUTION 4096.0f
@@ -21,8 +22,8 @@ static OPAMP_HandleTypeDef hopamp3;
2122
static DMA_HandleTypeDef hdma_adc1;
2223
static DMA_HandleTypeDef hdma_adc2;
2324

24-
uint16_t adcBuffer1[ADC_BUF_LEN_1] = {0}; // Buffer for store the results of the ADC conversion
25-
uint16_t adcBuffer2[ADC_BUF_LEN_2] = {0}; // Buffer for store the results of the ADC conversion
25+
volatile uint16_t adcBuffer1[ADC_BUF_LEN_1] = {0}; // Buffer for store the results of the ADC conversion
26+
volatile uint16_t adcBuffer2[ADC_BUF_LEN_2] = {0}; // Buffer for store the results of the ADC conversion
2627

2728
// function reading an ADC value and returning the read voltage
2829
// As DMA is being used just return the DMA result
@@ -52,7 +53,7 @@ void _configureOPAMP(OPAMP_HandleTypeDef *hopamp, OPAMP_TypeDef *OPAMPx_Def){
5253
hopamp->Init.UserTrimming = OPAMP_TRIMMING_FACTORY;
5354
if (HAL_OPAMP_Init(hopamp) != HAL_OK)
5455
{
55-
Error_Handler();
56+
SIMPLEFOC_DEBUG("HAL_OPAMP_Init failed!");
5657
}
5758
}
5859
void _configureOPAMPs(OPAMP_HandleTypeDef *OPAMPA, OPAMP_HandleTypeDef *OPAMPB, OPAMP_HandleTypeDef *OPAMPC){
@@ -75,13 +76,24 @@ void MX_DMA1_Init(ADC_HandleTypeDef *hadc, DMA_HandleTypeDef *hdma_adc, DMA_Chan
7576
HAL_DMA_DeInit(hdma_adc);
7677
if (HAL_DMA_Init(hdma_adc) != HAL_OK)
7778
{
78-
Error_Handler();
79+
SIMPLEFOC_DEBUG("HAL_DMA_Init failed!");
7980
}
8081
__HAL_LINKDMA(hadc, DMA_Handle, *hdma_adc);
8182
}
8283

8384
void* _configureADCInline(const void* driver_params, const int pinA,const int pinB,const int pinC){
8485
_UNUSED(driver_params);
86+
_UNUSED(pinA);
87+
_UNUSED(pinB);
88+
_UNUSED(pinC);
89+
90+
SIMPLEFOC_DEBUG("B-G431B does not implement inline current sense. Use low-side current sense instead.");
91+
return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
92+
}
93+
94+
95+
void* _configureADCLowSide(const void* driver_params, const int pinA,const int pinB,const int pinC){
96+
_UNUSED(driver_params);
8597

8698
HAL_Init();
8799
MX_GPIO_Init();
@@ -95,28 +107,21 @@ void* _configureADCInline(const void* driver_params, const int pinA,const int pi
95107

96108
if (HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adcBuffer1, ADC_BUF_LEN_1) != HAL_OK)
97109
{
98-
Error_Handler();
110+
SIMPLEFOC_DEBUG("DMA read init failed");
99111
}
100112
if (HAL_ADC_Start_DMA(&hadc2, (uint32_t*)adcBuffer2, ADC_BUF_LEN_2) != HAL_OK)
101113
{
102-
Error_Handler();
114+
SIMPLEFOC_DEBUG("DMA read init failed");
103115
}
104116

105117
HAL_OPAMP_Start(&hopamp1);
106118
HAL_OPAMP_Start(&hopamp2);
107119
HAL_OPAMP_Start(&hopamp3);
108-
109-
// Check if the ADC DMA is collecting any data.
110-
// If this fails, it likely means timer1 has not started. Verify that your application starts
111-
// the motor pwm (usually BLDCDriver6PWM::init()) before initializing the ADC engine.
112-
_delay(5);
113-
if (adcBuffer1[0] == 0 || adcBuffer1[1] == 0 || adcBuffer2[0] == 0) {
114-
return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
115-
}
116120

117121
Stm32CurrentSenseParams* params = new Stm32CurrentSenseParams {
118122
.pins = { pinA, pinB, pinC },
119-
.adc_voltage_conv = (_ADC_VOLTAGE) / (_ADC_RESOLUTION)
123+
.adc_voltage_conv = (_ADC_VOLTAGE) / (_ADC_RESOLUTION),
124+
.timer_handle = (HardwareTimer *)(HardwareTimer_Handle[get_timer_index(TIM1)]->__this)
120125
};
121126

122127
return params;

0 commit comments

Comments
 (0)