Digital Power Starter Kit 3 Firmware
dsPIC33C Boost 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_boost_typedef.h"
12 #include "devices/dev_boost_converter.h"
13 #include "config/hal.h"
14 
15 #include "fault_handler/app_fault_monitor.h"
16 
17 /**************************************************************************************************
18  * @ingroup app-layer-power-control-properties-public
19  * @var volatile struct BOOST_CONVERTER_s boost
20  * @brief Global data object for a BOOST CONVERTER
21  *
22  * @details
23  * The 'boost' data object holds all status, control and monitoring values of the BOOST power
24  * controller. The BOOST_CONVERTER_s data structure is defined in dev_boost_converter.h.
25  * Please refer to the comments on top of this file for further information.
26  *
27  **************************************************************************************************/
28 volatile struct BOOST_CONVERTER_s boost;
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 /*******************************************************************************
48  * @ingroup app-layer-power-control-functions-public
49  * @fn volatile uint16_t appPowerSupply_Execute(void)
50  * @brief This is the top-level function call triggering the most recent state
51  * machine of all associated power supply controllers
52  * @return 0=failure
53  * @return 1=success
54  *
55  * @details
56  * After initialization, the proprietary user code has to call this function
57  * on a deterministic, constant time base. In each execution step this function
58  * will call the power control state machines of each supported/included power
59  * supply unit.
60  *
61  *********************************************************************************/
62 
63 volatile uint16_t appPowerSupply_Execute(void)
64 {
65  volatile uint16_t retval=1;
66 
67  // Capture data values
69  boost.data.temp = TEMP_ADCBUF;
70  boost.data.i_sns[0] = BOOST_ISNS_ADCBUF;
71 
72  // Average inductor current value and calculate output current
73  isns_samples += boost.data.i_sns[0];
74 
75  if(!(++_isns_sample_count & ISNS_AVG_BITMASK))
76  {
77  isns_samples = (isns_samples >> 3);
78  isns_samples -= boost.i_loop[0].feedback_offset;
79  if((int16_t)isns_samples < 0) isns_samples = 0;
80 
81  // Output current is estimated without considering internal power losses
82  isns_samples = (uint16_t)( (float)boost.data.v_in * (float)isns_samples / (float)boost.data.v_out );
83  boost.data.i_out = isns_samples;
84 
85  isns_samples = 0; // Reset data buffer
86  }
87 
88  // Execute boost converter state machine
89  retval &= drv_BoostConverter_Execute(&boost);
90 
91  // Boost regulation error is only active while controller is running
92  // and while being tied to a valid reference
93  if(((boost.state_id.bits.opstate_id >= BOOST_OPSTATE_RAMPUP) &&
94  (boost.state_id.bits.substate_id >= BOOST_OPSTATE_V_RAMP_UP)) ||
95  ((boost.state_id.bits.opstate_id == BOOST_OPSTATE_ONLINE)))
96  {
98  #if (PLANT_MEASUREMENT == false)
99  fltobj_BoostRegErr.Status.bits.Enabled = boost.v_loop.controller->status.bits.enabled;
100  fltobj_BoostOCP.Status.bits.Enabled = boost.v_loop.controller->status.bits.enabled;
101  #endif
102  }
103  else {
104  fltobj_BoostRegErr.Status.bits.Enabled = false;
105  fltobj_BoostOCP.Status.bits.Enabled = false;
106  }
107 
108  return(retval);
109 }
110 
111 /*******************************************************************************
112  * @ingroup app-layer-power-control-functions-public
113  * @fn volatile uint16_t appPowerSupply_Initialize(void)
114  * @brief Calls the application layer power controller initialization
115  * @return 0=failure
116  * @return 1=success
117  *
118  * @details
119  * The power control application layer is the proprietary user code layer
120  * used to tailor the generic power converter device driver to the specific
121  * hardware of this design. The initialization routine covers three levels
122  *
123  * - Converter Object Configuration
124  * - Controller Configuration
125  * - Peripheral Set Configuration
126  * - Interrupt Vector Configuration
127  *
128  * Once all modules have been configured successfully, the power converter
129  * object is started with control loops and PWM outputs disabled. However,
130  * the PWM module will start triggering the ADC to allow the standby monitoring
131  * of system conditions to allow the firmware to decide if it is safe to start
132  * up the power converter.
133  *
134  *********************************************************************************/
135 
136 volatile uint16_t appPowerSupply_Initialize(void)
137 {
138  volatile uint16_t retval=1;
139 
140  // Run initialization sequence
141  retval &= appPowerSupply_ConverterObjectInitialize();
142  retval &= appPowerSupply_ControllerInitialize();
143  retval &= appPowerSupply_PeripheralsInitialize();
144 
145  // Initialize Control Interrupt
146  _BOOST_VLOOP_ISR_IP = BOOST_VOUT_ISR_PRIORITY;
147  _BOOST_VLOOP_ISR_IF = 0;
148  _BOOST_VLOOP_ISR_IE = true;
149 
150  // Enable Boost Converter
151  retval &= drv_BoostConverter_Start(&boost);
152 
153  return(retval);
154 }
155 
156 /*******************************************************************************
157  * @ingroup app-layer-power-control-functions-public
158  * @fn volatile uint16_t appPowerSupply_Start(void)
159  * @brief This function calls the boost converter device driver function starting the power supply
160  * @return 0=failure
161  * @return 1=success
162  *
163  * @details
164  * This function exposes the Power Converter Start function of the device driver.
165  *********************************************************************************/
166 
167 volatile uint16_t appPowerSupply_Start(void)
168 {
169  volatile uint16_t retval=1;
170 
171  retval &= drv_BoostConverter_Start(&boost); // Start PWM with outputs disabled to start ADC triggering
172 
173  return(retval);
174 }
175 
176 /*******************************************************************************
177  * @ingroup app-layer-power-control-functions-public
178  * @fn volatile uint16_t appPowerSupply_Stop(void)
179  * @brief This function calls the boost converter device driver function stopping the power supply
180  * @return 0=failure
181  * @return 1=success
182  *
183  * @details
184  * This function exposes the Power Converter Stop function of the device driver.
185  *
186  * @note
187  * The STOP function terminates the state machine and all peripherals used by
188  * the power controller. This includes the PWM and ADC peripheral modules and
189  * will therefore also stop all data acquisition.
190  * If you are trying to stop the power supply but keep its state machine and
191  * data acquisition running, use the SUSPEND function instead
192  *
193  *********************************************************************************/
194 
195 volatile uint16_t appPowerSupply_Stop(void)
196 {
197  volatile uint16_t retval=1;
198 
199  retval &= drv_BoostConverter_Stop(&boost); // Shut down all power supply peripherals and data objects
200 
201  return(retval);
202 }
203 /*******************************************************************************
204  * @ingroup app-layer-power-control-functions-public
205  * @fn volatile uint16_t appPowerSupply_Suspend(void)
206  * @brief This function stops the power supply operation
207  * @return 0=failure
208  * @return 1=success
209  *
210  * @details
211  * The SUSPEND function stops the power supply operation but keep its state machine
212  * and data acquisition running.
213  *********************************************************************************/
214 
215 volatile uint16_t appPowerSupply_Suspend(void)
216 {
217  volatile uint16_t retval=1;
218 
219  retval &= drv_BoostConverter_Suspend(&boost); // Shut down PWM immediately
220 
221  return(retval);
222 }
223 
224 /*******************************************************************************
225  * @ingroup app-layer-power-control-functions-public
226  * @fn volatile uint16_t appPowerSupply_Resume(void)
227  * @brief This function resumes the power supply operation
228  * @return unsigned integer (0=failure, 1=success)
229  *
230  * @details
231  * This function calls the boost converter device driver function recovering
232  * the power supply operation from a previously initiated shut-down.
233  *
234  *********************************************************************************/
235 
236 volatile uint16_t appPowerSupply_Resume(void)
237 {
238  volatile uint16_t retval=0;
239 
240  retval &= drv_BoostConverter_Resume(&boost); // Shut down PWM immediately
241 
242  return(retval);
243 }
244 
245 /* *************************************************************************************************
246  * PRIVATE FUNCTIONS
247  * ************************************************************************************************/
248 
249  // (none)
250 
251 // end of file
volatile struct NPNZ_PORTS_s Ports
Controller input and output ports.
Definition: npnz16b.h:505
volatile struct NPNZ16b_s * controller
pointer to control loop object data structure
volatile bool Enabled
Bit 15: Control bit enabling/disabling monitoring of the fault object.
volatile struct BOOST_LOOP_SETTINGS_s v_loop
BOOST voltage control loop object.
#define BOOST_VIN_OFFSET
Input voltage feedback offset.
volatile struct FLT_COMPARE_OBJECT_s ReferenceObject
Reference object the source should be compared with.
volatile struct FLT_OBJECT_STATUS_s Status
Status word of this fault object.
volatile uint16_t * ptrObject
Pointer to register or variable which should be monitored.
volatile uint16_t feedback_offset
Feedback offset value for calibration or bi-direction feedback signals.
volatile uint16_t temp
BOOST board temperature.
volatile uint16_t v_in
BOOST input voltage.
volatile uint16_t i_out
BOOST common output current.
#define TEMP_ADCBUF
GPIO analog function mode enable bit.
struct BOOST_STATE_ID_s::@372::@373 bits
volatile struct BOOST_CONVERTER_DATA_s data
BOOST runtime data.
volatile struct BOOST_LOOP_SETTINGS_s i_loop[BOOST_MPHASE_COUNT]
BOOST Current control loop objects.
volatile struct NPNZ_STATUS_s status
Control Loop Status and Control flags.
Definition: npnz16b.h:504
#define BOOST_VIN_ADCBUF
ADC input buffer of this ADC channel.
volatile uint16_t i_sns[BOOST_MPHASE_COUNT]
BOOST output current.
volatile uint16_t v_out
BOOST output voltage.
volatile uint16_t * ptrControlReference
Pointer to global variable of input register holding the controller reference value (e....
Definition: npnz16b.h:265
#define BOOST_ISNS_ADCBUF
ADC input buffer of this ADC channel.
volatile struct BOOST_STATE_ID_s state_id
BOOST state machine operating state ID.
BOOST control & monitoring data structure.
volatile bool enabled
Bit 15: enables/disables control loop execution.
Definition: npnz16b.h:202