---@diagnostic disable: duplicate-index
                                   --[[
  Copyright (c) 2023 by Plexim GmbH
  All rights reserved.

  A free license is granted to anyone to use this software for any legal
  non safety-critical purpose, including commercial applications, provided
  that:
  1) IT IS NOT USED TO DIRECTLY OR INDIRECTLY COMPETE WITH PLEXIM, and
  2) THIS COPYRIGHT NOTICE IS PRESERVED in its entirety.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
--]]--
local T = require('targets.TI28')
local U = require('common.utils')
local P = require('resources.TI28003x_pin_map')
local PLL = require('utils.pll_config')

local TripInputsAllocated = {}
local GpioAllocated = {}

-- must match info.xml file

function T.getFamilyPrefix()
  return '28003x'
end

function T.getC2pConfig(c2pAdapter)
  return '280039,38_JTAG %s' % {c2pAdapter}
end

function T.getOptions()
  -- these are the values corresponding to drop down menu selections 
  -- in Coder Options. The order must match info.xml file.
  return {
    ChipCombo = {'280039'},
    boards = {'custom', 'launchpad', 'controlcard'},
    uniFlashConfigs = {
      launchpad = 'Launchpad_TMS320F280039C.ccxml',
      controlcard = 'ControlCard_TMS320F280039C.ccxml',
    },
    c2pConfigs = {
      launchpad = T.getC2pConfig('xds110-2w'),
      controlcard = T.getC2pConfig('xds110-2w'),
    },
    linkerFiles = {
      {'f28003x_flash_lnk.cmd', 'f28003x_ram_lnk.cmd'},
    },
  }
end

function T.configure(resources)
  resources:add('ADC A')
  resources:add('ADC B')
  resources:add('ADC C')
  resources:add('ADCA-SOC', 0, 15)
  resources:add('ADCB-SOC', 0, 15)
  resources:add('ADCC-SOC', 0, 15)
  resources:add('Base Task Load')
  resources:add('CAN A')
  resources:add('CAP', 1, 3)
  resources:add('CMPSS', 1, 7)
  resources:add('CMPSS H', 1, 7)
  resources:add('CMPSS L', 1, 7)
  resources:add('CPU1TIMER', 0, 1)
  resources:add('DAC A')
  resources:add('DAC B')
  resources:add('EXTSYNC', 1, 2)
  resources:add('GPIO', 0, 199)
  resources:add('HRPWM', 1, 4)
  resources:add('MCAN A')
  resources:add('Powerstage Control')
  resources:add('PWM', 1, 8)
  resources:add('SCI A')
  resources:add('SCI B')
  resources:add('SDFM 1', 1, 4)
  resources:add('SDFM 2', 1, 4)
  resources:add('SPI A')
  resources:add('SPI B')
  resources:add('QEP', 1, 2)
  resources:add('XBAR_INPUT', 1, 16)
end

function T.validateAlternateFunction(fun)
  local settings = P.getPinSettings(fun)
  return settings ~= nil
end

function T.getTargetParameters()
  local params = {
    adcs = {
      peripheralType = 5, 
      vrefConfigInterfaceType = 5,
      defaultVref = 3.3,
      num_channels = 16, 
      -- Note this vrefMap is for the 100 pin count packages and is not being used
      -- at this time, since we won't support the complexity of different chip variants.
      -- However, for code compatibility we will leave it here, even though we only support
      -- one reference at this time.
      vrefMap = {A = 'A', B = 'A', C = 'A'},   -- only one reference
    },
    caps = {
      type = 3,
    },
    cbc_trip_inputs = {8, 9, 10, 11, 12},
    clas = {
      type = 2,
    },
    comps = {
      -- Listed as ANALOGPIN = {COMP#, INPUT#}, according to listed order in data sheet
      positive = {
        A0 = {3, 2}, B15 = {3, 2}, C15 = {3, 2},
        A1 = {1, 4}, B7 = {1, 4},
        A10 = {2, 3}, B1 = {2, 3}, C10 = {2, 3},
        A11 = {1, 1}, B10 = {1, 1}, C0 = {1, 1},
        A12 = {2, 1},
        A14 = {3, 4}, B14 = {3, 4}, C4 = {3, 4},
        A2 = {1, 0}, B6 = {1, 0}, C9 = {1, 0},
        A3 = {3, 5},
        A4 = {2, 0}, B8 = {2, 0},
        A5 = {2, 5},
        A6 = {1, 2},
        A8 = {4, 4},
        A9 = {2, 2},
        B0 = {2, 4}, C11 = {2, 4},
        B11 = {4, 5},
        B11 = {4, 5}, -- duplicate
        B2 = {3, 0}, C6 = {3, 0},
        B3 = {3, 3},
        B4 = {4, 0}, C8 = {4, 0},
        B5 = {1, 5},
        B5 = {1, 5}, -- duplicate
        C1 = {4, 2},
        C14 = {4, 3},
        C2 = {3, 1}, B12 = {3, 1},
        C3 = {4, 1}, A7 = {4, 1},
        A15 = {1, 3},
        A3 = {3, 5}, -- duplicate
        A5 = {2, 5}, -- duplicate
        A8 = {4, 4}, -- duplicate
        B0 = {2, 4}, -- duplicate
        C14 = {4, 3}, -- duplicate
      },
      vrefMap = {A = 'A', B = 'A', C = 'A'},
    },
    cpu_timers = {0},
    dacs = {
      minOutputVoltage = 0.0,
      maxOutputVoltage = 3.3,
      -- Note this mapping is retained even though all references are the same because the
      -- getDacVref code requires a mapping for type 4 ADCs as well.
      -- DAC A use vrefA
      -- DAC B uses vrefB
      vrefMap = {A = 'A', B = 'B'},
    },
    dcans = {
      type = 0,
    },
    epwms = {
      type = 4,
      max_event_period = 15,
      sync_group_size = 1,
      num_units = 8,
    },
    gpios = {
      openDrainSupported = true,
    },
    hrpwms = {
      num_units = 4,
    },
    mcans = {
      type = 1,
    },
    pgas = {num_units = 0},
    qeps = {},
    scis = {
      num_units = 2,
    },
    sdfms = {
      num_units = 2,
    },
    spis = {
      type = 2,
      fifo_depth = 16,
    },
    trip_groups = {A = 4, B = 5, C = 7},
  }
  return params
end

function T.allocateGpio(gpio, properties, req, label)
  GpioAllocated[gpio] = properties
  req:add('GPIO', gpio, label)
end

function T.isGpioAllocated(gpio)
  return (GpioAllocated[gpio] ~= nil)
end

function T.getGpioProperties(gpio)
  return GpioAllocated[gpio]
end

function T.getNextAvailableTripInput()
  -- find next available trip input
  local availableInputs = T.getTargetParameters().cbc_trip_inputs
  for _, tin in ipairs(availableInputs) do
    if TripInputsAllocated[tin] == nil then
      TripInputsAllocated[tin] = true
      return tin
    end
  end
end

function T.getComparatorsForPin(pin)
  local comp = T.getTargetParameters().comps.positive[pin]
  if comp == nil then
    return nil
  end

  local compLowHigh = {
    unit = comp[1],
    mux = comp[2],
  }
  return {
    low = compLowHigh,
    high = compLowHigh,
  }
end

function T.checkGpioIsValidPwmSync(gpio)
  local valid = (gpio <= 61) or ((gpio >= 224) and (gpio <= 253))
  if not valid then
    U.error('Only @param:Gpio: from 0-61 and 224-253 can be selected as external synchronization source for this target.')
  end
end

function T.getPwmSyncInSel(params)
  local mux
  if params.type == 'external' then
    if params.source_unit == 1 then
      mux = 0x18
    elseif params.source_unit == 2 then
      mux = 0x19
    else
      U.error('Unsupported external sync unit (%d).' % {params.source_unit})
    end
  elseif params.type == 'epwm' then
    mux = params.source_unit
  else
    U.error('Unsupported sync type (%s).' % {params.type})
  end
  return mux
end

function T.getLowSpeedClock()
  return T.getCleanSysClkHz() / 4
end

function T.getPwmClock()
  -- TBCLOCK - CLKDIV=/1, HSPCLKDIV=/1
  -- TBCLK = EPWMCLK/(HSPCLKDIV * CLKDIV)
  return T.getCleanSysClkHz()
end

function T.getDeadTimeClock()
  return T.getPwmClock()
end

function T.getTimerClock()
  -- relevant clock for dispatcher
  return T.getPwmClock()
end

function T.getCanClkAndMaxBrp()
  return T.getCleanSysClkHz(), 0x400
end

function T.getAdcClock()
  -- HAL automatically configures ADCCTL2.bit.PRESCALE
  local sysClkHz = T.getCleanSysClkHz()
  if sysClkHz <= 60000000 then
    return sysClkHz
  else
    return sysClkHz / 2
  end
end

function T.calcACQPS(ts, sigmodeDifferential)
  local tsMin = math.max(75e-9, 1 / T.getAdcClock()) -- per datasheet (note: 90e-9 for AGPIO, which we don't use?)
  local acqpsMax = 511
  local acqpsMin = 0

  if (not U.isScalar(ts)) or (ts < tsMin) then
    ts = tsMin
  end
  local sysClkHz = T.getCleanSysClkHz()

  return U.clampNumber(math.ceil(ts * sysClkHz) - 1, {acqpsMin, acqpsMax})
end

function T.getCpuTimerSetupCode(unit, params)
  local code = [[
    CpuTimer%(unit)dRegs.TCR.bit.TSS = 1; // stop timer
    CpuTimer%(unit)dRegs.TPRH.all = 0;
    CpuTimer%(unit)dRegs.PRD.all = %(period)d - 1;
    CpuTimer%(unit)dRegs.TCR.bit.TRB = 1; // reload period
    CpuTimer%(unit)dRegs.TCR.bit.TIE = 1; // enable trigger to SOC/interrupt
  ]] % {
    unit = unit,
    period = params.period,
  }

  if params.isr then
    -- note, this is really hard-coded for CPUTimer0
    local isrConfigCode = [[
      PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
      EALLOW;
      PieVectTable.TIMER%(unit)d_INT = &%(isr)s;
      EDIS;
      PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
      PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; // acknowledge interrupt to PIE
	  ]] % {
      unit = unit,
      isr = params.isr,
    }

    code = code..isrConfigCode
  end

  return code
end

function T.getEpwmTimersSyncCode()
  return [[
    CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 1; // start all the timers synced
  ]]
end

function T.getAdcSetupCode(unitAsChar, params)
  local code = [[
    EALLOW;
    Adc%(adc)sRegs.ADCINTSEL1N2.bit.INT1CONT = 0; // disable ADCINT1 Continuous mode
    Adc%(adc)sRegs.ADCINTSEL1N2.bit.INT1SEL = %(int1sel)d; // setup EOC%(int1sel)d to trigger ADCINT1
    Adc%(adc)sRegs.ADCINTSEL1N2.bit.INT1E = 1; // enable ADCINT1
    Adc%(adc)sRegs.ADCCTL1.bit.INTPULSEPOS = 1; // ADCINT1 trips after AdcResults latch
    EDIS;
  ]] % {
    adc = string.lower(unitAsChar),
    int1sel = params.INT1SEL,
  }

  local zeroIndexedUnit = U.charToZeroIndexedInt(unitAsChar)
  if params.isr then
    local pieVect, pieBitMask
    if zeroIndexedUnit <= 2 then
      pieVect = '(PINT *)((uint32_t)(&PieVectTable.ADCA1_INT) + ((uint32_t)%d)*sizeof(PINT *))' % {zeroIndexedUnit}
      pieBitMask = (1 << zeroIndexedUnit)
    else
      error('ADC %s not supported.' % {unitAsChar})
    end

    local isrConfigCode = [[
      EALLOW;
      *%(pieVect)s = &%(isr)s;
      PieCtrlRegs.PIEIER1.all |= %(pieBitMask)s;
      PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
      EDIS;
    ]] % {
      pieVect = pieVect,
      isr = params.isr,
      pieBitMask = pieBitMask,
    }

    code = code..isrConfigCode
  end

  return code
end

function T.getAdcInterruptAcknCode(unitAsChar, params)
  local code = [[
    Adc%sRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; // clear ADCINT1 flag reinitialize for next SOC
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; // acknowledge interrupt to PIE (All ADCS in group 1)
    IER |= M_INT1;
  ]] % {string.lower(unitAsChar)}

  return code
end

local function setAndGetPllLimits(useInternalOsc)
  local limits = PLL.new(useInternalOsc)

  limits:set('INTERNAL_OSCILLATOR_HZ', {value = 10e6}) -- INTOSC2

  limits:set('OSCCLK', {min = 10e6, max = 20e6})       -- external *oscillator* could go down to 2
  limits:set('INTCLK', {min = 2e6, max = 20e6})
  limits:set('VCOCLK', {min = 220e6, max = 600e6})
  limits:set('PLLRAWCLK', {min = 6e6, max = 240e6})
  limits:set('SYSCLK', {min = 2e6, max = 120e6})

  limits:set('REFDIV', {max = 32})
  limits:set('ODIV', {max = 32})
  limits:set('IMULT', {max = 127})
  limits:set('SYSDIV', {max = 126})
  return limits:getLimits() -- executes validation and returns limits
end

function T.getClockConfiguration()
  local useInternalOsc = (Target.Variables.useIntOsc == 1) -- convert gui value to boolean
  local limits = setAndGetPllLimits(useInternalOsc)

  local sysClkHz = T.getCleanSysClkHz()

  local inputClkHz
  if useInternalOsc then
    inputClkHz = limits.INTERNAL_OSCILLATOR_HZ.value
  else
    inputClkHz = T.getCleanExtClkHz()
  end

  -- establish PLL settings
  local pllConfig = PLL.getClockConfiguration(limits, inputClkHz, sysClkHz)
  return {
    pllConfig = pllConfig,
    inputClkHz = inputClkHz,
    sysClkHz = sysClkHz,
    useInternalOsc = useInternalOsc,
  }
end

function T.getClockConfigurationCode(clockConfig)
  local pllConfig = clockConfig.pllConfig
 
  local clkSrcMacro
  if clockConfig.useInternalOsc then
    clkSrcMacro = 'SYSCTL_OSCSRC_OSC2'
  else
    clkSrcMacro = 'SYSCTL_OSCSRC_XTAL'
  end
  local dccBase = 0

  local cpuRate = 100.0 / clockConfig.sysClkHz * 10000000

  local declarations = [[
    void DevInit(uint32_t aDeviceClkConf);
    void InitFlashHz(Uint32 clkHz);
    void PieCntlInit(void);
    void F28x_usDelay(long LoopCount);

// Clock configuration
#define PLX_DEVICE_SETCLOCK_CFG       (%(clkSrcMacro)s | SYSCTL_IMULT(%(imult)d) | \
                                      SYSCTL_REFDIV(%(refdiv)d) | SYSCTL_ODIV(%(odiv)d) | \
                                      SYSCTL_SYSDIV(%(sysdiv)d) | SYSCTL_PLL_ENABLE | \
                                      SYSCTL_DCC_BASE_%(dccBase)d)

#define SYSCLK_HZ %(sysClkHz)dL
#define LSPCLK_HZ (%(sysClkHz)dL / 4l)

#define PLX_DELAY_US(A)  F28x_usDelay(((((long double) A * 1000.0L) / (long double)%(cpuRate)fL) - 9.0L) / 5.0L)
  ]] % {
    clkSrcMacro = clkSrcMacro,
    imult = pllConfig.imult,
    refdiv = pllConfig.refdiv,
    odiv = pllConfig.odiv,
    sysdiv = pllConfig.sysdiv,
    dccBase = dccBase,
    sysClkHz = clockConfig.sysClkHz,
    cpuRate = cpuRate,
  }

  local code = [[
    SysCtl_disableWatchdog();
    {
        uint32_t sysPllConfig = PLX_DEVICE_SETCLOCK_CFG;
        DevInit(sysPllConfig);

        SysCtl_setLowSpeedClock(SYSCTL_LSPCLK_PRESCALE_4);
    }

    InitFlashHz(SYSCLK_HZ);

    // set cpu timers to same clock as ePWM
    CpuTimer0Regs.TPR.all = 0;
    CpuTimer1Regs.TPR.all = 0;
    CpuTimer2Regs.TPR.all = 0;
    EALLOW;
    CpuSysRegs.PCLKCR0.bit.CPUTIMER0 = 1;
    CpuSysRegs.PCLKCR0.bit.CPUTIMER1 = 1;
    CpuSysRegs.PCLKCR0.bit.CPUTIMER2 = 1;
    CpuSysRegs.PCLKCR0.bit.TBCLKSYNC = 0; // stop all the TB clocks
    EDIS;
  ]]

  return {declarations = declarations, code = code}
end

T.ts_epwm = require('peripherals.epwm_type4').getFunctions(T)

return T
