__attribute__((section(".ram_code")))
static inline void pseudo_pll_B_3PH(int32_t qVg_AC_B)
{
    static uint16_t plrty_transition_dly_OFF_B, plrty_transition_dly_ON_B;

    if((qVg_AC_B < VIN_OFF_THRSLD) && (qVg_AC_B > -VIN_OFF_THRSLD))
    {
        if(plrty_transition_dly_OFF_B == 0)
        {
            if(!AC_zero_cross_B.pwm_zc_disabled)
            {
                /* Disable PWM and SR signals. */
                ((XMC_GPIO_PORT_t *) PORT1_BASE)->IOCR[14u >> 2U] &= (uint32_t)~(PORT_IOCR_PC_Msk << (PORT_IOCR_PC_Size * (14u & 0x3U)));   //VT2 OFF
                ((XMC_GPIO_PORT_t *) PORT2_BASE)->IOCR[1u >> 2U] &= (uint32_t)~(PORT_IOCR_PC_Msk << (PORT_IOCR_PC_Size * (1u & 0x3U)));     //VT3 OFF
                //Set Low for SR_A_HS and SR_A_LS
                (((XMC_GPIO_PORT_t *) PORT0_BASE)->OMR = 0x10000U << 4u);       //VT1 OFF
                (((XMC_GPIO_PORT_t *) PORT0_BASE)->OMR = 0x10000U << 9u);       //VT6 OFF
                (((XMC_GPIO_PORT_t *) PORT2_BASE)->OMR = 0x10000U << 6u);       //VT4 OFF
                if (!(status_reg & ST_START_EN))
                {
                    (((XMC_GPIO_PORT_t *) PORT0_BASE)->OMR = 0x10000U << 1u);   //VT5 OFF
                }

                plrty_transition_dly_ON_B = PWM_TRANSITION_DLY;
                AC_zero_cross_B.pwm_zc_disabled = true;
            }
            /* Flag raised when the AC voltage gets close to zero; enables PWM start when AC voltage goes over ON_THRSLD. */
            if ((qVg_AC_B < VIN_ZERO_THRSLD) && (qVg_AC_B > -VIN_ZERO_THRSLD))
            {
                AC_zero_cross_B.vac_zero_cross = true;
            }
        }

        /* Choose the starting duty cycle and switching frequency for restart.
         * In the current implementation, Fsw is kept constant for all the conditions. */
        if (AC_zero_cross_B.pwm_zc_disabled)
        {
            if ((qVGrid_THETA.Angle > 10923) && (qVGrid_THETA.Angle < 27307))
            {
                input_polarity_B = NEGATIVE;
            }
            else
            {
                input_polarity_B = POSITIVE;
            }
            /* No current loop if the PWM is off. */
            current_loop_ON_B = false;
            I_ctrl_B.duty_ramp = false;
        }
    }

    if ((qVg_AC_B > VIN_ON_THRSLD) || (qVg_AC_B < -VIN_ON_THRSLD))
    {
        if ((AC_zero_cross_B.vac_zero_cross && (plrty_transition_dly_ON_B == 0)) || AC_zero_cross_B.neg_iL_fault_CLR)
        {
            //current_loop_ON_B = true;

            /* Flag for starting the PWM/SR after polarity decision. */
            AC_zero_cross_B.vin_on_allowed = true;

            /* Turn on PWM/SR if delay happened and polarity detected*/
            if ((plrty_transition_dly_ON_B == 0) && AC_zero_cross_B.vin_on_allowed)
            {

                /* Voltage has risen after a Vac crossing detection.*/
                if(AC_zero_cross_B.pwm_zc_disabled || AC_zero_cross_B.neg_iL_fault_CLR)
                {
                    /* POSITIVE AC */
                    if(input_polarity_B)
                    {
                        I_ctrl_B.duty_ramp = true;
                        I_ctrl_B.duty_ol = MAX_DUTY;
                        if(power_stage_ON)
                        {
                            /* LS Switch T3 ON for duty ramp. */
                            ((XMC_GPIO_PORT_t *) PORT2_BASE)->OMR = 0x10000U << 1;
                            ((XMC_GPIO_PORT_t *) PORT2_BASE)->IOCR[1 >> 2U] |= (uint32_t) XMC_GPIO_MODE_OUTPUT_PUSH_PULL_ALT2 << ((uint32_t)PORT_IOCR_PC_Size * ((uint32_t)1 & 0x3U));      //VT3 ON
                        }
                    }
                    /* NEGATIVE AC */
                    else
                    {
                        I_ctrl_B.duty_ramp = true;
                        I_ctrl_B.duty_ol = MIN_DUTY;
                        if(power_stage_ON)
                        {
                            /* HS Switch T2 ON only for duty ramp. */
                            ((XMC_GPIO_PORT_t *) PORT1_BASE)->OMR = 0x10000U << 14;
                            ((XMC_GPIO_PORT_t *) PORT1_BASE)->IOCR[14 >> 2U] |= (uint32_t) XMC_GPIO_MODE_OUTPUT_PUSH_PULL_ALT3 << ((uint32_t)PORT_IOCR_PC_Size * ((uint32_t)14 & 0x3U)); //VT2 ON
                        }
                    }

                    /* Start PWM with duty soft-start */
                    //I_ctrl_B.sftstrt_cnt = 1;

                    /* Voltage loop can be evaluated again. */
                    //power_stage_ON = true;
                    /* Reset counting of switching cycles after starting PWM. */
                    AC_zero_cross_B.vin_on_cnt = 0;
                    /* Sets a delay before starting PWM polarity transition. */
                    plrty_transition_dly_OFF_B = PWM_TRANSITION_DLY+6;

                    /* Removes Vac crossing PWM disabled status flag. */
                    AC_zero_cross_B.pwm_zc_disabled = false;
                    /* Wait for new zero crossing or current fault for starting PWM/SR. */
                    AC_zero_cross_B.vac_zero_cross = false;
                    AC_zero_cross_B.neg_iL_fault_CLR = false;
                }
                /* Clear AC zero crossing flags */
                AC_zero_cross_B.vin_on_allowed = false;
            }
        }

        /* Delay of LF pulses in PFC operation.*/
        //if(AC_zero_cross_B.vin_on_cnt < SR_DELAY_SWCYC)
        //if((power_stage_ON) && (AC_zero_cross_B.vin_on_cnt < SR_DELAY_SWCYC))
        if((current_loop_ON_B) && (AC_zero_cross_B.vin_on_cnt < SR_DELAY_SWCYC))
        {
            /* Update cycle count. */
            AC_zero_cross_B.vin_on_cnt++;
            if (AC_zero_cross_B.vin_on_cnt == SR_DELAY_SWCYC)
            {
                if(input_polarity_B)
                {
                    ((XMC_GPIO_PORT_t *) PORT1_BASE)->OMR = 0x10000U << 14;
                    ((XMC_GPIO_PORT_t *) PORT1_BASE)->IOCR[14 >> 2U] |= (uint32_t) XMC_GPIO_MODE_OUTPUT_PUSH_PULL_ALT3 << ((uint32_t)PORT_IOCR_PC_Size * ((uint32_t)14 & 0x3U));    //VT2 ON
                    //((XMC_GPIO_PORT_t *) PORT2_BASE)->OMR = 0x10000U << 1;
                    //((XMC_GPIO_PORT_t *) PORT2_BASE)->IOCR[1 >> 2U] |= (uint32_t) XMC_GPIO_MODE_OUTPUT_PUSH_PULL_ALT2 << ((uint32_t)PORT_IOCR_PC_Size * ((uint32_t)1 & 0x3U));    //VT3 ON

                    /* Set High for SR_B_HS */
                    (((XMC_GPIO_PORT_t *) PORT0_BASE)->OMR = 0x1U << 4u);       //VT1 ON
                    (((XMC_GPIO_PORT_t *) PORT0_BASE)->OMR = 0x1U << 9u);       //VT6 ON
                    /* Set Low for SR_B_LS */
                    (((XMC_GPIO_PORT_t *) PORT2_BASE)->OMR = 0x10000UL << 6u);  //VT4 OFF
                    (((XMC_GPIO_PORT_t *) PORT0_BASE)->OMR = 0x10000UL << 1u);  //VT5 OFF
                }
                else
                {
                    //((XMC_GPIO_PORT_t *) PORT1_BASE)->OMR = 0x10000U << 14;
                    //((XMC_GPIO_PORT_t *) PORT1_BASE)->IOCR[14 >> 2U] |= (uint32_t) XMC_GPIO_MODE_OUTPUT_PUSH_PULL_ALT3 << ((uint32_t)PORT_IOCR_PC_Size * ((uint32_t)14 & 0x3U));  //VT2 ON
                    ((XMC_GPIO_PORT_t *) PORT2_BASE)->OMR = 0x10000U << 1;
                    ((XMC_GPIO_PORT_t *) PORT2_BASE)->IOCR[1 >> 2U] |= (uint32_t) XMC_GPIO_MODE_OUTPUT_PUSH_PULL_ALT2 << ((uint32_t)PORT_IOCR_PC_Size * ((uint32_t)1 & 0x3U));      //VT3 ON

                    /* Set Low for SR_B_HS */
                    (((XMC_GPIO_PORT_t *) PORT0_BASE)->OMR = 0x10000UL << 4u);  //VT1 OFF
                    (((XMC_GPIO_PORT_t *) PORT0_BASE)->OMR = 0x10000UL << 9u);  //VT6 OFF
                    /* Set High for SR_B_LS */
                    (((XMC_GPIO_PORT_t *) PORT2_BASE)->OMR = 0x1U << 6u);       //VT4 ON
                    (((XMC_GPIO_PORT_t *) PORT0_BASE)->OMR = 0x1U << 1u);       //VT5 ON
                }
            }
        }
    }

    /* Transition timers*/
    if(plrty_transition_dly_OFF_B)
    {
        plrty_transition_dly_OFF_B--;
    }
    if(plrty_transition_dly_ON_B)
    {
        plrty_transition_dly_ON_B--;
    }
}