Firmware/FFBoard/UserExtensions/Src/TMC4671.cpp
/*
 * Calibrates the ADC by disabling the power stage and sampling a mean value. Takes time!
 */
Calibrates the ADC by disabling the power stage and sampling a mean value. Takes time!
Calibrates the ADC by disabling the power stage and sampling a mean value . Takes time!
bool TMC4671::calibrateAdcOffset(uint16_t time){
	uint16_t measuretime_idle = time;
	uint32_t measurements_idle = 0;
	uint64_t totalA=0;
	uint64_t totalB=0;
	writeReg(0x03, 0); 
	PhiE lastphie = getPhiEtype();
	MotionMode lastmode = getMotionMode();
	setMotionMode(MotionMode::stop,true);
	uint16_t lastrawA=conf.adc_I0_offset, lastrawB=conf.adc_I1_offset;
	uint32_t tick = HAL_GetTick();
	while(HAL_GetTick() - tick < measuretime_idle){ 
		uint32_t adcraw = readReg(0x02);
		uint16_t rawA = adcraw & 0xffff;
		uint16_t rawB = (adcraw >> 16) & 0xffff;
		if(abs(lastrawA-rawA) < 10000 && abs(lastrawB-rawB) < 10000){
			totalA += rawA;
			totalB += rawB;
			measurements_idle++;
			lastrawA = rawA;
			lastrawB = rawB;
		}
	}
	uint32_t offsetAidle = totalA / (measurements_idle);
	uint32_t offsetBidle = totalB / (measurements_idle);
	if(totalA < 100 || totalB < 100 || (abs((int32_t)offsetAidle - 0x7fff) > 5000 || abs((int32_t)offsetBidle - 0x7fff) > 5000)){
		ErrorHandler::addError(Error(ErrorCode::adcCalibrationError,ErrorType::critical,"TMC Adc/Shunt offset calibration failed."));
		blinkErrLed(100, 0); 
		this->changeState(TMC_ControlState::HardError);
		return false; 
	}
	conf.adc_I0_offset = offsetAidle;
	conf.adc_I1_offset = offsetBidle;
	setAdcOffset(conf.adc_I0_offset, conf.adc_I1_offset);
	setPhiEtype(lastphie);
	setMotionMode(lastmode,true);
	return true;
}
bool TMC4671 :: calibrateAdcOffset ( uint16_t time ) { uint16_t measuretime_idle = time ; uint32_t measurements_idle = 0 ; uint64_t totalA = 0 ; uint64_t totalB = 0 ; writeReg ( 0x03 , 0 ) ; PhiE lastphie = getPhiEtype ( ) ; MotionMode lastmode = getMotionMode ( ) ; setMotionMode ( MotionMode :: stop , true ) ; uint16_t lastrawA = conf . adc_I0_offset , lastrawB = conf . adc_I1_offset ; uint32_t tick = HAL_GetTick ( ) ; while ( HAL_GetTick ( ) - tick < measuretime_idle ) { uint32_t adcraw = readReg ( 0x02 ) ; uint16_t rawA = adcraw & 0xffff ; uint16_t rawB = ( adcraw >> 16 ) & 0xffff ; if ( abs ( lastrawA - rawA ) < 10000 && abs ( lastrawB - rawB ) < 10000 ) { totalA += rawA ; totalB += rawB ; measurements_idle ++ ; lastrawA = rawA ; lastrawB = rawB ; } } uint32_t offsetAidle = totalA / ( measurements_idle ) ; uint32_t offsetBidle = totalB / ( measurements_idle ) ; if ( totalA < 100 || totalB < 100 || ( abs ( ( int32_t ) offsetAidle - 0x7fff ) > 5000 || abs ( ( int32_t ) offsetBidle - 0x7fff ) > 5000 ) ) { ErrorHandler :: addError ( Error ( ErrorCode :: adcCalibrationError , ErrorType :: critical , " " ) ) ; blinkErrLed ( 100 , 0 ) ; this -> changeState ( TMC_ControlState :: HardError ) ; return false ; } conf . adc_I0_offset = offsetAidle ; conf . adc_I1_offset = offsetBidle ; setAdcOffset ( conf . adc_I0_offset , conf . adc_I1_offset ) ; setPhiEtype ( lastphie ) ; setMotionMode ( lastmode , true ) ; return true ; }
Calibrates the ADC by disabling the power stage and sampling a mean value.
Calibrates the ADC by disabling the power stage and sampling a mean value .
// Read raw adc //pulseClipLed(); // Turn on led // Disable drivers and measure many samples of zero current //enablePin.reset(); // Measure idle // Signflip filter for SPI bug //enablePin.set(); // Check if offsets are in a valid range // Blink forever // An adc or shunt amp is likely broken. do not proceed. // ADC Offsets should now be close to perfect