6
6
#include " Arduino.h"
7
7
#include " ../stm32_mcu.h"
8
8
#include " ../../../../drivers/hardware_specific/stm32_mcu.h"
9
+ #include " communication/SimpleFOCDebug.h"
9
10
10
11
#define _ADC_VOLTAGE 3 .3f
11
12
#define _ADC_RESOLUTION 4096 .0f
@@ -21,8 +22,8 @@ static OPAMP_HandleTypeDef hopamp3;
21
22
static DMA_HandleTypeDef hdma_adc1;
22
23
static DMA_HandleTypeDef hdma_adc2;
23
24
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
26
27
27
28
// function reading an ADC value and returning the read voltage
28
29
// As DMA is being used just return the DMA result
@@ -52,7 +53,7 @@ void _configureOPAMP(OPAMP_HandleTypeDef *hopamp, OPAMP_TypeDef *OPAMPx_Def){
52
53
hopamp->Init .UserTrimming = OPAMP_TRIMMING_FACTORY;
53
54
if (HAL_OPAMP_Init (hopamp) != HAL_OK)
54
55
{
55
- Error_Handler ( );
56
+ SIMPLEFOC_DEBUG ( " HAL_OPAMP_Init failed! " );
56
57
}
57
58
}
58
59
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
75
76
HAL_DMA_DeInit (hdma_adc);
76
77
if (HAL_DMA_Init (hdma_adc) != HAL_OK)
77
78
{
78
- Error_Handler ( );
79
+ SIMPLEFOC_DEBUG ( " HAL_DMA_Init failed! " );
79
80
}
80
81
__HAL_LINKDMA (hadc, DMA_Handle, *hdma_adc);
81
82
}
82
83
83
84
void * _configureADCInline (const void * driver_params, const int pinA,const int pinB,const int pinC){
84
85
_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);
85
97
86
98
HAL_Init ();
87
99
MX_GPIO_Init ();
@@ -95,28 +107,21 @@ void* _configureADCInline(const void* driver_params, const int pinA,const int pi
95
107
96
108
if (HAL_ADC_Start_DMA (&hadc1, (uint32_t *)adcBuffer1, ADC_BUF_LEN_1) != HAL_OK)
97
109
{
98
- Error_Handler ( );
110
+ SIMPLEFOC_DEBUG ( " DMA read init failed " );
99
111
}
100
112
if (HAL_ADC_Start_DMA (&hadc2, (uint32_t *)adcBuffer2, ADC_BUF_LEN_2) != HAL_OK)
101
113
{
102
- Error_Handler ( );
114
+ SIMPLEFOC_DEBUG ( " DMA read init failed " );
103
115
}
104
116
105
117
HAL_OPAMP_Start (&hopamp1);
106
118
HAL_OPAMP_Start (&hopamp2);
107
119
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
- }
116
120
117
121
Stm32CurrentSenseParams* params = new Stm32CurrentSenseParams {
118
122
.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 )
120
125
};
121
126
122
127
return params;
0 commit comments