EPC9143 300 W 16th Brick DC/DC Module Reference Design
app_power_control.c
1 
32 /*
33  * File: app_power_control.c
34  * Author: M91406
35  *
36  * Created on March 12, 2020, 11:55 AM
37  */
38 
39 
40 #include <xc.h> // include processor files - each processor file is guarded.
41 #include <stdint.h> // include standard integer data types
42 #include <stdbool.h> // include standard boolean data types
43 #include <stddef.h> // include standard definition data types
44 
45 #include "config/hal.h" // include hardware abstraction layer declarations
46 #include "app_power_control.h" // include applicaiton layer power control header file
47 
48 #include "./devices/dev_buck_converter.h" // include buck converter device
49 #include "fault_handler/app_fault_monitor.h" // include fault monitor application object declarations
50 
51 
52 
63 volatile struct BUCK_CONVERTER_s buck;
64 
65 /* PRIVATE FUNCTION PROTOTYPES */
66 
67 extern volatile uint16_t appPowerSupply_ConverterObjectInitialize(void);
68 extern volatile uint16_t appPowerSupply_ControllerInitialize(void);
69 extern volatile uint16_t appPowerSupply_PeripheralsInitialize(void);
70 
71 void __attribute__((always_inline)) appPowerSupply_CurrentBalancing(void);
72 
73 
74 /* *************************************************************************************************
75  * PUBLIC FUNCTIONS
76  * ************************************************************************************************/
77 
78 
79 
95 volatile uint16_t appPowerSupply_Execute(void)
96 {
97  volatile uint16_t retval=1;
98  volatile uint16_t _i=0;
99  volatile uint16_t i_dummy=0;
100 
101  // Capture most recent samples
103 
104  // Collecting all phase current samples
107 
108  // Accumulate phase currents
109  for (_i=0; _i<buck.set_values.no_of_phases; _i++)
110  {
112  buck.data.i_sns[_i] = 0;
113  else
115 
116  i_dummy += buck.data.i_sns[_i];
117  }
118 
119  buck.data.i_out = i_dummy; // Set output current value
120 
121  // Execute buck converter state machine
122  retval &= drv_BuckConverter_Execute(&buck);
123 
124  // Execute slower, advanced control options
125 // appPowerSupply_CurrentBalancing();
126 
127  // Buck regulation error is only active while controller is running
128  // and while being tied to a valid reference
129  if((buck.state_id.bits.opstate_id >= BUCK_OPSTATE_RAMPUP) &&
130  (buck.state_id.bits.substate_id >= BUCK_OPSTATE_V_RAMP_UP))
131  {
132  // During and after startup, the dynamic compare object needs to be assigned to the correct source
134 
135  // Regulation Error and Over Current Protection fault objects only work if the converter is running
136  // Hence, they need to be enabled/disabled with the control loops
137  #if (PLANT_MEASUREMENT == false)
140  #endif
141  }
142  else
143  {
144  fltobj_BuckRegErr.Status.bits.Enabled = false;
145  fltobj_BuckOCP.Status.bits.Enabled = false;
146  }
147 
148  return(retval);
149 }
150 
151 
176 volatile uint16_t appPowerSupply_Initialize(void)
177 {
178  volatile uint16_t retval=1;
179 
180  // Run initialization sequence
184 
185  // Initialize Control Interrupt
187  _BUCK_VLOOP_ISR_IF = 0;
188  _BUCK_VLOOP_ISR_IE = true;
189 
190  // Start power supply engine
191  retval &= appPowerSupply_Start();
192 
193  // Enable Buck Converter
194  buck.status.bits.enabled = true;
195 
196  return(retval);
197 }
198 
199 
210 volatile uint16_t appPowerSupply_Start(void)
211 {
212  volatile uint16_t retval=1;
213 
214  retval &= drv_BuckConverter_Start(&buck); // Start PWM with outputs disabled to start ADC triggering
215 
216  return(retval);
217 }
218 
219 
238 volatile uint16_t appPowerSupply_Stop(void)
239 {
240  volatile uint16_t retval=1;
241 
242  retval &= drv_BuckConverter_Stop(&buck); // Shut down all power supply peripherals and data objects
243 
244  return(retval);
245 }
246 
247 
259 volatile uint16_t appPowerSupply_Suspend(void)
260 {
261  volatile uint16_t retval=1;
262 
263  retval &= drv_BuckConverter_Suspend(&buck); // Shut down PWM immediately
264 
265  return(retval);
266 }
267 
268 
280 volatile uint16_t appPowerSupply_Resume(void)
281 {
282  volatile uint16_t retval=0;
283 
284  retval &= drv_BuckConverter_Resume(&buck); // Shut down PWM immediately
285 
286  return(retval);
287 }
288 
289 
321 {
322  static int16_t offset=0;
323 
324  // Current balancing is only executed in nominal running mode
325  if(buck.state_id.bits.opstate_id != BUCK_OPSTATE_ONLINE)
326  return;
327 
328  // if current in phase #1 is higher than phase current #2...
329  if(buck.data.i_sns[0] > buck.data.i_sns[1])
330  { // .. add 1 tick to phase #2 duty cycle
331  offset = (((++offset)>0x07) ? 0x07 : offset);
332  }
333  else
334  { // .. sub 1 tick from phase #2 duty cycle
335  offset = (((--offset)<0x00) ? 0x00 : offset);
336  }
337 
338  buck.v_loop.controller->Ports.AltTarget.Offset = (uint16_t)offset;
339 
340 }
341 
342 // ____________________________
343 // end of file
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_ISNS2_ADCBUF
#define BUCK_ISNS2_ADCBUF
ADC input buffer of this ADC channel.
Definition: epc9143_r40_hwdescr.h:639
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_ADC_INPUT_SETTINGS_s::scaling
volatile struct BUCK_ADC_INPUT_SCALING_s scaling
normalization scaling settings
Definition: dev_buck_typedef.h:438
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
BUCK_CONVERTER_DATA_s::i_out
volatile uint16_t i_out
BUCK common output current.
Definition: dev_buck_typedef.h:299
BUCK_CONVERTER_SETTINGS_s::no_of_phases
volatile uint16_t no_of_phases
number of converter phases
Definition: dev_buck_typedef.h:325
BUCK_CONVERTER_DATA_s::i_sns
volatile uint16_t i_sns[BUCK_MPHASE_COUNT]
BUCK output current.
Definition: dev_buck_typedef.h:298
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:420
BUCK_CONVERTER_s
BUCK control & monitoring data structure.
Definition: dev_buck_typedef.h:501
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:210
NPNZ_STATUS_s::enabled
volatile bool enabled
Bit 15: enables/disables control loop execution.
Definition: npnz16b.h:202
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:238
BUCK_CONVERTER_s::feedback
volatile struct BUCK_FEEDBACK_SETTINGS_s feedback
BUCK converter feedback settings.
Definition: dev_buck_typedef.h:507
BUCK_ADC_INPUT_SCALING_s::offset
volatile int16_t offset
Signal offset as signed integer to be subtracted from ADC input.
Definition: dev_buck_typedef.h:414
NPNZ16b_s::Ports
volatile struct NPNZ_PORTS_s Ports
Controller input and output ports.
Definition: npnz16b.h:505
NPNZ16b_s::status
volatile struct NPNZ_STATUS_s status
Control Loop Status and Control flags.
Definition: npnz16b.h:504
appPowerSupply_ConverterObjectInitialize
volatile uint16_t appPowerSupply_ConverterObjectInitialize(void)
This function initializes the buck converter device driver instance.
Definition: app_power_config.c:66
fltobj_BuckRegErr
volatile struct FAULT_OBJECT_s fltobj_BuckRegErr
Regulation Error Fault Object.
Definition: app_fault_monitor.c:37
NPNZ_PORTS_s::AltTarget
volatile struct NPNZ_PORT_s AltTarget
Secondary data output port declaration.
Definition: npnz16b.h:264
BUCK_VIN_ADCBUF
#define BUCK_VIN_ADCBUF
ADC input buffer of this ADC channel.
Definition: epc9143_r40_hwdescr.h:436
BUCK_CONVERTER_DATA_s::v_in
volatile uint16_t v_in
BUCK input voltage.
Definition: dev_buck_typedef.h:300
FAULT_OBJECT_s::Status
volatile struct FLT_OBJECT_STATUS_s Status
Status word of this fault object.
Definition: drv_fault_handler.h:133
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
volatile struct BUCK_CONVERTER_s buck
Global data object for a BUCK CONVERTER.
Definition: app_power_control.c:63
fltobj_BuckOCP
volatile struct FAULT_OBJECT_s fltobj_BuckOCP
Over Current Protection Fault Object.
Definition: app_fault_monitor.c:36
_BUCK_VLOOP_ISR_IE
#define _BUCK_VLOOP_ISR_IE
Interupt vector enable bit register bit.
Definition: epc9143_r40_hwdescr.h:821
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:71
_BUCK_VLOOP_ISR_IF
#define _BUCK_VLOOP_ISR_IF
Interupt vector flag bit register bit.
Definition: epc9143_r40_hwdescr.h:820
BUCK_CONVERTER_s::status
volatile struct BUCK_CONVERTER_STATUS_s status
BUCK operation status bits.
Definition: dev_buck_typedef.h:502
appPowerSupply_Suspend
volatile uint16_t appPowerSupply_Suspend(void)
This function stops the power supply operation.
Definition: app_power_control.c:259
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: epc9143_r40_hwdescr.h:810
_BUCK_VLOOP_ISR_IP
#define _BUCK_VLOOP_ISR_IP
Interupt vector priority register.
Definition: epc9143_r40_hwdescr.h:819
BUCK_CONVERTER_s::v_loop
volatile struct BUCK_LOOP_SETTINGS_s v_loop
BUCK voltage control loop object.
Definition: dev_buck_typedef.h:512
BUCK_STATE_ID_s::bits
struct BUCK_STATE_ID_s::@3::@4 bits
BUCK_CONVERTER_STATUS_s::enabled
volatile bool enabled
Bit #15: Control bit enabling/disabling the charger port.
Definition: dev_buck_typedef.h:224
appPowerSupply_CurrentBalancing
void appPowerSupply_CurrentBalancing(void)
This function performs current balancing between the power supply phases.
Definition: app_power_control.c:320
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
appPowerSupply_ControllerInitialize
volatile uint16_t appPowerSupply_ControllerInitialize(void)
This function initializes the control system feedback loop objects.
Definition: app_power_config.c:305
appPowerSupply_Resume
volatile uint16_t appPowerSupply_Resume(void)
This function resumes the power supply operation.
Definition: app_power_control.c:280
BUCK_CONVERTER_s::data
volatile struct BUCK_CONVERTER_DATA_s data
BUCK runtime data.
Definition: dev_buck_typedef.h:506
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
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
BUCK_ISNS1_ADCBUF
#define BUCK_ISNS1_ADCBUF
ADC input buffer of this ADC channel.
Definition: epc9143_r40_hwdescr.h:629
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
BUCK_LOOP_SETTINGS_s::controller
volatile struct NPNZ16b_s * controller
pointer to control loop object data structure
Definition: dev_buck_typedef.h:353
NPNZ_PORT_s::Offset
volatile int16_t Offset
Value/signal offset of this port.
Definition: npnz16b.h:231
BUCK_FEEDBACK_SETTINGS_s::ad_isns
volatile struct BUCK_ADC_INPUT_SETTINGS_s ad_isns[BUCK_MPHASE_COUNT]
ADC input sampling phase current.
Definition: dev_buck_typedef.h:453
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
appPowerSupply_Initialize
volatile uint16_t appPowerSupply_Initialize(void)
Calls the application layer power controller initialization.
Definition: app_power_control.c:176
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