XMC42_800WPSU_LLC
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adc.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 <XMC4200.h> //SFR declarations of the selected device
47 
48 #include <xmc_vadc.h>
49 #include "adc.h"
50 #include <./interrupts_control.h>
51 
52 
53 /**********************************************************************************************************************
54 * DATA STRUCTURES
55 **********************************************************************************************************************/
61 const XMC_VADC_GLOBAL_CONFIG_t g_global_config =
62 {
63  .disable_sleep_mode_control = true,
64  .module_disable = false,
65  .class0 =
66  {
67  .conversion_mode_standard = XMC_VADC_CONVMODE_12BIT,
68  .sample_time_std_conv = 3U,
69  .conversion_mode_emux = XMC_VADC_CONVMODE_12BIT,
70  .sampling_phase_emux_channel = 3U,
71  },
72  .class1 = {
73  .conversion_mode_standard = XMC_VADC_CONVMODE_12BIT,
74  .sample_time_std_conv = 3U,
75  .conversion_mode_emux = XMC_VADC_CONVMODE_12BIT,
76  .sampling_phase_emux_channel = 3U
77  },
78  .clock_config =
79  {
80  .analog_clock_divider = 0,
81  .msb_conversion_clock = 0,
82  .arbiter_clock_divider = 0
83  },
84 };
92 const XMC_VADC_GROUP_CONFIG_t g_group0_config =
93 {
94  .class0 = /* Input class register 0, Group 0 */
95  {
96  .conversion_mode_standard = XMC_VADC_CONVMODE_12BIT,
97  .sample_time_std_conv = 3U, /* Additional cycles at sample time = 3 --> sample time = 5/fADC */
98  }
99 };
100 
102 const XMC_VADC_GROUP_CONFIG_t g_group1_config =
103 {
104  .class0 = /* Input class register 0, Group 0 */
105  {
106  .conversion_mode_standard = XMC_VADC_CONVMODE_12BIT,
107  .sample_time_std_conv = 3U, /* Additional cycles at sample time = 3 --> sample time = 5/fADC */
108  }
109 };
117 const XMC_VADC_CHANNEL_CONFIG_t g0_channel6_config =
118 {
119  .input_class = XMC_VADC_CHANNEL_CONV_GROUP_CLASS0,
120  .result_reg_number = RES7_CHx,
121  .channel_priority = true,
122  .alias_channel = XMC_VADC_CHANNEL_ALIAS_DISABLED,
123  .sync_conversion = SYNC_REQUIRED, // Synchronized with G1-CH6 for parallel measurement of output voltages
124  .alternate_reference = XMC_VADC_CHANNEL_REF_ALT_CH0 // CH0 is used as alternative reference
125 };
126 
128 const XMC_VADC_CHANNEL_CONFIG_t g1_channel6_config =
129 {
130  .input_class = XMC_VADC_CHANNEL_CONV_GROUP_CLASS0,
131  .result_reg_number = RES7_CHx,
132  .channel_priority = true,
133  .alias_channel = XMC_VADC_CHANNEL_ALIAS_DISABLED,
134  .alternate_reference = XMC_VADC_CHANNEL_REF_ALT_CH0 // CH0 is used as alternative reference
135 };
136 
138 const XMC_VADC_CHANNEL_CONFIG_t g0_channel3_config =
139 {
140  .input_class = XMC_VADC_CHANNEL_CONV_GROUP_CLASS0,
141  .result_reg_number = RES3_CH3,
142  .channel_priority = true,
143  .alias_channel = XMC_VADC_CHANNEL_ALIAS_DISABLED,
144  .alternate_reference = XMC_VADC_CHANNEL_REF_ALT_CH0 // CH0 is used as alternative reference
145 };
146 
148 const XMC_VADC_CHANNEL_CONFIG_t g0_channel5_config =
149 {
150  .input_class = XMC_VADC_CHANNEL_CONV_GROUP_CLASS0,
151  .result_reg_number = RES15_CH5,
152  .channel_priority = true,
153  .alias_channel = XMC_VADC_CHANNEL_ALIAS_DISABLED,
154  .alternate_reference = XMC_VADC_CHANNEL_REF_ALT_CH0 // CH0 is used as alternative reference
155 };
163 const XMC_VADC_RESULT_CONFIG_t g_result3_handle = {
164  .post_processing_mode = XMC_VADC_DMM_REDUCTION_MODE,
165  .data_reduction_control = 0, /* Normal operation */
166  .part_of_fifo = 0, /* No FIFO */
167  .wait_for_read_mode = (bool) false, /* WFS */
168  .event_gen_enable = (bool) false /* result event */
169 };
170 
172 const XMC_VADC_RESULT_CONFIG_t g_result7_handle = {
173  .post_processing_mode = XMC_VADC_DMM_REDUCTION_MODE,
174  .data_reduction_control = 0, /* Normal operation */
175  .part_of_fifo = 0, /* No FIFO */
176  .wait_for_read_mode = (bool) false, /* WFS */
177  .event_gen_enable = (bool) false /* result event */
178 };
179 
181 const XMC_VADC_RESULT_CONFIG_t g_result15_handle = {
182  .post_processing_mode = XMC_VADC_DMM_REDUCTION_MODE,
183  .data_reduction_control = 0, /* Normal operation */
184  .part_of_fifo = 0, /* No FIFO */
185  .wait_for_read_mode = (bool) false, /* WFS */
186  .event_gen_enable = (bool) false /* result event */
187 };
195 const XMC_VADC_QUEUE_CONFIG_t g_queue_config =
196 {
197  .conv_start_mode = XMC_VADC_STARTMODE_CIR, /* wait for start mode */
198  .req_src_priority = XMC_VADC_GROUP_RS_PRIORITY_3, /* Highest priority for the queue in the arbitration round */
199  .src_specific_result_reg = INDIVIDUAL_RESULT_REGISTERS, /* Individual configured result registers used */
200  .trigger_signal = XMC_VADC_REQ_TR_J, /* CCU80.SR3 as trigger for the queue */ //= XMC_VADC_REQ_TR_C, /* CCU41.SR2 as trigger for the queue */
201  .trigger_edge = XMC_VADC_TRIGGER_EDGE_RISING,
202  .external_trigger = TRIGGER_ENABLE
203 };
204 
206 const XMC_VADC_QUEUE_ENTRY_t g_queue_entry1 =
207 {
208  .channel_num = CH6,
209  .refill_needed = true, /* Refill is needed */
210  .generate_interrupt = false, /* Interrupt generation is needed */
211  .external_trigger = true /* External trigger is required */
212 };
213 
215 const XMC_VADC_QUEUE_ENTRY_t g_queue_entry2 =
216 {
217  .channel_num = CH3,
218  .refill_needed = true, /* Refill is needed */
219  .generate_interrupt = false, /* NO Interrupt generation is needed */
220  .external_trigger = false /* External trigger is NOT required */
221 };
225 /**********************************************************************************************************************
226 * API IMPLEMENTATION
227 ***********************************************************************************************************************/
228 /*---------------------------------------------------------------------------*/
241 /* Function to initialize the ADC module */
242 void VADC_Init(void)
243 {
244  /* Remove reset from VADC */
245  XMC_SCU_CLOCK_UngatePeripheralClock(XMC_SCU_PERIPHERAL_CLOCK_VADC);
246 
247  /* Initialize the VADC global registers */
248  XMC_VADC_GLOBAL_Init(VADC, &g_global_config);
249 
250  /* Configure both conversion kernels */
251  XMC_VADC_GROUP_Init(VADC_G0, &g_group0_config);
252  XMC_VADC_GROUP_Init(VADC_G1, &g_group1_config);
253 
254  /* Synchronization of measurements G0CH6 (Voring) and G1CH6 (Vout) */
255  /* Define Master group */
256  XMC_VADC_GROUP_SetSyncMaster(VADC_G0);
257  /* Define Slave group */
258  XMC_VADC_GROUP_SetSyncSlave(VADC_G1, MASTER, SLAVE);
259 
260  /* Configuration of the channels to be measured */
261  XMC_VADC_GROUP_ChannelInit(VADC_G0, CH6, &g0_channel6_config);
262  XMC_VADC_GROUP_ChannelInit(VADC_G1, CH6, &g1_channel6_config);
263  XMC_VADC_GROUP_ChannelInit(VADC_G0, CH3, &g0_channel3_config);
264  XMC_VADC_GROUP_ChannelInit(VADC_G0, CH5, &g0_channel5_config);
265 
266  /* Result registers initialization */
267  XMC_VADC_GROUP_ResultInit(VADC_G0,RES3_CH3,&g_result3_handle);
268  XMC_VADC_GROUP_ResultInit(VADC_G0,RES7_CHx,&g_result7_handle);
269  XMC_VADC_GROUP_ResultInit(VADC_G1,RES7_CHx,&g_result7_handle);
270  XMC_VADC_GROUP_ResultInit(VADC_G0,RES15_CH5,&g_result15_handle);
271 
272  /* Initialization of the queue */
273  XMC_VADC_GROUP_QueueInit(VADC_G0, &g_queue_config);
274 
275  /* Fill the queue */
276  XMC_VADC_GROUP_QueueInsertChannel(VADC_G0,g_queue_entry1);
277  XMC_VADC_GROUP_QueueInsertChannel(VADC_G0,g_queue_entry2);
278 
279  /* Enable the analog converters */
280  XMC_VADC_GROUP_SetPowerMode(VADC_G0, XMC_VADC_GROUP_POWERMODE_NORMAL);
281  XMC_VADC_GROUP_SetPowerMode(VADC_G1, XMC_VADC_GROUP_POWERMODE_NORMAL);
282 
283  /* Perform calibration of the converter */
284  XMC_VADC_GLOBAL_StartupCalibration(VADC);
285 
286 
287 // // Pointers to ADC results of the Queue Source
288  ADCResult_12V = &VADC_G1->RES[RES7_CHx];
289  ADCResult_12VOr = &VADC_G0->RES[RES7_CHx];
290  ADCResult_iOut = &VADC_G0->RES[RES3_CH3];
291 
292  // Pointers to ADC results of the scan Source
293  ADCResult_iR = &VADC_G0->RES[RES15_CH5];
294 
295 }