Digital Power Starter Kit 3 Firmware
dsPIC33C Buck Converter Adaptive Gain 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 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 /* *************************************************************************************************
36  * PRIVATE VARIABLE DECLARATIONS
37  * ************************************************************************************************/
38 #define ISNS_AVG_BITMASK (uint16_t)0x0007
39 volatile uint16_t _isns_sample_count = 0;
40 volatile uint16_t isns_samples;
41 
42 /* *************************************************************************************************
43  * PUBLIC FUNCTIONS
44  * ************************************************************************************************/
45 
46 
47 
63 volatile uint16_t appPowerSupply_Execute(void)
64 {
65  volatile uint16_t retval=1;
66 
67  // Capture data values
71 
72  // Average inductor current value
73  isns_samples += buck.data.i_sns[0];
74  if(!(++_isns_sample_count & ISNS_AVG_BITMASK))
75  {
76  isns_samples = (isns_samples >> 3);
77  isns_samples -= buck.i_loop[0].feedback_offset;
78  if((int16_t)isns_samples < 0) isns_samples = 0;
79 
80  buck.data.i_out = isns_samples;
81 
82  isns_samples = 0; // Reset data buffer
83  }
84 
85  // Execute buck converter state machine
86  retval &= drv_BuckConverter_Execute(&buck);
87 
88 
89  // Buck regulation error is only active while controller is running
90  // and while being tied to a valid reference
91  if(((buck.state_id.bits.opstate_id >= BUCK_OPSTATE_RAMPUP) &&
92  (buck.state_id.bits.substate_id >= BUCK_OPSTATE_V_RAMP_UP)) ||
93  ((buck.state_id.bits.opstate_id == BUCK_OPSTATE_ONLINE)))
94  {
96  #if (PLANT_MEASUREMENT == false)
99  #endif
100  }
101  else {
102  fltobj_BuckRegErr.Status.bits.Enabled = false;
103  fltobj_BuckOCP.Status.bits.Enabled = false;
104  }
105 
106  return(retval);
107 }
108 
109 
134 volatile uint16_t appPowerSupply_Initialize(void)
135 {
136  volatile uint16_t retval=1;
137 
138  // Run initialization sequence
142 
143  // Initialize Control Interrupt
145  _BUCK_VLOOP_ISR_IF = 0;
146  _BUCK_VLOOP_ISR_IE = true;
147 
148  // Enable Buck Converter
149  retval &= drv_BuckConverter_Start(&buck);
150 
151  return(retval);
152 }
153 
154 
165 volatile uint16_t appPowerSupply_Start(void)
166 {
167  volatile uint16_t retval=1;
168 
169  retval &= drv_BuckConverter_Start(&buck); // Start PWM with outputs disabled to start ADC triggering
170 
171  return(retval);
172 }
173 
174 
193 volatile uint16_t appPowerSupply_Stop(void)
194 {
195  volatile uint16_t retval=1;
196 
197  retval &= drv_BuckConverter_Stop(&buck); // Shut down all power supply peripherals and data objects
198 
199  return(retval);
200 }
201 
213 volatile uint16_t appPowerSupply_Suspend(void)
214 {
215  volatile uint16_t retval=1;
216 
217  retval &= drv_BuckConverter_Suspend(&buck); // Shut down PWM immediately
218 
219  return(retval);
220 }
221 
222 
234 volatile uint16_t appPowerSupply_Resume(void)
235 {
236  volatile uint16_t retval=0;
237 
238  retval &= drv_BuckConverter_Resume(&buck); // Shut down PWM immediately
239 
240  return(retval);
241 }
242 
243 /* *************************************************************************************************
244  * PRIVATE FUNCTIONS
245  * ************************************************************************************************/
246 
247  // (none)
248 
249 // 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_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.
volatile bool Enabled
Bit 15: Control bit enabling/disabling monitoring of the fault object.
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::@234::@235 bits
volatile uint16_t i_sns[BUCK_MPHASE_COUNT]
BUCK output current.
volatile uint16_t i_out
BUCK common output current.
volatile uint16_t temp
BUCK board temperature.
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_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.
volatile struct BUCK_LOOP_SETTINGS_s i_loop[BUCK_MPHASE_COUNT]
BUCK Current control loop objects.
volatile bool enabled
Bit 15: enables/disables control loop execution.
Definition: npnz16b.h:202
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