Digital Power Starter Kit 3 Firmware
dsPIC33C Buck Converter Voltage 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 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  // Buck regulation error is only active while controller is running
89  // and while being tied to a valid reference
90  if(((buck.state_id.bits.opstate_id >= BUCK_OPSTATE_RAMPUP) &&
91  (buck.state_id.bits.substate_id >= BUCK_OPSTATE_V_RAMP_UP)) ||
92  ((buck.state_id.bits.opstate_id == BUCK_OPSTATE_ONLINE)))
93  {
95  #if (PLANT_MEASUREMENT == false)
98  #endif
99  }
100  else {
101  fltobj_BuckRegErr.Status.bits.Enabled = false;
102  fltobj_BuckOCP.Status.bits.Enabled = false;
103  }
104 
105  return(retval);
106 }
107 
108 
133 volatile uint16_t appPowerSupply_Initialize(void)
134 {
135  volatile uint16_t retval=1;
136 
137  // Run initialization sequence
141 
142  // Initialize Control Interrupt
144  _BUCK_VLOOP_ISR_IF = 0;
145  _BUCK_VLOOP_ISR_IE = true;
146 
147  // Enable Buck Converter
148  retval &= drv_BuckConverter_Start(&buck);
149 
150  return(retval);
151 }
152 
153 
164 volatile uint16_t appPowerSupply_Start(void)
165 {
166  volatile uint16_t retval=1;
167 
168  retval &= drv_BuckConverter_Start(&buck); // Start PWM with outputs disabled to start ADC triggering
169 
170  return(retval);
171 }
172 
173 
192 volatile uint16_t appPowerSupply_Stop(void)
193 {
194  volatile uint16_t retval=1;
195 
196  retval &= drv_BuckConverter_Stop(&buck); // Shut down all power supply peripherals and data objects
197 
198  return(retval);
199 }
200 
212 volatile uint16_t appPowerSupply_Suspend(void)
213 {
214  volatile uint16_t retval=1;
215 
216  retval &= drv_BuckConverter_Suspend(&buck); // Shut down PWM immediately
217 
218  return(retval);
219 }
220 
221 
233 volatile uint16_t appPowerSupply_Resume(void)
234 {
235  volatile uint16_t retval=0;
236 
237  retval &= drv_BuckConverter_Resume(&buck); // Shut down PWM immediately
238 
239  return(retval);
240 }
241 
242 /* *************************************************************************************************
243  * PRIVATE FUNCTIONS
244  * ************************************************************************************************/
245 
246  // (none)
247 
248 // end of file
appPowerSupply_Resume
volatile uint16_t appPowerSupply_Resume(void)
This function resumes the power supply operation.
Definition: app_power_control.c:233
fltobj_BuckRegErr
volatile struct FAULT_OBJECT_s fltobj_BuckRegErr
Regulation Error Fault Object.
Definition: app_fault_monitor.c:53
_BUCK_VLOOP_ISR_IP
#define _BUCK_VLOOP_ISR_IP
Interupt vector priority register.
Definition: dpsk3_hwdescr.h:754
_BUCK_VLOOP_ISR_IE
#define _BUCK_VLOOP_ISR_IE
Interupt vector enable bit register bit.
Definition: dpsk3_hwdescr.h:756
BUCK_CONVERTER_DATA_s::v_in
volatile uint16_t v_in
BUCK input voltage.
Definition: dev_buck_typedef.h:300
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
FAULT_OBJECT_s::Status
volatile struct FLT_OBJECT_STATUS_s Status
Status word of this fault object.
Definition: drv_fault_handler.h:133
appPowerSupply_ConverterObjectInitialize
volatile uint16_t appPowerSupply_ConverterObjectInitialize(void)
This function initializes the buck converter device driver instance.
Definition: app_power_config.c:32
BUCK_VIN_OFFSET
#define BUCK_VIN_OFFSET
Input voltage feedback offset.
Definition: dpsk3_hwdescr.h:458
appPowerSupply_Stop
volatile uint16_t appPowerSupply_Stop(void)
This function calls the buck converter device driver function stopping the power supply.
Definition: app_power_control.c:192
appPowerSupply_ControllerInitialize
volatile uint16_t appPowerSupply_ControllerInitialize(void)
This function initializes the control system feedback loop objects.
Definition: app_power_config.c:243
BUCK_CONVERTER_DATA_s::i_sns
volatile uint16_t i_sns[BUCK_MPHASE_COUNT]
BUCK output current.
Definition: dev_buck_typedef.h:298
drv_BuckConverter_Resume
volatile uint16_t drv_BuckConverter_Resume(volatile struct BUCK_CONVERTER_s *buckInstance)
This function resume the operation of the buck converter.
Definition: dev_buck_converter.c:294
drv_BuckConverter_Start
volatile uint16_t drv_BuckConverter_Start(volatile struct BUCK_CONVERTER_s *buckInstance)
This function starts the buck converter.
Definition: dev_buck_converter.c:197
NPNZ16b_s::status
volatile struct NPNZ_STATUS_s status
Control Loop Status and Control flags.
Definition: npnz16b.h:504
BUCK_VOUT_ISR_PRIORITY
#define BUCK_VOUT_ISR_PRIORITY
Voltage loop interrupt vector priority (valid settings between 0...6 with 6 being the highest priorit...
Definition: dpsk3_hwdescr.h:742
BUCK_ISNS_ADCBUF
#define BUCK_ISNS_ADCBUF
ADC input buffer of this ADC channel.
Definition: dpsk3_hwdescr.h:651
appPowerSupply_Start
volatile uint16_t appPowerSupply_Start(void)
This function calls the buck converter device driver function starting the power supply.
Definition: app_power_control.c:164
drv_BuckConverter_Stop
volatile uint16_t drv_BuckConverter_Stop(volatile struct BUCK_CONVERTER_s *buckInstance)
This function stop the buck converter opration.
Definition: dev_buck_converter.c:240
FAULT_OBJECT_s::ReferenceObject
volatile struct FLT_COMPARE_OBJECT_s ReferenceObject
Reference object the source should be compared with.
Definition: drv_fault_handler.h:136
NPNZ_STATUS_s::enabled
volatile bool enabled
Bit 15: enables/disables control loop execution.
Definition: npnz16b.h:202
appPowerSupply_PeripheralsInitialize
volatile uint16_t appPowerSupply_PeripheralsInitialize(void)
This function is used to load peripheral configuration templates from the power controller device dri...
Definition: app_power_config.c:385
_BUCK_VLOOP_ISR_IF
#define _BUCK_VLOOP_ISR_IF
Interupt vector flag bit register bit.
Definition: dpsk3_hwdescr.h:755
appPowerSupply_Suspend
volatile uint16_t appPowerSupply_Suspend(void)
This function stops the power supply operation.
Definition: app_power_control.c:212
FLT_OBJECT_STATUS_s::Enabled
volatile bool Enabled
Bit 15: Control bit enabling/disabling monitoring of the fault object.
Definition: drv_fault_handler.h:89
BUCK_VIN_ADCBUF
#define BUCK_VIN_ADCBUF
ADC input buffer of this ADC channel.
Definition: dpsk3_hwdescr.h:429
BUCK_CONVERTER_s::data
volatile struct BUCK_CONVERTER_DATA_s data
BUCK runtime data.
Definition: dev_buck_typedef.h:506
FLT_COMPARE_OBJECT_s::ptrObject
volatile uint16_t * ptrObject
Pointer to register or variable which should be monitored.
Definition: drv_fault_handler.h:106
BUCK_CONVERTER_DATA_s::i_out
volatile uint16_t i_out
BUCK common output current.
Definition: dev_buck_typedef.h:299
appPowerSupply_Execute
volatile uint16_t appPowerSupply_Execute(void)
This is the top-level function call triggering the most recent state machine of all associated power ...
Definition: app_power_control.c:63
BUCK_LOOP_SETTINGS_s::controller
volatile struct NPNZ16b_s * controller
pointer to control loop object data structure
Definition: dev_buck_typedef.h:353
BUCK_CONVERTER_DATA_s::temp
volatile uint16_t temp
BUCK board temperature.
Definition: dev_buck_typedef.h:302
fltobj_BuckOCP
volatile struct FAULT_OBJECT_s fltobj_BuckOCP
Over Current Protection Fault Object.
Definition: app_fault_monitor.c:52
BUCK_CONVERTER_s
BUCK control & monitoring data structure.
Definition: dev_buck_typedef.h:501
BUCK_STATE_ID_s::bits
struct BUCK_STATE_ID_s::@3::@4 bits
BUCK_CONVERTER_s::state_id
volatile struct BUCK_STATE_ID_s state_id
BUCK state machine operating state ID.
Definition: dev_buck_typedef.h:503
BUCK_CONVERTER_s::v_loop
volatile struct BUCK_LOOP_SETTINGS_s v_loop
BUCK voltage control loop object.
Definition: dev_buck_typedef.h:512
appPowerSupply_Initialize
volatile uint16_t appPowerSupply_Initialize(void)
Calls the application layer power controller initialization.
Definition: app_power_control.c:133
TEMP_ADCBUF
#define TEMP_ADCBUF
GPIO analog function mode enable bit.
Definition: dpsk3_hwdescr.h:1579
drv_BuckConverter_Execute
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.
Definition: dev_buck_converter.c:82
NPNZ16b_s::Ports
volatile struct NPNZ_PORTS_s Ports
Controller input and output ports.
Definition: npnz16b.h:505
buck
volatile struct BUCK_CONVERTER_s buck
Global data object for a BUCK CONVERTER.
Definition: app_power_control.c:28
drv_BuckConverter_Suspend
volatile uint16_t drv_BuckConverter_Suspend(volatile struct BUCK_CONVERTER_s *buckInstance)
This function suspends the operation of the buck converter.
Definition: dev_buck_converter.c:273
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
NPNZ_PORTS_s::ptrControlReference
volatile uint16_t * ptrControlReference
Pointer to global variable of input register holding the controller reference value (e....
Definition: npnz16b.h:265