3.3kW Totem-pole PFC with CoolMOS and XMC1402
control_interrupts.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, 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: 14 (CH33GT+E18-62t)
105 
107 #define COEFF_OFFSET_3k_Q (4140) //1P1Z/400V/5Vdd/65kHz/3kHzBW/zero=0.95/750R-47R-1.9SW: 4140 (CH33GT+E18-62t)
108 
109 #define COEFF_SLOPE_3k_Q (5928) //1P1Z/400V/5Vdd/65kHz/3kHzBW/zero=0.95/750R-47R-1.9SW: 5928 (CH33GT+E18-62t)
110 
111 #define COEFF_MIN_3k_Q (473) //1P1Z/400V/5Vdd/65kHz/3kHzBW/zero=0.95/750R-47R-1.9SW: 473 (CH33GT+E18-62t)
112 
114 #define ZERO_LOC_3k_Q (15564) //1P1Z/400V/5Vdd/65kHz/3kHzBW/zero=0.95/750R-47R-1.9SW: 15564 (CH33GT+E18-62t)
115 
117 #define REG1_ILIMIT_Q15 (20267) //5Vdd/750R-47R-1.9SW --> 34A-20267 (CH33GT+E18-62t)
118 
120 #define COEFF_a0_INEG_Q (3373) //1P1Z/400V/5Vdd/65kHz/3kHzBW/zero=0.95/750R-47R-1.9SW: 3373 (CH33GT+E18-62t)
121 
122 #define INEG_FAULT_Q15 (-4238) //-7.11A/5Vdd/750R-47R-1.9SW: -4238 (CH33GT+E18-62t)
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_CLR_OVP = 0x40,//64
267 // ST_D = 0x80,//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); //GPIO
439  PORT0->IOCR4 &= ~(uint32_t)(0x7c0000UL); //GPIO
440 // PORT1->IOCR0 &= ~(uint32_t)(0x7c000000UL);
441 }
442 
443 /*---------------------------------------------------------------------------*/
445 __STATIC_INLINE void
447 {
448  PORT0->IOCR0 |= 0x009C0000; //ALT7 Push-pull
449  PORT0->IOCR4 |= 0x00940000; //ALT5 Push-pull
450 // PORT1->IOCR0 |= 0x94000000; //ALT5 Push-pull
451 }
452 
453 /*---------------------------------------------------------------------------*/
455 __STATIC_INLINE void
457 {
458  PORT0-> OMR = (uint32_t)((0x10000U << 8) | (0x10000U << 9));
459 }
460 
461 /*---------------------------------------------------------------------------*/
463 __STATIC_INLINE void
465 {
466  PORT0-> OMR = (uint32_t)((0x1U << 8) | (0x10000U << 9));
467 }
468 
469 /*---------------------------------------------------------------------------*/
471 __STATIC_INLINE void
473 {
474  PORT0-> OMR = (uint32_t)((0x1U << 9) | (0x10000U << 8));
475 }
476 
477 /*---------------------------------------------------------------------------*/
479 __STATIC_INLINE void
481 {
482  PORT0->OMR = (uint32_t)0x1U << 11;
483 }
484 
485 /*---------------------------------------------------------------------------*/
487 __STATIC_INLINE void
489 {
490  PORT0-> OMR = (uint32_t)0x10000U << 11;
491 }
492 
493 /*---------------------------------------------------------------------------*/
496 __STATIC_INLINE __attribute__((section(".ram_code"))) void
497 set_duty_ADC(int16_t duty,int16_t period)
498 {
499  // PFC mode
500  if(!PFC.pfc_inv_mode)
501  {
502  // Distribute duty cycle since there is no change of pin function
503  if (!input_polarity[0]) // AC<0, LS/ST1_N is switch functionality
504  {
505  // PWM duty cycle
506  CCU80_CC81->CR1S = duty;
507 
508  // iL and Vac ADC (Queue) trigger
509  CCU80_CC81->CR2S = duty >> 1;
510 
511  // Vdc ADC (Scan) trigger
512  CCU80_CC80->CR2S = (period + duty) >> 1;//CCU80_CC83->CR2S = (period + duty) >> 1;
513  }
514  else // AC>0, HS/ST1 is switch functionality
515  {
516  // PWM duty cycle
517  CCU80_CC81->CR1S = period - duty;
518 
519  // iL and Vac ADC (Queue) trigger
520  CCU80_CC81->CR2S = period - (duty >> 1);
521 
522  // Vdc ADC (Scan) trigger
523  CCU80_CC80->CR2S = (period - duty) >> 1;//CCU80_CC83->CR2S = (period - duty) >> 1;
524  }
525  }
526 
527  // DEBUG DFF CALCULATION WITH PWM OUTPUT
528 // if(input_polarity[0])
529 // {
530 // CCU80_CC83->CR2S = 200;//current_PI_ctr.ratio_FF;
531 // }
532 // else
533 // {
534 // CCU80_CC83->CR2S = 800;
535 // }
536 // CCU80_CC83->CR2S = current_PI_ctr.ratio_FF;
537 
538  // Shadow transfer allowed
539  CCU80->GCSS |= (CCU8_GCSS_S1SE_Msk | CCU8_GCSS_S0SE_Msk);//CCU8_GCSS_S2SE_Msk | CCU8_GCSS_S3SE_Msk);
540 }
541 /*---------------------------------------------------------------------------*/
543 __STATIC_INLINE __attribute__((section(".ram_code"))) void
544 set_period(int16_t period)
545 {
546 // // PWM duty cycle
547 // CCU80_CC81->CR1S = duty;
548 // // iL and Vac ADC (Queue) trigger
549 // CCU80_CC81->CR2S = duty >> 1;
550 // // Vdc and T ADC (Scan) trigger
551 // CCU80_CC83->CR2S = (period + duty) >> 1;
552 
553  // PWM switching frequency
554  CCU80_CC81->PRS = period;
555 
556  // Shadow transfer allowed
557  CCU80->GCSS |= CCU8_GCSS_S1SE_Msk;
558 }
559 
560 /*---------------------------------------------------------------------------*/
566 #ifndef DOXYGEN_SKIP
567 __attribute__((section(".ram_code")))
568 #endif
569 static inline void iL_offset_calc()
570 {
571  /*------------------------------------------------------------------------------------------*/
572  /* Current offset (zero level) acquisition before starting the PFC (ST_STB_BOP) */
573  /*------------------------------------------------------------------------------------------*/
574 
575  if (PFC.iL_offset_en) // Current offset is calculated during start up sequence, before closing the relay
576  {
577  if (PFC.qiL_offset_count < (OFFSET_SAMPLES) ) // Accumulate a defined number of samples
578  {
579  if (PFC.qVin_AC < OFFSET_VAC) // Use current information for offset calculation if Vac is under 60V (432-ADC)
580  {
581  PFC.qiL_offset_acc += (*ADCResult_iL & 0x7FFF);
582  PFC.qiL_offset_count++;
583 
584 // /* DEBUGGING pin */
585 // XMC_GPIO_ToggleOutput(P0_12);
586  }
587  }
588  else
589  {
590  // Average of the accumulated current measurement is current offset. Normalization to Q15
593  PFC.iL_offset_en = false; // No current offset calculation in next iteration
594 // PFC.iL_offset_done = true;
595  }
596 
597  }
598 }
599 
600 /*---------------------------------------------------------------------------*/
606 #ifndef DOXYGEN_SKIP
607 __attribute__((section(".ram_code")))
608 #endif
609 static inline void iL_modif()
610 {
611  /*---------------------------------------------------------------------------*/
612  /* Current sensing modification */
613  /*---------------------------------------------------------------------------*/
614 
615  // Sensed current. Modified later according to positive/negative detection.
616  PFC.qiL = (*ADCResult_iL & 0x7FFF) << ADC_TO_Q15;
617  // Read AC voltage
618  PFC.qVin_AC = (*ADCResult_VAC & 0xFFF);
619 
620 
621  // PFC mode
622  if(!PFC.pfc_inv_mode)
623  {
624  // Remove the measurement offset
625  PFC.qiL_int = PFC.qiL - PFC.qiL_offset;
626 
627  // Rectified current measurement according to AC polarity for PFC mode
628  if (!input_polarity[0])
629  {
630  PFC.qiL_int = -PFC.qiL_int;
631  }
632  }
633  else // Inverter mode
634  {
635  if(input_polarity[0])
636  {
637  PFC.qiL_int = PFC.qiL - PFC.qiL_offset + 650;//3*262; //+262 added in inverter test
638 
639  // Rectified current measurement according to AC polarity for Inverter mode
640  PFC.qiL_int = -PFC.qiL_int;
641  }
642  else
643  {
644  PFC.qiL_int = PFC.qiL - PFC.qiL_offset - 650;//3*262; //-262 added in inverter test
645  }
646  }
647 
648  // Inductor current to be used in the current loop (Q15) --> SW gain!
649  PFC.qiL_k = (PFC.qiL_int * IL_AVG_GAIN) >> IL_AVG_GAIN_Q;
650 }
651 
652 /*---------------------------------------------------------------------------*/
659 #ifndef DOXYGEN_SKIP
660 __attribute__((section(".ram_code")))
661 #endif
662 static inline void iL_loop()
663 {
664  if (current_loop_ON) //Current loop runs only if the converter is ON
665  {
666  /*---------------------------------------------------------------------------*/
667  /* Feed-forward of the duty cycle in control path */
668  /*---------------------------------------------------------------------------*/
669  // Calculate ratio of AC measurement to constant 400V. duty = Vac/Vdc in inverter mode.
670  if (PFC.qVin_AC < AC_400V)
671  {
672  current_PI_ctr.ratio_FF = (PFC.qVin_AC * 14789) >> 15; //d=(1475/3268*2^15)*Vac*2^-15
673  }
674  else
675  {
676  current_PI_ctr.ratio_FF = MAX_DUTY; // Vac close to 400V, ratio is limited to maximum duty.
677 
678 // /* DEBUGGING pin */
679 // XMC_GPIO_ToggleOutput(P0_12);
680  }
681  // Apply a weight to the feed-forward path.
682  // Move the current tracking to the current controller to account for mismatches and constant Vdc in calculation.
683  current_PI_ctr.ratio_FF = (current_PI_ctr.ratio_FF * 32767) >> 15; //29491 --> 0.9; 31129 --> 0.95; 32767 --> 1
684 
685  // PFC mode
686  if(!PFC.pfc_inv_mode)
687  {
688  //Duty in PFC mode is 1-Vac/Vdc
690  }
691 
692  /*---------------------------------------------------------------------------*/
693  /* Current loop calculation */
694  /*---------------------------------------------------------------------------*/
695  // Current error (Q15) for control loop calculation
696  current_PI_ctr.state_error[0] = (int32_t)current_PI_ctr.PI_ref - (int32_t)PFC.qiL_k;
697 
698  /*
699  * Current compensator with one zero (PI)
700  * d[k] = d[k-1] + a0 * e[k] + a1 * e[k-1]
701  *
702  * error*coeff => Q15 (16b) * Q14 (16b) = Q29 (32b) --> 1cycle 32bit multiplication
703  *
704  * state_output Q29-32b --> duty Q13 16b
705  */
708 #if (ILOOP_COEFF_Q < 14)
709  current_PI_ctr.state_output[0] = current_PI_ctr.state_output[0] << current_PI_ctr.shift_to_q29; //Not necessary if Q14 coeff.
710 #endif
712 
713  // Current controller limitation
715  {
717  }
719  {
721  }
722 
723  /*---------------------------------------------------------------------------*/
724  /* Duty cycle calculation: feed-foward + current loop */
725  /*---------------------------------------------------------------------------*/
726  // Duty composes of feed-forward plus current loop
728 
729  // Duty cycle limitation
731  {
733  }
734  else if (current_PI_ctr.duty < MIN_DUTY)
735  {
737 
738 // /* DEBUGGING pin */
739 // XMC_GPIO_ToggleOutput(P0_12);
740  }
741 
742  // Duty cycle update in close-loop operation without feed-forward
743  set_duty_ADC(current_PI_ctr.duty,PR_MAX);//current_PI_ctr.PR_calc);
744 
745  // Save output values for the next calculation
747 
748  // Save error values for the next calculation
750  }
751 }
752 
753 /*---------------------------------------------------------------------------*/
759 #ifndef DOXYGEN_SKIP
760 __attribute__((section(".ram_code")))
761 #endif
762 static inline void iL_ref()
763 {
764  /*--------------------------------------------------------------------------*/
765  /* CURRENT REFERENCE CALCULATION */
766  /*--------------------------------------------------------------------------*/
767 // PFC.qVin_AC = (*ADCResult_VAC & 0xFFF);
768 
769  // PFC mode
770  if(!PFC.pfc_inv_mode)
771  {
772  // Calculate I_ref for the current loop in PFC mode: VAC * 1/Vrms2 * V-loop-output
774  }
775  else // Inverter mode
776  {
777  // Calculate I_ref for the current loop in inverter mode: VAC * 1/Vrms2 * P_available
779  }
780 
781  // Iref in Q15 --> consider iL as Q15, therefore as a gain in the current measurement
782  current_PI_ctr.PI_ref = ((uint64_t) current_PI_ctr.PI_ref_int * (uint64_t) PFC.qVin_AC) >> CURRENT_REF_TO_Q15;
783 
784  // AC sensing asymmetry compensation (Kneg = 1.009973 * Kpos; 0.009973 = 20915(Q21))
785  if (input_polarity[0])
786  {
788  }
789 
790 // /* Multiply current reference when the bulk voltage is under certain range ("wake-up circuit") --> PFC */
791 // if (offset_detected)
792 // {
793 // if (current_PI_ctr.PI_ref < (I_IN_MAX_FACTOR-10))
794 // {
795 // current_PI_ctr.PI_ref = current_PI_ctr.PI_ref << I_IN_FACTOR;
796 // }
797 // else
798 // current_PI_ctr.PI_ref = current_PI_ctr.PI_ref_max;
799 // }
800 
801  /* Maximum current limitation. AVG current limitation. */
803  {
805  }
806  else if (current_PI_ctr.PI_ref < 1)
807  {
809 
810 // /* DEBUGGING pin */
811 // XMC_GPIO_ToggleOutput(P0_12);
812  }
813 }
814 
815 /*---------------------------------------------------------------------------*/
822 #ifndef DOXYGEN_SKIP
823 __attribute__((section(".ram_code")))
824 #endif
825 static inline void iL_coeff()
826 {
827  /*------------------------------------------------------------------------------------------------------------------*/
828  /* Current loop coefficients based on current (variable inductance - CCM; variable BW according to current level) */
829  /*------------------------------------------------------------------------------------------------------------------*/
830  // Region below 0A
831  if ((PFC.qiL_k < INEG_FAULT_Q15) && current_loop_ON) //Fast turn off if high negative current detected during operation.
832  {
833  // Turn off as protection. Something is not working as it should.
834  PWM_OFF();
835  /* Enable trap of the PWM */
836  CCU80_CC82->CR1S = 0;
837  CCU80_CC80->CR1S = 0;
838  CCU80->GCSS |= (CCU8_GCSS_S0SE_Msk|CCU8_GCSS_S2SE_Msk);
839 
840  SR_OFF();
841 
842  // Voltage loop should keep running in AC-LCDO, no current loop since something went wrong.
843  current_loop_ON = false;
844 
845  // Do not allow turn ON in the pseudo-pll function.
846  fault_reg |= F_NEG_I;
847 
848  // Time to restart the converter operation
849  AC_zero_cross.neg_iL_fault_count = PWM_TRANSITION_DLY + 5; // 5*1/65kHzs = 77us
850 
851 // /* DEBUGGING - LATCH THE CONVERTER IN CASE OF OVP FOR LOW VOLTAGE TEST */
852 // status_reg = ST_LATCH;
853 
854  }
855  else if (PFC.qiL_k < 0) //a0 value at highest current (lowest coefficient)
856  {
858  }
859  else if (PFC.qiL_k <= REG1_ILIMIT_Q15) // Linear Region (below 36A)
860  {
862  }
863  else //Constant Region (over 36A)
864  {
866  }
867 
868  // a1 coefficient calculation based on a0 and zero location
870 }
871 
872 /*---------------------------------------------------------------------------*/
880 #ifndef DOXYGEN_SKIP
881 __attribute__((section(".ram_code")))
882 #endif
883 static inline void iL_deadtime()
884 {
885  /*------------------------------------------------------------------------------------------------------------------*/
886  /* Dead time and switching frequency calculation based on current */
887  /*------------------------------------------------------------------------------------------------------------------*/
888 
889 // if (PFC.qiL_k < parameters_RAM.dt_reg2_iLimit)
890 // {
891 // if (PFC.qiL_k < parameters_RAM.dt_const_iLimit) // maximum value
892 // {
893 // pwm_pfc.ccu8_slice_ptr->DC1R = (((uint32_t) parameters_RAM.dt_d2s) << CCU8_CC8_DC1R_DT1F_Pos) | ((uint32_t) parameters_RAM.dt_s2d_MAX);
894 // }
895 // else if (PFC.qiL_k < parameters_RAM.dt_reg1_iLimit)// DT_REG1_ILIMIT_Q15) // linear region 1
896 // {
897 // 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)));
898 // }
899 // else // linear region 2
900 // {
901 // 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)));
902 // }
903 // }
904 // else
905 // {
906 // // minimum DT value
907 // pwm_pfc.ccu8_slice_ptr->DC1R = (((uint32_t) parameters_RAM.dt_d2s) << CCU8_CC8_DC1R_DT1F_Pos) | ((uint32_t) parameters_RAM.dt_s2d_MIN);
908 // }
909 
910  if (PFC.qiL_k < 5960) //5960-->10A
911  {
912  if(PFC.qiL_k < 0) //594-->1A
913  {
914  if(!input_polarity[0])// AC<0, LS/ST1 is switch functionality
915  {
916  pwm_pfc.ccu8_slice_ptr->DC1R = ((30 << CCU8_CC8_DC1R_DT1F_Pos) | (117));
917  }
918  else // AC>0, HS/ST1_N is switch functionality
919  {
920  pwm_pfc.ccu8_slice_ptr->DC1R = ((117 << CCU8_CC8_DC1R_DT1F_Pos) | (30));
921  }
922  }
923  else if(PFC.qiL_k < 1788) //1788-->3A
924  {
925  if(!input_polarity[0])// AC<0, LS/ST1_N is switch functionality
926  {
927  pwm_pfc.ccu8_slice_ptr->DC1R = ((30 << CCU8_CC8_DC1R_DT1F_Pos) | ((uint32_t)(117 - ((1062 * PFC.qiL_k) >> DT_SLOPE_Q))));
928  }
929  else // AC>0, HS/ST1 is switch functionality
930  {
931  pwm_pfc.ccu8_slice_ptr->DC1R = (((uint32_t) (117 - ((1062 * PFC.qiL_k) >> DT_SLOPE_Q)) << CCU8_CC8_DC1R_DT1F_Pos) | (30));
932  }
933  }
934  else
935  {
936  if(!input_polarity[0])// AC<0, LS/ST1_N is switch functionality
937  {
938  pwm_pfc.ccu8_slice_ptr->DC1R = ((30 << CCU8_CC8_DC1R_DT1F_Pos) | ((uint32_t)(70 - ((219 * PFC.qiL_k) >> DT_SLOPE_Q))));
939  }
940  else // AC>0, HS/ST1 is switch functionality
941  {
942  pwm_pfc.ccu8_slice_ptr->DC1R = (((uint32_t) (70 - ((219 * PFC.qiL_k) >> DT_SLOPE_Q)) << CCU8_CC8_DC1R_DT1F_Pos) | (30));
943  }
944 
945  }
946 
947  }
948  else
949  {
950  // minimum DT value
951  pwm_pfc.ccu8_slice_ptr->DC1R = ((30 << CCU8_CC8_DC1R_DT1F_Pos) | (30));
952  }
953 
954 
955 // // Constant DT for debugging
956 // pwm_pfc.ccu8_slice_ptr->DC1R = ((80 << CCU8_CC8_DC1R_DT1F_Pos) | (80));
957 
958 }
959 
960 /*---------------------------------------------------------------------------*/
966 #ifndef DOXYGEN_SKIP
967 __attribute__((section(".ram_code")))
968 #endif
969 static inline void notch_filter_Vbulk()
970 {
971  /*--------------------------------------------------------------------------*
972  * DC Voltage filter --> cascaded notch - START *
973  *--------------------------------------------------------------------------*/
974 
975  //input to the filter
976  auxDC = (PFC.qVbus_DC * DCfilter.k1) >> IIR_QFORMAT;
977 
978  // Section 1 calculation
979  DCfilter.calc11 = ((int64_t)DCfilter.u1[1]*(int64_t)DCfilter.b1[1]) >> IIR_QFORMAT;
980  DCfilter.calc21 = ((int64_t)DCfilter.u1[2]*(int64_t)DCfilter.b1[2]) >> IIR_QFORMAT;
981 
982  DCfilter.u1[0] = auxDC - DCfilter.calc11 - DCfilter.calc21; //Intermediate state
983  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
984 
985  // Save section 1 values for next iteration
986  DCfilter.u1[2] = DCfilter.u1[1];
987  DCfilter.u1[1] = DCfilter.u1[0];
988 
989  // Section 2 calculation
990  DCfilter.calc12 = ((int64_t)DCfilter.u2[1]*(int64_t)DCfilter.b2[1]) >> IIR_QFORMAT;
991  DCfilter.calc22 = ((int64_t)DCfilter.u2[2]*(int64_t)DCfilter.b2[2]) >> IIR_QFORMAT;
992 
994  DCfilter.u2[0] -= DCfilter.calc12;
995  DCfilter.u2[0] -= DCfilter.calc22;
997 
998  // Limit to avoid negative numbers
999  if(DCfilter.output < 1)
1000  DCfilter.output = 1;
1001 
1002  // DC value for control loop
1004 
1005  // Save section 2 values for next iteration
1006  DCfilter.u2[2] = DCfilter.u2[1];
1007  DCfilter.u2[1] = DCfilter.u2[0];
1008 
1009  /*--------------------------------------------------------------------------*
1010  * DC Voltage filter --> cascaded notch - END *
1011  *--------------------------------------------------------------------------*/
1012 }
1013 
1014 /*---------------------------------------------------------------------------*/
1021 #ifndef DOXYGEN_SKIP
1022 __attribute__((section(".ram_code")))
1023 #endif
1024 static inline void lpf_filter_RMS()
1025 {
1026  /*--------------------------------------------------------------------------*
1027  * RMS IIR filter calculation (LPF), two-section Direct-form II - START *
1028  *--------------------------------------------------------------------------*/
1029 
1030  auxAC = ((PFC.qVin_AC << ADC_TO_Q15) * ACfilter.k1) >> IIR_QFORMAT; //input to the filter
1031 
1032  // Section 1 calculation
1033  ACfilter.calc11 = ((int64_t)ACfilter.u1[1]*(int64_t)ACfilter.b1[1]) >> IIR_QFORMAT;
1034  ACfilter.calc21 = ((int64_t)ACfilter.u1[2]*(int64_t)ACfilter.b1[2]) >> IIR_QFORMAT;
1035 
1036  ACfilter.u1[0] = auxAC - ACfilter.calc11 - ACfilter.calc21; //Intermediate values Q15
1037  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
1038 
1039  // Save section 1 values for next iteration
1040  ACfilter.u1[2] = ACfilter.u1[1];
1041  ACfilter.u1[1] = ACfilter.u1[0];
1042 
1043  // Section 2 calculation
1044  ACfilter.calc12 = ((int64_t)ACfilter.u2[1]*(int64_t)ACfilter.b2[1]) >> IIR_QFORMAT;
1045  ACfilter.calc22 = ((int64_t)ACfilter.u2[2]*(int64_t)ACfilter.b2[2]) >> IIR_QFORMAT;
1046 
1048  ACfilter.u2[0] -= ACfilter.calc12;
1049  ACfilter.u2[0] -= ACfilter.calc22;
1050  ACfilter.output = ACfilter.a2[0]*ACfilter.u2[0] + ACfilter.a2[1]*ACfilter.u2[1] + ACfilter.a2[2]*ACfilter.u2[2]; //Q29 output
1051 
1052  // Limit to avoid negative numbers and division by zero
1053  if(ACfilter.output < 1)
1054  ACfilter.output = 1;
1055 
1056  // "RMS" value for multiplier input
1057  PFC.qVin_BOP = (uint16_t)(ACfilter.output >> LPF_TO_2ADC);
1058 
1059  // Low limit of AC-filter output to avoid division by zero or a small RMS value.
1060  if (PFC.qVin_BOP < VRMS_MIN_LIMIT) // 1176 (80V_RMS) is half of the minimum voltage considered to start the converter (2589 --> 176V_RMS)
1061  {
1062  PFC.qVin_BOP = VRMS_MIN_LIMIT;
1063  }
1064 
1065  // Save section 2 values for next iteration
1066  ACfilter.u2[2] = ACfilter.u2[1];
1067  ACfilter.u2[1] = ACfilter.u2[0];
1068 
1069  /*--------------------------------------------------------------------------*
1070  * RMS IIR filter calculation (LPF), two-section Direct-form II - END *
1071  *--------------------------------------------------------------------------*/
1072 }
1073 
1074 /*---------------------------------------------------------------------------*/
1080 #ifndef DOXYGEN_SKIP
1081 __attribute__((section(".ram_code")))
1082 #endif
1083 static inline void state_machine()
1084 {
1085  /*---------------------------------------------------------------------------*/
1086  /* Starting sequence / state machine - START */
1087  /*---------------------------------------------------------------------------*/
1088  switch (status_reg) {
1089 
1090  case ST_INIT:
1091 
1092  // Turn off PWM, SR and relay in case INIT state is reached
1093  PWM_OFF();
1094  /* Enable trap of the PWM */
1095  CCU80_CC82->CR1S = 0;
1096  CCU80_CC80->CR1S = 0;
1097  CCU80->GCSS |= (CCU8_GCSS_S0SE_Msk|CCU8_GCSS_S2SE_Msk);
1098 
1099  SR_OFF();
1100  relay_OFF();
1101 
1102  /* Power stage is OFF, loops not executed */
1103  power_stage_ON = false;
1104  current_loop_ON = false;
1105 
1106  /* Reset control loops and update status */
1108 
1109  // PFC mode
1110  if(!PFC.pfc_inv_mode)
1111  {
1112  // Close the relay and move to stand-by based on bulk voltage (Vout) range
1113  if ((!time_INIT) && (PFC.qVbus_DC_filt > VOUT_START_MIN))
1114  {
1115  // Next state: converter ready to start.
1116  status_reg = ST_STB;
1117 
1118  /* Set start-up timer in case the state machine arrives to INIT during operation */
1119  time_INIT = 550;//30;
1120 
1121  /* Reset BOP state timer in case the state machine arrives to INIT during operation */
1122  stb_bop_count = 0;
1123  }
1124  }
1125  //Inverter mode
1126  else
1127  {
1128  // Close the relay and move to stand-by based on bulk voltage (Vin) range
1129  if ((!time_INIT) && (PFC.qVbus_DC_OK)) //qVbus_DC_OK is managed in bulk_monitoring() function
1130  {
1131  // Next state: converter ready to start.
1132  status_reg = ST_STB;
1133 
1134  /* Set start-up timer in case the state machine arrives to INIT during operation */
1135  time_INIT = 550;//30;
1136 
1137  /* Reset BOP state timer in case the state machine arrives to INIT during operation */
1138  stb_bop_count = 0;
1139  }
1140  }
1141 
1142  break;
1143 
1144  case ST_STB: // Wait for proper AC voltage (background process)
1145 
1146  // Keep PWM and relay off
1147  PWM_OFF();
1148  /* Enable trap of the PWM */
1149  CCU80_CC82->CR1S = 0;
1150  CCU80_CC80->CR1S = 0;
1151  CCU80->GCSS |= (CCU8_GCSS_S0SE_Msk|CCU8_GCSS_S2SE_Msk);
1152 
1153  SR_OFF();
1154  relay_OFF();
1155 
1156  /* Power stage is OFF, loops not executed */
1157  power_stage_ON = false;
1158  current_loop_ON = false;
1159 
1160  /* Reset counter for relay on-off timing.
1161  * Wait a defined number of cycles after closing the relay, before starting the PFC */
1162  stb_bop_count = 0;
1163 
1164  break;
1165 
1166  case ST_STB_BOP: // Relay management after DC and AC voltages are in range
1167 
1169  {
1170  // PWM and relay remain OFF
1171  PWM_OFF();
1172  /* Enable trap of the PWM */
1173  CCU80_CC82->CR1S = 0;
1174  CCU80_CC80->CR1S = 0;
1175  CCU80->GCSS |= (CCU8_GCSS_S0SE_Msk|CCU8_GCSS_S2SE_Msk);
1176 
1177  SR_OFF();
1178  relay_OFF();
1179 
1180  /* Power stage is OFF, loops not executed */
1181  power_stage_ON = false;
1182  current_loop_ON = false;
1183 
1184  // Relay on counter update
1185  stb_bop_count++;
1186  }
1188  {
1189  // Current offset sampling
1190  if ((PFC.qiL_offset_count == 0) && (!PFC.iL_offset_done))
1191  {
1192  PFC.iL_offset_en = true;
1193  }
1194  else if (PFC.qiL_offset_count == OFFSET_END)
1195  {
1196  // Relay is switched on after current offset has been obtained
1197  relay_ON();
1198 
1199  // Relay on counter update
1200  stb_bop_count++;
1201 
1202  // No offset calculation after decision to start
1203  PFC.iL_offset_en = false;
1204 // PFC.iL_offset_done = true;
1205  }
1206  }
1208  {
1209  /* Save DC-bus value for V-loop soft-start */
1211 
1212  // Relay on counter update
1213  stb_bop_count++;
1214  }
1215  else if (stb_bop_count == ST_STB_BOP_END)
1216  {
1217  /* Go to soft-start, starting in negative AC cycle */
1218  status_reg = ST_START_EN;//ST_LATCH;//
1219  }
1220  else
1221  {
1222  // Relay on counter update
1223  stb_bop_count++;
1224  }
1225 
1226  break;
1227 
1228  case ST_START_EN:
1229 
1230  break;
1231 
1232  case ST_SOFT_START:
1233 
1234  // PFC mode
1235  if(!PFC.pfc_inv_mode)
1236  {
1237  /*--------------------------------------------------------------------------*/
1238  /* Bulk voltage soft-start for PFC operation */
1239  /*--------------------------------------------------------------------------*/
1240 
1241  if (!time_V_step)
1242  {
1244  {
1245  /* Ramp-up the output voltage target (SteadyREF / 2^6) */
1247 
1248  // Limit the maximum reference
1250  {
1252  }
1253 
1254  // Xms until next loop step
1255  time_V_step = 6;//4;
1256  }
1257  else
1258  {
1259  /* Soft-start is done, voltage reference is the targeted output voltage */
1261 
1262  /* State change, for the moment no action when running */
1263  status_reg = ST_RUNNING;//ST_LATCH;//
1264 
1265  // Soft-start is done
1267  }
1268  }
1269  }
1270 
1271  break;
1272 
1273  case ST_LATCH: // For debugging purposes. Keep converter OFF
1274 
1275  PWM_OFF();
1276  /* Enable trap of the PWM */
1277  CCU80_CC82->CR1S = 0;
1278  CCU80_CC80->CR1S = 0;
1279  CCU80->GCSS |= (CCU8_GCSS_S0SE_Msk|CCU8_GCSS_S2SE_Msk);
1280 
1281  SR_OFF();
1282 
1283  power_stage_ON = false;
1284  current_loop_ON = false;
1285 
1286  break;
1287 
1288  case ST_RUNNING:
1289 
1290  // Inverter mode
1291  if (PFC.pfc_inv_mode)
1292  {
1293  // Inverter power is changed close to the zero crossing
1294  if ((PFC.qVin_AC < OFFSET_VAC) && (PFC.inv_power != parameters_RAM.power_target))
1295  {
1296  // Update of the inverter power
1298  {
1300  {
1301  PFC.inv_power += (parameters_DFLT.power_target >> 1);
1302  }
1303  else
1304  {
1306  }
1307  }
1308  else if (PFC.inv_power <= parameters_DFLT.power_target)
1309  {
1311  }
1312  else
1313  {
1314  PFC.inv_power -= (parameters_DFLT.power_target >> 1);
1315  }
1316  }
1317  }
1318 
1319  break;
1320 
1321  default:
1322 
1323  break;
1324  }
1325 }
1326 
1327 /*---------------------------------------------------------------------------*/
1333 #ifndef DOXYGEN_SKIP
1334 __attribute__((section(".ram_code")))
1335 #endif
1336 static inline void Vbulk_ctrl_loop()
1337 {
1338  // Voltage error (Q15)
1340 
1341  /* Error management in controller first iteration */
1343  {
1344  // Fill the error array for smooth transition at first iteration
1345  //voltage_int_ctr.error[2] = voltage_int_ctr.error[0];
1347 
1348  // No first iteration any more
1349  voltage_int_ctr.first_iter = false;
1350  }
1351 
1352  /*
1353  * Voltage compensator with integrator, single pole and single zero (PI + pole)
1354  * Vloop[k] = b1*Vloop[k-1] + b2*Vloop[k-2] + a0*ev[k] + a1*ev[k-1]
1355  * error*coeff => Q15 (16b) * Qx (16b) = Q(15+x) (32b) --> 1cycle 32bit multiplication
1356  * vloop*coeff => Q15 (16b) * Qx (16b) = Q(15+x) (32b) --> 1cycle 32bit multiplication
1357  * output_int = Vloop[k] << (16-x) (Q31)
1358  * output_int_limit = output (Q15) --> upper byte of output_int
1359  * integrator-antiwindup
1360  */
1361  voltage_int_ctr.interm_calc1 = (int32_t)voltage_int_ctr.a[0] * (int32_t)voltage_int_ctr.error[0];
1362  voltage_int_ctr.interm_calc1 += (int32_t)voltage_int_ctr.a[1] * (int32_t)voltage_int_ctr.error[1];
1363 
1364  voltage_int_ctr.interm_calc2 = (int32_t)voltage_int_ctr.b[1] * (int32_t)voltage_int_ctr.state[1];
1365  voltage_int_ctr.interm_calc2 += (int32_t)voltage_int_ctr.b[2] * (int32_t)voltage_int_ctr.state[2];
1366 
1368 
1369  // Voltage control loop saturation (Current reference limitation)
1371  {
1373  }
1374  else if ((int16_t)(*voltage_int_ctr.output_int_limit) < 1)
1375  {
1377  }
1378 
1379  // External variable for current reference calculation
1381 
1382  // Save state for next calculation
1385 
1386  // Save error for next calculation
1389 }
1390 
1391 /*---------------------------------------------------------------------------*/
1397 #ifndef DOXYGEN_SKIP
1398 __attribute__((section(".ram_code")))
1399 #endif
1400 static inline void inv_RMS_acok()
1401 {
1402  // Low limit of AC-filter output to avoid division by zero or a small RMS value.
1403  if (PFC.qVin_BOP < VRMS_MIN_LIMIT) // 1176 (80V_RMS) is half of the minimum voltage considered to start the converter (2589 --> 176V_RMS)
1404  {
1405  PFC.qVin_BOP = VRMS_MIN_LIMIT;
1406  }
1407 
1408  /* 1/Vrms² --> uint32 multiplication & DIV kernel */
1409  Vrms2 = PFC.qVin_BOP*PFC.qVin_BOP;
1410 
1411  /* Load division operands.
1412  * 0x7FFFFFFF is 0.9999999 in Q31.
1413  * Vrms2 is 32bits unsigned
1414  * Result is 1/Vrms^2 with 32 bit Q31 resolution */
1415  MATH ->DVD = 0x7FFFFFFF;
1416  MATH ->DVS = Vrms2;
1417 
1418  /* Use the DIV kernel calculation time to check AC voltage*/
1419  if (PFC.qVin_AC < 72)
1420  {
1421  // Count 3 consecutive samples under 10V of no AC to trigger ACOK LOW
1422  acok_counter++;
1423  if ((acok_counter > 2) && acok)
1424  {
1425  // AC LCDO detected
1426  acok = false;
1427  // Reset counter
1428  acok_counter = 0;
1429  }
1430  }
1431  else if ((PFC.qVin_AC > 1699) && !acok) //1699: AC is over 165Vrms (233Vac_pk)
1432  {
1433  // Input voltage considered ok
1434  acok = true;
1435 
1436  // 150ms of extra voltage after AC lost (LCDO - LLC regulation)
1437  time_extra_bulk = 150;
1438  }
1439  else
1440  // Reset counter
1441  acok_counter = 0;
1442 
1443 
1444  /* Poll the division result */
1445  while (MATH ->DIVST);
1446  /* Result of the division in Q31 */
1447  PFC.qVin_inv2 = MATH ->QUOT;
1448 }
1449 
1450 /*---------------------------------------------------------------------------*/
1458 #ifndef DOXYGEN_SKIP
1459 __attribute__((section(".ram_code")))
1460 #endif
1461 static inline void bulk_monitoring()
1462 {
1463  // PFC mode
1464  if(!PFC.pfc_inv_mode)
1465  {
1466  /*---------------------------------------------------------------------------*/
1467  /* Output voltage monitoring in PFC operation - start */
1468  /*---------------------------------------------------------------------------*/
1469  if (status_reg & PFC_RUNNING) // Protection active from soft-start;
1470  {
1471  if ((PFC.qVbus_DC > VOUT_OVP_STOP) && !(fault_reg & F_OVP))
1472  {
1473  /* Turn-off PWM if the Vout is over the limit for X samples in a row */
1475  if (count_Vout_OVP_stop > 0)
1476  {
1477  /* Set fault flag */
1478  fault_reg |= F_OVP;
1479 
1480  /* Turn-off PWM and SR */
1481  PWM_OFF();
1482  /* Enable trap of the PWM */
1483  CCU80_CC82->CR1S = 0;
1484  CCU80_CC80->CR1S = 0;
1485  CCU80->GCSS |= (CCU8_GCSS_S0SE_Msk|CCU8_GCSS_S2SE_Msk);
1486 
1487 
1488  /* Power stage is OFF, loops not executed */
1489  power_stage_ON = false;
1490  current_loop_ON = false;
1491 
1492  /* Reset Over Voltage counter */
1493  count_Vout_OVP_stop = 0;
1494 
1495 // /* DEBUGGING - LATCH THE CONVERTER IN CASE OF OVP FOR LOW VOLTAGE TEST */
1496 // status_reg = ST_LATCH;
1497  }
1498  }
1500  else if ((PFC.qVbus_DC < voltage_int_ctr.int_ref) && (fault_reg & F_OVP))
1501  {
1502  /* Turn-on PWM if the Vout is under the limit for X samples in a row */
1504 
1505  if (count_Vout_OVP_resume > 0)
1506  {
1507  /* Voltage error initialization */
1508  voltage_int_ctr.error[0] = 0x0;
1509  voltage_int_ctr.error[1] = 0x0;
1510  voltage_int_ctr.error[2] = 0x0;
1511 
1512  // Voltage loop soft start
1513  voltage_int_ctr.output = 0;
1515  voltage_int_ctr.state[1] = 0;
1516  voltage_int_ctr.state[2] = 0;
1517 
1518  // First iteration in resume operation for smoother V-controller transition
1519  voltage_int_ctr.first_iter = true;
1520 
1521  /* Current compensator output (duty) and input (error) initialization */
1522  // Error
1523  current_PI_ctr.state_error[0] = 0x0;
1524  current_PI_ctr.state_error[1] = 0x0;
1525  // Output
1526  current_PI_ctr.state_output[0] = 0x0; // Starting duty cycle
1527  current_PI_ctr.state_output[1] = 0x0;
1528 
1529  /* Reset OverVoltage Counter */
1531 
1532  /* Fault flag can be cleared. Wait for negative cycle to start operation */
1533  status_reg |= ST_CLR_OVP;
1534 
1535 // /* Clear the fault flag.
1536 // * Allow converter to resume operation in next AC zero crossing */
1537 // fault_reg &= ~F_OVP;
1538  }
1539  }
1540  else
1541  {
1542  /* Reset OverVoltage Stop Counter */
1543  count_Vout_OVP_stop = 0;
1544  /* Reset OverVoltage Resume Counter */
1546  }
1547  }
1548 
1549  // Undervoltage 310V restarts the converter operation with soft-start sequence
1550  if ((PFC.qVbus_DC < VOUT_UVP_MIN) && (status_reg & ST_RUNNING))
1551  {
1552  /* Turn-off the converter */
1553  PWM_OFF();
1554  /* Enable trap of the PWM */
1555  CCU80_CC82->CR1S = 0;
1556  CCU80_CC80->CR1S = 0;
1557  CCU80->GCSS |= (CCU8_GCSS_S0SE_Msk|CCU8_GCSS_S2SE_Msk);
1558 
1559  SR_OFF();
1560  // PWM cannot be turned ON --> Decision in PWM interrupt
1561  power_stage_ON = false;
1562  current_loop_ON = false;
1563 
1564  // RELAY OFF for soft-start procedure
1565  relay_OFF();
1566 
1567  /* Go to INIT state to start the soft-start sequence including the relay management */
1568  status_reg = ST_INIT;//ST_LATCH;//
1569 
1570  // Wait 30ms before starting the sequence
1571  time_INIT = 550;//30;
1572  }
1573 
1574 
1575  /* Detect error outside a defined slot for "waking-up circuit" --> NOT USED */
1576  if (status_reg == ST_RUNNING)
1577  {
1578  if (!offset_detected)
1579  {
1580  if (PFC.qVbus_DC < VOLTAGE_OFFSET_NL_ON)
1581  {
1582  // From next iteration check for the voltage to return to nominal value
1583  offset_detected = 1;
1584  }
1585  }
1586  else
1587  {
1588  if (PFC.qVbus_DC > voltage_int_ctr.int_ref)
1589  {
1590  //Allow again detection of bulk voltage out of the defined slot
1591  offset_detected = 0;
1592  }
1593  }
1594  }
1595 
1596  /*---------------------------------------------------------------------------*/
1597  /* Output voltage monitoring in PFC operation - end */
1598  /*---------------------------------------------------------------------------*/
1599  }
1600  else // Inverter mode
1601  {
1602  // If converter is in operation, turn off if bulk voltage is out of range (370V - 430V)
1603  if(status_reg & 0xF)
1604  {
1606  {
1607  /* Turn-off the converter */
1608  PWM_OFF();
1609  /* Enable trap of the PWM */
1610  CCU80_CC82->CR1S = 0;
1611  CCU80_CC80->CR1S = 0;
1612  CCU80->GCSS |= (CCU8_GCSS_S0SE_Msk|CCU8_GCSS_S2SE_Msk);
1613 
1614  SR_OFF();
1615  // PWM cannot be turned ON --> Decision in PWM interrupt
1616  power_stage_ON = false;
1617  current_loop_ON = false;
1618 
1620 // // RELAY OFF for soft-start procedure
1621 // relay_OFF();
1622 
1623  /* Go to INIT state to start the soft-start sequence including the relay management */
1624  status_reg = ST_INIT;//ST_LATCH;//
1625  fault_reg |= F_DC_INV;
1626 
1627  // Wait 250ms before starting the sequence
1628  time_INIT = 550;
1629 
1630  // No start in inverter mode until DC voltage is in range
1631  PFC.qVbus_DC_OK = false;
1632  }
1633  }
1634  // If converter is off, turn on if bulk voltage is in range (390V - 410V)
1635  else
1636  {
1638  {
1639  if(!status_reg && !fault_reg) //((!status_reg || !(status_reg & ST_LATCH)) && !fault_reg)
1640  {
1641  // Turn on converter --> Start state machine
1642  PFC.qVbus_DC_OK = true;
1643  }
1644  }
1645  }
1646  }
1647 }
1648 
1649 /*---------------------------------------------------------------------------*/
1658 #ifndef DOXYGEN_SKIP
1659 __attribute__((section(".ram_code")))
1660 #endif
1661 static inline void pseudo_pll()
1662 {
1663  static uint16_t plrty_transition_dly_OFF,plrty_transition_dly_ON;
1664 
1665 // /* Pin P0.10: software GPIO*/
1666 // XMC_GPIO_SetOutputHigh(P0_12);
1667 
1668 
1669  /* Vac voltage drops under OFF threshold, going into a zero crossing. */
1670  if(PFC.qVin_AC < VIN_OFF_THRSLD)
1671  {
1672  /*
1673  * Switches OFF PWM and starts transition to
1674  * opposite polarity after delay.
1675  */
1676  if(plrty_transition_dly_OFF == 0)
1677  {
1678  /* Checks first sample going under the OFF threshold. */
1679  if(!AC_zero_cross.pwm_zc_disabled)
1680  {
1681  /* Disable PWM and SR signals. */
1682  PWM_OFF();
1683  /* Enable trap of the PWM */
1684  CCU80_CC82->CR1S = 0;
1685  CCU80_CC80->CR1S = 0;
1686  CCU80->GCSS |= (CCU8_GCSS_S0SE_Msk|CCU8_GCSS_S2SE_Msk);
1687 
1688  SR_OFF();
1689 
1690  /* Sets a delay before starting PWM polarity transition. */
1691  plrty_transition_dly_ON = PWM_TRANSITION_DLY;
1692 
1693  /* Normal PWM modulation disabled during Vac cross transition. */
1694  AC_zero_cross.pwm_zc_disabled = true;
1695 
1696  //Clear current fault. Allow restart after zero crossing
1698  fault_reg &= ~F_NEG_I;
1699  AC_zero_cross.neg_iL_fault_count = 0;
1700 
1701  //Save polarity as input to next polarity decision filter
1703 
1704 // /* Pin P0.10: software GPIO*/
1705 // XMC_GPIO_SetOutputLow(P0_12);
1706 
1707  }
1708 
1709  // Flag raised when the AC voltage gets close to zero; enables PWM start when AC voltage goes over ON_THRSLD
1710  if (PFC.qVin_AC < VIN_ZERO_THRSLD)
1711  {
1712  AC_zero_cross.vac_zero_cross = true;
1713  }
1714  }
1715 
1716  /* Counter of Vac drop out detection. */
1717  AC_zero_cross.vin_low_cnt ++;
1718 
1719  /* Choose the starting duty cycle and switching frequency for restart.
1720  * In the current implementation, Fsw is kept constant for all the conditions. */
1721  if (AC_zero_cross.pwm_zc_disabled)
1722  {
1724  if(AC_zero_cross.vin_low_cnt > AC_LCDO_DETECT)
1725  {
1726  // Increased BW after AC lost detected
1727  AC_zero_cross.after_AClost_cnt = AFTER_ACLOST_TIME;
1728 
1729 // /* DEBUGGING - LATCH THE CONVERTER IN CASE OF OVP FOR LOW VOLTAGE TEST */
1730 // status_reg = ST_LATCH;
1731 
1732 
1733  // In INVERTER mode, if AC is lost, set the current reference to the starting value
1734  if (PFC.pfc_inv_mode)
1735  {
1736  status_reg = ST_START_EN;//ST_SOFT_START;
1738 
1739  // Set starting power to zero for current/power soft start, according to DFLT parameters target */
1740  PFC.inv_power = parameters_DFLT.power_target;//PFC.inv_power_ref>>3;//700;//0;//
1741  }
1742  }
1743 
1744  //No current loop if the PWM is off
1745  current_loop_ON = false;
1746 
1747  /* Current compensator output (duty) and input (error) initialization */
1748  // Error
1749  current_PI_ctr.state_error[0] = 0x0;
1751  // Output
1752  // Starting loop value in steady state with duty feed-forward operation.
1755 
1756 // // PFC mode
1757 // if(!PFC.pfc_inv_mode)
1758 // {
1759 // // MAX duty cycle to resume in PFC OPERATION
1760 // // Loop will not execute until AC voltage goes over ON_THRSLD
1761 // set_period(PR_MAX);
1762 // set_duty_ADC(MAX_DUTY_LCDO,PR_MAX);
1763 // }
1764 // else // Inverter mode
1765 // {
1766 // // MIN Duty cycle to resume in INV OPERATION
1767 // // Loop will not execute until AC voltage goes over ON_THRSLD
1768 // set_period(PR_MAX);
1769 // set_duty_ADC(MIN_DUTY,PR_MAX);
1771 // }
1772  }
1773  }
1774  else
1775  {
1776  /* Resets counter for Vin drop out detection. */
1777  AC_zero_cross.vin_low_cnt = 0;
1778  }
1779 
1780  /*
1781  * Vac above ON threshold.
1782  * Note: This could be happening left side or right side of the actual
1783  * Vac crossing, as Vac OFF threshold is > than Vac ON threshold.
1784  * Wait for polarity change decision.
1785  */
1786  if ((PFC.qVin_AC > VIN_ON_THRSLD))
1787  {
1788  if ((AC_zero_cross.vac_zero_cross) || (AC_zero_cross.neg_iL_fault_CLR))
1789  {
1790  /*--------------------------------------------------------------------------*/
1791  /* polarity detection before decision of starting PWM/SR */
1792  /*--------------------------------------------------------------------------*/
1793  if ((ac_polarity.gpio_port->IN) & 0x10)
1794  {
1795  if (input_polarity[1])
1796  {
1798  }
1799  else
1800  {
1802  }
1803  input_polarity[1] = POSITIVE;
1804  }
1805  else
1806  {
1807  if (input_polarity[1])
1808  {
1810  }
1811  else
1812  {
1814  }
1815  input_polarity[1] = NEGATIVE;
1816  }
1817 
1818  if (input_polarity_count > 2) // Filter of x-times HIGH/LOW detected
1819  {
1820  if(input_polarity[1]) /* Positive AC voltage detected */
1821  {
1822  input_polarity[0] = POSITIVE;
1823 
1824  // PFC mode
1825  if(!PFC.pfc_inv_mode)
1826  {
1827 // // Boost switch is HS
1828 // pwm_pfc.ccu8_slice_ptr->CHC |= ((1 << CCU8_CC8_CHC_OCS1_Pos) | (1 << CCU8_CC8_CHC_OCS2_Pos));
1829 
1830  // Configure the use of proper current comparator output for peak current limitation
1831  peak_PWM_trap.eru->EXOCON[0] = 0x1004; //0x1004;--> ERU1.ETL0 flag for pattern detection //0x2004;--> ERU1.ETL1 flag for pattern detection
1832  }
1833  else // Inverter mode
1834  {
1835  // Buck switch is LS
1836  pwm_pfc.ccu8_slice_ptr->CHC &= ~(CCU8_CC8_CHC_OCS1_Msk | CCU8_CC8_CHC_OCS2_Msk);
1837 
1838  // Configure the use of proper current comparator output for peak current limitation
1839  peak_PWM_trap.eru->EXOCON[0] = 0x8004;
1840  }
1841  }
1842  else /* Negative AC voltage detected */
1843  {
1844  input_polarity[0] = NEGATIVE;
1845 
1846  // PFC mode
1847  if(!PFC.pfc_inv_mode)
1848  {
1849 // // Boost switch is LS
1850 // pwm_pfc.ccu8_slice_ptr->CHC &= ~(CCU8_CC8_CHC_OCS1_Msk | CCU8_CC8_CHC_OCS2_Msk);
1851 
1852  // Configure the use of proper current comparator output for peak current limitation
1853  peak_PWM_trap.eru->EXOCON[0] = 0x8004; //0x8004;--> ERU1.ETL3 flag for pattern detection
1854  }
1855  else // Inverter mode
1856  {
1857  // Buck switch is HS
1858  pwm_pfc.ccu8_slice_ptr->CHC |= ((1 << CCU8_CC8_CHC_OCS1_Pos) | (1 << CCU8_CC8_CHC_OCS2_Pos));
1859 
1860  // Configure the use of proper current comparator output for peak current limitation
1861  peak_PWM_trap.eru->EXOCON[0] = 0x2004;
1862  }
1863  }
1864 
1865  //Reset polarity change time filter
1867  // Flag for starting the PWM/SR after polarity decision
1868  AC_zero_cross.vin_on_allowed = true;
1869 
1871  // Set initial duty cycle according to the sensed polarity
1873 
1874 // /* Pin P0.10: software GPIO*/
1875 // XMC_GPIO_SetOutputHigh(P0_12);
1876 
1877  }
1878 
1879  /*
1880  * Turn on PWM/SR if delay happened and polarity detected
1881  */
1882  if(plrty_transition_dly_ON == 0)
1883  {
1884  if(AC_zero_cross.vin_on_allowed)
1885  {
1886  /*
1887  * Voltage has risen after a Vac crossing detection.
1888  */
1889  if((AC_zero_cross.pwm_zc_disabled) || (AC_zero_cross.neg_iL_fault_CLR))
1890  {
1891  // Force the converter to start with negative AC cycle
1892  if((status_reg & ST_START_EN) && !input_polarity[0])
1893  {
1894  // Clear START_ALLOWED flag to avoid this step in the next cycles. Clear OFF to start the converter.
1895  status_reg = ST_SOFT_START;
1896  }
1897 
1898  // Clear OVP flag if the conditions are given and negative AC cycle starts
1899  if((status_reg & ST_CLR_OVP) && !input_polarity[0])
1900  {
1901  status_reg &= ~ST_CLR_OVP;
1902  fault_reg &= ~F_OVP;
1903  }
1904 
1905  // Start converter
1906  if((!fault_reg) && (status_reg & PFC_RUNNING))
1907  {
1908  /*------------------------------------------------------------------------------------------*/
1909  /* Power soft-start for grid-connected inverter operation with variable power reference */
1910  /* Start power reference set in control_loop_init() as param_DFLT value */
1911  /*------------------------------------------------------------------------------------------*/
1912  if (PFC.pfc_inv_mode) //Inverter mode
1913  {
1914  if (stb_bop_count == (ST_STB_BOP_END+4)) //Some AC cycle with DFLT power_target
1915  {
1916  /* Go to running to manage the power reference */
1917  status_reg = ST_RUNNING;//ST_LATCH;//
1918 
1919  // Soft-start is done
1921  }
1922  else if (status_reg & ST_SOFT_START)
1923  {
1924  // Relay on counter update
1925  stb_bop_count++;
1926  }
1927  }
1928 
1929  /* Low frequency switches activated before the PWM. */
1930  if(input_polarity[0]) // POSITIVE AC
1931  {
1932  /* Appropriate slow MOSFET is turned ON. PWM config in polarity detection. */
1933  SR_HS_ON();
1934  }
1935  else // NEGATIVE AC
1936  {
1937  /* Appropriate slow MOSFET is turned ON. PWM config in polarity detection. */
1938  SR_LS_ON();
1939  }
1940 
1941  /* Voltage loop can be evaluated again. */
1942  power_stage_ON = true;
1943 
1944  /* Reset counting of switching cycles after starting PWM. */
1945  AC_zero_cross.vin_on_cnt = 0;
1946 
1947  /* Sets a delay before starting PWM polarity transition. */
1948  plrty_transition_dly_OFF = PWM_TRANSITION_DLY;
1949 
1950  /*----------------------------------------------------------*/
1951  /* COUNT THE NUMBER OF AC CHANGES FOR DEBUGGING - START */
1952  /*----------------------------------------------------------*/
1953 // iDebug++;
1954 //
1955 // if (iDebug > 12) //((status_reg & ST_RUNNING) && (iDebug > 4)) //
1956 // {
1957 // PWM_OFF();
1958 // /* Enable trap of the PWM */
1959 // CCU80_CC82->CR1S = 0;
1960 // CCU80_CC80->CR1S = 0;
1961 // CCU80->GCSS |= (CCU8_GCSS_S0SE_Msk|CCU8_GCSS_S2SE_Msk);
1962 //
1963 // SR_OFF();
1964 //
1965 // power_stage_ON = false;
1966 //
1967 // status_reg = ST_LATCH;
1968 // }
1969  /*----------------------------------------------------------*/
1970  /* COUNT THE NUMBER OF AC CHANGES FOR DEBUGGING - END */
1971  /*----------------------------------------------------------*/
1972 
1973  }
1974  /* Removes Vac crossing PWM disabled status flag. */
1975  AC_zero_cross.pwm_zc_disabled = false;
1976  /* Wait for new zero crossing or current fault for starting PWM/SR. */
1977  AC_zero_cross.vac_zero_cross = false;
1978  AC_zero_cross.neg_iL_fault_CLR = false;
1979  }
1980 
1981  /* Clear AC zero crossing flags */
1982  AC_zero_cross.vin_on_allowed = false;
1983  }
1984  }
1985  }
1986 
1987  /* Delay of PWM pulses in PFC/INV operation.
1988  * In inverter mode, the delay reduces the zero current distortion */
1989  if((power_stage_ON) && (AC_zero_cross.vin_on_cnt < SR_DELAY_SWCYC))
1990  {
1991  // Update cycle count
1992  AC_zero_cross.vin_on_cnt++;
1993 
1994  if (AC_zero_cross.vin_on_cnt == SR_DELAY_SWCYC)
1995  {
1996  if((!fault_reg) && (status_reg & PFC_RUNNING))
1997  {
1998  // Start PWM after SR is started
1999  PWM_ON();
2000  /* Release trap of the PWM */
2001  CCU80_CC82->CR1S = 0xFFFF;//timer_replica_N.config_ptr->period_value;
2002  CCU80_CC80->CR1S = 0xFFFF;//timer_replica_P.config_ptr->period_value;;
2003  CCU80->GCSS |= (CCU8_GCSS_S0SE_Msk|CCU8_GCSS_S2SE_Msk);
2004 
2005  /* Current loop can be evaluated again */
2006  current_loop_ON = true;
2007  }
2008  // Update cycle count
2009  AC_zero_cross.vin_on_cnt++;
2010  }
2011  }
2012 
2013  // Timer after negative current fault is detected
2014  if(AC_zero_cross.neg_iL_fault_count)
2015  {
2016  AC_zero_cross.neg_iL_fault_count--;
2017  }
2018  else if(fault_reg & F_NEG_I) //after timer, check if there was a negative current fault
2019  {
2020  // Raise a flag a certain time after the fault is detected and still voltage present
2021  AC_zero_cross.neg_iL_fault_CLR = true;
2022  AC_zero_cross.neg_iL_fault_count = 0;
2023 
2024  // Clear the fault, so converter can restart.
2025  fault_reg &= ~F_NEG_I;
2026 
2027  //Save polarity as input to next polarity decision filter
2029 
2030  //Reset polarity change time filter
2032  }
2033  }
2034 
2035  /*
2036  * Transition timers
2037  */
2038  if(plrty_transition_dly_OFF)
2039  {
2040  plrty_transition_dly_OFF--;
2041  }
2042 
2043  if(plrty_transition_dly_ON)
2044  {
2045  plrty_transition_dly_ON--;
2046  }
2047 
2048  /*
2049  * AC lost timer for increased BW
2050  */
2051  if(AC_zero_cross.after_AClost_cnt)
2052  {
2053  AC_zero_cross.after_AClost_cnt--;
2054  }
2055 
2056 }
2057 
2058 #endif /* CONTROL_INTERRUPTS_H_ */
2059 
__STATIC_INLINE void set_period(int16_t period)
Definition: control_interrupts.h:544
uint16_t time_INIT
Definition: init_bckgnd_funct.c:101
int32_t a1
Definition: init_bckgnd_funct.h:190
#define MAX_DELTA_DUTY
Definition: init_bckgnd_funct.h:79
static volatile uint8_t count_Vout_OVP_resume
Definition: control_interrupts.h:393
#define OFFSET_END
Definition: control_interrupts.h:89
#define ADC_TO_Q15
Definition: control_interrupts.h:62
int16_t k2
Definition: init_bckgnd_funct.h:172
volatile int32_t auxDC
Definition: init_bckgnd_funct.c:72
#define COEFF_OFFSET_3k_Q
Definition: control_interrupts.h:107
Communication code.
uint16_t qiL_offset
Definition: control_interrupts.h:308
static volatile grid_voltage_t input_polarity[2]
Definition: control_interrupts.h:404
int32_t state_output[3]
Definition: init_bckgnd_funct.h:195
Header of the functions for SW initialization and running in the background during operation...
Definition: control_interrupts.h:298
Definition: control_interrupts.h:297
uint16_t qiL_offset_count
Definition: control_interrupts.h:307
Definition: control_interrupts.h:340
__STATIC_INLINE void relay_ON()
Definition: control_interrupts.h:480
Definition: control_interrupts.h:280
op_mode_t
Definition: control_interrupts.h:295
int16_t qVin_AC_Diff
Definition: control_interrupts.h:318
__STATIC_INLINE void SR_LS_ON()
Definition: control_interrupts.h:472
uint16_t vin_low_cnt
Definition: control_interrupts.h:349
uint16_t qVbus_DC_send
Definition: control_interrupts.h:314
__IO uint32_t * ADCResult_Temp
Definition: control_interrupts.c:74
uint8_t time_extra_bulk
Definition: init_bckgnd_funct.c:102
static volatile AC_monitoring_t AC_zero_cross
Definition: control_interrupts.h:410
int32_t calc11
Definition: init_bckgnd_funct.h:179
int32_t output_int
Definition: init_bckgnd_funct.h:217
enum grid_voltage grid_voltage_t
#define ST_SFTSTRT_END
Definition: control_interrupts.h:218
Definition: control_interrupts.h:279
__STATIC_INLINE void set_duty_ADC(int16_t duty, int16_t period)
Definition: control_interrupts.h:497
comp_BW_t bandwitdh
Definition: control_interrupts.h:309
#define PFC_RUNNING
Definition: control_interrupts.h:271
uint32_t PI_ref_int
Definition: init_bckgnd_funct.h:203
int32_t ratio_FF
Definition: init_bckgnd_funct.h:197
int32_t calc12
Definition: init_bckgnd_funct.h:181
uint16_t int_ref
Definition: init_bckgnd_funct.h:221
uint16_t qTemp_amb
Definition: control_interrupts.h:323
volatile fault_t fault_reg
Definition: control_interrupts.c:86
int16_t a1[3]
Definition: init_bckgnd_funct.h:169
uint32_t qiL_offset_acc
Definition: control_interrupts.h:306
#define CURRENT_REF_TO_Q15
Definition: control_interrupts.h:75
Definition: control_interrupts.h:290
bool vac_zero_cross
Definition: control_interrupts.h:342
uint16_t qiL
Definition: control_interrupts.h:304
volatile bool power_stage_ON
Definition: init_bckgnd_funct.c:85
const params_t parameters_DFLT
Definition: communication.c:59
uint8_t time_V_step
Definition: init_bckgnd_funct.c:100
int32_t state_error[3]
Definition: init_bckgnd_funct.h:199
static void bulk_monitoring()
Bulk voltage monitoring. OVP and UVP during operation for PFC. In inverter operation the DC range i...
Definition: control_interrupts.h:1461
int32_t calc22
Definition: init_bckgnd_funct.h:182
#define MIN_DUTY
Definition: init_bckgnd_funct.h:77
state
Definition: control_interrupts.h:257
__STATIC_INLINE void SR_OFF()
Definition: control_interrupts.h:456
uint16_t inv_power
Definition: control_interrupts.h:328
volatile PFC_var_t PFC
Definition: control_interrupts.c:90
uint8_t shift_to_q29
Definition: init_bckgnd_funct.h:194
__STATIC_INLINE void SR_HS_ON()
Definition: control_interrupts.h:464
enum fault fault_t
static volatile uint8_t input_polarity_count
Definition: control_interrupts.h:407
bool pwm_zc_disabled
Definition: control_interrupts.h:343
#define VOUT_START_MIN
Definition: control_interrupts.h:182
#define VDC_STOP_MAX_INV
Definition: control_interrupts.h:203
uint16_t int_ref_steady
Definition: init_bckgnd_funct.h:223
static volatile uint8_t iDebug
Definition: control_interrupts.h:414
AC_LCDO_t
Definition: control_interrupts.h:332
#define IL_AVG_GAIN
Definition: control_interrupts.h:94
#define LPF_TO_2ADC
Definition: control_interrupts.h:68
static volatile uint8_t acok_counter
Definition: control_interrupts.h:380
#define IL_AVG_GAIN_Q
Definition: control_interrupts.h:96
uint16_t store_params
Definition: control_interrupts.h:325
Definition: control_interrupts.h:283
bool first_iter
Definition: init_bckgnd_funct.h:226
bool neg_iL_fault_CLR
Definition: control_interrupts.h:347
#define OFFSET_VAC
Definition: control_interrupts.h:91
volatile controller_int_t voltage_int_ctr
Definition: init_bckgnd_funct.c:80
#define INEG_FAULT_Q15
Definition: control_interrupts.h:122
__STATIC_INLINE void PWM_ON()
Definition: control_interrupts.h:446
volatile IIR_lowpass_int_t ACfilter
Definition: init_bckgnd_funct.c:58
struct AC_monitoring AC_monitoring_t
bool first_pol_change
Definition: control_interrupts.h:353
__IO uint32_t * ADCResult_VAC
Definition: control_interrupts.c:73
#define NOTCH_TO_Q15
Definition: control_interrupts.h:70
#define AFTER_ACLOST_TIME
Definition: control_interrupts.h:243
uint16_t qVbus_DC
Definition: control_interrupts.h:313
static void iL_modif()
Current rectification according to polarity and offset removal.
Definition: control_interrupts.h:609
#define VOUT_OVP_STOP
Definition: control_interrupts.h:173
#define SR_DELAY_SWCYC
Definition: control_interrupts.h:246
Definition: control_interrupts.h:277
int16_t * output_int_limit
Definition: init_bckgnd_funct.h:218
volatile IIR_lowpass_int_t DCfilter
Definition: init_bckgnd_funct.c:69
#define VDC_STOP_MIN_INV
Definition: control_interrupts.h:201
Definition: control_interrupts.h:278
void control_loop_init()
Assign value to the coefficients of voltage and current loops. Reset the state and output of both c...
Definition: init_bckgnd_funct.c:140
int16_t k1
Definition: init_bckgnd_funct.h:168
int16_t duty
Definition: init_bckgnd_funct.h:198
uint32_t qVin_inv2
Definition: control_interrupts.h:320
#define ILOOP_COEFF_Q
Definition: control_interrupts.h:104
grid_voltage
Definition: control_interrupts.h:357
__IO uint32_t * ADCResult_DCBus
Definition: control_interrupts.c:72
static void iL_ref()
Current reference calculation for next loop iteration.
Definition: control_interrupts.h:762
static void iL_deadtime()
Dead time modification based on sensed current. Parameters for calculation can be changed through UA...
Definition: control_interrupts.h:883
__IO uint32_t * ADCResult_iL
Definition: control_interrupts.c:71
#define VDC_START_MAX_INV
Definition: control_interrupts.h:198
op_mode_t pfc_inv_mode
Definition: control_interrupts.h:326
static volatile bool acok
Definition: control_interrupts.h:382
int16_t qiL_int
Definition: control_interrupts.h:312
#define MAX_DUTY_LCDO
Definition: init_bckgnd_funct.h:82
int16_t output_max
Definition: init_bckgnd_funct.h:220
struct PFC_var PFC_var_t
#define ST_STB_BOP_SFTSTRT_WAIT
Definition: control_interrupts.h:214
static volatile uint8_t count_Vout_OVP_stop
Definition: control_interrupts.h:392
volatile bool current_loop_ON
Definition: init_bckgnd_funct.c:87
#define VIN_ZERO_THRSLD
Definition: control_interrupts.h:233
uint16_t output
Definition: init_bckgnd_funct.h:219
#define VDC_START_MIN_INV
Definition: control_interrupts.h:196
uint16_t qVin_BOP
Definition: control_interrupts.h:319
volatile int32_t auxAC
Definition: init_bckgnd_funct.c:61
#define REG1_ILIMIT_Q15
Definition: control_interrupts.h:117
static void inv_RMS_acok()
1/RMS^2 for line feed-forward in the current reference calculation.
Definition: control_interrupts.h:1400
bool pwm_on_seqnc_enbl
Definition: control_interrupts.h:346
#define IIR_QFORMAT
Definition: control_interrupts.h:64
static volatile int16_t offset_detected
Definition: control_interrupts.h:397
#define DT_SLOPE_Q
Definition: control_interrupts.h:152
Definition: control_interrupts.h:281
enum state state_t
uint16_t after_AClost_cnt
Definition: control_interrupts.h:350
uint16_t PI_ref
Definition: init_bckgnd_funct.h:202
#define MAX_DUTY
Definition: init_bckgnd_funct.h:75
static void pseudo_pll()
AC zero crossing management. High-frequency and Low-frequency signals management.
Definition: control_interrupts.h:1661
static volatile uint32_t Vrms2
Definition: control_interrupts.h:386
#define ACCOMP_TO_Q15
Definition: control_interrupts.h:77
int16_t a[3]
Definition: init_bckgnd_funct.h:211
#define VOLTAGE_OFFSET_NL_ON
Definition: control_interrupts.h:185
int32_t output
Definition: init_bckgnd_funct.h:177
params_t parameters_RAM
Definition: communication.c:53
AC_LCDO_t AC_LCDO_detect
Definition: control_interrupts.h:352
#define OFFSET_SAMPLES_2
Definition: control_interrupts.h:85
#define OFFSET_SAMPLES
Definition: control_interrupts.h:87
comp_BW_t
Definition: control_interrupts.h:288
uint16_t PI_ref_max
Definition: init_bckgnd_funct.h:204
static void state_machine()
State machine for start up process.
Definition: control_interrupts.h:1083
int32_t output1
Definition: init_bckgnd_funct.h:176
fault
Definition: control_interrupts.h:275
Definition: control_interrupts.h:302
int32_t u1[3]
Definition: init_bckgnd_funct.h:171
#define PR_MAX
Definition: control_interrupts.h:155
#define VOUT_UVP_MIN
Definition: control_interrupts.h:179
#define AC_LCDO_DETECT
Definition: control_interrupts.h:241
#define VIN_OFF_THRSLD
Definition: control_interrupts.h:226
#define TO_QiLOOP
Definition: control_interrupts.h:73
uint16_t inv_power_ref
Definition: control_interrupts.h:327
static void Vbulk_ctrl_loop()
Voltage loop.
Definition: control_interrupts.h:1336
int16_t qiL_k
Definition: control_interrupts.h:305
#define VIN_ON_THRSLD
Definition: control_interrupts.h:228
#define COEFF_MIN_3k_Q
Definition: control_interrupts.h:111
static void lpf_filter_RMS()
LPF of AC voltage for "RMS" calculation. The result is the average of the AC voltage, which is proportional to the AC RMS value.
Definition: control_interrupts.h:1024
int32_t u2[3]
Definition: init_bckgnd_funct.h:175
#define VRMS_MIN_LIMIT
Definition: init_bckgnd_funct.h:102
int16_t b1[3]
Definition: init_bckgnd_funct.h:170
Definition: control_interrupts.h:282
#define AC_400V
Definition: control_interrupts.h:249
#define ST_STB_BOP_END
Definition: control_interrupts.h:216
bool iL_offset_en
Definition: control_interrupts.h:310
__STATIC_INLINE void PWM_OFF()
Definition: control_interrupts.h:436
int32_t qVbus_DC_filt
Definition: control_interrupts.h:315
bool vin_on_allowed
Definition: control_interrupts.h:345
static void iL_coeff()
Current loop coefficients according to the sensed currents. Single linear approximation.
Definition: control_interrupts.h:825
int32_t calc21
Definition: init_bckgnd_funct.h:180
volatile state_t status_reg
Definition: control_interrupts.c:85
int16_t b[3]
Definition: init_bckgnd_funct.h:212
uint16_t qTemp
Definition: control_interrupts.h:322
#define ST_STB_BOP_CHECK_BULK
Definition: control_interrupts.h:212
uint16_t power_target
Definition: communication.h:197
int16_t error[3]
Definition: init_bckgnd_funct.h:215
bool qVbus_DC_OK
Definition: control_interrupts.h:316
int16_t * duty_iL
Definition: init_bckgnd_funct.h:196
uint16_t qVin_AC
Definition: control_interrupts.h:317
int16_t b2[3]
Definition: init_bckgnd_funct.h:174
__STATIC_INLINE void relay_OFF()
Definition: control_interrupts.h:488
int32_t a0
Definition: init_bckgnd_funct.h:189
#define COEFF_SLOPE_3k_Q
Definition: control_interrupts.h:109
static void notch_filter_Vbulk()
Notch filter of bulk voltage for voltage loop.
Definition: control_interrupts.h:969
uint8_t shift_to_q15
Definition: init_bckgnd_funct.h:214
#define ZERO_LOC_3k_Q
Definition: control_interrupts.h:114
uint16_t qVin_inv2_16b
Definition: control_interrupts.h:321
uint16_t qTemp_send
Definition: control_interrupts.h:324
static void iL_loop()
Current loop and duty feed-forward (DFF) calculation. DFF is calculated assuming fixed 400V output...
Definition: control_interrupts.h:662
Definition: control_interrupts.h:291
static void iL_offset_calc()
Current offset calculation based on averaging ADC samples before start-up of the power stage...
Definition: control_interrupts.h:569
int32_t interm_calc2
Definition: init_bckgnd_funct.h:225
#define PWM_TRANSITION_DLY
Definition: control_interrupts.h:239
Definition: control_interrupts.h:284
int32_t interm_calc1
Definition: init_bckgnd_funct.h:224
bool iL_offset_done
Definition: control_interrupts.h:311
volatile controller_PI_t current_PI_ctr
Definition: init_bckgnd_funct.c:82
int32_t state[3]
Definition: init_bckgnd_funct.h:216
uint8_t neg_iL_fault_count
Definition: control_interrupts.h:348
bool pwm_on_allowed
Definition: control_interrupts.h:344
uint16_t vin_on_cnt
Definition: control_interrupts.h:351
static volatile uint16_t stb_bop_count
Definition: control_interrupts.h:372
int16_t a2[3]
Definition: init_bckgnd_funct.h:173