3.3kW Totem-pole PFC with SiC and XMC1404
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
control_interrupts.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, Infineon Technologies AG
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification,are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * - Neither the name of the copyright holders nor the names of its contributors
14  * may be used to endorse or promote products derived from this software without
15  * specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * To improve the quality of the software, users are encouraged to share
30  * modifications, enhancements or bug fixes with Infineon Technologies AG
31  * dave@infineon.com).
32  */
43 #ifndef CONTROL_INTERRUPTS_H_
44 #define CONTROL_INTERRUPTS_H_
45 
46 /***********************************************************************************************************************
47  * HEADER FILES
48  **********************************************************************************************************************/
49 
50 #include <DAVE.h>
51 #include "init_bckgnd_funct.h"
52 #include "communication.h"
53 
54 /*********************************************************************************************************************
55  * MACROS
56  ********************************************************************************************************************/
62 #define ADC_TO_Q15 3
63 
64 #define IIR_QFORMAT 14
65 
66 #define ADC_TO_IIR (IIR_QFORMAT+ADC_TO_Q15)
67 
68 #define LPF_TO_2ADC (ADC_TO_IIR-1)
69 
70 #define NOTCH_TO_Q15 14
71 
73 #define TO_QiLOOP 15
74 
75 #define CURRENT_REF_TO_Q15 19 //19 --> 5Vdd
76 
77 #define ACCOMP_TO_Q15 21
78 
85 #define OFFSET_SAMPLES_2 8
86 
87 #define OFFSET_SAMPLES 256
88 
89 #define OFFSET_END 0xFFFF
90 
91 #define OFFSET_VAC 432 //432 --> 53Vac
92 
94 #define IL_AVG_GAIN 31129 //31129-Q14 --> kI_SW = 1.9
95 
96 #define IL_AVG_GAIN_Q 14 //31129-Q14 --> kI_SW = 1.9
97 
104 #define ILOOP_COEFF_Q 14 //1P1Z/400V/5Vdd/65kHz/3kHzBW/zero=0.95/750R-47R-1.9SW: 3489 (PQ-HEL-60t)
105 
107 #define COEFF_OFFSET_3k_Q 3986 //1P1Z/400V/5Vdd/65kHz/3kHzBW/zero=0.95/750R-47R-1.9SW: 3986 (PQ-HEL-60t)/4823 (PQ-HEL-66t)
108 
109 #define COEFF_SLOPE_3k_Q 4901 //1P1Z/400V/5Vdd/65kHz/3kHzBW/zero=0.95/750R-47R-1.9SW: 4901 (PQ-HEL-60t)/6491 (PQ-HEL-66t)
110 
111 #define COEFF_MIN_3k_Q 776 //1P1Z/400V/5Vdd/65kHz/3kHzBW/zero=0.95/750R-47R-1.9SW: 776 (PQ-HEL-60t)/572 (PQ-HEL-66t)
112 
114 #define ZERO_LOC_3k_Q 15564 //1P1Z/400V/5Vdd/65kHz/3kHzBW/zero=0.95/750R-47R-1.9SW: 15564 (PQ-HEL-60t)/(PQ-HEL-66t)
115 
117 #define REG1_ILIMIT_Q15 21459 //5Vdd/750R-47R-1.9SW --> 36A-21459 (PQ-HEL-60t)/4823 (PQ-HEL-66t)
118 
120 #define COEFF_a0_INEG_Q 3349 //1P1Z/400V/5Vdd/65kHz/3kHzBW/zero=0.95/750R-47R-1.9SW: 3349 (PQ-HEL-60t)/3979 (PQ-HEL-66t)
121 
122 #define INEG_FAULT_Q15 -4256 //-7.14A/5Vdd/750R-47R-1.9SW: -4256 (PQ-HEL-60t)/(PQ-HEL-66t)
123 
130 #define DT_CONST_ILIMIT_Q15 1788 //5Vdd/750R-47R-1.9SW --> 3A-1788
131 
132 #define DT_REG1_ILIMIT_Q15 4172 //5Vdd/750R-47R-1.9SW --> 7A-4172
133 
134 #define DT_REG2_ILIMIT_Q15 6556 //5Vdd/750R-47R-1.9SW --> 11A-6556
135 
137 #define DT_D2S 5 //5 --> 52ns
138 
139 #define DT_S2D_MAX 12 //12 --> 125ns (65mOhm - 5R6)
140 
141 #define DT_S2D_MIN 4 //4 --> 42ns (65mOhm - 5R6)
142 
144 #define DT_REG1_OFFSET 17 //17 --> 177ns (65mOhm - 5R6)
145 
146 #define DT_REG1_SLOPEQ15 82 //82 --> 15ns/A (65mOhm - 5R6)
147 
148 #define DT_REG2_OFFSET 10 //10 --> 96ns (65mOhm - 5R6)
149 
150 #define DT_REG2_SLOPEQ15 27 //27 --> 5ns/A (65mOhm - 5R6)
151 
152 #define DT_SLOPE_Q 15
153 
155 #define PR_MAX 1475
156 
165 #define VOUT_OFFSET 392 //196 --> 3VDC/5Vdd
166 
167 #define VOUT_OFFSET_LCDO 0
168 
170 #define VOUT_STEADY 26144+VOUT_OFFSET // 26144 --> 400VDC/5Vdd // 25490 --> 390VDC/5Vdd // 24837 --> 380VDC/5Vdd // 22876 --> 350VDC/5Vdd // 19608 --> 300VDC/5Vdd // 16340 --> 250VDC/5Vdd // 13072 --> 200VDC/5Vdd
171 
173 #define VOUT_OVP_STOP 29412+VOUT_OFFSET // 29412 --> 450V/5Vdd // 27451 --> 420V/5Vdd // 24837 --> 380V/5Vdd // 20915 --> 320V/5Vdd // 19608 --> 300V/5Vdd
174 #if (VOUT_OVP_STOP <= VOUT_STEADY)
175  #error "STOP threshold should be greater than steady state voltage"
176 #endif
177 
179 #define VOUT_UVP_MIN 19608 //19608 --> 300VDC/5Vdd // 13072 --> 200VDC/5Vdd // 9804 --> 150VDC/5Vdd // 7843 --> 120VDC/5Vdd // 5228 --> 80VDC/5Vdd // 2614 --> 40VDC/5Vdd
180 
182 #define VOUT_START_MIN 14789 // 14789 --> 226V(160Vrms)/5Vdd // 10167 --> 156V(110Vrms)/5Vdd // 6470 --> 99V(70Vrms)/5Vdd
183 
185 #define VOLTAGE_OFFSET_NL_ON 23530 //23530 --> 360Vdc/5Vdd
186 
187 #define VOLTAGE_OFFSET_NL_OFF 24837 //24837 --> 380Vdc/5Vdd
188 
196 #define VDC_START_MIN_INV 25490 // 25490 --> 390VDC/5Vdd
197 
198 #define VDC_START_MAX_INV 26798 // 26798 --> 410VDC/5Vdd
199 
201 #define VDC_STOP_MIN_INV 22876 // 22876 --> 350VDC/5Vdd // 24183 --> 370VDC/5Vdd
202 
203 #define VDC_STOP_MAX_INV 29412 // 29412 --> 450VDC/5Vdd // 28105 --> 430VDC/5Vdd
204 
212 #define ST_STB_BOP_CHECK_BULK 190 //380
213 
214 #define ST_STB_BOP_SFTSTRT_WAIT (20+ST_STB_BOP_CHECK_BULK) //(40+ST_STB_BOP_CHECK_BULK)
215 
216 #define ST_STB_BOP_END (ST_STB_BOP_SFTSTRT_WAIT+1)
217 
218 #define ST_SFTSTRT_END (ST_STB_BOP_END+7)
219 
226 #define VIN_OFF_THRSLD 179 //245 //326 --> 40Vac //245 --> 30Vac //204 --> 25Vac //179 --> 22Vac //163 --> 20Vac
227 
228 #define VIN_ON_THRSLD 98 //151 --> 18.5Vac //138 --> 17Vac //98 --> 12Vac //122 --> 15Vac
229 #if !(VIN_OFF_THRSLD >= VIN_ON_THRSLD)
230  #error "OFF threshold should be greater than ON threshold"
231 #endif
232 
233 #define VIN_ZERO_THRSLD 65 //98 --> 12Vac //65 --> 8Vac //20 IS DEBUG VALUE!!!
234 #if !(VIN_ON_THRSLD >= VIN_ZERO_THRSLD)
235  #error "ON threshold should be greater than ZERO threshold"
236 #endif
237 
239 #define PWM_TRANSITION_DLY (18) //(18) --> 65kHz(15.3us) rate, 277us; (14) --> 45kHz(22.2us) rate, 311us; (4) --> 10kHz(100us) rate, 400us;
240 
241 #define AC_LCDO_DETECT (130) //(130) --> 65kHz(15.3us) rate, 2ms; (90) --> 45kHz(22.2us) rate, 2ms; (20) --> 10kHz(100us) rate, 2ms;
242 
243 #define AFTER_ACLOST_TIME (2600) //(2600) --> 65kHz(15.3us) rate, 40ms;
244 
246 #define SR_DELAY_SWCYC 2 //3 //6
247 
249 #define AC_400V 3268 //3268 is 400V in qVin_AC units
250 
253 /*---------------------------------------------------------------------------*/
254 /* Enums / Type Definitions */
255 /*---------------------------------------------------------------------------*/
257 typedef enum state
258 {
259  ST_INIT = 0x00,//0,
260  ST_STB = 0x01,//1,
261  ST_STB_BOP = 0x02,//2,
262  ST_SOFT_START = 0x04,//4,
263  ST_RUNNING = 0x08,//8,
264  ST_LATCH = 0x10,//16,
265  ST_START_EN = 0x20,//32,
266 // ST_NO_PWM = 64,
267 // ST_D = 128
268 } state_t;
269 
271 #define PFC_RUNNING (0x04|0x08)
272 
273 
275 typedef enum fault
276 {
277  F_INIT = 0x0,
278  F_OVP = 0x1,
279  F_BOP = 0x2,
280  F_UVP = 0x4,
281  F_PEAK = 0x8,
282  F_OTP = 0x10,
283  F_NEG_I = 0x20,
284  F_DC_INV = 0x40
285 } fault_t;
286 
288 typedef enum
289 {
290  BW_L = 0,
291  BW_H = 1,
292 } comp_BW_t;
293 
295 typedef enum
296 {
297  PFC_MODE = 0,
299 } op_mode_t;
300 
302 typedef struct PFC_var
303 {
304  uint16_t qiL;
305  int16_t qiL_k;
306  uint32_t qiL_offset_acc;
307  uint16_t qiL_offset_count;
308  uint16_t qiL_offset;
312  int16_t qiL_int;
313  uint16_t qVbus_DC;
314  uint16_t qVbus_DC_send;
315  int32_t qVbus_DC_filt;
316  bool qVbus_DC_OK;
317  uint16_t qVin_AC;
318  int16_t qVin_AC_Diff;
319  uint16_t qVin_BOP;
320  uint32_t qVin_inv2;
321  uint16_t qVin_inv2_16b;
322  uint16_t qTemp;
323  uint16_t qTemp_amb;
324  uint16_t qTemp_send;
325  uint16_t store_params;
327  uint16_t inv_power_ref;
328  uint16_t inv_power;
329 } PFC_var_t;
330 
332 typedef enum
333 {
334  NO_LCDO = 0x0,
335  LCDO_DETECTED = 0x1,
336  AFTER_LCDO = 0x2,
337 } AC_LCDO_t;
338 
340 typedef struct AC_monitoring
341 {
349  uint16_t vin_low_cnt;
350  uint16_t after_AClost_cnt;
351  uint16_t vin_on_cnt;
355 
357 typedef enum grid_voltage
358 {
359  NEGATIVE,
360  POSITIVE
362 
363 /*********************************************************************************************************************
364 * Static variables
365  ********************************************************************************************************************/
366 
372 volatile static uint16_t stb_bop_count = 0;
380 static volatile uint8_t acok_counter = 0;
382 static volatile bool acok = false;
386 volatile static uint32_t Vrms2 = 0;
387 
392 volatile static uint8_t count_Vout_OVP_stop = 0;
393 volatile static uint8_t count_Vout_OVP_resume = 0;
397 static volatile int16_t offset_detected = 0;
398 
404 volatile static grid_voltage_t input_polarity[2];
405 
407 volatile static uint8_t input_polarity_count;
408 
414 volatile static uint8_t iDebug = 0;
415 
416 /*********************************************************************************************************************
417 * External global variables
418  ********************************************************************************************************************/
419 extern __IO uint32_t *ADCResult_iL;
420 extern __IO uint32_t *ADCResult_DCBus;
421 extern __IO uint32_t *ADCResult_VAC;
422 extern __IO uint32_t *ADCResult_Temp;
423 extern __IO uint32_t *ADCResult_BOP;
424 
425 extern volatile PFC_var_t PFC;
426 
427 extern volatile state_t status_reg;
428 extern volatile fault_t fault_reg;
429 
430 /*********************************************************************************************************************
431 * Inline functions
432  ********************************************************************************************************************/
433 /*---------------------------------------------------------------------------*/
435 __STATIC_INLINE void
437 {
438  PORT0->IOCR0 &= ~(uint32_t)(0x7c0000UL);
439  PORT1->IOCR0 &= ~(uint32_t)(0x7c000000UL);
440 }
441 
442 /*---------------------------------------------------------------------------*/
444 __STATIC_INLINE void
446 {
447  PORT0->IOCR0 |= 0x009C0000; //ALT7 Push-pull
448  PORT1->IOCR0 |= 0x94000000; //ALT5 Push-pull
449 }
450 
451 /*---------------------------------------------------------------------------*/
453 __STATIC_INLINE void
455 {
456  PORT0-> OMR = (uint32_t)((0x10000U << 8) | (0x10000U << 9));
457 }
458 
459 /*---------------------------------------------------------------------------*/
461 __STATIC_INLINE void
463 {
464  PORT0-> OMR = (uint32_t)((0x1U << 8) | (0x10000U << 9));
465 }
466 
467 /*---------------------------------------------------------------------------*/
469 __STATIC_INLINE void
471 {
472  PORT0-> OMR = (uint32_t)((0x1U << 9) | (0x10000U << 8));
473 }
474 
475 /*---------------------------------------------------------------------------*/
477 __STATIC_INLINE void
479 {
480  PORT0->OMR = (uint32_t)0x1U << 11;
481 }
482 
483 /*---------------------------------------------------------------------------*/
485 __STATIC_INLINE void
487 {
488  PORT0-> OMR = (uint32_t)0x10000U << 11;
489 }
490 
491 /*---------------------------------------------------------------------------*/
493 __STATIC_INLINE __attribute__((section(".ram_code"))) void
494 set_duty(int16_t duty)
495 {
496  // PWM duty cycle
497  CCU80_CC81->CR1S = duty;
498  // iL and Vac ADC (Queue) trigger
499  CCU80_CC81->CR2S = duty >> 1;
500 // // Vdc and T ADC (Scan) trigger
501 // CCU80_CC82->CR1S = (pwm_pfc.config_ptr->period_value + duty) >> 1;
502  // Shadow transfer allowed
503  CCU80->GCSS |= (CCU8_GCSS_S1SE_Msk);// | CCU8_GCSS_S2SE_Msk);
504 }
505 /*---------------------------------------------------------------------------*/
507 __STATIC_INLINE __attribute__((section(".ram_code"))) void
508 set_duty_ADC(int16_t duty,int16_t period)
509 {
510  // PWM duty cycle
511  CCU80_CC81->CR1S = duty;
512 
513  // iL and Vac ADC (Queue) trigger
514  CCU80_CC81->CR2S = duty >> 1;
515 
516  // Vdc and T ADC (Scan) trigger
517  CCU80_CC82->CR1S = (period + duty) >> 1;
518 
519  // DEBUG DFF CALCULATION WITH PWM OUTPUT
520 // if(input_polarity[0])
521 // {
522 // CCU80_CC83->CR2S = 200;//current_PI_ctr.ratio_FF;
523 // }
524 // else
525 // {
526 // CCU80_CC83->CR2S = 800;
527 // }
528 // CCU80_CC83->CR2S = current_PI_ctr.ratio_FF;
529 
530  // Shadow transfer allowed
531  CCU80->GCSS |= (CCU8_GCSS_S1SE_Msk | CCU8_GCSS_S2SE_Msk | CCU8_GCSS_S3SE_Msk);
532 }
533 /*---------------------------------------------------------------------------*/
535 __STATIC_INLINE __attribute__((section(".ram_code"))) void
536 set_duty_period(int16_t duty,int16_t period)
537 {
538  // PWM duty cycle
539  CCU80_CC81->CR1S = duty;
540  // iL and Vac ADC (Queue) trigger
541  CCU80_CC81->CR2S = duty >> 1;
542  // Vdc and T ADC (Scan) trigger
543  CCU80_CC82->CR1S = (period + duty) >> 1;
544 
545  // PWM switching frequency
546  CCU80_CC81->PRS = period;
547 
548  // Shadow transfer allowed
549  CCU80->GCSS |= (CCU8_GCSS_S1SE_Msk | CCU8_GCSS_S2SE_Msk);
550 }
551 
552 /*---------------------------------------------------------------------------*/
558 #ifndef DOXYGEN_SKIP
559 __attribute__((section(".ram_code")))
560 #endif
561 static inline void iL_offset_calc()
562 {
563  /*------------------------------------------------------------------------------------------*/
564  /* Current offset (zero level) acquisition before starting the PFC (ST_STB_BOP) */
565  /*------------------------------------------------------------------------------------------*/
566 
567  if (PFC.iL_offset_en) // Current offset is calculated during start up sequence, before closing the relay
568  {
569  if (PFC.qiL_offset_count < (OFFSET_SAMPLES) ) // Accumulate a defined number of samples
570  {
571  if (PFC.qVin_AC < OFFSET_VAC) // Use current information for offset calculation if Vac is under 60V (432-ADC)
572  {
573  PFC.qiL_offset_acc += (*ADCResult_iL & 0x7FFF);
574  PFC.qiL_offset_count++;
575 
576 // /* DEBUGGING pin */
577 // XMC_GPIO_ToggleOutput(P0_12);
578  }
579  }
580  else
581  {
582  // Average of the accumulated current measurement is current offset. Normalization to Q15
585  PFC.iL_offset_en = false; // No current offset calculation in next iteration
586 // PFC.iL_offset_done = true;
587  }
588 
589  }
590 }
591 
592 /*---------------------------------------------------------------------------*/
598 #ifndef DOXYGEN_SKIP
599 __attribute__((section(".ram_code")))
600 #endif
601 static inline void iL_modif()
602 {
603  /*---------------------------------------------------------------------------*/
604  /* Current sensing modification */
605  /*---------------------------------------------------------------------------*/
606 
607  // Sensed current. Modified later according to positive/negative detection.
608  PFC.qiL = (*ADCResult_iL & 0x7FFF) << ADC_TO_Q15;
609  // Read AC voltage
610  PFC.qVin_AC = (*ADCResult_VAC & 0xFFF);
611 
612 
613  // PFC mode
614  if(!PFC.pfc_inv_mode)
615  {
616  // Remove the measurement offset
617  PFC.qiL_int = PFC.qiL - PFC.qiL_offset;
618 
619  // Rectified current measurement according to AC polarity for PFC mode
620  if (!input_polarity[0])
621  {
622  PFC.qiL_int = -PFC.qiL_int;
623  }
624  }
625  else // Inverter mode
626  {
627  if(input_polarity[0])
628  {
629  PFC.qiL_int = PFC.qiL - PFC.qiL_offset + 650;//3*262; //+262 added in inverter test
630 
631  // Rectified current measurement according to AC polarity for Inverter mode
632  PFC.qiL_int = -PFC.qiL_int;
633  }
634  else
635  {
636  PFC.qiL_int = PFC.qiL - PFC.qiL_offset - 650;//3*262; //-262 added in inverter test
637  }
638  }
639 
640  // Inductor current to be used in the current loop (Q15) --> SW gain!
641  PFC.qiL_k = (PFC.qiL_int * IL_AVG_GAIN) >> IL_AVG_GAIN_Q;
642 }
643 
644 /*---------------------------------------------------------------------------*/
651 #ifndef DOXYGEN_SKIP
652 __attribute__((section(".ram_code")))
653 #endif
654 static inline void iL_loop()
655 {
656  if (current_loop_ON) //Current loop runs only if the converter is ON
657  {
658  /*---------------------------------------------------------------------------*/
659  /* Feed-forward of the duty cycle in control path */
660  /*---------------------------------------------------------------------------*/
661  // Calculate ratio of AC measurement to constant 400V. duty = Vac/Vdc in inverter mode.
662  if (PFC.qVin_AC < AC_400V)
663  {
664  current_PI_ctr.ratio_FF = (PFC.qVin_AC * 14789) >> 15; //d=(1475/3268*2^15)*Vac*2^-15
665  }
666  else
667  {
668  current_PI_ctr.ratio_FF = MAX_DUTY; // Vac close to 400V, ratio is limited to maximum duty.
669 
670 // /* DEBUGGING pin */
671 // XMC_GPIO_ToggleOutput(P0_12);
672  }
673  // Apply a weight to the feed-forward path.
674  // Move the current tracking to the current controller to account for mismatches and constant Vdc in calculation.
675  current_PI_ctr.ratio_FF = (current_PI_ctr.ratio_FF * 32767) >> 15; //29491 --> 0.9; 31129 --> 0.95; 32767 --> 1
676 
677  // PFC mode
678  if(!PFC.pfc_inv_mode)
679  {
680  //Duty in PFC mode is 1-Vac/Vdc
682  }
683 
684  /*---------------------------------------------------------------------------*/
685  /* Current loop calculation */
686  /*---------------------------------------------------------------------------*/
687  // Current error (Q15) for control loop calculation
688  current_PI_ctr.state_error[0] = (int32_t)current_PI_ctr.PI_ref - (int32_t)PFC.qiL_k;
689 
690  /*
691  * Current compensator with one zero (PI)
692  * d[k] = d[k-1] + a0 * e[k] + a1 * e[k-1]
693  *
694  * error*coeff => Q15 (16b) * Q14 (16b) = Q29 (32b) --> 1cycle 32bit multiplication
695  *
696  * state_output Q29-32b --> duty Q13 16b
697  */
700 #if (ILOOP_COEFF_Q < 14)
701  current_PI_ctr.state_output[0] = current_PI_ctr.state_output[0] << current_PI_ctr.shift_to_q29; //Not necessary if Q14 coeff.
702 #endif
704 
705  // Current controller limitation
707  {
709  }
711  {
713  }
714 
715  /*---------------------------------------------------------------------------*/
716  /* Duty cycle calculation: feed-foward + current loop */
717  /*---------------------------------------------------------------------------*/
718  // Duty composes of feed-forward plus current loop
720 
721  // Duty cycle limitation
723  {
725  }
726  else if (current_PI_ctr.duty < MIN_DUTY)
727  {
729 
730 // /* DEBUGGING pin */
731 // XMC_GPIO_ToggleOutput(P0_12);
732  }
733 
734  // Duty cycle update in close-loop operation without feed-forward
735  set_duty_ADC(current_PI_ctr.duty,PR_MAX);//current_PI_ctr.PR_calc);
736 
737  // Save output values for the next calculation
739 
740  // Save error values for the next calculation
742  }
743 }
744 
745 /*---------------------------------------------------------------------------*/
751 #ifndef DOXYGEN_SKIP
752 __attribute__((section(".ram_code")))
753 #endif
754 static inline void iL_ref()
755 {
756  /*--------------------------------------------------------------------------*/
757  /* CURRENT REFERENCE CALCULATION */
758  /*--------------------------------------------------------------------------*/
759 // PFC.qVin_AC = (*ADCResult_VAC & 0xFFF);
760 
761  // PFC mode
762  if(!PFC.pfc_inv_mode)
763  {
764  // Calculate I_ref for the current loop in PFC mode: VAC * 1/Vrms2 * V-loop-output
766  }
767  else // Inverter mode
768  {
769  // Calculate I_ref for the current loop in inverter mode: VAC * 1/Vrms2 * P_available
771  }
772 
773  // Iref in Q15 --> consider iL as Q15, therefore as a gain in the current measurement
774  current_PI_ctr.PI_ref = ((uint64_t) current_PI_ctr.PI_ref_int * (uint64_t) PFC.qVin_AC) >> CURRENT_REF_TO_Q15;
775 
776  // AC sensing asymmetry compensation (Kneg = 1.009973 * Kpos; 0.009973 = 20915(Q21))
777  if (input_polarity[0])
778  {
780  }
781 
782 // /* Multiply current reference when the bulk voltage is under certain range ("wake-up circuit") --> PFC */
783 // if (offset_detected)
784 // {
785 // if (current_PI_ctr.PI_ref < (I_IN_MAX_FACTOR-10))
786 // {
787 // current_PI_ctr.PI_ref = current_PI_ctr.PI_ref << I_IN_FACTOR;
788 // }
789 // else
790 // current_PI_ctr.PI_ref = current_PI_ctr.PI_ref_max;
791 // }
792 
793  /* Maximum current limitation. AVG current limitation. */
795  {
797  }
798  else if (current_PI_ctr.PI_ref < 1)
799  {
801 
802 // /* DEBUGGING pin */
803 // XMC_GPIO_ToggleOutput(P0_12);
804  }
805 }
806 
807 /*---------------------------------------------------------------------------*/
813 #ifndef DOXYGEN_SKIP
814 __attribute__((section(".ram_code")))
815 #endif
816 static inline void iL_coeff()
817 {
818  /*------------------------------------------------------------------------------------------------------------------*/
819  /* Current loop coefficients based on current (variable inductance - CCM; variable BW according to current level) */
820  /*------------------------------------------------------------------------------------------------------------------*/
821  // Region below 0A
822  if ((PFC.qiL_k < INEG_FAULT_Q15) && current_loop_ON) //Fast turn off if high negative current detected during operation.
823  {
824  // Turn off as protection. Something is not working as it should.
825  PWM_OFF();
826  SR_OFF();
827 
828  // Voltage loop should keep running in AC-LCDO, no current loop since something went wrong.
829  current_loop_ON = false;
830 
831  // Do not allow turn ON in the pseudo-pll function.
832  fault_reg |= F_NEG_I;
833 
834  // Time to restart the converter operation
835  AC_zero_cross.neg_iL_fault_count = PWM_TRANSITION_DLY + 5; // 5*1/65kHzs = 77us
836  }
837  else if (PFC.qiL_k < 0) //a0 value at highest current (lowest coefficient)
838  {
840  }
841  else if (PFC.qiL_k <= REG1_ILIMIT_Q15) // Linear Region (below 36A)
842  {
844  }
845  else //Constant Region (over 36A)
846  {
848  }
849 
850  // a1 coefficient calculation based on a0 and zero location
852 }
853 
854 /*---------------------------------------------------------------------------*/
861 #ifndef DOXYGEN_SKIP
862 __attribute__((section(".ram_code")))
863 #endif
864 static inline void iL_deadtime()
865 {
866  /*------------------------------------------------------------------------------------------------------------------*/
867  /* Dead time and switching frequency calculation based on current */
868  /*------------------------------------------------------------------------------------------------------------------*/
869 
871  {
872  if (PFC.qiL_k < parameters_RAM.dt_const_iLimit) // maximum value
873  {
874  pwm_pfc.ccu8_slice_ptr->DC1R = (((uint32_t) parameters_RAM.dt_d2s) << CCU8_CC8_DC1R_DT1F_Pos) | ((uint32_t) parameters_RAM.dt_s2d_MAX);
875  }
876  else if (PFC.qiL_k < parameters_RAM.dt_reg1_iLimit)// DT_REG1_ILIMIT_Q15) // linear region 1
877  {
878  pwm_pfc.ccu8_slice_ptr->DC1R = (((uint32_t) parameters_RAM.dt_d2s) << CCU8_CC8_DC1R_DT1F_Pos) | ((uint32_t) (parameters_RAM.dt_s2d_offreg1 - ((parameters_RAM.dt_s2d_slopereg1 * PFC.qiL_k) >> DT_SLOPE_Q)));
879  }
880  else // linear region 2
881  {
882  pwm_pfc.ccu8_slice_ptr->DC1R = (((uint32_t) parameters_RAM.dt_d2s) << CCU8_CC8_DC1R_DT1F_Pos) | ((uint32_t) (parameters_RAM.dt_s2d_offreg2 - ((parameters_RAM.dt_s2d_slopereg2 * PFC.qiL_k) >> DT_SLOPE_Q)));
883  }
884  }
885  else
886  {
887  // minimum DT value
888  pwm_pfc.ccu8_slice_ptr->DC1R = (((uint32_t) parameters_RAM.dt_d2s) << CCU8_CC8_DC1R_DT1F_Pos) | ((uint32_t) parameters_RAM.dt_s2d_MIN);
889  }
890 }
891 
892 /*---------------------------------------------------------------------------*/
898 #ifndef DOXYGEN_SKIP
899 __attribute__((section(".ram_code")))
900 #endif
901 static inline void notch_filter_Vbulk()
902 {
903  /*--------------------------------------------------------------------------*
904  * DC Voltage filter --> cascaded notch - START *
905  *--------------------------------------------------------------------------*/
906 
907  //input to the filter
908  auxDC = (PFC.qVbus_DC * DCfilter.k1) >> IIR_QFORMAT;
909 
910  // Section 1 calculation
911  DCfilter.calc11 = ((int64_t)DCfilter.u1[1]*(int64_t)DCfilter.b1[1]) >> IIR_QFORMAT;
912  DCfilter.calc21 = ((int64_t)DCfilter.u1[2]*(int64_t)DCfilter.b1[2]) >> IIR_QFORMAT;
913 
914  DCfilter.u1[0] = auxDC - DCfilter.calc11 - DCfilter.calc21; //Intermediate state
915  DCfilter.output1 = (DCfilter.a1[0]*DCfilter.u1[0] + DCfilter.a1[1]*DCfilter.u1[1] + DCfilter.a1[2]*DCfilter.u1[2]) >> IIR_QFORMAT; //Q15 output is the input of the next section
916 
917  // Save section 1 values for next iteration
918  DCfilter.u1[2] = DCfilter.u1[1];
919  DCfilter.u1[1] = DCfilter.u1[0];
920 
921  // Section 2 calculation
922  DCfilter.calc12 = ((int64_t)DCfilter.u2[1]*(int64_t)DCfilter.b2[1]) >> IIR_QFORMAT;
923  DCfilter.calc22 = ((int64_t)DCfilter.u2[2]*(int64_t)DCfilter.b2[2]) >> IIR_QFORMAT;
924 
926  DCfilter.u2[0] -= DCfilter.calc12;
927  DCfilter.u2[0] -= DCfilter.calc22;
929 
930  // Limit to avoid negative numbers
931  if(DCfilter.output < 1)
932  DCfilter.output = 1;
933 
934  // DC value for control loop
936 
937  // Save section 2 values for next iteration
938  DCfilter.u2[2] = DCfilter.u2[1];
939  DCfilter.u2[1] = DCfilter.u2[0];
940 
941  /*--------------------------------------------------------------------------*
942  * DC Voltage filter --> cascaded notch - END *
943  *--------------------------------------------------------------------------*/
944 }
945 
946 /*---------------------------------------------------------------------------*/
953 #ifndef DOXYGEN_SKIP
954 __attribute__((section(".ram_code")))
955 #endif
956 static inline void lpf_filter_RMS()
957 {
958  /*--------------------------------------------------------------------------*
959  * RMS IIR filter calculation (LPF), two-section Direct-form II - START *
960  *--------------------------------------------------------------------------*/
961 
962  auxAC = ((PFC.qVin_AC << ADC_TO_Q15) * ACfilter.k1) >> IIR_QFORMAT; //input to the filter
963 
964  // Section 1 calculation
965  ACfilter.calc11 = ((int64_t)ACfilter.u1[1]*(int64_t)ACfilter.b1[1]) >> IIR_QFORMAT;
966  ACfilter.calc21 = ((int64_t)ACfilter.u1[2]*(int64_t)ACfilter.b1[2]) >> IIR_QFORMAT;
967 
968  ACfilter.u1[0] = auxAC - ACfilter.calc11 - ACfilter.calc21; //Intermediate values Q15
969  ACfilter.output1 = (ACfilter.a1[0]*ACfilter.u1[0] + ACfilter.a1[1]*ACfilter.u1[1] + ACfilter.a1[2]*ACfilter.u1[2]) >> IIR_QFORMAT; //Q29 output is the input of the next section
970 
971  // Save section 1 values for next iteration
972  ACfilter.u1[2] = ACfilter.u1[1];
973  ACfilter.u1[1] = ACfilter.u1[0];
974 
975  // Section 2 calculation
976  ACfilter.calc12 = ((int64_t)ACfilter.u2[1]*(int64_t)ACfilter.b2[1]) >> IIR_QFORMAT;
977  ACfilter.calc22 = ((int64_t)ACfilter.u2[2]*(int64_t)ACfilter.b2[2]) >> IIR_QFORMAT;
978 
980  ACfilter.u2[0] -= ACfilter.calc12;
981  ACfilter.u2[0] -= ACfilter.calc22;
982  ACfilter.output = ACfilter.a2[0]*ACfilter.u2[0] + ACfilter.a2[1]*ACfilter.u2[1] + ACfilter.a2[2]*ACfilter.u2[2]; //Q29 output
983 
984  // Limit to avoid negative numbers and division by zero
985  if(ACfilter.output < 1)
986  ACfilter.output = 1;
987 
988  // "RMS" value for multiplier input
989  PFC.qVin_BOP = (uint16_t)(ACfilter.output >> LPF_TO_2ADC);
990 
991  // Low limit of AC-filter output to avoid division by zero or a small RMS value.
992  if (PFC.qVin_BOP < VRMS_MIN_LIMIT) // 1176 (80V_RMS) is half of the minimum voltage considered to start the converter (2589 --> 176V_RMS)
993  {
994  PFC.qVin_BOP = VRMS_MIN_LIMIT;
995  }
996 
997  // Save section 2 values for next iteration
998  ACfilter.u2[2] = ACfilter.u2[1];
999  ACfilter.u2[1] = ACfilter.u2[0];
1000 
1001  /*--------------------------------------------------------------------------*
1002  * RMS IIR filter calculation (LPF), two-section Direct-form II - END *
1003  *--------------------------------------------------------------------------*/
1004 }
1005 
1006 /*---------------------------------------------------------------------------*/
1012 #ifndef DOXYGEN_SKIP
1013 __attribute__((section(".ram_code")))
1014 #endif
1015 static inline void state_machine()
1016 {
1017  /*---------------------------------------------------------------------------*/
1018  /* Starting sequence / state machine - START */
1019  /*---------------------------------------------------------------------------*/
1020  switch (status_reg) {
1021 
1022  case ST_INIT:
1023 
1024  // Turn off PWM, SR and relay in case INIT state is reached
1025  PWM_OFF();
1026  SR_OFF();
1027  relay_OFF();
1028 
1029  /* Power stage is OFF, loops not executed */
1030  power_stage_ON = false;
1031  current_loop_ON = false;
1032 
1033  /* Reset control loops and update status */
1035 
1036  // PFC mode
1037  if(!PFC.pfc_inv_mode)
1038  {
1039  // Close the relay and move to stand-by based on bulk voltage (Vout) range
1040  if ((!time_INIT) && (PFC.qVbus_DC_filt > VOUT_START_MIN))
1041  {
1042  // Next state: converter ready to start.
1043  status_reg = ST_STB;
1044 
1045  /* Set start-up timer in case the state machine arrives to INIT during operation */
1046  time_INIT = 550;//30;
1047 
1048  /* Reset BOP state timer in case the state machine arrives to INIT during operation */
1049  stb_bop_count = 0;
1050  }
1051  }
1052  //Inverter mode
1053  else
1054  {
1055  // Close the relay and move to stand-by based on bulk voltage (Vin) range
1056  if ((!time_INIT) && (PFC.qVbus_DC_OK)) //qVbus_DC_OK is managed in bulk_monitoring() function
1057  {
1058  // Next state: converter ready to start.
1059  status_reg = ST_STB;
1060 
1061  /* Set start-up timer in case the state machine arrives to INIT during operation */
1062  time_INIT = 550;//30;
1063 
1064  /* Reset BOP state timer in case the state machine arrives to INIT during operation */
1065  stb_bop_count = 0;
1066  }
1067  }
1068 
1069  break;
1070 
1071  case ST_STB: // Wait for proper AC voltage (background process)
1072 
1073  // Keep PWM and relay off
1074  PWM_OFF();
1075  SR_OFF();
1076  relay_OFF();
1077 
1078  /* Power stage is OFF, loops not executed */
1079  power_stage_ON = false;
1080  current_loop_ON = false;
1081 
1082  /* Reset counter for relay on-off timing.
1083  * Wait a defined number of cycles after closing the relay, before starting the PFC */
1084  stb_bop_count = 0;
1085 
1086  break;
1087 
1088  case ST_STB_BOP: // Relay management after DC and AC voltages are in range
1089 
1091  {
1092  // PWM and relay remain OFF
1093  PWM_OFF();
1094  SR_OFF();
1095  relay_OFF();
1096 
1097  /* Power stage is OFF, loops not executed */
1098  power_stage_ON = false;
1099  current_loop_ON = false;
1100 
1101  // Relay on counter update
1102  stb_bop_count++;
1103  }
1105  {
1106  // Current offset sampling
1107  if ((PFC.qiL_offset_count == 0) && (!PFC.iL_offset_done))
1108  {
1109  PFC.iL_offset_en = true;
1110  }
1111  else if (PFC.qiL_offset_count == OFFSET_END)
1112  {
1113  // Relay is switched on after current offset has been obtained
1114  relay_ON();
1115 
1116  // Relay on counter update
1117  stb_bop_count++;
1118 
1119  // No offset calculation after decision to start
1120  PFC.iL_offset_en = false;
1121 // PFC.iL_offset_done = true;
1122  }
1123  }
1125  {
1126  /* Save DC-bus value for V-loop soft-start */
1128 
1129  // Relay on counter update
1130  stb_bop_count++;
1131  }
1132  else if (stb_bop_count == ST_STB_BOP_END)
1133  {
1134  /* Go to soft-start, starting in negative AC cycle */
1135  status_reg = ST_START_EN;//ST_LATCH;//
1136  }
1137  else
1138  {
1139  // Relay on counter update
1140  stb_bop_count++;
1141  }
1142 
1143  break;
1144 
1145  case ST_START_EN:
1146 
1147  break;
1148 
1149  case ST_SOFT_START:
1150 
1151  // PFC mode
1152  if(!PFC.pfc_inv_mode)
1153  {
1154  /*--------------------------------------------------------------------------*/
1155  /* Bulk voltage soft-start for PFC operation */
1156  /*--------------------------------------------------------------------------*/
1157 
1158  if (!time_V_step)
1159  {
1161  {
1162  /* Ramp-up the output voltage target (SteadyREF / 2^6) */
1164 
1165  // Limit the maximum reference
1167  {
1169  }
1170 
1171  // Xms until next loop step
1172  time_V_step = 4;
1173  }
1174  else
1175  {
1176  /* Soft-start is done, voltage reference is the targeted output voltage */
1178 
1179  /* State change, for the moment no action when running */
1180  status_reg = ST_RUNNING;//ST_LATCH;//
1181 
1182  // Soft-start is done
1184  }
1185  }
1186  }
1187 
1188  break;
1189 
1190  case ST_LATCH: // For debugging purposes. Keep converter OFF
1191 
1192  PWM_OFF();
1193  SR_OFF();
1194 
1195  power_stage_ON = false;
1196  current_loop_ON = false;
1197 
1198  break;
1199 
1200  case ST_RUNNING:
1201 
1202  // Inverter mode
1203  if (PFC.pfc_inv_mode)
1204  {
1205  // Inverter power is changed close to the zero crossing
1206  if ((PFC.qVin_AC < OFFSET_VAC) && (PFC.inv_power != parameters_RAM.power_target))
1207  {
1208  // Update of the inverter power
1210  {
1212  {
1213  PFC.inv_power += (parameters_DFLT.power_target >> 1);
1214  }
1215  else
1216  {
1218  }
1219  }
1220  else if (PFC.inv_power <= parameters_DFLT.power_target)
1221  {
1223  }
1224  else
1225  {
1226  PFC.inv_power -= (parameters_DFLT.power_target >> 1);
1227  }
1228  }
1229  }
1230 
1231  break;
1232 
1233  default:
1234 
1235  break;
1236  }
1237 }
1238 
1239 /*---------------------------------------------------------------------------*/
1245 #ifndef DOXYGEN_SKIP
1246 __attribute__((section(".ram_code")))
1247 #endif
1248 static inline void Vbulk_ctrl_loop()
1249 {
1250  // Voltage error (Q15)
1252 
1253  /* Error management in controller first iteration */
1255  {
1256  // Fill the error array for smooth transition at first iteration
1257  //voltage_int_ctr.error[2] = voltage_int_ctr.error[0];
1259 
1260  // No first iteration any more
1261  voltage_int_ctr.first_iter = false;
1262  }
1263 
1264  /*
1265  * Voltage compensator with integrator, single pole and single zero (PI + pole)
1266  * Vloop[k] = b1*Vloop[k-1] + b2*Vloop[k-2] + a0*ev[k] + a1*ev[k-1]
1267  * error*coeff => Q15 (16b) * Qx (16b) = Q(15+x) (32b) --> 1cycle 32bit multiplication
1268  * vloop*coeff => Q15 (16b) * Qx (16b) = Q(15+x) (32b) --> 1cycle 32bit multiplication
1269  * output_int = Vloop[k] << (16-x) (Q31)
1270  * output_int_limit = output (Q15) --> upper byte of output_int
1271  * integrator-antiwindup
1272  */
1273  voltage_int_ctr.interm_calc1 = (int32_t)voltage_int_ctr.a[0] * (int32_t)voltage_int_ctr.error[0];
1274  voltage_int_ctr.interm_calc1 += (int32_t)voltage_int_ctr.a[1] * (int32_t)voltage_int_ctr.error[1];
1275 
1276  voltage_int_ctr.interm_calc2 = (int32_t)voltage_int_ctr.b[1] * (int32_t)voltage_int_ctr.state[1];
1277  voltage_int_ctr.interm_calc2 += (int32_t)voltage_int_ctr.b[2] * (int32_t)voltage_int_ctr.state[2];
1278 
1280 
1281  // Voltage control loop saturation (Current reference limitation)
1283  {
1285  }
1286  else if ((int16_t)(*voltage_int_ctr.output_int_limit) < 1)
1287  {
1289  }
1290 
1291  // External variable for current reference calculation
1293 
1294  // Save state for next calculation
1297 
1298  // Save error for next calculation
1301 }
1302 
1303 /*---------------------------------------------------------------------------*/
1309 #ifndef DOXYGEN_SKIP
1310 __attribute__((section(".ram_code")))
1311 #endif
1312 static inline void inv_RMS_acok()
1313 {
1314  // Low limit of AC-filter output to avoid division by zero or a small RMS value.
1315  if (PFC.qVin_BOP < VRMS_MIN_LIMIT) // 1176 (80V_RMS) is half of the minimum voltage considered to start the converter (2589 --> 176V_RMS)
1316  {
1317  PFC.qVin_BOP = VRMS_MIN_LIMIT;
1318  }
1319 
1320  /* 1/Vrms² --> uint32 multiplication & DIV kernel */
1321  Vrms2 = PFC.qVin_BOP*PFC.qVin_BOP;
1322 
1323  /* Load division operands.
1324  * 0x7FFFFFFF is 0.9999999 in Q31.
1325  * Vrms2 is 32bits unsigned
1326  * Result is 1/Vrms^2 with 32 bit Q31 resolution */
1327  MATH ->DVD = 0x7FFFFFFF;
1328  MATH ->DVS = Vrms2;
1329 
1330  /* Use the DIV kernel calculation time to check AC voltage*/
1331  if (PFC.qVin_AC < 72)
1332  {
1333  // Count 3 consecutive samples under 10V of no AC to trigger ACOK LOW
1334  acok_counter++;
1335  if ((acok_counter > 2) && acok)
1336  {
1337  // AC LCDO detected
1338  acok = false;
1339  // Reset counter
1340  acok_counter = 0;
1341  }
1342  }
1343  else if ((PFC.qVin_AC > 1699) && !acok) //1699: AC is over 165Vrms (233Vac_pk)
1344  {
1345  // Input voltage considered ok
1346  acok = true;
1347 
1348  // 150ms of extra voltage after AC lost (LCDO - LLC regulation)
1349  time_extra_bulk = 150;
1350  }
1351  else
1352  // Reset counter
1353  acok_counter = 0;
1354 
1355 
1356  /* Poll the division result */
1357  while (MATH ->DIVST);
1358  /* Result of the division in Q31 */
1359  PFC.qVin_inv2 = MATH ->QUOT;
1360 }
1361 
1362 /*---------------------------------------------------------------------------*/
1370 #ifndef DOXYGEN_SKIP
1371 __attribute__((section(".ram_code")))
1372 #endif
1373 static inline void bulk_monitoring()
1374 {
1375  // PFC mode
1376  if(!PFC.pfc_inv_mode)
1377  {
1378  /*---------------------------------------------------------------------------*/
1379  /* Output voltage monitoring in PFC operation - start */
1380  /*---------------------------------------------------------------------------*/
1381  if (status_reg & PFC_RUNNING) // Protection active from soft-start;
1382  {
1383  if ((PFC.qVbus_DC > VOUT_OVP_STOP) && !(fault_reg & F_OVP))
1384  {
1385  /* Turn-off PWM if the Vout is over the limit for X samples in a row */
1387  if (count_Vout_OVP_stop > 0)
1388  {
1389  /* Set fault flag */
1390  fault_reg |= F_OVP;
1391 
1392  /* Turn-off PWM and SR */
1393  PWM_OFF();
1394 
1395  /* Power stage is OFF, loops not executed */
1396  power_stage_ON = false;
1397  current_loop_ON = false;
1398 
1399  /* Reset Over Voltage counter */
1400  count_Vout_OVP_stop = 0;
1401 
1402  // /* DEBUGGING - LATCH THE CONVERTER IN CASE OF OVP FOR LOW VOLTAGE TEST */
1403  // status_reg = ST_LATCH;
1404  }
1405  }
1407  else if ((PFC.qVbus_DC < voltage_int_ctr.int_ref) && (fault_reg & F_OVP))
1408  {
1409  /* Turn-on PWM if the Vout is under the limit for X samples in a row */
1411 
1412  if (count_Vout_OVP_resume > 0)
1413  {
1414  /* Voltage error initialization */
1415  voltage_int_ctr.error[0] = 0x0;
1416  voltage_int_ctr.error[1] = 0x0;
1417  voltage_int_ctr.error[2] = 0x0;
1418 
1419  // Voltage loop soft start
1420  voltage_int_ctr.output = 0;
1422  voltage_int_ctr.state[1] = 0;
1423  voltage_int_ctr.state[2] = 0;
1424 
1425  // First iteration in resume operation for smoother V-controller transition
1426  voltage_int_ctr.first_iter = true;
1427 
1428  /* Current compensator output (duty) and input (error) initialization */
1429  // Error
1430  current_PI_ctr.state_error[0] = 0x0;
1431  current_PI_ctr.state_error[1] = 0x0;
1432  // Output
1433  current_PI_ctr.state_output[0] = 0x0; // Starting duty cycle
1434  current_PI_ctr.state_output[1] = 0x0;
1435 
1438  /* zero duty cycle to resume operation.
1439  * It will be overwritten by pseudo_pll() at AC zero crossing */
1441 
1442  /* Reset OverVoltage Counter */
1444 
1445  /* Clear the fault flag.
1446  * Allow converter to resume operation in next AC zero crossing */
1447  fault_reg &= ~F_OVP;
1448  }
1449  }
1450  else
1451  {
1452  /* Reset OverVoltage Stop Counter */
1453  count_Vout_OVP_stop = 0;
1454  /* Reset OverVoltage Resume Counter */
1456  }
1457  }
1458 
1459  // Undervoltage 310V restarts the converter operation with soft-start sequence
1460  if ((PFC.qVbus_DC < VOUT_UVP_MIN) && (status_reg & ST_RUNNING))
1461  {
1462  /* Turn-off the converter */
1463  PWM_OFF();
1464  SR_OFF();
1465  // PWM cannot be turned ON --> Decision in PWM interrupt
1466  power_stage_ON = false;
1467  current_loop_ON = false;
1468 
1469  // RELAY OFF for soft-start procedure
1470  relay_OFF();
1471 
1472  /* Go to INIT state to start the soft-start sequence including the relay management */
1473  status_reg = ST_INIT;//ST_LATCH;//
1474 
1475  // Wait 30ms before starting the sequence
1476  time_INIT = 550;//30;
1477  }
1478 
1479 
1480  /* Detect error outside a defined slot for "waking-up circuit" */
1481  if (status_reg == ST_RUNNING)
1482  {
1483  if (!offset_detected)
1484  {
1485  if (PFC.qVbus_DC < VOLTAGE_OFFSET_NL_ON)
1486  {
1487  // From next iteration check for the voltage to return to nominal value
1488  offset_detected = 1;
1489  }
1490  }
1491  else
1492  {
1493  if (PFC.qVbus_DC > voltage_int_ctr.int_ref)
1494  {
1495  //Allow again detection of bulk voltage out of the defined slot
1496  offset_detected = 0;
1497  }
1498  }
1499  }
1500 
1501  /*---------------------------------------------------------------------------*/
1502  /* Output voltage monitoring in PFC operation - end */
1503  /*---------------------------------------------------------------------------*/
1504  }
1505  else // Inverter mode
1506  {
1507  // If converter is in operation, turn off if bulk voltage is out of range (370V - 430V)
1508  if(status_reg & 0xF)
1509  {
1511  {
1512  /* Turn-off the converter */
1513  PWM_OFF();
1514  SR_OFF();
1515  // PWM cannot be turned ON --> Decision in PWM interrupt
1516  power_stage_ON = false;
1517  current_loop_ON = false;
1518 
1520 // // RELAY OFF for soft-start procedure
1521 // relay_OFF();
1522 
1523  /* Go to INIT state to start the soft-start sequence including the relay management */
1524  status_reg = ST_LATCH;//ST_INIT;//
1525  fault_reg |= F_DC_INV;
1526 
1527  // Wait 250ms before starting the sequence
1528  time_INIT = 550;
1529 
1530  // No start in inverter mode until DC voltage is in range
1531  PFC.qVbus_DC_OK = false;
1532  }
1533  }
1534  // If converter is off, turn on if bulk voltage is in range (390V - 410V)
1535  else
1536  {
1538  {
1539  if(!status_reg && !fault_reg) //((!status_reg || !(status_reg & ST_LATCH)) && !fault_reg)
1540  {
1541  // Turn on converter --> Start state machine
1542  PFC.qVbus_DC_OK = true;
1543  }
1544  }
1545  }
1546  }
1547 }
1548 
1549 /*---------------------------------------------------------------------------*/
1558 #ifndef DOXYGEN_SKIP
1559 __attribute__((section(".ram_code")))
1560 #endif
1561 static inline void pseudo_pll()
1562 {
1563  static uint16_t plrty_transition_dly_OFF,plrty_transition_dly_ON;
1564 
1565 // /* Pin P0.10: software GPIO*/
1566 // XMC_GPIO_SetOutputHigh(P0_12);
1567 
1568 
1569  /* Vac voltage drops under OFF threshold, going into a zero crossing. */
1570  if(PFC.qVin_AC < VIN_OFF_THRSLD)
1571  {
1572  /*
1573  * Switches OFF PWM and starts transition to
1574  * opposite polarity after delay.
1575  */
1576  if(plrty_transition_dly_OFF == 0)
1577  {
1578  /* Checks first sample going under the OFF threshold. */
1579  if(!AC_zero_cross.pwm_zc_disabled)
1580  {
1581  /* Disable PWM and SR signals. */
1582  PWM_OFF();
1583  SR_OFF();
1584 
1585  /* Sets a delay before starting PWM polarity transition. */
1586  plrty_transition_dly_ON = PWM_TRANSITION_DLY;
1587 
1588  /* Normal PWM modulation disabled during Vac cross transition. */
1589  AC_zero_cross.pwm_zc_disabled = true;
1590 
1591  //Clear current fault. Allow restart after zero crossing
1593  fault_reg &= ~F_NEG_I;
1594  AC_zero_cross.neg_iL_fault_count = 0;
1595 
1596  //Save polarity as input to next polarity decision filter
1598  }
1599 
1600  // Flag raised when the AC voltage gets close to zero; enables PWM start when AC voltage goes over ON_THRSLD
1601  if (PFC.qVin_AC < VIN_ZERO_THRSLD)
1602  {
1603  AC_zero_cross.vac_zero_cross = true;
1604  }
1605  }
1606 
1607  /* Counter of Vac drop out detection. */
1608  AC_zero_cross.vin_low_cnt ++;
1609 
1610  /* Choose the starting duty cycle and switching frequency for restart.
1611  * In the current implementation, Fsw is kept constant for all the conditions. */
1612  if (AC_zero_cross.pwm_zc_disabled)
1613  {
1615  if(AC_zero_cross.vin_low_cnt > AC_LCDO_DETECT)
1616  {
1617  // Increased BW after AC lost detected
1618  AC_zero_cross.after_AClost_cnt = AFTER_ACLOST_TIME;
1619 
1620  // In INVERTER mode, if AC is lost, set the current reference to the starting value
1621  if (PFC.pfc_inv_mode)
1622  {
1623  status_reg = ST_START_EN;//ST_SOFT_START;
1625 
1626  // Set starting power to zero for current/power soft start, according to DFLT parameters target */
1627  PFC.inv_power = parameters_DFLT.power_target;//PFC.inv_power_ref>>3;//700;//0;//
1628  }
1629  }
1630 
1631  //No current loop if the PWM is off
1632  current_loop_ON = false;
1633 
1634  /* Current compensator output (duty) and input (error) initialization */
1635  // Error
1636  current_PI_ctr.state_error[0] = 0x0;
1638  // Output
1639  // Starting loop value in steady state with duty feed-forward operation.
1642 
1643  // PFC mode
1644  if(!PFC.pfc_inv_mode)
1645  {
1646  // MAX duty cycle to resume in PFC OPERATION
1647  // Loop will not execute until AC voltage goes over ON_THRSLD
1649  }
1650  else // Inverter mode
1651  {
1652  // MIN Duty cycle to resume in INV OPERATION
1653  // Loop will not execute until AC voltage goes over ON_THRSLD
1655  }
1656  }
1657  }
1658  else
1659  {
1660  /* Resets counter for Vin drop out detection. */
1661  AC_zero_cross.vin_low_cnt = 0;
1662  }
1663 
1664  /*
1665  * Vac above ON threshold.
1666  * Note: This could be happening left side or right side of the actual
1667  * Vac crossing, as Vac OFF threshold is > than Vac ON threshold.
1668  * Wait for polarity change decision.
1669  */
1670  if ((PFC.qVin_AC > VIN_ON_THRSLD))
1671  {
1672  if ((AC_zero_cross.vac_zero_cross) || (AC_zero_cross.neg_iL_fault_CLR))
1673  {
1674  /*--------------------------------------------------------------------------*/
1675  /* polarity detection before decision of starting PWM/SR */
1676  /*--------------------------------------------------------------------------*/
1677  if ((ac_polarity.gpio_port->IN) & 0x10)
1678  {
1679  if (input_polarity[1])
1680  {
1682  }
1683  else
1684  {
1686  }
1687  input_polarity[1] = POSITIVE;
1688  }
1689  else
1690  {
1691  if (input_polarity[1])
1692  {
1694  }
1695  else
1696  {
1698  }
1699  input_polarity[1] = NEGATIVE;
1700  }
1701 
1702  if (input_polarity_count > 1) // Filter of x-times HIGH/LOW detected
1703  {
1704  if(input_polarity[1]) /* Positive AC voltage detected */
1705  {
1706  input_polarity[0] = POSITIVE;
1707 
1708  // PFC mode
1709  if(!PFC.pfc_inv_mode)
1710  {
1711  // Boost switch is HS
1712  pwm_pfc.ccu8_slice_ptr->CHC |= ((1 << CCU8_CC8_CHC_OCS1_Pos) | (1 << CCU8_CC8_CHC_OCS2_Pos));
1713 
1714  // Configure the use of proper current comparator output for peak current limitation
1715  peak_PWM_trap.eru->EXOCON[0] = 0x2004;
1716  }
1717  else // Inverter mode
1718  {
1719  // Buck switch is LS
1720  pwm_pfc.ccu8_slice_ptr->CHC &= ~(CCU8_CC8_CHC_OCS1_Msk | CCU8_CC8_CHC_OCS2_Msk);
1721 
1722  // Configure the use of proper current comparator output for peak current limitation
1723  peak_PWM_trap.eru->EXOCON[0] = 0x8004;
1724  }
1725  }
1726  else /* Negative AC voltage detected */
1727  {
1728  input_polarity[0] = NEGATIVE;
1729 
1730  // PFC mode
1731  if(!PFC.pfc_inv_mode)
1732  {
1733  // Boost switch is LS
1734  pwm_pfc.ccu8_slice_ptr->CHC &= ~(CCU8_CC8_CHC_OCS1_Msk | CCU8_CC8_CHC_OCS2_Msk);
1735 
1736  // Configure the use of proper current comparator output for peak current limitation
1737  peak_PWM_trap.eru->EXOCON[0] = 0x8004;
1738  }
1739  else // Inverter mode
1740  {
1741  // Buck switch is HS
1742  pwm_pfc.ccu8_slice_ptr->CHC |= ((1 << CCU8_CC8_CHC_OCS1_Pos) | (1 << CCU8_CC8_CHC_OCS2_Pos));
1743 
1744  // Configure the use of proper current comparator output for peak current limitation
1745  peak_PWM_trap.eru->EXOCON[0] = 0x2004;
1746  }
1747  }
1748 
1749  //Reset polarity change time filter
1751  // Flag for starting the PWM/SR after polarity decision
1752  AC_zero_cross.vin_on_allowed = true;
1753  }
1754 
1755  /*
1756  * Turn on PWM/SR if delay happened and polarity detected
1757  */
1758  if(plrty_transition_dly_ON == 0)
1759  {
1760  if(AC_zero_cross.vin_on_allowed)
1761  {
1762  /*
1763  * Voltage has risen after a Vac crossing detection.
1764  */
1765  if((AC_zero_cross.pwm_zc_disabled) || (AC_zero_cross.neg_iL_fault_CLR))
1766  {
1767  // Force the converter to start with negative AC cycle
1768  if((status_reg & ST_START_EN) && !input_polarity[0])
1769  {
1770  // Clear START_ALLOWED flag to avoid this step in the next cycles. Clear OFF to start the converter.
1771  status_reg = ST_SOFT_START;
1772  }
1773 
1774  if((!fault_reg) && (status_reg & PFC_RUNNING))
1775  {
1776  /*------------------------------------------------------------------------------------------*/
1777  /* Power soft-start for grid-connected inverter operation with variable power reference */
1778  /* Start power reference set in control_loop_init() as param_DFLT value */
1779  /*------------------------------------------------------------------------------------------*/
1780  if (PFC.pfc_inv_mode) //Inverter mode
1781  {
1782  if (stb_bop_count == (ST_STB_BOP_END+4)) //Some AC cycle with DFLT power_target
1783  {
1784  /* Go to running to manage the power reference */
1785  status_reg = ST_RUNNING;//ST_LATCH;//
1786 
1787  // Soft-start is done
1789  }
1790  else if (status_reg & ST_SOFT_START)
1791  {
1792  // Relay on counter update
1793  stb_bop_count++;
1794  }
1795  }
1796 
1797  /* Low frequency switches activated before the PWM. */
1798  if(input_polarity[0]) // POSITIVE AC
1799  {
1800  /* Appropriate slow MOSFET is turned ON. PWM config in polarity detection. */
1801  SR_HS_ON();
1802  }
1803  else // NEGATIVE AC
1804  {
1805  /* Appropriate slow MOSFET is turned ON. PWM config in polarity detection. */
1806  SR_LS_ON();
1807  }
1808 
1809  /* Voltage loop can be evaluated again. */
1810  power_stage_ON = true;
1811 
1812  /* Reset counting of switching cycles after starting PWM. */
1813  AC_zero_cross.vin_on_cnt = 0;
1814 
1815  /* Sets a delay before starting PWM polarity transition. */
1816  plrty_transition_dly_OFF = PWM_TRANSITION_DLY;
1817 
1818 // /*----------------------------------------------------------*/
1819 // /* COUNT THE NUMBER OF AC CHANGES FOR DEBUGGING - START */
1820 // /*----------------------------------------------------------*/
1821 // iDebug++;
1822 //
1823 // if (iDebug > 16) //((status_reg & ST_RUNNING) && (iDebug > 4)) //
1824 // {
1825 // PWM_OFF();
1826 // SR_OFF();
1827 //
1828 // power_stage_ON = false;
1829 //
1830 // status_reg = ST_LATCH;
1831 // }
1832 // /*----------------------------------------------------------*/
1833 // /* COUNT THE NUMBER OF AC CHANGES FOR DEBUGGING - END */
1834 // /*----------------------------------------------------------*/
1835 
1836  }
1837  /* Removes Vac crossing PWM disabled status flag. */
1838  AC_zero_cross.pwm_zc_disabled = false;
1839  /* Wait for new zero crossing or current fault for starting PWM/SR. */
1840  AC_zero_cross.vac_zero_cross = false;
1841  AC_zero_cross.neg_iL_fault_CLR = false;
1842  }
1843 
1844  /* Clear AC zero crossing flags */
1845  AC_zero_cross.vin_on_allowed = false;
1846  }
1847  }
1848  }
1849 
1850  /* Delay of PWM pulses in PFC/INV operation.
1851  * In inverter mode, the delay reduces the zero current distortion */
1852  if((power_stage_ON) && (AC_zero_cross.vin_on_cnt < SR_DELAY_SWCYC))
1853  {
1854  // Update cycle count
1855  AC_zero_cross.vin_on_cnt++;
1856 
1857  if (AC_zero_cross.vin_on_cnt == SR_DELAY_SWCYC)
1858  {
1859  if((!fault_reg) && (status_reg & PFC_RUNNING))
1860  {
1861  // Start PWM after SR is started
1862  PWM_ON();
1863 
1864  /* Current loop can be evaluated again */
1865  current_loop_ON = true;
1866  }
1867  // Update cycle count
1868  AC_zero_cross.vin_on_cnt++;
1869  }
1870  }
1871 
1872  // Timer after negative current fault is detected
1873  if(AC_zero_cross.neg_iL_fault_count)
1874  {
1875  AC_zero_cross.neg_iL_fault_count--;
1876  }
1877  else if(fault_reg & F_NEG_I) //after timer, check if there was a negative current fault
1878  {
1879  // Raise a flag a certain time after the fault is detected and still voltage present
1880  AC_zero_cross.neg_iL_fault_CLR = true;
1881  AC_zero_cross.neg_iL_fault_count = 0;
1882 
1883  // Clear the fault, so converter can restart.
1884  fault_reg &= ~F_NEG_I;
1885 
1886  //Save polarity as input to next polarity decision filter
1888 
1889  //Reset polarity change time filter
1891  }
1892  }
1893 
1894  /*
1895  * Transition timers
1896  */
1897  if(plrty_transition_dly_OFF)
1898  {
1899  plrty_transition_dly_OFF--;
1900  }
1901 
1902  if(plrty_transition_dly_ON)
1903  {
1904  plrty_transition_dly_ON--;
1905  }
1906 
1907  /*
1908  * AC lost timer for increased BW
1909  */
1910  if(AC_zero_cross.after_AClost_cnt)
1911  {
1912  AC_zero_cross.after_AClost_cnt--;
1913  }
1914 
1915 }
1916 
1917 #endif /* CONTROL_INTERRUPTS_H_ */
1918