XMC42_800WPSU_LLC
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
interrupts_control.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, Infineon Technologies AG
3  * <br>All rights reserved.
4  * <br>Redistribution and use in source and binary forms, with or without
5  * modification,are permitted provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice,
9  * this list of conditions and the following disclaimer in the documentation
10  * and/or other materials provided with the distribution.
11  * - Neither the name of the copyright holders nor the names of its contributors
12  * may be used to endorse or promote products derived from this software without
13  * specific prior written permission.
14  * .
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  * <br>To improve the quality of the software, users are encouraged to share
27  * modifications, enhancements or bug fixes with Infineon Technologies AG
28  * dave@infineon.com).
29  */
30 
42 /***********************************************************************************************************************
43  * HEADER FILES
44  **********************************************************************************************************************/
45 
46 #include "interrupts_control.h"
47 #include <init_bckgnd.h>
48 #include "xmc_vadc.h"
49 
50 /**********************************************************************************************************************
51 * VARAIBLE DEFINITION
52 **********************************************************************************************************************/
53 
58 volatile uint32_t *ADCResult_iR;
59 volatile uint32_t *ADCResult_12V;
60 volatile uint32_t *ADCResult_12VOr;
61 volatile uint32_t *ADCResult_iOut;
72 volatile LLC_t LLC;
73 //LLC_t LLC;
74 
76 //static msg_t UART_rx_buffer;
77 
79 static volatile uint16_t test = 0;
80 
85 
86 //static volatile uint16_t UART_rx_type = 0xFF;
87 
89 //static volatile uint8_t UART_rx_index = 0xFF;
90 
92 static volatile uint16_t retval;
93 
95 static volatile uint16_t VDC_bus[] = {0, 0};
96 
98 static volatile uint16_t Temp[] = {0, 0, 0};
102 volatile uint16_t Temp_fan = 0;
103 
104 
106 static volatile int16_t VDC_bus_diff = 0;
107 
109 static volatile bool first_reception = false;
110 
116 static volatile uint8_t start_ok = 0;
118 static volatile uint8_t start_reception_n = 0;
120 static volatile uint8_t stop_ok = 0;
122 static volatile uint8_t stop_reception_n = 0;
124 static volatile uint8_t reception_n = 0;
128 static volatile uint8_t in_high_count = 0;
129 
131 volatile uint16_t time_count_100kHz = 0;
132 
134 volatile uint16_t time_IMON_100kHz = 0;
135 
137 volatile uint8_t start_sequence_count = 0;
138 
146 static volatile uint16_t iOut_OCP_count = 0;
150 static volatile uint16_t charge_HS_drive;
151 
153 static volatile uint16_t comp_time_counting;
154 
156 
157 //volatile static uint32_t freq_capacitive;
165 static volatile uint16_t timer_value1 = LLC_PERIOD_MIN;
166 static volatile uint16_t timer_value2 = 0;
167 static volatile uint16_t timer_status[32];
168 static volatile uint16_t debug_burst = 0;
169 static volatile uint16_t burst = 0;
178 static const uint16_t linear_fsw_step[] = {1, 2, 3, 4, 5, 6, 7, 8, 9,\
179  10, 11, 12, 14, 15, 17, 20, 24, 28, \
180  29, 31, 33, 35, 37, 41, 43, 47, 51, 56};
181 
183 static volatile uint16_t linear_fsw_count = 0;
187 static volatile bool oring_status = false;
188 
190 static volatile uint16_t pwm_out_status = 0;
191 
197 static uint16_t counter_fifo = 0;
199 volatile static uint8_t i_send = 0;
200 
203 /**********************************************************************************************************************
204 * FUNCTIONS
205 **********************************************************************************************************************/
206 
207 /*---------------------------------------------------------------------------*/
217 #ifndef DOXYGEN_SKIP
218 __attribute__((section(".ram_code")))
219 #endif
220 __STATIC_INLINE void uart_receive()
221 {
222 // /* CAREFUL!!!!!!!!!!!!
223 // * THIS IS JUST FOR DEBUGGING PURPOSES */
224 // VDC_bus[0] = 11500;
225 // // Counter to check between receptions
226 // reception_n++;
227 
228 
229  if(XMC_USIC_CH_RXFIFO_GetLevel(UART_XMC13.channel))
230  {
231 
232  retval = (uint16_t) UART_XMC13.channel->OUTR;
233 
234  /* Message header */
235  if (retval & 0x8000)//== 0xFF0F)
236  {
237  // Parity bit error: omit received data and use previous correct data
238  if ((UART_XMC13.channel->RBUFSR & 0x200))
239  {
240  // The received data is not correct: use the last good data received
241  VDC_bus[0] = VDC_bus[1];
242  }
243  else // No parity bit error, data considered proper.
244  {
245  // Good data
246  VDC_bus[0] = retval & 0x7FF8;
247 
248  // Counter to check between receptions
249  reception_n++;
250 
251  // Save data to use in case of bad data is received
252  VDC_bus[1] = VDC_bus[0];
253  }
254  }
255  else
256  {
257  Temp[2] = Temp[1];
258  Temp[0] = (retval & 0x78) << 1;
259  Temp[1] = (retval & 0x7F80) >> 7;
260 
261  Temp_fan = (Temp[2]+Temp[1]) >> 1;
262  }
263  }
264 }
265 /*---------------------------------------------------------------------------*/
280 #ifndef DOXYGEN_SKIP
281 __attribute__((section(".ram_code")))
282 #endif
283 __STATIC_INLINE void state_control()
284 {
285  /*------------------------------*/
286  /* EXTERNAL ON/OFF */
287  /*------------------------------*/
288 
289  // If PS_ON signal is high, the converter is not allowed to start
290  // Filter based on n-times consecutive with high value
291  if ((ps_on.gpio_port->IN & 0x8) && (in_high_count < 250))
292  in_high_count++;
293  else if (ps_on.gpio_port->IN & 0x8)
294  {
295  /* ON/OFF is done via the VDC start-stop control */
296  VDC_bus[0] = 0;
297  // Do not allow to start
298  start_ok = 0;
300  // keep the converter off
301  stop_ok = 0xFF;
303 
304  in_high_count = 0xFF;
305  }
306  // Reset filter counter
307  else if (!(ps_on.gpio_port->IN & 0x8))
308  in_high_count = 0;
309  // The converter is allowed to start
310 
311 
312 
313  /*------------------------------*/
314  /* FAULT MANAGEMENT */
315  /*------------------------------*/
316 
317  if (fault_reg & (F_OVP|F_UVP|F_OCP_H))
318  {
319  // Keep in latch status (converter off)
321  }
322 
323  /*----------------------------------*/
324  /* VDC START-STOP CONTROL */
325  /*----------------------------------*/
326 
327  /* State machine for input voltage start process */
329  {
330  // The data must be consistent 'n' times in a row
331  if(start_ok < 0x7F)
332  {
333  start_ok++;
334 
335  if (start_ok > 0)
336  {
337  // Data considered consistent
338  start_ok = 0xFF;
339 
340  // Prepare for stop-voltage detection
341  stop_ok = 0;
342  }
343 
344  // Set the number of reception
346  }
347  else
348  // Reset filter counter for stop-voltage detection
349  stop_ok = 0;
350 
351 
352  // Check if initialization time is done after correct voltage is received
353  if (!time_INIT && !status_reg && (start_ok > 0x7F))
354  {
355  /* Initialization of control loops */
357 
358  // Converter off
359  PWM_OFF();
360  sync_OFF();
361 
362  // Oring off
363  ORING_OFF();
364  oring_status = false;
365 
366  // Main LED off
367  LED_OFF_DCNOTOK();
368 
369  // Stop the HB and SR timers --> force shadow transfer to occur for proper timing in start-up
370  XMC_CCU8_SLICE_StopTimer(llc_pwm.ccu8_slice_ptr);
371  XMC_CCU8_SLICE_StopTimer(sync1.ccu8_slice_ptr);
372  XMC_CCU8_SLICE_StopTimer(sync2.ccu8_slice_ptr);
373 
374  // Clear the timers
375  XMC_CCU8_SLICE_ClearTimer(llc_pwm.ccu8_slice_ptr);
376  XMC_CCU8_SLICE_ClearTimer(sync1.ccu8_slice_ptr);
377  XMC_CCU8_SLICE_ClearTimer(sync2.ccu8_slice_ptr);
378 
379  // HB timing for resume of operation
380  // LLC frequency modification
381  llc_pwm.ccu8_slice_ptr->PRS = PR_START;//152;
382  llc_pwm.ccu8_slice_ptr->CR1S = DUTY_START;//28;
383  llc_pwm.ccu8_slice_ptr->CR2S = ADC_START;//14;
384 
385  // LLC dead-time modification
386  llc_pwm.hrc_slice_ptr->SDCR = DEADTIME_R_START;//10;
387  llc_pwm.hrc_slice_ptr->SDCF = DEADTIME_F_START;//10;
388 
389  // SR timing modification for synch single-shot SR
390  sync1.ccu8_slice_ptr->CR1S = 0;
391  sync1.ccu8_slice_ptr->CR2S = 0;
392  sync1.ccu8_slice_ptr->PRS = MIN_DEADTIME;
393 
394  sync2.ccu8_slice_ptr->CR1S = 0;
395  sync2.ccu8_slice_ptr->CR2S = 0;
396  sync2.ccu8_slice_ptr->PRS = MIN_DEADTIME;
397 
398  // Shadow transfer LLC and SR
399  CCU80->GCSS |= SHADOW_TRANSFER_LLC_SR;
400 
401  // Start SR timers
402  XMC_CCU8_SLICE_StartTimer(sync1.ccu8_slice_ptr);
403  XMC_CCU8_SLICE_StartTimer(sync2.ccu8_slice_ptr);
404 
405  /* Start state machine */
406  status_reg = ST_STB;
407 
408  // Restart counter for sequence definition ISR
410  /* Enable interrupt for proper starting sequence timing */
411  NVIC_ClearPendingIRQ(start_sequence.node);
412  NVIC_EnableIRQ(start_sequence.node);
413 
415  // Differentiate start-up than resume operation in burst mode.
416  debug_burst = 0;
417  burst = 0;
418  }
419 
420  }
421  /* State machine for input voltage stop process */
422  else if ((VDC_bus[0] < VDC_BUS_STOP) && (stop_reception_n != reception_n))// || fault_reg)
423  {
424  // The data must be consistent n times in a row
425  if(stop_ok < 0x7F)
426  {
427  stop_ok++;
428 
429  if (stop_ok > 0)
430  {
431  // Data considered consistent
432  stop_ok = 0xFF;
433 
434  // Clear the faults to allow restarting
435  fault_reg = F_INIT;
436 
437  // Prepare for start-voltage detection
438  start_ok = 0;
439  }
440 
441  // Set the number of reception
443  }
444  // If the data is consistent, turn off the converter
445  else
446  {
447  // Clear pendant shadow transfer (non-synchronous ISR)
448  CCU80->GCSC |= (SHADOW_TRANSFER_LLC_SR_CLEAR|CCU8_GCSC_S3SC_Msk);
449 
450  /* Turn off the converter */
451  // Passive level of PWM outputs (synchro trap)
452  CCU80_CC83->CR1S = 0;
453  CCU80->GCSS |= CCU8_GCSS_S3SE_Msk;
454  // SR off
455  sync_OFF();
456  // Oring is off
457  ORING_OFF();
458  oring_status = false;
459 
460  // Main LED off
461  LED_OFF_DCNOTOK();
462 
463  // Frequency ramp counter
464  linear_fsw_count = 0;
465 
466  // Time to next restart (ms)
467  time_INIT = 1;
468 
469  /* Disable resonant current comparison interrupt for SC detection, to allow converter restarting */
470  NVIC_ClearPendingIRQ(HRPWM_3_IRQn);
471  NVIC_DisableIRQ(HRPWM_3_IRQn);
472 
473  // Allow starting sequence
475  }
476  }
477 
478  /*------------------------------*/
479  /* STATE MACHINE */
480  /*------------------------------*/
481 
482  // If the input voltage is in range after certain time, starts driver bootstrap charge
483  if (!time_BOP && (status_reg == ST_STB) && (VDC_bus[0] > VDC_BUS_START_HYST))
484  {
485  // Converter off
486  PWM_OFF();
487  sync_OFF();
488 
489  // Oring off
490  ORING_OFF();
491  oring_status = false;
492 
493  // Main LED off
494  LED_OFF_DCNOTOK();
495 
496  // Charge driver bootstrap by turning on LS
497  XMC_GPIO_SetOutputLow(P0_7);
498  XMC_GPIO_SetOutputHigh(P0_8);
499 
500  // Next state is charging sequence of HS driver
502 
503  // Bootstrap capacitor charging time
504  charge_HS_drive = 14;//14 for 10us ISR;//7 for 20us ISR;//CHARGE_HS_TOTAL;
505  }
506  // Driver bootstrap charge sequence: 140us
507  else if (status_reg == ST_CHARGE)
508  {
509  // HS driver charge counting (waiting to initiate PWM operation)
511  {
512  charge_HS_drive--;
513  }
514  // ADC measurement the cycle before starting the converter to calculate error
515  else if (charge_HS_drive > CHARGE_HS_LOW)
516  {
517  XMC_VADC_GROUP_QueueTriggerConversion(VADC_G0);
518  charge_HS_drive--;
519 
520  // Disable synchro trap to start
521  CCU80_CC83->CR1S = 0xFFFF;//100;//
522  CCU80->GCSS |= CCU8_GCSS_S3SE_Msk;
523  }
524  /* Start PWM sequence: asymmetric PWM based on resonant current */
525  else
526  {
527  // Both signals to zero before starting the PWM
528  XMC_GPIO_SetOutputLow(P0_7);
529  XMC_GPIO_SetOutputLow(P0_8);
530 
531  // Oring is off during comparison sequence
532  ORING_OFF();
533  oring_status = false;
534  // SYNC.RECT. is off during comparison sequence
535  sync_OFF();
536 
537  /* Start the timer */
538  llc_pwm.ccu8_slice_ptr->TCSET |= CCU8_CC8_TCSET_TRBS_Msk;
539 
540  // Change to PWM operation
541  PWM_ON();
542 
543  /* Starting frequency is the last captured frequency */
544  voltage_int_ctr.soft_start = START_FREQ;//*voltage_int_ctr.output_int_limit = START_FREQ;
545 
546  // At starting of the linear frequency ramp the voltage loop starts with maximum period
547  *voltage_int_ctr.output_int_limit = LLC_PERIOD_MAX;
548  /* Save controller state for next calculation */
549  voltage_int_ctr.state[0] = voltage_int_ctr.output_int;
550 
551  /* No ramp reference, the reference for the voltage loop is always the target voltage */
552  voltage_int_ctr.int_ref = voltage_int_ctr.int_ref_steady;//*LLC.qVout;
553 
554  // Test resume of operation --> NO ADC MEASUREMENT SINCE TURN-OFF OF THE PWM (timers is stopped)
555  *LLC.qVout = (*ADCResult_12VOr & 0xFFF) << ADC_TO_Q15;//0;
556 
557  /* Next state: linear variation of the switching frequency to the minimum allowed frequency */
558  status_reg = ST_SOFT_START;//ST_LATCH;
559 
560  // Period ramp in soft-start
561  voltage_int_ctr.ramp_loop = ramp;//false;
562  }
563  }
564  /* Sync. Rect and Oring management */
565  else if ((status_reg == ST_SOFT_START) && (LLC.q12V_oring > VOUT_SR_ON))
566  {
567  // Turn on Sync. Rect when the output voltage reaches a certain limit
568  sync_ON();
569 
570  // Change state
572  }
573  else if ((status_reg == ST_SOFT_START_SR) && (LLC.q12V_oring > VOUT_ORING_ON))
574  {
575  if (!oring_status)
576  {
577  // Turn on Oring switch with proper output voltage
578  ORING_ON();
579  oring_status = true;
580  }
581  else
582  {
583  // After closing oring, voltage after oring is controlled
584  LLC.qVout = (uint16_t *)(&LLC.q12V);
585 
586  // Go to running state
588  }
589  }
590 // else if (status_reg == ST_RUNNING) // NO decision in RUNNING state. Used only for debugging purposes
591 // {
592 // if (VDC_bus[0] < VDC_BUS_STOP)
593 // XMC_GPIO_SetOutputHigh(P0_6);
594 // }
595  // Burst resume operation
596  else if(status_reg == ST_BURST)
597  {
598  // Monitoring output voltage to check when normal operation must be resumed
599  if (*LLC.qVout <= VOUT_TARGET)
600  {
601  /* Prepare to resume the loop operation */
602  // Leave burst mode with resonant/maximum frequency operation
603  *voltage_int_ctr.output_int_limit = LLC_PERIOD_MIN;//RES_FREQ;//
604  //voltage_int_ctr.qOut_peak = LLC_PERIOD_MIN;
605 
606  // Save period state for next calculation
607  voltage_int_ctr.state[0] = voltage_int_ctr.output_int;// >> COMP_SHIFT;//*voltage_int_ctr.output_int_limit;
608 
609  // Error calculated in voltage loop when resuming normal operation
610  voltage_int_ctr.first_iter = true;
611 
612  // Disable synchro trap to start PWM operation
613  CCU80_CC83->CR1S = 0xFFFF;//100;//
614  CCU80->GCSS |= CCU8_GCSS_S3SE_Msk;
615 
616  // Resume normal operation
618  }
619  }
620 
621 
622  if (status_reg & ST_LATCH) // Latch condition in case of detected fault
623  {
624  /* Turn off the converter */
625  PWM_OFF();
626  sync_OFF();
627  ORING_OFF();
628  oring_status = false;
629 
630  // Main LED off
631  LED_OFF_DCNOTOK();
632  }
633 
634 }
635 
636 /*---------------------------------------------------------------------------*/
656 #ifndef DOXYGEN_SKIP
657 __attribute__((section(".ram_code")))
658 #endif
659 __STATIC_INLINE void voltage_loop()
660 {
661 
663  {
664  // Voltage error: used in start-up and voltage loop
665  //voltage_int_ctr.error[0] = voltage_int_ctr.int_ref - LLC.q12V;
666  voltage_int_ctr.error[0] = voltage_int_ctr.int_ref - *LLC.qVout;
667 
668  /*-----------------------------------------------------------------------*/
669  /* Linear frequency modification at start up */
670  /*-----------------------------------------------------------------------*/
671 
672  // Soft-start by ramping linearly the period until the loop takes over
673  if ((voltage_int_ctr.soft_start < LLC_PERIOD_MAX) && !voltage_int_ctr.ramp_loop)//(*voltage_int_ctr.output_int_limit < LLC_PERIOD_MIN)
674  {
675  // 1.5ms ramp @10us
676  if (linear_fsw_count < 24)
677  {
678  voltage_int_ctr.soft_start += linear_fsw_step[0];
680  }
681  else if(linear_fsw_count < 63)
682  {
683  voltage_int_ctr.soft_start += linear_fsw_step[1];
685  }
686  else if(linear_fsw_count < 77)
687  {
688  voltage_int_ctr.soft_start += linear_fsw_step[2];
690  }
691  else if(linear_fsw_count < 90)
692  {
693  voltage_int_ctr.soft_start += linear_fsw_step[3];
695  }
696  else if(linear_fsw_count < 99)
697  {
698  voltage_int_ctr.soft_start += linear_fsw_step[4];
700  }
701  else if(linear_fsw_count < 104)
702  {
703  voltage_int_ctr.soft_start += linear_fsw_step[5];
705  }
706  else if(linear_fsw_count < 109)
707  {
708  voltage_int_ctr.soft_start += linear_fsw_step[6];
710  }
711  else if(linear_fsw_count < 113)
712  {
713  voltage_int_ctr.soft_start += linear_fsw_step[7];
715  }
716  else if(linear_fsw_count < 116)
717  {
718  voltage_int_ctr.soft_start += linear_fsw_step[8];
720  }
721  else if(linear_fsw_count < 120)
722  {
723  voltage_int_ctr.soft_start += linear_fsw_step[9];
725  }
726  else if(linear_fsw_count < 122)
727  {
728  voltage_int_ctr.soft_start += linear_fsw_step[10];
730  }
731  else if(linear_fsw_count < 125)
732  {
733  voltage_int_ctr.soft_start += linear_fsw_step[11];
735  }
736  else if(linear_fsw_count < 128)
737  {
738  voltage_int_ctr.soft_start += linear_fsw_step[12];
740  }
741  else if(linear_fsw_count < 131)
742  {
743  voltage_int_ctr.soft_start += linear_fsw_step[13];
745  }
746  else if(linear_fsw_count < 134)
747  {
748  voltage_int_ctr.soft_start += linear_fsw_step[14];
750  }
751  else if(linear_fsw_count < 137)
752  {
753  voltage_int_ctr.soft_start += linear_fsw_step[15];
755  }
756  else if(linear_fsw_count < 140)
757  {
758  voltage_int_ctr.soft_start += linear_fsw_step[16];
760  }
761  else if(linear_fsw_count < 151)
762  {
763  voltage_int_ctr.soft_start += linear_fsw_step[linear_fsw_count-123];
765  }
766 
767 
768  // Change to voltage loop if the error is lower than 100mV or if the loop period is lower than the ramp
769  if (voltage_int_ctr.error[0] < 150)
770  {
771  // Start frequency of the loop is the last frequency of the ramp
772  *voltage_int_ctr.output_int_limit = voltage_int_ctr.soft_start;
773 
774  // The loop takes over, no period ramp
775  voltage_int_ctr.output = voltage_int_ctr.output_int_limit;
776  //voltage_int_ctr.output = (int16_t *)(&voltage_int_ctr.qOut_peak);
777 
778  // Ramp of the period ends because the loop takes over
779  voltage_int_ctr.ramp_loop = loop;//true;
780 
781  // Smooth transition when the loop takes over
782  voltage_int_ctr.first_iter = true;
783 
784  // Save period state for next calculation
785  voltage_int_ctr.state[0] = voltage_int_ctr.output_int;
786 
787  // Main LED on --> 12V OK
788  LED_ON_DCOK();
789  }
790  else if (*voltage_int_ctr.output_int_limit < voltage_int_ctr.soft_start)
791  {
792  // The loop takes over, no period ramp
793  voltage_int_ctr.output = voltage_int_ctr.output_int_limit;
794  //voltage_int_ctr.output = (int16_t *)(&voltage_int_ctr.qOut_peak);
795 
796  // Ramp of the period ends because the loop takes over
797  voltage_int_ctr.ramp_loop = loop;//true;
798 
799  // Smooth transition when the loop takes over
800  voltage_int_ctr.first_iter = true;
801 
802  // Save period state for next calculation
803  voltage_int_ctr.state[0] = voltage_int_ctr.output_int;
804 
805  // Main LED on --> 12V OK
806  LED_ON_DCOK();
807  }
808  }
809 
810 
811  /*-----------------------------------------------------------------------*/
812  /* Voltage loop */
813  /*-----------------------------------------------------------------------*/
814  /* Error management in compensator first iteration */
815  if (voltage_int_ctr.first_iter)
816  {
817  // Fill the error array for smooth transition at first iteration after linear period variation or Burst
818  voltage_int_ctr.error[2] = voltage_int_ctr.error[0];
819  voltage_int_ctr.error[1] = voltage_int_ctr.error[0];
820 
821  // No first iteration any more
822  voltage_int_ctr.first_iter = false;
823  }
824 
825 
826  /*
827  * Voltage compensator PID
828  * T[k] = T[k-1] + a0 * e[k] + a1 * e[k-1] + a2 * e[k-2]
829  * coefficients => Qx
830  * input => Q0
831  */
832  voltage_int_ctr.int_calc = (int32_t)voltage_int_ctr.a[0] * (int32_t)voltage_int_ctr.error[0];
833  voltage_int_ctr.int_calc += (int32_t)voltage_int_ctr.a[1] * (int32_t)voltage_int_ctr.error[1];
834  voltage_int_ctr.int_calc += (int32_t)voltage_int_ctr.a[2] * (int32_t)voltage_int_ctr.error[2];
835  voltage_int_ctr.int_calc = voltage_int_ctr.int_calc << COMP_SHIFT;
836  voltage_int_ctr.output_int = voltage_int_ctr.int_calc + voltage_int_ctr.state[0];
837 
838 
839  // Frequency limitation (control loop saturation) and BURST detection
840  if ((int16_t)(*voltage_int_ctr.output_int_limit) > LLC_PERIOD_MAX)
841  {
842  voltage_int_ctr.output_int = LLC_PERIOD_MAX << 16;
843 
844  // No trigger of burst mode operation in state machine
846  }
847  else if ((int16_t)(*voltage_int_ctr.output_int_limit) < LLC_PERIOD_MIN)
848  {
849  voltage_int_ctr.output_int = LLC_PERIOD_MIN << 16;
850 
851  // Trigger burst mode operation in state machine
853  }
854  else
855  {
856  // No trigger of burst mode operation in state machine
858  }
859 
860 
861  /*-----------------------------------------------------------------------*/
862  /* Dead time calculation */
863  /*-----------------------------------------------------------------------*/
864 
865  // Dead time calculation: 160ns if fsw<200kHz, linear variation to 350ns@fsw=400kHz
866  if (!voltage_int_ctr.ramp_loop)
867  LLC.deadtime = MIN_DEADTIME;
868  else
869  {
870  if (*voltage_int_ctr.output_int_limit >= 400)
871  LLC.deadtime = MIN_DEADTIME;
872  else
873  LLC.deadtime = DEADTIME_0Hz-(((int32_t)DEADTIME_SLOPE * (int32_t)*voltage_int_ctr.output_int_limit) >> Q15_PRODUCT_SHIFT);
874  }
875 
876 
877  /*-----------------------------------------------------------------------*/
878  /* LLC-HB and SR timing */
879  /*-----------------------------------------------------------------------*/
880 
881  // LLC frequency modification
882  llc_pwm.ccu8_slice_ptr->CR1S = (*voltage_int_ctr.output >> 1)+1;// +1 to compensate asymmetric converter operation
883  llc_pwm.ccu8_slice_ptr->CR2S = (*voltage_int_ctr.output >> 2);
884  llc_pwm.ccu8_slice_ptr->PRS = *voltage_int_ctr.output;
885 
886  // LLC dead-time modification
887  llc_pwm.hrc_slice_ptr->SDCR = LLC.deadtime;
888  llc_pwm.hrc_slice_ptr->SDCF = LLC.deadtime;
889 
890 
891  // SR timing modification for synch single-shot SR
892  sync1.ccu8_slice_ptr->CR1S = DELAY_SR_ON+LLC.deadtime; // On time does not change with frequency
893  if (*voltage_int_ctr.output > BELOW_FREQ)
894  LLC.SR1_off_timing = DELAY_SR_OFF_BELOW_2 - LLC.deadtime; // Off Delay under 90kHz
895  else if (*voltage_int_ctr.output > RES_FREQ)
896  LLC.SR1_off_timing = DELAY_SR_OFF_BELOW_1 - LLC.deadtime; // Off Delay under resonant frequency (149kHz)
897  else
898  LLC.SR1_off_timing = (*voltage_int_ctr.output >> 1) + DELAY_SR_OFF_ABOVE - LLC.deadtime; // Off Delay above resonant frequency (149kHz)
899  sync1.ccu8_slice_ptr->PRS = LLC.SR1_off_timing;
900 
901  sync2.ccu8_slice_ptr->CR1S = DELAY_SR_ON+LLC.deadtime; // On time does not change with frequency
902  if (*voltage_int_ctr.output > BELOW_FREQ)
903  LLC.SR2_off_timing = DELAY_SR_OFF_BELOW_2 - LLC.deadtime; // Off Delay under 90kHz
904  else if (*voltage_int_ctr.output > RES_FREQ)
905  LLC.SR2_off_timing = DELAY_SR_OFF_BELOW_1 - LLC.deadtime; // Off Delay under resonant frequency (149kHz)
906  else
907  LLC.SR2_off_timing = (*voltage_int_ctr.output >> 1) + DELAY_SR_OFF_ABOVE - LLC.deadtime; // Off Delay above resonant frequency (149kHz)
908  sync2.ccu8_slice_ptr->PRS = LLC.SR2_off_timing;
909 
910  // Timer replica period
911  timer_replica.ccu8_slice_ptr->PRS = *voltage_int_ctr.output;
912 
913  // Shadow transfer LLC and SR
914  CCU80->GCSS |= (SHADOW_TRANSFER_LLC_SR|CCU8_GCSS_S3SE_Msk);
915 
916  /*-----------------------------------------------------------------------*/
917  /* Save values for next iteration */
918  /*-----------------------------------------------------------------------*/
919 
920  // Save loop period state for next calculation
921  voltage_int_ctr.state[0] = voltage_int_ctr.output_int;
922  // Save loop error state for next calculation
923  voltage_int_ctr.error[2] = voltage_int_ctr.error[1];
924  voltage_int_ctr.error[1] = voltage_int_ctr.error[0];
925  }
926  else if (!status_reg)
927  {
928  /* Initialization of control loops */
930  }
931 } // 2P2Z: 1.9us (15.02.16)
932  // PID Q14: 2.9us (17.05.16)
933 
934 /*---------------------------------------------------------------------------*/
941 #ifndef DOXYGEN_SKIP
942 __attribute__((section(".ram_code")))
943 #endif
944 __STATIC_INLINE void burst_mode()
945 {
946  // Burst mode operation triggered by maximum frequency and output voltage out of range
947  if ((status_reg & ST_RUNNING) && (burst_status & BURST_ON) && (*LLC.qVout > VOUT_BURST))//(LLC.q12V >= VOUT_BURST))
948  {
949  // Count the times that burst mode conditions happen in a row
950  burst++;
951  if(burst > 2)
952  {
953  /* Burst mode keeping SR operating at maximum frequency to keep C-HB until operation is resumed */
954 
955  /* Turn off LLC PWM outputs by synchronous trap */
956  CCU80_CC83->CR1S = 0;
957  CCU80->GCSS |= CCU8_GCSS_S3SE_Msk;
958 
959  // Resonant/Maximum frequency operation in burst mode
960  *voltage_int_ctr.output_int_limit = LLC_PERIOD_MIN;
961 
962  // Min deadtime at 149kZ (resonant); 350ns dead time at 300kHz
963  LLC.deadtime = DEADTIME_0Hz-(((int32_t)DEADTIME_SLOPE * (int32_t)*voltage_int_ctr.output_int_limit) >> Q15_PRODUCT_SHIFT);
964 
965  // LLC frequency modification
966  llc_pwm.ccu8_slice_ptr->PRS = *voltage_int_ctr.output_int_limit;
967  llc_pwm.ccu8_slice_ptr->CR1S = (*voltage_int_ctr.output >> 1)+1;
968  llc_pwm.ccu8_slice_ptr->CR2S = (*voltage_int_ctr.output_int_limit >> 2);
969 
970  // LLC dead-time modification
971  llc_pwm.hrc_slice_ptr->SDCR = LLC.deadtime;
972  llc_pwm.hrc_slice_ptr->SDCF = LLC.deadtime;
973 
974  // SR timing modification
975  sync1.ccu8_slice_ptr->CR1S = 43+LLC.deadtime; // On time delay
976  sync1.ccu8_slice_ptr->PRS = (*voltage_int_ctr.output_int_limit >> 1) + DELAY_SR_OFF_ABOVE - LLC.deadtime; //Above resonant operation at burst mode
977  sync2.ccu8_slice_ptr->CR1S = 43+LLC.deadtime; // On time delay
978  sync2.ccu8_slice_ptr->PRS = (*voltage_int_ctr.output_int_limit >> 1) + DELAY_SR_OFF_ABOVE - LLC.deadtime; //Above resonant operation at burst mode
979  // Timer replica period
980  timer_replica.ccu8_slice_ptr->PRS = *voltage_int_ctr.output_int_limit;
981 
982  // Shadow transfer LLC and SR
983  CCU80->GCSS |= (SHADOW_TRANSFER_LLC_SR|CCU8_GCSS_S3SE_Msk);
984 
985  // Save period state for next calculation
986  voltage_int_ctr.state[0] = voltage_int_ctr.output_int;// >> COMP_SHIFT;//*voltage_int_ctr.output_int_limit;
987 
988  // Go to burst state to monitor output voltage
990  }
991  }
992  else
993  burst = 0;
994 }
995 
996 
997 /*---------------------------------------------------------------------------*/
1004 #ifndef DOXYGEN_SKIP
1005 __attribute__((section(".ram_code")))
1006 #endif
1007 __STATIC_INLINE void uart_send()
1008 {
1009  if((!time_send_UART) && (status_reg & ST_RUNNING))
1010  {
1011  if (XMC_USIC_CH_TXFIFO_GetLevel(UART_XMC13.channel) > 4)
1012  {
1013  counter_fifo = i_send; // Debug
1014  }
1015  else
1016  {
1017  /* Send info (current and fault) to the primary side controller */
1018  UART_XMC13.channel->IN[0U] = (0x8000 | 0x1000 | fault_reg);//0xFF0F;
1019  UART_XMC13.channel->IN[0U] = (((uint16_t)(LLC.qIout) << 3));// | fault_reg);
1020 
1021  // Set timer (500us) to wait for next information transmission
1022  time_send_UART = 6;
1023  }
1024  }
1025 }
1026 
1027 
1028 /*---------------------------------------------------------------------------*/
1044 __attribute__((section(".ram_code")))
1046 {
1047 // /* Debug LED1 */
1048 // XMC_GPIO_SetOutputHigh(P1_1);
1049 
1050  /* Time counting */
1052 
1053  /* counter for current monitoring change */
1054  time_IMON_100kHz++;
1055 
1056  // Output voltage measurement, synchronized channels
1057  LLC.q12V_oring = (*ADCResult_12VOr & 0xFFF) << ADC_TO_Q15;
1058  LLC.q12V = (*ADCResult_12V & 0xFFF) << ADC_TO_Q15;
1059 
1060  // Over-voltage protection --> latch the converter if the output voltage reaches 14V.
1062 
1063  // Check output current and OCP
1064  LLC.qIout = (*ADCResult_iOut & 0xFFF);
1065 
1066  /* Over-current protection --> latch the converter according to output current monitoring */
1068 
1069  // Receive data from XMC13
1070  uart_receive();
1071 
1072  // State machine
1073  state_control();
1074 
1075  // Control loop
1076  voltage_loop();
1077 
1078  // Burst mode detection
1079  burst_mode();
1080 
1081  // Send information to the secondary side
1082  uart_send();
1083 
1084 // /* Debug LED1 */
1085 // XMC_GPIO_SetOutputLow(P1_1);
1086 } // 4.7us/20us (@50kHz ISR); PID Q15 (11.05.16)
1087  // 6us (running)/8.8us (soft-start) (@100kHz ISR); PID Q14 (30.08.16)
1088  // 8us (running)/9.8us (soft-start) (@100kHz ISR); PID + peaking (31.08.16)
1089 
1090 
1091 /*---------------------------------------------------------------------------*/
1098 __attribute__((section(".ram_code")))
1100 {
1101  if (!start_sequence_count)
1102  {
1104 
1105  // Second cycle asymmetric operation. Values obtained experimentally.
1106  llc_pwm.ccu8_slice_ptr->PRS = 111;
1107  llc_pwm.ccu8_slice_ptr->CR1S = 48;
1108  llc_pwm.ccu8_slice_ptr->CR2S = 14;
1109  llc_pwm.ccu8_module_ptr->GCSS |= llc_pwm.shadow_txfr_msk;
1110  }
1111  else if (start_sequence_count < 2)
1112  {
1114 
1115  // Second cycle asymmetric operation. Values obtained experimentally.
1116  llc_pwm.ccu8_slice_ptr->PRS = 111;
1117  llc_pwm.ccu8_slice_ptr->CR1S = 56;
1118  llc_pwm.ccu8_slice_ptr->CR2S = 14;
1119  llc_pwm.ccu8_module_ptr->GCSS |= llc_pwm.shadow_txfr_msk;
1120 
1121  }
1122  else
1123  {
1124 
1125  // From next cycle is 50% duty operation
1126  llc_pwm.ccu8_slice_ptr->PRS = START_FREQ;
1127  llc_pwm.ccu8_slice_ptr->CR1S = START_FREQ >> 1;
1128  llc_pwm.ccu8_slice_ptr->CR2S = START_FREQ >> 2;
1129  llc_pwm.ccu8_module_ptr->GCSS |= llc_pwm.shadow_txfr_msk;
1130 
1131  /* Disable interrupt after necessary changes */
1132  NVIC_DisableIRQ(start_sequence.node);
1133 
1134  /* Enable resonant current comparison interrupt for SC detection */
1135  NVIC_ClearPendingIRQ(HRPWM_3_IRQn);
1136  NVIC_EnableIRQ(HRPWM_3_IRQn);
1137 
1139  }
1140 }
1141 
1142 
1143 /*---------------------------------------------------------------------------*/
1150 __attribute__((section(".ram_code")))
1151 void CSG_rise_ISR(void)
1152 {
1153  // Turn off SR
1154  sync_OFF();
1155 
1156  // ORing off
1157  ORING_OFF();
1158  oring_status = false;
1159 
1160  // Stop the timer to keep the PWM output
1161  XMC_CCU8_SLICE_StopTimer(llc_pwm.ccu8_slice_ptr);
1162 
1163  // Main LED off
1164  LED_OFF_DCNOTOK();
1165 
1166  // State with no decision. Controller reset necessary to leave OCP state.
1167  status_reg = ST_OCP;
1168  fault_reg = F_SC;
1169 }