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  * Variables
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 
110 static volatile int16_t VDC_bus_diff = 0;
111 
113 static volatile bool first_reception = false;
114 
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;
164 static volatile uint16_t timer_value1 = LLC_PERIOD_MIN;
165 static volatile uint16_t timer_value2 = 0;
166 static volatile uint16_t timer_status[32];
167 static volatile uint16_t debug_burst = 0;
168 static volatile uint16_t burst = 0;
176 static const uint16_t linear_fsw_step[] = {1, 2, 3, 4, 5, 6, 7, 8, 9,\
177  10, 11, 12, 14, 15, 17, 20, 24, 28, \
178  29, 31, 33, 35, 37, 41, 43, 47, 51, 56};
179 
181 static volatile uint16_t linear_fsw_count = 0;
185 static volatile bool oring_status = false;
186 
188 static volatile uint16_t pwm_out_status = 0;
189 
195 static uint16_t counter_fifo = 0;
197 volatile static uint8_t i_send = 0;
198 
201 /**********************************************************************************************************************
202 * FUNCTIONS
203 **********************************************************************************************************************/
204 
205 /*---------------------------------------------------------------------------*/
215 #ifndef DOXYGEN_SKIP
216 __attribute__((section(".ram_code")))
217 #endif
218 __STATIC_INLINE void uart_receive()
219 {
220  if(XMC_USIC_CH_RXFIFO_GetLevel(UART_XMC13.channel))
221  {
222  retval = (uint16_t) UART_XMC13.channel->OUTR;
223 
224  /* Message header */
225  if (retval & 0x8000)//== 0xFF0F)
226  {
227  // Parity bit error: omit received data and use previous correct data
228  if ((UART_XMC13.channel->RBUFSR & 0x200))
229  {
230  // The received data is not correct: use the last good data received
231  VDC_bus[0] = VDC_bus[1];
232  }
233  else // No parity bit error, data considered proper.
234  {
235  // Good data
236  VDC_bus[0] = retval & 0x7FF8;
237 
238  // Counter to check between receptions
239  reception_n++;
240 
241  // Save data to use in case of bad data is received
242  VDC_bus[1] = VDC_bus[0];
243  }
244  }
245  else
246  {
247  Temp[2] = Temp[1];
248  Temp[0] = (retval & 0x78) << 1;
249  Temp[1] = (retval & 0x7F80) >> 7;
250 
251  Temp_fan = (Temp[2]+Temp[1]) >> 1;
252  }
253  }
254 }
255 /*---------------------------------------------------------------------------*/
270 #ifndef DOXYGEN_SKIP
271 __attribute__((section(".ram_code")))
272 #endif
273 __STATIC_INLINE void state_control()
274 {
275  /*------------------------------*/
276  /* EXTERNAL ON/OFF */
277  /*------------------------------*/
278 
279  // If PS_ON signal is high, the converter is not allowed to start
280  // Filter based on n-times consecutive with high value
281  if ((ps_on.gpio_port->IN & 0x8) && (in_high_count < 250))
282  in_high_count++;
283  else if (ps_on.gpio_port->IN & 0x8)
284  {
285  /* ON/OFF is done via the VDC start-stop control */
286  VDC_bus[0] = 0;
287  // Do not allow to start
288  start_ok = 0;
290  // keep the converter off
291  stop_ok = 0xFF;
293 
294  in_high_count = 0xFF;
295  }
296  // Reset filter counter
297  else if (!(ps_on.gpio_port->IN & 0x8))
298  in_high_count = 0;
299  // The converter is allowed to start
300 
301 
302 
303  /*------------------------------*/
304  /* FAULT MANAGEMENT */
305  /*------------------------------*/
306 
307  if (fault_reg & (F_OVP|F_UVP|F_OCP_H))
308  {
309  // Keep in latch status (converter off)
311  }
312 
313  /*----------------------------------*/
314  /* VDC START-STOP CONTROL */
315  /*----------------------------------*/
316 
317  /* State machine for input voltage start process */
319  {
320  // The data must be consistent 'n' times in a row
321  if(start_ok < 0x7F)
322  {
323  start_ok++;
324 
325  if (start_ok > 0)
326  {
327  // Data considered consistent
328  start_ok = 0xFF;
329 
330  // Prepare for stop-voltage detection
331  stop_ok = 0;
332  }
333 
334  // Set the number of reception
336  }
337  else
338  // Reset filter counter for stop-voltage detection
339  stop_ok = 0;
340 
341 
342  // Check if initialization time is done after correct voltage is received
343  if (!time_INIT && !status_reg && (start_ok > 0x7F))
344  {
345  /* Initialization of control loops */
347 
348  // Converter off
349  PWM_OFF();
350  sync_OFF();
351 
352  // Oring off
353  ORING_OFF();
354  oring_status = false;
355 
356  // Main LED off
357  LED_OFF_DCNOTOK();
358 
359  // Stop the HB and SR timers --> force shadow transfer to occur for proper timing in start-up
360  XMC_CCU8_SLICE_StopTimer(llc_pwm.ccu8_slice_ptr);
361  XMC_CCU8_SLICE_StopTimer(sync1.ccu8_slice_ptr);
362  XMC_CCU8_SLICE_StopTimer(sync2.ccu8_slice_ptr);
363 
364  // Clear the timers
365  XMC_CCU8_SLICE_ClearTimer(llc_pwm.ccu8_slice_ptr);
366  XMC_CCU8_SLICE_ClearTimer(sync1.ccu8_slice_ptr);
367  XMC_CCU8_SLICE_ClearTimer(sync2.ccu8_slice_ptr);
368 
369  // HB timing for resume of operation
370  // LLC frequency modification
371  llc_pwm.ccu8_slice_ptr->PRS = PR_START;//152;
372  llc_pwm.ccu8_slice_ptr->CR1S = DUTY_START;//28;
373  llc_pwm.ccu8_slice_ptr->CR2S = ADC_START;//14;
374 
375  // LLC dead-time modification
376  llc_pwm.hrc_slice_ptr->SDCR = DEADTIME_R_START;//10;
377  llc_pwm.hrc_slice_ptr->SDCF = DEADTIME_F_START;//10;
378 
379  // SR timing modification for synch single-shot SR
380  sync1.ccu8_slice_ptr->CR1S = 0;
381  sync1.ccu8_slice_ptr->CR2S = MIN_DEADTIME-1;
382  sync1.ccu8_slice_ptr->PRS = MIN_DEADTIME;
383 
384  sync2.ccu8_slice_ptr->CR1S = 0;
385  sync2.ccu8_slice_ptr->CR2S = MIN_DEADTIME-1;
386  sync2.ccu8_slice_ptr->PRS = MIN_DEADTIME;
387 
388  // Shadow transfer LLC and SR
389  CCU80->GCSS |= SHADOW_TRANSFER_LLC_SR;
390 
391  // Start SR timers
392  XMC_CCU8_SLICE_StartTimer(sync1.ccu8_slice_ptr);
393  XMC_CCU8_SLICE_StartTimer(sync2.ccu8_slice_ptr);
394 
395 
396  /* Start state machine */
397  status_reg = ST_STB;
398 
399  // Restart counter for sequence definition ISR
401  /* Enable interrupt for proper starting sequence timing */
402  NVIC_ClearPendingIRQ(start_sequence.node);
403  NVIC_EnableIRQ(start_sequence.node);
404 
406  // Differentiate start-up than resume operation in burst mode.
407  debug_burst = 0;
408  burst = 0;
409  }
410 
411  }
412  /* State machine for input voltage stop process */
413  else if ((VDC_bus[0] < VDC_BUS_STOP) && (stop_reception_n != reception_n))// || fault_reg)
414  {
415  // The data must be consistent n times in a row
416  if(stop_ok < 0x7F)
417  {
418  stop_ok++;
419 
420  if (stop_ok > 0)
421  {
422  // Data considered consistent
423  stop_ok = 0xFF;
424 
425  // Clear the faults to allow restarting
426  fault_reg = F_INIT;
427 
428  // Prepare for start-voltage detection
429  start_ok = 0;
430  }
431 
432  // Set the number of reception
434  }
435  // If the data is consistent, turn off the converter
436  else
437  {
438  // Clear pendant shadow transfer (non-synchronous ISR)
439  CCU80->GCSC |= (SHADOW_TRANSFER_LLC_SR_CLEAR|CCU8_GCSC_S3SC_Msk);
440 
441  /* Turn off the converter */
442  // Passive level of PWM outputs (synchro trap)
443  CCU80_CC83->CR1S = 0;
444  CCU80->GCSS |= CCU8_GCSS_S3SE_Msk;
445  // SR off
446  sync_OFF();
447  // Oring is off
448  ORING_OFF();
449  oring_status = false;
450 
451  // Main LED off
452  LED_OFF_DCNOTOK();
453 
454  // Frequency ramp counter
455  linear_fsw_count = 0;
456 
457  // Time to next restart (ms)
458  time_INIT = 1;
459 
460  /* Disable resonant current comparison interrupt for SC detection, to allow converter restarting */
461  NVIC_ClearPendingIRQ(HRPWM_3_IRQn);
462  NVIC_DisableIRQ(HRPWM_3_IRQn);
463 
464  // Allow starting sequence
466  }
467  }
468 
469  /*------------------------------*/
470  /* STATE MACHINE */
471  /*------------------------------*/
472 
473 // /* Debug LED2 */
474 // XMC_GPIO_ToggleOutput(P0_6);
475 
476  // If the input voltage is in range after certain time, starts driver bootstrap charge
477  if (!time_BOP && (status_reg == ST_STB) && (VDC_bus[0] > VDC_BUS_START_HYST))
478  {
479  // Converter off
480  PWM_OFF();
481  sync_OFF();
482 
483  // Oring off
484  ORING_OFF();
485  oring_status = false;
486 
487  // Main LED off
488  LED_OFF_DCNOTOK();
489 
490  // Charge driver bootstrap by turning on LS
491  XMC_GPIO_SetOutputLow(P0_7);
492  XMC_GPIO_SetOutputHigh(P0_8);
493 
494  // Next state is charging sequence of HS driver
496 
497  // Bootstrap capacitor charging time
498  charge_HS_drive = 14;//14 for 10us ISR;//7 for 20us ISR;//CHARGE_HS_TOTAL;
499  }
500  // Driver bootstrap charge sequence: 140us
501  else if (status_reg == ST_CHARGE)
502  {
503  // HS driver charge counting (waiting to initiate PWM operation)
505  {
506  charge_HS_drive--;
507  }
508  // ADC measurement the cycle before starting the converter to calculate error
509  else if (charge_HS_drive > CHARGE_HS_LOW)
510  {
511  XMC_VADC_GROUP_QueueTriggerConversion(VADC_G0);
512  charge_HS_drive--;
513 
514  // Disable synchro trap to start
515  CCU80_CC83->CR1S = 0xFFFF;//100;//
516  CCU80->GCSS |= CCU8_GCSS_S3SE_Msk;
517  }
518 
519  /* Start PWM sequence: asymmetric PWM based on resonant current */
520  else
521  {
522  // Both signals to zero before starting the PWM
523  XMC_GPIO_SetOutputLow(P0_7);
524  XMC_GPIO_SetOutputLow(P0_8);
525 
526  // Oring is off during comparison sequence
527  ORING_OFF();
528  oring_status = false;
529  // SYNC.RECT. is off during comparison sequence
530  sync_OFF();
531 
532  /* Start the timer */
533  llc_pwm.ccu8_slice_ptr->TCSET |= CCU8_CC8_TCSET_TRBS_Msk;
534 
535  // Change to PWM operation
536  PWM_ON();
537 
538  /* Starting frequency is the last captured frequency */
539  voltage_int_ctr.soft_start = START_FREQ;//*voltage_int_ctr.output_int_limit = START_FREQ;
540 
541  // At starting of the linear frequency ramp the voltage loop starts with maximum period
542  *voltage_int_ctr.output_int_limit = LLC_PERIOD_MAX;
543  /* Save controller state for next calculation */
544  voltage_int_ctr.state[0] = voltage_int_ctr.output_int;
545 
546  /* No ramp reference, the reference for the voltage loop is always the target voltage */
547  voltage_int_ctr.int_ref = voltage_int_ctr.int_ref_steady;//*LLC.qVout;
548 
549  // Test resume of operation --> NO ADC MEASUREMENT SINCE TURN-OFF OF THE PWM (timers is stopped)
550  *LLC.qVout = (*ADCResult_12VOr & 0xFFF) << ADC_TO_Q15;//0;
551 
552  /* Next state: linear variation of the switching frequency to the minimum allowed frequency */
554 
555  // Period ramp in soft-start
556  voltage_int_ctr.ramp_loop = ramp;//false;
557  }
558  }
559  /* Sync. Rect and Oring management */
560  else if ((status_reg == ST_SOFT_START) && (LLC.q12V_oring > VOUT_SR_ON))
561  {
562  // Turn on Sync. Rect when the output voltage reaches a certain limit
563  sync_ON();
564 
565  // Change state
567  }
568  else if ((status_reg == ST_SOFT_START_SR) && (LLC.q12V_oring > VOUT_ORING_ON))
569  {
570  if (!oring_status)
571  {
572  // Turn on Oring switch with proper output voltage
573  ORING_ON();
574  oring_status = true;
575  }
576  else
577  {
578  // After closing oring, voltage after oring is controlled
579  LLC.qVout = (uint16_t *)(&LLC.q12V);
580 
581  // Go to running state
583  }
584  }
585 // else if (status_reg == ST_RUNNING) // NO decision in RUNNING state. Used only for debugging purposes
586 // {
587 // if (VDC_bus[0] < VDC_BUS_STOP)
588 // XMC_GPIO_SetOutputHigh(P0_6);
589 // }
590  // Burst resume operation
591  else if(status_reg == ST_BURST)
592  {
593  // Monitoring output voltage to check when normal operation must be resumed
594  if (*LLC.qVout <= VOUT_TARGET)
595  {
596  /* Prepare to resume the loop operation */
597  // Leave burst mode with maximum frequency operation
598  *voltage_int_ctr.output_int_limit = LLC_PERIOD_MIN;
599 
600  // Save period state for next calculation
601  voltage_int_ctr.state[0] = voltage_int_ctr.output_int;
602 
603  // Error calculated in voltage loop when resuming normal operation
604  voltage_int_ctr.first_iter = true;
605 
606  // Disable synchro trap to start PWM operation
607  CCU80_CC83->CR1S = 0xFFFF;//100;//
608  CCU80->GCSS |= CCU8_GCSS_S3SE_Msk;
609 
610  // Resume normal operation
612  }
613  }
614 
615 
616  if (status_reg & ST_LATCH) // Latch condition
617  {
618  /* Turn off the converter */
619  PWM_OFF();
620  sync_OFF();
621  ORING_OFF();
622  oring_status = false;
623 
624  // Main LED off
625  LED_OFF_DCNOTOK();
626  }
627 
628 }
629 
630 /*---------------------------------------------------------------------------*/
650 #ifndef DOXYGEN_SKIP
651 __attribute__((section(".ram_code")))
652 #endif
653 __STATIC_INLINE void voltage_loop()
654 {
655 
657  {
658  // Voltage error: used in start-up and voltage loop
659  voltage_int_ctr.error[0] = voltage_int_ctr.int_ref - *LLC.qVout;
660 
661  /*-----------------------------------------------------------------------*/
662  /* Linear frequency modification at start up */
663  /*-----------------------------------------------------------------------*/
664 
665  // Soft-start by ramping linearly the period until the loop takes over
666  if ((voltage_int_ctr.soft_start < LLC_PERIOD_MAX) && !voltage_int_ctr.ramp_loop)//(*voltage_int_ctr.output_int_limit < LLC_PERIOD_MIN)
667  {
668  // 1.5ms ramp @10us
669  if (linear_fsw_count < 24)
670  {
671  voltage_int_ctr.soft_start += linear_fsw_step[0];
673  }
674  else if(linear_fsw_count < 63)
675  {
676  voltage_int_ctr.soft_start += linear_fsw_step[1];
678  }
679  else if(linear_fsw_count < 77)
680  {
681  voltage_int_ctr.soft_start += linear_fsw_step[2];
683  }
684  else if(linear_fsw_count < 90)
685  {
686  voltage_int_ctr.soft_start += linear_fsw_step[3];
688  }
689  else if(linear_fsw_count < 99)
690  {
691  voltage_int_ctr.soft_start += linear_fsw_step[4];
693  }
694  else if(linear_fsw_count < 104)
695  {
696  voltage_int_ctr.soft_start += linear_fsw_step[5];
698  }
699  else if(linear_fsw_count < 109)
700  {
701  voltage_int_ctr.soft_start += linear_fsw_step[6];
703  }
704  else if(linear_fsw_count < 113)
705  {
706  voltage_int_ctr.soft_start += linear_fsw_step[7];
708  }
709  else if(linear_fsw_count < 116)
710  {
711  voltage_int_ctr.soft_start += linear_fsw_step[8];
713  }
714  else if(linear_fsw_count < 120)
715  {
716  voltage_int_ctr.soft_start += linear_fsw_step[9];
718  }
719  else if(linear_fsw_count < 122)
720  {
721  voltage_int_ctr.soft_start += linear_fsw_step[10];
723  }
724  else if(linear_fsw_count < 125)
725  {
726  voltage_int_ctr.soft_start += linear_fsw_step[11];
728  }
729  else if(linear_fsw_count < 128)
730  {
731  voltage_int_ctr.soft_start += linear_fsw_step[12];
733  }
734  else if(linear_fsw_count < 131)
735  {
736  voltage_int_ctr.soft_start += linear_fsw_step[13];
738  }
739  else if(linear_fsw_count < 134)
740  {
741  voltage_int_ctr.soft_start += linear_fsw_step[14];
743  }
744  else if(linear_fsw_count < 137)
745  {
746  voltage_int_ctr.soft_start += linear_fsw_step[15];
748  }
749  else if(linear_fsw_count < 140)
750  {
751  voltage_int_ctr.soft_start += linear_fsw_step[16];
753  }
754  else if(linear_fsw_count < 151)
755  {
756  voltage_int_ctr.soft_start += linear_fsw_step[linear_fsw_count-123];
758  }
759 
760 
761  // Change to voltage loop if the error is lower than 100mV or if the loop period is lower than the ramp
762  if (voltage_int_ctr.error[0] < 150)
763  {
764  // Start frequency of the loop is the last frequency of the ramp
765  *voltage_int_ctr.output_int_limit = voltage_int_ctr.soft_start;
766 
767  // The loop takes over, no period ramp
768  voltage_int_ctr.output = voltage_int_ctr.output_int_limit;
769  //voltage_int_ctr.output = (int16_t *)(&voltage_int_ctr.qOut_peak);
770 
771  // Ramp of the period ends because the loop takes over
772  voltage_int_ctr.ramp_loop = loop;//true;
773 
774  // Smooth transition when the loop takes over
775  voltage_int_ctr.first_iter = true;
776 
777  // Save period state for next calculation
778  voltage_int_ctr.state[0] = voltage_int_ctr.output_int;
779 
780  // Main LED on --> 12V OK
781  LED_ON_DCOK();
782  }
783  else if (*voltage_int_ctr.output_int_limit < voltage_int_ctr.soft_start)
784  {
785 
786  // The loop takes over, no period ramp
787  voltage_int_ctr.output = voltage_int_ctr.output_int_limit;
788  //voltage_int_ctr.output = (int16_t *)(&voltage_int_ctr.qOut_peak);
789 
790  // Ramp of the period ends because the loop takes over
791  voltage_int_ctr.ramp_loop = loop;//true;
792 
793  // Smooth transition when the loop takes over
794  voltage_int_ctr.first_iter = true;
795 
796  // Save period state for next calculation
797  voltage_int_ctr.state[0] = voltage_int_ctr.output_int;
798 
799  // Main LED on --> 12V OK
800  LED_ON_DCOK();
801  }
802  }
803 
804  /*-----------------------------------------------------------------------*/
805  /* Voltage loop */
806  /*-----------------------------------------------------------------------*/
807  /* Error management in compensator first iteration */
808  if (voltage_int_ctr.first_iter)
809  {
810  // Fill the error array for smooth transition at first iteration after linear period variation or Burst
811  voltage_int_ctr.error[2] = voltage_int_ctr.error[0];
812  voltage_int_ctr.error[1] = voltage_int_ctr.error[0];
813 
814  // No first iteration any more
815  voltage_int_ctr.first_iter = false;
816  }
817 
818 
819  /*
820  * Voltage compensator PID
821  * T[k] = T[k-1] + a0 * e[k] + a1 * e[k-1] + a2 * e[k-2]
822  * coefficients => Qx
823  * input => Q0
824  */
825  voltage_int_ctr.int_calc = (int32_t)voltage_int_ctr.a[0] * (int32_t)voltage_int_ctr.error[0];
826  voltage_int_ctr.int_calc += (int32_t)voltage_int_ctr.a[1] * (int32_t)voltage_int_ctr.error[1];
827  voltage_int_ctr.int_calc += (int32_t)voltage_int_ctr.a[2] * (int32_t)voltage_int_ctr.error[2];
828  voltage_int_ctr.int_calc = voltage_int_ctr.int_calc << COMP_SHIFT;
829  voltage_int_ctr.output_int = voltage_int_ctr.int_calc + voltage_int_ctr.state[0];
830 
831  /*-----------------------------------------------------------------------*/
832  /* Frequency limitation and burst flag */
833  /*-----------------------------------------------------------------------*/
834  // Frequency limitation (control loop saturation) and BURST detection
835  if ((int16_t)(*voltage_int_ctr.output_int_limit) > LLC_PERIOD_MAX)
836  {
837  voltage_int_ctr.output_int = LLC_PERIOD_MAX << 16;
838 
839  // No trigger of burst mode operation in state machine
841  }
842  else if ((int16_t)(*voltage_int_ctr.output_int_limit) < LLC_PERIOD_MIN)
843  {
844  voltage_int_ctr.output_int = LLC_PERIOD_MIN << 16;
845 
846  // Trigger burst mode operation in state machine
848  }
849  else
850  {
851  // No trigger of burst mode operation in state machine
853  }
854 
855  /*-----------------------------------------------------------------------*/
856  /* Dead time calculation */
857  /*-----------------------------------------------------------------------*/
858 
859  // Dead time calculation: 160ns if fsw<200kHz, linear variation to 350ns@fsw=400kHz
860  if (!voltage_int_ctr.ramp_loop)
861  LLC.deadtime = MIN_DEADTIME;
862  else
863  {
864  if (*voltage_int_ctr.output_int_limit >= 400)
865  LLC.deadtime = MIN_DEADTIME;
866  else
867  LLC.deadtime = DEADTIME_0Hz-(((int32_t)DEADTIME_SLOPE * (int32_t)*voltage_int_ctr.output_int_limit) >> Q15_PRODUCT_SHIFT);
868  }
869 
870  /*-----------------------------------------------------------------------*/
871  /* LLC-HB and SR timing */
872  /*-----------------------------------------------------------------------*/
873 
874  // LLC frequency modification
875  llc_pwm.ccu8_slice_ptr->PRS = *voltage_int_ctr.output;
876  llc_pwm.ccu8_slice_ptr->CR1S = (*voltage_int_ctr.output >> 1) + DRIVER_DELAY_COMP; // DRIVER_DELAY_COMP to compensate driver asymmetry
877  llc_pwm.ccu8_slice_ptr->CR2S = (*voltage_int_ctr.output >> 2);
878 
879  // LLC dead-time modification, with driver asymmetry compensation
880  llc_pwm.hrc_slice_ptr->SDCR = LLC.deadtime + DEADTIME_R_COMP;
881  llc_pwm.hrc_slice_ptr->SDCF = LLC.deadtime + DEADTIME_F_COMP;
882 
883 
884  // SR timing modification for synch single-shot SR (sync 1 associated to rising edge; with new driving scheme)
885  if (status_reg & ST_RUNNING)
886  sync1.ccu8_slice_ptr->CR1S = DELAY_SR1_ON + LLC.deadtime; // On time does not change with frequency
887  else
888  sync1.ccu8_slice_ptr->CR1S = DELAY_SR1_ON + 8 + LLC.deadtime; // Different delay during start-up
889  if (*voltage_int_ctr.output > BELOW_FREQ)
890  LLC.SR1_off_timing = DELAY_SR1_OFF_BELOW_1 - LLC.deadtime; // Off Delay under 90kHz
891  else if (*voltage_int_ctr.output > RES_FREQ)
892  LLC.SR1_off_timing = DELAY_SR1_OFF_BELOW_2 - LLC.deadtime; // Off Delay under resonant frequency (149kHz)
893  else
894  LLC.SR1_off_timing = (*voltage_int_ctr.output >> 1) + DELAY_SR1_OFF_ABOVE - LLC.deadtime; // Off Delay above resonant frequency (149kHz)
895  sync1.ccu8_slice_ptr->PRS = LLC.SR1_off_timing;
896 
897  // SR timing modification for synch single-shot SR (sync 2 associated to falling edge; with new driving scheme)
898  if (status_reg & ST_RUNNING)
899  sync2.ccu8_slice_ptr->CR1S = DELAY_SR2_ON + LLC.deadtime; // On time does not change with frequency
900  else
901  sync2.ccu8_slice_ptr->CR1S = DELAY_SR2_ON + 8 + LLC.deadtime; // Different delay during start-up
902 // sync2.ccu8_slice_ptr->CR1S = DELAY_SR2_ON + LLC.deadtime; //17+LLC.deadtime // On time does not change
903  if (*voltage_int_ctr.output > BELOW_FREQ)
904  LLC.SR2_off_timing = DELAY_SR2_OFF_BELOW_1 - LLC.deadtime; // Off Delay under 90kHz
905  else if (*voltage_int_ctr.output > RES_FREQ)
906  LLC.SR2_off_timing = DELAY_SR2_OFF_BELOW_2 - LLC.deadtime; // Off Delay under resonant frequency (149kHz)
907  else
908  LLC.SR2_off_timing = (*voltage_int_ctr.output >> 1) + DELAY_SR2_OFF_ABOVE - LLC.deadtime; // Off Delay above resonant frequency (149kHz)
909  sync2.ccu8_slice_ptr->PRS = LLC.SR2_off_timing;
910 
911  // Timer replica period
912  timer_replica.ccu8_slice_ptr->PRS = *voltage_int_ctr.output;
913 
914  // Shadow transfer LLC and SR
915  CCU80->GCSS |= (SHADOW_TRANSFER_LLC_SR|CCU8_GCSS_S3SE_Msk);
916 
917  /*-----------------------------------------------------------------------*/
918  /* Save values for next iteration */
919  /*-----------------------------------------------------------------------*/
920 
921  // Save loop period state for next calculation
922  voltage_int_ctr.state[0] = voltage_int_ctr.output_int;
923  // Save loop error state for next calculation
924  voltage_int_ctr.error[2] = voltage_int_ctr.error[1];
925  voltage_int_ctr.error[1] = voltage_int_ctr.error[0];
926  }
927  else if (!status_reg)
928  {
929  /* Initialization of control loops */
931  }
932 } // 2P2Z: 1.9us (15.02.16)
933  // PID Q14: 2.9us (17.05.16)
934 
935 /*---------------------------------------------------------------------------*/
942 #ifndef DOXYGEN_SKIP
943 __attribute__((section(".ram_code")))
944 #endif
945 __STATIC_INLINE void burst_mode()
946 {
947  // Burst mode operation triggered by maximum frequency and output voltage out of range
948  if ((status_reg & ST_RUNNING) && (burst_status & BURST_ON) && (*LLC.qVout > VOUT_BURST))//(LLC.q12V >= VOUT_BURST))
949  {
950  // Count the times that burst mode conditions happen in a row
951  burst++;
952  if(burst > 2)
953  {
954  /* Burst mode keeping SR operating at maximum frequency to keep C-HB until operation is resumed */
955 
956  /* Turn off LLC PWM outputs by synchronous trap */
957  CCU80_CC83->CR1S = 0;
958  CCU80->GCSS |= CCU8_GCSS_S3SE_Msk;
959 
960  // Maximum frequency operation in burst mode
961  *voltage_int_ctr.output_int_limit = LLC_PERIOD_MIN;
962 
963  // 350ns dead time at 300kHz
964  LLC.deadtime = DEADTIME_0Hz-(((int32_t)DEADTIME_SLOPE * (int32_t)*voltage_int_ctr.output_int_limit) >> Q15_PRODUCT_SHIFT);
965 
966  // LLC frequency modification
967  llc_pwm.ccu8_slice_ptr->PRS = *voltage_int_ctr.output_int_limit;
968  llc_pwm.ccu8_slice_ptr->CR1S = (*voltage_int_ctr.output_int_limit >> 1) + DRIVER_DELAY_COMP;
969  llc_pwm.ccu8_slice_ptr->CR2S = (*voltage_int_ctr.output_int_limit >> 2);
970 
971  // LLC dead-time modification
972  llc_pwm.hrc_slice_ptr->SDCR = LLC.deadtime + DEADTIME_R_COMP;
973  llc_pwm.hrc_slice_ptr->SDCF = LLC.deadtime + DEADTIME_F_COMP;
974 
975  // SR timing modification
976  sync1.ccu8_slice_ptr->CR1S = DELAY_SR1_ON_BURST+LLC.deadtime; // On time delay does not change
977  sync1.ccu8_slice_ptr->PRS = (*voltage_int_ctr.output_int_limit >> 1) + DELAY_SR1_OFF_BURST - LLC.deadtime; //Above resonant operation at burst mode
978 
979  sync2.ccu8_slice_ptr->CR1S = DELAY_SR2_ON_BURST+LLC.deadtime; // On time delay does not change
980  sync2.ccu8_slice_ptr->PRS = (*voltage_int_ctr.output_int_limit >> 1) + DELAY_SR2_OFF_BURST - LLC.deadtime; //Above resonant operation at burst mode
981 
982  // Timer replica period
983  timer_replica.ccu8_slice_ptr->PRS = *voltage_int_ctr.output_int_limit;
984 
985  // Shadow transfer LLC and SR
986  CCU80->GCSS |= (SHADOW_TRANSFER_LLC_SR|CCU8_GCSS_S3SE_Msk);
987 
988  // Save period state for next calculation
989  voltage_int_ctr.state[0] = voltage_int_ctr.output_int;
990 
991  // Go to burst state to monitor output voltage
993  }
994  }
995  else
996  burst = 0;
997 }
998 
999 /*---------------------------------------------------------------------------*/
1006 #ifndef DOXYGEN_SKIP
1007 __attribute__((section(".ram_code")))
1008 #endif
1009 __STATIC_INLINE void uart_send()
1010 {
1011  if((!time_send_UART) && (status_reg & ST_RUNNING))
1012  {
1013  if (XMC_USIC_CH_TXFIFO_GetLevel(UART_XMC13.channel) > 4)
1014  {
1015  counter_fifo = i_send; // Debug
1016  }
1017  else
1018  {
1019  /* Send info (current and fault) to the primary side controller */
1020  UART_XMC13.channel->IN[0U] = (0x8000 | 0x1000 | fault_reg);//0xFF0F;
1021  UART_XMC13.channel->IN[0U] = (((uint16_t)(LLC.qIout) << 3));// | fault_reg);
1022 
1023  // Set timer (500us) to wait for next information transmission
1024  time_send_UART = 6;
1025  }
1026  }
1027 }
1028 
1029 
1030 /*---------------------------------------------------------------------------*/
1046 //void control_loop_ISR(void)
1047 __attribute__((section(".ram_code")))
1049 {
1050 // /* Debug LED1 */
1051 // XMC_GPIO_SetOutputHigh(P1_1);
1052 
1053  /* Time counting */
1055 
1056  /* counter for current monitoring change */
1057  time_IMON_100kHz++;
1058 
1059  // Output voltage measurement, synchronized channels
1060  LLC.q12V_oring = (*ADCResult_12VOr & 0xFFF) << ADC_TO_Q15;
1061  LLC.q12V = (*ADCResult_12V & 0xFFF) << ADC_TO_Q15;
1062 
1063  // Over-voltage protection --> latch the converter if the output voltage reaches 14V.
1065 
1066  // Check output current and OCP
1067  LLC.qIout = (*ADCResult_iOut & 0xFFF);
1068 
1069  /* Over-current protection --> latch the converter according to output current monitoring */
1071 
1072  // Receive data from XMC13
1073  uart_receive();
1074 
1075  // State machine
1076  state_control();
1077 
1078  // Control loop
1079  voltage_loop();
1080 
1081  // Burst mode detection
1082  burst_mode();
1083 
1084  // Send information to the secondary side
1085  uart_send();
1086 
1087 // /* Debug LED1 */
1088 // XMC_GPIO_SetOutputLow(P1_1);
1089 } // 4.7us/20us (@50kHz ISR); PID Q15 (11.05.16)
1090  // 6us (running)/8.8us (soft-start) (@100kHz ISR); PID Q14 (30.08.16)
1091  // 8us (running)/9.8us (soft-start) (@100kHz ISR); PID + peaking (31.08.16)
1092 
1093 /*---------------------------------------------------------------------------*/
1100 //void control_loop_ISR(void)
1101 __attribute__((section(".ram_code")))
1103 {
1104  if (!start_sequence_count)
1105  {
1107 
1108  // Second cycle asymmetric operation
1109  llc_pwm.ccu8_slice_ptr->PRS = PR_START_SEQ;
1110  llc_pwm.ccu8_slice_ptr->CR1S = 28;
1111  llc_pwm.ccu8_slice_ptr->CR2S = ADC_START;
1112  llc_pwm.ccu8_module_ptr->GCSS |= llc_pwm.shadow_txfr_msk;
1113  }
1114  else if (start_sequence_count < 2)
1115  {
1117 
1118  // Second cycle asymmetric operation
1119  llc_pwm.ccu8_slice_ptr->PRS = PR_START_SEQ;
1120  llc_pwm.ccu8_slice_ptr->CR1S = 40;
1121  llc_pwm.ccu8_slice_ptr->CR2S = ADC_START;
1122  llc_pwm.ccu8_module_ptr->GCSS |= llc_pwm.shadow_txfr_msk;
1123  }
1124  else
1125  {
1126  // From next cycle is 50% duty operation
1127  llc_pwm.ccu8_slice_ptr->PRS = START_FREQ;
1128  llc_pwm.ccu8_slice_ptr->CR1S = (START_FREQ >> 1) + DRIVER_DELAY_COMP;
1129  llc_pwm.ccu8_slice_ptr->CR2S = START_FREQ >> 2;
1130  llc_pwm.ccu8_module_ptr->GCSS |= llc_pwm.shadow_txfr_msk;
1131 
1132  /* Disable interrupt after necessary changes */
1133  NVIC_DisableIRQ(start_sequence.node);
1134 
1135  /* Enable resonant current comparison interrupt for SC detection */
1136  NVIC_ClearPendingIRQ(HRPWM_3_IRQn);
1137  NVIC_EnableIRQ(HRPWM_3_IRQn);
1138 
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  // State with no decision. Controller reset necessary to leave OCP state.
1164  status_reg = ST_OCP;
1165  fault_reg = F_SC;
1166 }
1167