Digital Power Starter Kit 3 Firmware  DM330017-3, Rev.3.0
dsPIC33C Buck Converter Peak Current Mode Control Example
app_power_control.c
1 /*
2  * File: app_power_control.c
3  * Author: M91406
4  *
5  * Created on March 12, 2020, 11:55 AM
6  */
7 
8 #include <stdbool.h>
9 
10 #include "drivers/v_loop.h"
11 #include "devices/dev_buck_typedef.h"
12 #include "devices/dev_buck_converter.h"
13 #include "config/hal.h"
14 
15 #include "fault_handler/app_fault_monitor.h"
16 
17 
28 volatile struct BUCK_CONVERTER_s buck;
29 
30 /* PRIVATE EXTERNAL FUNCTION PROTOTYPES */
31 extern volatile uint16_t appPowerSupply_ConverterObjectInitialize(void);
32 extern volatile uint16_t appPowerSupply_ControllerInitialize(void);
33 extern volatile uint16_t appPowerSupply_PeripheralsInitialize(void);
34 
35 /* PRIVATE INTERNAL FUNCTION PROTOTYPES */
36 volatile uint16_t __attribute__((always_inline)) appPowerSupply_DataCapture(void);
37 
38 /* *************************************************************************************************
39  * PRIVATE VARIABLE DECLARATIONS
40  * ************************************************************************************************/
41 #define ISNS_AVG_BITMASK (uint16_t)0x0007
42 volatile uint16_t _isns_sample_count = 0;
43 volatile uint16_t isns_samples;
44 
45 /* *************************************************************************************************
46  * PUBLIC FUNCTIONS
47  * ************************************************************************************************/
48 
49 
50 
66 volatile uint16_t appPowerSupply_Execute(void)
67 {
68  volatile uint16_t retval=1;
69 
70  // Capture data values, which are not captured by the controller automatically
71  retval = appPowerSupply_DataCapture();
72 
73  // Execute buck converter state machine
74  retval &= drv_BuckConverter_Execute(&buck);
75 
76  // Buck regulation error is only active while controller is running
77  // and while being tied to a valid reference
78  if(((buck.state_id.bits.opstate_id >= BUCK_OPSTATE_RAMPUP) &&
79  (buck.state_id.bits.substate_id >= BUCK_OPSTATE_V_RAMP_UP)) ||
80  ((buck.state_id.bits.opstate_id == BUCK_OPSTATE_ONLINE)))
81  {
83  #if (PLANT_MEASUREMENT == false)
86  #endif
87  }
88  else {
89  fltobj_BuckRegErr.Status.bits.Enabled = false;
90  fltobj_BuckOCP.Status.bits.Enabled = false;
91  }
92 
93  return(retval);
94 }
95 
96 
121 volatile uint16_t appPowerSupply_Initialize(void)
122 {
123  volatile uint16_t retval=1;
124 
125  // Run initialization sequence
129 
130  // Initialize Control Interrupt
132  _BUCK_VLOOP_ISR_IF = 0;
133  _BUCK_VLOOP_ISR_IE = true;
134 
135  // Enable Buck Converter
136  retval &= drv_BuckConverter_Start(&buck);
137 
138  return(retval);
139 }
140 
141 
152 volatile uint16_t appPowerSupply_Start(void)
153 {
154  volatile uint16_t retval=1;
155 
156  retval &= drv_BuckConverter_Start(&buck); // Start PWM with outputs disabled to start ADC triggering
157 
158  return(retval);
159 }
160 
161 
180 volatile uint16_t appPowerSupply_Stop(void)
181 {
182  volatile uint16_t retval=1;
183 
184  retval &= drv_BuckConverter_Stop(&buck); // Shut down all power supply peripherals and data objects
185 
186  return(retval);
187 }
188 
200 volatile uint16_t appPowerSupply_Suspend(void)
201 {
202  volatile uint16_t retval=1;
203 
204  retval &= drv_BuckConverter_Suspend(&buck); // Shut down PWM immediately
205 
206  return(retval);
207 }
208 
209 
221 volatile uint16_t appPowerSupply_Resume(void)
222 {
223  volatile uint16_t retval=0;
224 
225  retval &= drv_BuckConverter_Resume(&buck); // Shut down PWM immediately
226 
227  return(retval);
228 }
229 
230 /* *************************************************************************************************
231  * PRIVATE FUNCTIONS
232  * ************************************************************************************************/
233 
234 
263 volatile uint16_t appPowerSupply_DataCapture(void)
264 {
265  volatile uint16_t retval=0;
266 
270  if (buck.data.control_output < 210)
272  else
273  buck.data.i_sns[0] = (buck.data.control_output - 210);
274 
275  // Average inductor current value
276  isns_samples += buck.data.i_sns[0];
277  if(!(++_isns_sample_count & ISNS_AVG_BITMASK))
278  {
279  isns_samples = (isns_samples >> 3);
280  isns_samples -= buck.i_loop[0].feedback_offset;
281  if((int16_t)isns_samples < 0) isns_samples = 0;
282 
283  buck.data.i_out = isns_samples;
284 
285  isns_samples = 0; // Reset data buffer
286  }
287 
288  return(retval);
289 }
290 
291 // end of file
#define BUCK_VIN_OFFSET
Input voltage feedback offset.
#define BUCK_VIN_ADCBUF
ADC input buffer of this ADC channel.
#define BUCK_ISNS_ADCBUF
ADC input buffer of this ADC channel.
#define TEMP_ADCBUF
GPIO analog function mode enable bit.
#define _BUCK_VLOOP_ISR_IE
Interupt vector enable bit register bit.
#define BUCK_VOUT_ISR_PRIORITY
Voltage loop interrupt vector priority (valid settings between 0...6 with 6 being the highest priorit...
#define _BUCK_VLOOP_ISR_IF
Interupt vector flag bit register bit.
#define _BUCK_VLOOP_ISR_IP
Interupt vector priority register.
volatile struct FAULT_OBJECT_s fltobj_BuckOCP
Over Current Protection Fault Object.
volatile struct FAULT_OBJECT_s fltobj_BuckRegErr
Regulation Error Fault Object.
volatile uint16_t appPowerSupply_Execute(void)
This is the top-level function call triggering the most recent state machine of all associated power ...
volatile uint16_t appPowerSupply_Resume(void)
This function resumes the power supply operation.
volatile uint16_t appPowerSupply_Initialize(void)
Calls the application layer power controller initialization.
volatile uint16_t appPowerSupply_Start(void)
This function calls the buck converter device driver function starting the power supply.
volatile uint16_t appPowerSupply_Stop(void)
This function calls the buck converter device driver function stopping the power supply.
volatile uint16_t appPowerSupply_Suspend(void)
This function stops the power supply operation.
volatile struct BUCK_CONVERTER_s buck
Global data object for a BUCK CONVERTER.
volatile uint16_t appPowerSupply_DataCapture(void)
Captures runtime data not captured automatically by other firmware modules.
volatile uint16_t appPowerSupply_PeripheralsInitialize(void)
This function is used to load peripheral configuration templates from the power controller device dri...
volatile uint16_t appPowerSupply_ControllerInitialize(void)
This function initializes the control system feedback loop objects.
volatile uint16_t appPowerSupply_ConverterObjectInitialize(void)
This function initializes the buck converter device driver instance.
volatile uint16_t drv_BuckConverter_Suspend(volatile struct BUCK_CONVERTER_s *buckInstance)
This function suspends the operation of the buck converter.
volatile uint16_t drv_BuckConverter_Stop(volatile struct BUCK_CONVERTER_s *buckInstance)
This function stop the buck converter opration.
volatile uint16_t drv_BuckConverter_Start(volatile struct BUCK_CONVERTER_s *buckInstance)
This function starts the buck converter.
volatile uint16_t drv_BuckConverter_Resume(volatile struct BUCK_CONVERTER_s *buckInstance)
This function resume the operation of the buck converter.
volatile uint16_t drv_BuckConverter_Execute(volatile struct BUCK_CONVERTER_s *buckInstance)
This function is the main buck converter state machine executing the most recent state.
struct FLT_OBJECT_STATUS_s::@111::@113 bits
volatile uint16_t * ptrObject
Pointer to register or variable which should be monitored.
volatile struct FLT_OBJECT_STATUS_s Status
Status word of this fault object.
volatile struct FLT_COMPARE_OBJECT_s ReferenceObject
Reference object the source should be compared with.
struct BUCK_STATE_ID_s::@129::@131 bits
volatile uint16_t i_out
BUCK common output current.
volatile uint16_t temp
BUCK board temperature.
volatile uint16_t control_output
BUCK most recent control output value.
volatile uint16_t i_sns[BUCK_NO_OF_PHASES]
BUCK output current.
volatile uint16_t v_in
BUCK input voltage.
volatile uint16_t feedback_offset
Feedback offset value for calibration or bi-direction feedback signals.
volatile struct NPNZ16b_s * controller
pointer to control loop object data structure
BUCK control & monitoring data structure.
volatile struct BUCK_LOOP_SETTINGS_s i_loop[BUCK_NO_OF_PHASES]
BUCK Current control loop objects.
volatile struct BUCK_STATE_ID_s state_id
BUCK state machine operating state ID.
volatile struct BUCK_LOOP_SETTINGS_s v_loop
BUCK voltage control loop object.
volatile struct BUCK_CONVERTER_DATA_s data
BUCK runtime data.
struct NPNZ_STATUS_s::@132::@134 bits
Controller status bit-field for direct bit access.
volatile uint16_t * ptrControlReference
Pointer to global variable of input register holding the controller reference value (e....
Definition: npnz16b.h:265
volatile struct NPNZ_STATUS_s status
Control Loop Status and Control flags.
Definition: npnz16b.h:504
volatile struct NPNZ_PORTS_s Ports
Controller input and output ports.
Definition: npnz16b.h:505