function inverter = mcb_updateInverterParameters(motor,inverter,target)
% mcb_updateInverterParameters Update inverter parameters based on target and
% motor chosen
%
% mcb_updateInverterParam(motor,inverter,target) calculates the maximum 
% measurable current (inverter.ISenseMax) based on inverter, motor and 
% target chosen
%
% The script uses these parameters: 'motor.I_rated',
% 'inverter.ISenseVoltPerAmp', 'inverter.ADCGain', 'inverter.ISenseMax', 
% 'inverter.ISenseVref', 'target.ADC_Vref'
%
% The script updates these parameters depending on inverter and target
% chosen: 'inverter.ADCGain', 'inverter.SPI_Gain_Setting',
% 'inverter.ISenseVoltPerAmp', 'inverter.ISenseMax'

% Function      :   mcb_updateInverterParameters
% Description   :   Compute inverter parameters based on target and motor chosen
% File name     :   mcb_updateInverterParameters.m
% Inputs        :   pmsm object, inverter object and target object
% Authors       :   Shivaprasad Narayan

% Copyright 2020 The MathWorks, Inc.

switch inverter.model
    case 'BoostXL-DRV8305'
        if motor.I_rated < 4
			% BoostXL-DRV8305 Current sense gain is changed to 40 V/V
            inverter.ADCGain = 4;   % ADC Range = +- 4.825A wrt 0-4095 counts
            inverter.SPI_Gain_Setting = 0x502A;
            
        elseif motor.I_rated < 7
			% BoostXL-DRV8305 Current sense gain is changed to 20 V/V
            inverter.ADCGain = 2;   % ADC Range = +- 9.650A wrt 0-4095 counts
            inverter.SPI_Gain_Setting = 0x5015;
            
        else
			% BoostXL-DRV8305 Current sense gain remains 10 V/V (default value)
            inverter.ADCGain = 1;   % ADC Range = +- 19.300A wrt 0-4095 counts
            inverter.SPI_Gain_Setting = 0x5000;
            
        end
        
% Add more cases below to customize for any other inverter board
%     case ' '

% Default gain set to 1 and dummy SPI_Gain_Setting added to resolve any
% model compile issues
    otherwise
        inverter.ADCGain = 1;   % ADC Range = +- 19.300A wrt 0-4095 counts (default value)
        inverter.SPI_Gain_Setting = 0x0000;
end

% Voltage output of inverter current sense circuit
inverter.ISenseVoltPerAmp = inverter.ISenseVoltPerAmp * inverter.ADCGain;

% Update ISenseMax according to set ADC gain
inverter.ISenseMax = inverter.ISenseMax/inverter.ADCGain;

switch target.model
    case {'LAUNCHXL-F28379D'}
        % Update maximum measurable current if target ADC reference voltage is not equal to  
		% inverter current sense reference voltage
		% inverter.IsenseMax gets updated below, if target.ADC_Vref ~= inverter.ISenseVref
        inverter.ISenseMax = inverter.ISenseMax * target.ADC_Vref / inverter.ISenseVref;
        
    otherwise
end
end