Digital Power Starter Kit 3 Firmware
dsPIC33C Buck Converter Voltage Mode Control Example
dev_buck_special_functions.c
1 /*
2  * File: dev_buck_special_functions.c
3  * Author: M91406
4  *
5  * Created on December 3, 2020, 10:14 AM
6  */
7 
8 #include <xc.h> // include processor files - each processor file is guarded.
9 #include <stdint.h> // include standard integer data types
10 #include <stdbool.h> // include standard boolean data types
11 #include <stddef.h> // include standard definition data types
12 
13 #include "xc16_pral.h" // include peripheral register abstraction layer drivers
14 #include "dev_buck_typedef.h" // include buck converter type definitions
15 #include "dev_buck_special_functions.h" // include buck converter special function defiition
16 
17 
22 struct CS_CALIBRATION_s
23 {
24  bool start;
25  bool stop;
26  volatile uint16_t counter;
27  volatile uint32_t buffer;
28 };
29 typedef struct CS_CALIBRATION_s CS_CALIBRATION_t;
30 
31 
45 volatile struct CS_CALIBRATION_s calib_cs[BUCK_MPHASE_COUNT];
46 
47 
52 // Current Sense
53 #define CS_CALIB_STEPS 8
54 
55 
56 // Private function prototypes of sub-state function calls
57 
58 volatile uint16_t CurrentSenseOffsetCalibration(volatile struct BUCK_CONVERTER_s *buckInstance);
59 
60 
88 volatile uint16_t (*BuckConverterSpecialFunctions[])
89  (volatile struct BUCK_CONVERTER_s *buckInstance) =
90 {
92 };
93 
94 
95 
134  volatile struct BUCK_CONVERTER_s * buckInstance,
135  volatile enum BUCK_SPECIAL_FUNCTIONS_e specialFunction
136  )
137 {
138  volatile uint16_t retval=0;
139 
140  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
141  /* NULL POINTER PROTECTION */
142  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
143  // If no buck instance has been declared, leave here
144  if(buckInstance == NULL)
145  return(0);
146 
147  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
148  /* CALL SPECIAL FUNCTION */
149  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
150  retval = BuckConverterSpecialFunctions[specialFunction](buckInstance);
151 
152  return(retval);
153 
154 }
155 
156 /* *****************************************************************************
157  * PRIVATE FUNCTIONS
158  * ****************************************************************************/
159 
160 
178 volatile uint16_t CurrentSenseOffsetCalibration(volatile struct BUCK_CONVERTER_s *buckInstance)
179 {
180  volatile uint16_t _i=0;
181  volatile bool _complete=true;
182  volatile enum BUCK_OPSTATE_RETURNS_e retval = BUCK_OPSRET_ERROR;
183 
184  // if current sense calibration is disabled, return COMPLETE and leave
185  if(!buckInstance->status.bits.cs_calib_enable)
186  {
187  return((uint16_t)BUCK_OPSRET_COMPLETE); // Set return value to COMPLETE and leave function
188  }
189 
190  // Protect against floored Current Calibration Procedure
191  if ((!buckInstance->status.bits.adc_active) ||
192  (!buckInstance->status.bits.pwm_active)
193  )
194  { return((uint16_t)BUCK_OPSRET_REPEAT); } // Return REPEAT
195 
196 
197  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
198  // Perform Calibration Step
199 
200  for(_i = 0; _i<buckInstance->set_values.no_of_phases; _i++)
201  {
202  // Reset Calibration Data Buffers if Calibration is rerun
203  if ((calib_cs[_i].start && calib_cs[_i].stop))
204  {
205  // Clear calibration data buffer
206  calib_cs[_i].buffer = 0; // Clear buffer
207  calib_cs[_i].counter = 0; // Clear counter
208  calib_cs[_i].start = false; // Clear START flag bit
209  calib_cs[_i].stop = false; // Clear STOP flag bit
210  }
211 
212  // Collect data samples
213  if (calib_cs[_i].counter++ < CS_CALIB_STEPS)
214  {
215  calib_cs[_i].start = true; // Clear START flag bit
216  calib_cs[_i].stop = false; // Clear STOP flag bit
217  calib_cs[_i].buffer += buckInstance->data.i_sns[_i]; // Read ADC offset value
218  }
219  // Collection of data samples is complete
220  else
221  {
222  calib_cs[_i].buffer >>= 3; // Divide accumulated ADC samples (calculate average)
223  calib_cs[_i].stop = true; // Set STOP flag bit
224 
225  // Write calibration data to buck converter data structure
226  buckInstance->i_loop[_i].feedback_offset = calib_cs[_i].buffer;
227 
228  }
229 
230  _complete &= calib_cs[_i].stop; // track STOP flag bits
231  }
232 
233  // Return REPEAT until calibration is complete
234  if (_complete)
235  retval = (uint16_t)BUCK_OPSRET_COMPLETE; // Return COMPLETE
236  else
237  retval = (uint16_t)BUCK_OPSRET_REPEAT; // Return REPEAT
238 
239  return(retval);
240 
241 }
242 
243 // end of file
BuckConverterSpecialFunctions
volatile uint16_t(* BuckConverterSpecialFunctions[])(volatile struct BUCK_CONVERTER_s *buckInstance)
Function pointer list of all special function sub-state functions.
Definition: dev_buck_special_functions.c:89
drv_BuckConverter_SpecialFunctionExecute
volatile uint16_t drv_BuckConverter_SpecialFunctionExecute(volatile struct BUCK_CONVERTER_s *buckInstance, volatile enum BUCK_SPECIAL_FUNCTIONS_e specialFunction)
This is the public function call access point to call dedicated special sub-functions.
Definition: dev_buck_special_functions.c:133
BUCK_CONVERTER_s::i_loop
volatile struct BUCK_LOOP_SETTINGS_s i_loop[BUCK_MPHASE_COUNT]
BUCK Current control loop objects.
Definition: dev_buck_typedef.h:513
BUCK_CONVERTER_s::status
volatile struct BUCK_CONVERTER_STATUS_s status
BUCK operation status bits.
Definition: dev_buck_typedef.h:502
CS_CALIB_STEPS
#define CS_CALIB_STEPS
Number of signal oversampling steps used to determine the calibration value.
Definition: dev_buck_special_functions.c:53
BUCK_CONVERTER_DATA_s::i_sns
volatile uint16_t i_sns[BUCK_MPHASE_COUNT]
BUCK output current.
Definition: dev_buck_typedef.h:298
BUCK_SPECIAL_FUNCTIONS_e
BUCK_SPECIAL_FUNCTIONS_e
Enumeration of special function sub-states.
Definition: dev_buck_special_functions.h:51
BUCK_MPHASE_COUNT
#define BUCK_MPHASE_COUNT
Declaration of the number of power train phases of the Buck Converter.
Definition: dev_buck_typedef.h:62
BUCK_CONVERTER_s::data
volatile struct BUCK_CONVERTER_DATA_s data
BUCK runtime data.
Definition: dev_buck_typedef.h:506
calib_cs
volatile struct CS_CALIBRATION_s calib_cs[BUCK_MPHASE_COUNT]
Array of current sense calibration data objects of type CS_CALIBRATION_t.
Definition: dev_buck_special_functions.c:45
BUCK_CONVERTER_STATUS_s::cs_calib_enable
volatile bool cs_calib_enable
Bit #8: Flag bit indicating that current sensors need to calibrated.
Definition: dev_buck_typedef.h:217
BUCK_CONVERTER_s::set_values
volatile struct BUCK_CONVERTER_SETTINGS_s set_values
Control field for global access to references.
Definition: dev_buck_typedef.h:505
BUCK_CONVERTER_STATUS_s::pwm_active
volatile bool pwm_active
Bit #2: indicating that PWM has been started and ADC triggers are generated.
Definition: dev_buck_typedef.h:210
BUCK_CONVERTER_STATUS_s::adc_active
volatile bool adc_active
Bit #1: indicating that ADC has been started and samples are taken.
Definition: dev_buck_typedef.h:209
BUCK_CONVERTER_s
BUCK control & monitoring data structure.
Definition: dev_buck_typedef.h:501
CurrentSenseOffsetCalibration
volatile uint16_t CurrentSenseOffsetCalibration(volatile struct BUCK_CONVERTER_s *buckInstance)
Performs an offset calibration of the current sense feedback signal(s)
Definition: dev_buck_special_functions.c:178
BUCK_CONVERTER_SETTINGS_s::no_of_phases
volatile uint16_t no_of_phases
number of converter phases
Definition: dev_buck_typedef.h:325
BUCK_LOOP_SETTINGS_s::feedback_offset
volatile uint16_t feedback_offset
Feedback offset value for calibration or bi-direction feedback signals.
Definition: dev_buck_typedef.h:348
BUCK_OPSTATE_RETURNS_e
BUCK_OPSTATE_RETURNS_e
Enumeration of state machine operating state return values.
Definition: dev_buck_typedef.h:135