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