function [ DesiredCPUClockSpeedinHz ] = getRuntimeCPUClock( hCS )
%GETRUNTIMECPUCLOCK Summary of this function goes here
% Obtain the clock value selected in the 'Desired CPU Clock in MHz' option
% in configuration parameters
%   Detailed explanation goes here
%   This is a callback provided in all the attribute xml files in the
%   parameter TimerTicksPerS of profiler tab
%   This file is called by codertarget.profile.Timer

%   Copyright 2016-2022 The MathWorks, Inc.


% Having the reference in the registry parameter file, obtain the
% desired CPU Clock speed in MHz
if codertarget.data.isValidParameter(hCS,'TargetHardware')
    if ~contains(codertarget.data.getParameterValue(hCS,'TargetHardware'),'ARM')
        if codertarget.data.isValidParameter(hCS,'ESB.ProcessingUnit')
            if contains(codertarget.data.getParameterValue(hCS,'ESB.ProcessingUnit'),'CPU2')
                cpuClockinMHz = str2double(codertarget.data.getParameterValue(hCS,'Clocking.ClosestCpuClock1'));
            elseif  contains(codertarget.data.getParameterValue(hCS,'ESB.ProcessingUnit'),'CPU1')
                cpuClockinMHz = str2double(codertarget.data.getParameterValue(hCS,'Clocking.ClosestCpuClock'));
            else
                cpuClockinMHz = str2double(codertarget.data.getParameterValue(hCS,'Clocking.cpuClockRateMHz'));
            end
        else
            cpuClockinMHz = str2double(codertarget.data.getParameterValue(hCS,'Clocking.ClosestCpuClock'));
        end
    else
        if contains(codertarget.data.getParameterValue(hCS,'TargetHardware'),'ARM Cortex-M3')
            cpuClockinMHz = str2double(codertarget.data.getParameterValue(hCS,'Clocking.ClosestM3CpuClock'));
        else
            % Having the reference in the registry parameter file, obtain the
            % desired CPU Clock speed in MHz
            cpuClockinMHz = str2double(codertarget.data.getParameterValue(hCS,'Clocking.cpuClockRateMHz'));
        end
    end

    extModeXcp = isequal(get_param(hCS,'ExtModeMexFile'),'ext_xcp');
    enableRealTimeLog = codertarget.data.isValidParameter(hCS,'ExtMode.UseRealtimeStampLogging') ...
        && logical(codertarget.data.getParameterValue(hCS,'ExtMode.UseRealtimeStampLogging'));

    if extModeXcp && enableRealTimeLog
        [~,timerPreScalar] = codertarget.registry.xcpExternalModeHardwareTimerData(hCS);
    else
        timerPreScalar = 1;
    end
    %Convert it into Hz
    DesiredCPUClockSpeedinHz = (cpuClockinMHz / timerPreScalar) * 1e6;
end
end

% [EOF]
% LocalWords:  codertarget