XMC42_800WPSU_LLC
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
main.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  */
94 /***********************************************************************************************************************
95  * HEADER FILES
96  **********************************************************************************************************************/
97 
98 #include <DAVE.h> //Declarations from DAVE Code Generation (includes SFR declaration)
99 
100 
101 /* Include header files of the peripherals configured with LLD */
104 #include "lld_conf_code/adc.h"
105 
106 #include <interrupts_control.h>
107 #include <init_bckgnd.h>
108 
109 
110 
111 
112 /***********************************************************************************************************************
113  * MAIN FUNCTION
114  **********************************************************************************************************************/
115 /*---------------------------------------------------------------------------*/
136 int main(void)
137 {
138  DAVE_STATUS_t status;
139 
140  status = DAVE_Init(); /* Initialization of DAVE APPs */
141 
142  /* Start timer for LLC bridge */
143  XMC_CCU8_EnableClock(llc_pwm.ccu8_module_ptr,llc_pwm.slice_number);
144 // XMC_CCU8_SLICE_StartTimer(llc_pwm.ccu8_slice_ptr);
145 // XMC_CCU8_SLICE_StopTimer(llc_pwm.ccu8_slice_ptr);// Makes sense to start and stop the timer!!!!!!!!!!!!!!!!
146 
147  /* DEBUGGING PINS CONFIGURATION */
148  /* Pin P0.6: GPIO / CCU80.OUT30 (ALT3) --> timer replica (LLC-PWM trap) */
149  XMC_GPIO_SetMode(P0_6,XMC_GPIO_MODE_OUTPUT_PUSH_PULL_ALT3);//XMC_GPIO_MODE_OUTPUT_PUSH_PULL);//
150  XMC_GPIO_SetOutputLow(P0_6);
151  /* Pin P1.1: GPIO / CCU40.OUT20 (ALT3) --> debug LED 1 */
152  XMC_GPIO_SetMode(P1_1,XMC_GPIO_MODE_OUTPUT_PUSH_PULL);//XMC_GPIO_MODE_OUTPUT_PUSH_PULL_ALT3);//
153  XMC_GPIO_SetOutputLow(P1_1);
154  /* Pin P1.0: CCU40.OUT3 (ALT3)-fan PWM / PDOUT3(ALT4)-C2O ONLY FOR DEBUGGING!!!!!!!!!!!!!*/
155  XMC_GPIO_SetMode(P1_0,XMC_GPIO_MODE_OUTPUT_OPEN_DRAIN_ALT3);//XMC_GPIO_MODE_OUTPUT_PUSH_PULL);//XMC_GPIO_MODE_OUTPUT_PUSH_PULL_ALT3);
156 // XMC_GPIO_SetOutputLow(P1_0);
157 
158 
159  /* Main LED pin output changed from PWM output to GPIO */
160  /* Pin P1.3 GPIO / CCU40.OUT0 (ALT3) / ERU1.PDOUT0 (ALT4) --> CCU80.ST2 debvug */
161  XMC_GPIO_SetMode(P1_3,XMC_GPIO_MODE_OUTPUT_PUSH_PULL);
162  XMC_GPIO_SetOutputLow(P1_3);
163 
164 
165  /* DEBUGGING PINS CONFIGURATION */
166  /* Configure pin 2.14 as ST1 --> CCU80.CC82 output --> CCU80.OUT21 */
167  XMC_GPIO_CONFIG_t CCU80_CC82_output =
168  {
169  .mode = XMC_GPIO_MODE_OUTPUT_PUSH_PULL_ALT3,
170  .output_level = XMC_GPIO_OUTPUT_LEVEL_LOW,
171  .output_strength = XMC_GPIO_OUTPUT_STRENGTH_STRONG_SHARP_EDGE
172  };
173 // XMC_CCU8_SLICE_SetOutPath(llc_pwm.ccu8_slice_ptr,XMC_CCU8_OUT_PATH_OUT1_INV_ST1);
174  XMC_GPIO_Init(P2_14,&CCU80_CC82_output);
175 
176 
177  /* Turn off Sync. Rect. and PWM */
178  sync_OFF();
179  PWM_OFF();
180  /* ORING off */
181  ORING_OFF();
182 
183  // Main LED off
184  LED_OFF_DCNOTOK();
185 
186 
187  /* Initialization of the modules configured using LLD */
188  /* Comparator of resonant current initialization */
189  HRPWM0_CSG_Init();
190 
191  /* Modification to the main timer connections for proper ADC trigger */
193 
194  // Activate shadow transfer of timer replica on clear event
195  XMC_CCU8_SLICE_SetTimerCompareMatch(timer_replica.ccu8_slice_ptr,XMC_CCU8_SLICE_COMPARE_CHANNEL_1,0);//timer_replica.config_ptr->period_value);
196  XMC_CCU8_EnableShadowTransfer(timer_replica.ccu8_module_ptr,timer_replica.shadow_txfr_msk);
197  timer_replica.ccu8_slice_ptr->TC |= CCU8_CC8_TC_CLST_Msk;
198 
199  // SR load event change to CR2 load
200  XMC_CCU8_SLICE_LoadSelector(sync1.ccu8_slice_ptr,XMC_CCU8_SLICE_COMPARE_CHANNEL_2);
201  XMC_CCU8_SLICE_LoadSelector(sync2.ccu8_slice_ptr,XMC_CCU8_SLICE_COMPARE_CHANNEL_2);
202 
203  /* SR trap of OUTx1 to avoid SR overlap */
204  XMC_CCU8_SLICE_EnableTrap(sync1.ccu8_slice_ptr,XMC_CCU8_SLICE_OUTPUT_1);
205  XMC_CCU8_SLICE_EnableTrap(sync2.ccu8_slice_ptr,XMC_CCU8_SLICE_OUTPUT_1);
206 
207  /* Disable trap functionality of HRC source selector 1 */
208  llc_pwm.hrc_slice_ptr->TSEL &= ~(HRPWM0_HRC_TSEL_TSEL1_Msk|HRPWM0_HRC_TSEL_TS1E_Msk);
209 // XMC_HRPWM_HRC_ConfigSourceSelect0()
210 
211  /* Initialization of control loops */
213 
214  /* Configure ADC */
215  VADC_Init();
216 
217  // ERU input modification for comparator output debugging
218  XMC_ERU_ETL_SetInput(XMC_ERU1,3,XMC_ERU_ETL_INPUT_A0,XMC_ERU_ETL_INPUT_B3);
219 
220  /* Modify the number of bits to be sent by UART to the secondary controller.
221  * in the App this value is limited to 8 and the actual number of bits to be sent is 20.
222  * The maximum word available (FIFO word) is used. */
223  XMC_UART_CH_SetWordLength(UART_XMC13.channel,16);
224  XMC_UART_CH_SetFrameLength(UART_XMC13.channel,16);
225 
226  // Enable communication with primary controller
227  ENABLE_SEC_ON();
228 
229  /*--------------------------------------------*/
230  /* Timers start-up configuration */
231  /*--------------------------------------------*/
232  // LLC bridge
233  XMC_CCU8_SLICE_SetTimerPeriodMatch(llc_pwm.ccu8_slice_ptr,PR_START);//152); //160 --> 500kHz
234  XMC_CCU8_SLICE_SetTimerCompareMatch(llc_pwm.ccu8_slice_ptr,XMC_CCU8_SLICE_COMPARE_CHANNEL_1,DUTY_START);//28-9);//25% duty in first pulse
235  XMC_CCU8_SLICE_SetTimerCompareMatch(llc_pwm.ccu8_slice_ptr,XMC_CCU8_SLICE_COMPARE_CHANNEL_2,ADC_START);//14); // ADC trigger
236 
237  // Dead time of LLC pulses
238  XMC_HRPWM_HRC_SetDeadTimeRising(llc_pwm.hrc_slice_ptr,DEADTIME_R_START);//10+8); //fixed during starting
239  XMC_HRPWM_HRC_SetDeadTimeFalling(llc_pwm.hrc_slice_ptr,DEADTIME_F_START);//10); //fixed during starting
240 
241  // Shadow transfer for duty and frequency in CCU8 and deadtime of HRC
242  XMC_CCU8_EnableShadowTransfer(llc_pwm.ccu8_module_ptr,llc_pwm.shadow_txfr_msk);
243 
244  // SR1 timing
245  sync1.ccu8_slice_ptr->CR1S = 0;// On time = 0 before starting
246  sync1.ccu8_slice_ptr->CR2S = MIN_DEADTIME-1;// Off time = 1 before starting
247  sync1.ccu8_slice_ptr->PRS = MIN_DEADTIME;// Low period counting to always allow period match and therefore shadow register
248  XMC_CCU8_EnableShadowTransfer(sync1.ccu8_module_ptr,sync1.shadow_txfr_msk);
249 
250  // SR2 timing
251  sync2.ccu8_slice_ptr->CR1S = 0;// On time = 0 before starting
252  sync2.ccu8_slice_ptr->CR2S = MIN_DEADTIME-1;// On time = 1 before starting
253  sync2.ccu8_slice_ptr->PRS = MIN_DEADTIME;// Low period counting to always allow period match and therefore shadow register
254  XMC_CCU8_EnableShadowTransfer(sync2.ccu8_module_ptr,sync2.shadow_txfr_msk);
255 
256  // SR delay counters
257  sync1_stop_timer.ccu4_slice_ptr->PRS = 14;
258  sync1_stop_timer.ccu4_slice_ptr->CRS = 1;
259 // sync1_stop_timer.ccu4_module_ptr->GCSS |= sync1_stop_timer.shadow_txfr_msk;
260  XMC_CCU4_EnableShadowTransfer(sync1_stop_timer.ccu4_module_ptr,sync1_stop_timer.shadow_txfr_msk);
261  sync2_stop_timer.ccu4_slice_ptr->PRS = 14;
262  sync2_stop_timer.ccu4_slice_ptr->CRS = 1;
263 // sync2_stop_timer.ccu4_module_ptr->GCSS |= sync1_stop_timer.shadow_txfr_msk;
264  XMC_CCU4_EnableShadowTransfer(sync2_stop_timer.ccu4_module_ptr,sync2_stop_timer.shadow_txfr_msk);
265 
266  // Start timers for SR
267  XMC_CCU8_EnableClock(sync1.ccu8_module_ptr,sync1.slice_number);
268  XMC_CCU8_SLICE_StartTimer(sync1.ccu8_slice_ptr);
269  XMC_CCU8_EnableClock(sync2.ccu8_module_ptr,sync2.slice_number);
270  XMC_CCU8_SLICE_StartTimer(sync2.ccu8_slice_ptr);
271 
272  // Sync off, turn on when output voltage reaches 10V
273  sync_OFF();
274 
275  /* Start loop timer after all the configuration is done */
276  XMC_CCU4_EnableClock(loop_timer.ccu4_module_ptr,loop_timer.slice_number);
277  XMC_CCU4_SLICE_StartTimer(loop_timer.ccu4_slice_ptr);
278 
279 
280  if(status == DAVE_STATUS_FAILURE)
281  {
282  /* Placeholder for error handler code. The while loop below can be replaced with an user error handler. */
283  XMC_DEBUG("DAVE APPs initialization failed\n");
284 
285  while(1U)
286  {
287 
288  }
289  }
290 
291  /* Placeholder for user application code. The while loop below can be replaced with user application code. */
292  while(1U)
293  {
294  // Low priority tasks
295  time_counter();
297  I_MON_update();
298  }
299 }
300