/*
   Copyright (c) 2026 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.
 */

#ifndef PLX_EPWM_TYPE5_H_
#define PLX_EPWM_TYPE5_H_

#include "epwm_type4.h"

static const uint16_t PLX_PWM_channelBRemap[4] = {EPWM_AQ_OUTPUT_NO_CHANGE, EPWM_AQ_OUTPUT_HIGH, EPWM_AQ_OUTPUT_LOW, EPWM_AQ_OUTPUT_TOGGLE};
 
#pragma CODE_SECTION(PLX_PWM_setXcmpAB, "ramfuncs")
#pragma FUNC_ALWAYS_INLINE(PLX_PWM_setXcmpAB)
static inline void PLX_PWM_setXcmpAB(
    PLX_PWM_Handle_t aHandle, 
    const float *aDuty, 
    const uint16_t aAq) 
{
  PLX_PWM_Obj_t *obj = (PLX_PWM_Obj_t *)aHandle;

  const uint16_t fullPeriod = obj->pwm->TBPRD+1;
  uint32_t xcmp[8];
  for(int i=0; i<8; i++){
    xcmp[i] = (uint32_t)(fullPeriod * aDuty[i] + 0.5f);
  }
  EPWM_setXCMPRegValue(obj->base, EPWM_XCMP1_SHADOW1, xcmp[0]);
  EPWM_setXCMPRegValue(obj->base, EPWM_XCMP2_SHADOW1, xcmp[1]);
  EPWM_setXCMPRegValue(obj->base, EPWM_XCMP3_SHADOW1, xcmp[2]);
  EPWM_setXCMPRegValue(obj->base, EPWM_XCMP4_SHADOW1, xcmp[3]);
  EPWM_setXCMPRegValue(obj->base, EPWM_XCMP5_SHADOW1, xcmp[4]);
  EPWM_setXCMPRegValue(obj->base, EPWM_XCMP6_SHADOW1, xcmp[5]);
  EPWM_setXCMPRegValue(obj->base, EPWM_XCMP7_SHADOW1, xcmp[6]);
  EPWM_setXCMPRegValue(obj->base, EPWM_XCMP8_SHADOW1, xcmp[7]);

  uint16_t aq = aAq;
  EPWM_setXCMPActionQualifierAction(obj->base, EPWM_XCMP_SHADOW1, EPWM_AQ_OUTPUT_A, (EPWM_ActionQualifierOutput)(aq & 3), EPWM_AQ_OUTPUT_ON_TIMEBASE_XCMP1);	
  aq >>= 2;
  EPWM_setXCMPActionQualifierAction(obj->base, EPWM_XCMP_SHADOW1, EPWM_AQ_OUTPUT_A, (EPWM_ActionQualifierOutput)(aq & 3), EPWM_AQ_OUTPUT_ON_TIMEBASE_XCMP2);	
  aq >>= 2;
  EPWM_setXCMPActionQualifierAction(obj->base, EPWM_XCMP_SHADOW1, EPWM_AQ_OUTPUT_A, (EPWM_ActionQualifierOutput)(aq & 3), EPWM_AQ_OUTPUT_ON_TIMEBASE_XCMP3);	
  aq >>= 2;
  EPWM_setXCMPActionQualifierAction(obj->base, EPWM_XCMP_SHADOW1, EPWM_AQ_OUTPUT_A, (EPWM_ActionQualifierOutput)(aq & 3), EPWM_AQ_OUTPUT_ON_TIMEBASE_XCMP4);
  // b-channel action is inverted - therefore we need to invert action qualifier
  aq >>= 2;
  EPWM_setXCMPActionQualifierAction(obj->base, EPWM_XCMP_SHADOW1, EPWM_AQ_OUTPUT_B, (EPWM_ActionQualifierOutput)(PLX_PWM_channelBRemap[aq & 3]), EPWM_AQ_OUTPUT_ON_TIMEBASE_XCMP5);	
  aq >>= 2;
  EPWM_setXCMPActionQualifierAction(obj->base, EPWM_XCMP_SHADOW1, EPWM_AQ_OUTPUT_B, (EPWM_ActionQualifierOutput)(PLX_PWM_channelBRemap[aq & 3]), EPWM_AQ_OUTPUT_ON_TIMEBASE_XCMP6);	
  aq >>= 2;
  EPWM_setXCMPActionQualifierAction(obj->base, EPWM_XCMP_SHADOW1, EPWM_AQ_OUTPUT_B, (EPWM_ActionQualifierOutput)(PLX_PWM_channelBRemap[aq & 3]), EPWM_AQ_OUTPUT_ON_TIMEBASE_XCMP7);	
  aq >>= 2;
  EPWM_setXCMPActionQualifierAction(obj->base, EPWM_XCMP_SHADOW1, EPWM_AQ_OUTPUT_B, (EPWM_ActionQualifierOutput)(PLX_PWM_channelBRemap[aq & 3]), EPWM_AQ_OUTPUT_ON_TIMEBASE_XCMP8);	
}

static inline void PLX_PWM_loadXcmp(PLX_PWM_Handle_t aHandle){
  PLX_PWM_Obj_t *obj = (PLX_PWM_Obj_t *)aHandle;
  EPWM_enableXLoad(obj->base);
}

#endif /* PLX_EPWM_TYPE5_H_ */
