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;
2122static DMA_HandleTypeDef hdma_adc1;
2223static 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}
5859void _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
8384void * _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