Digital Power Starter Kit 3 Firmware  DM330017-3, Rev.3.0
dsPIC33C Buck Converter Peak Current Mode Control Example
npnz16b.h
1 /* *********************************************************************************
2  * PowerSmartâ„¢ Digital Control Library Designer, Version 0.9.14.678
3  * *********************************************************************************
4  * Generic library header for z-domain compensation filter assembly functions
5  * CGS Version: 3.0.8
6  * CGS Date: 03/12/2021
7  * ********************************************************************************/
8 // This is a guard condition so that contents of this file are not included
9 // more than once.
10 #ifndef __SPECIAL_FUNCTION_LAYER_LIB_NPNZ16B_H__
11 #define __SPECIAL_FUNCTION_LAYER_LIB_NPNZ16B_H__
12 
13 #include <xc.h> // include processor files - each processor file is guarded
14 #include <dsp.h> // include DSP data types (e.g. fractional)
15 #include <stdint.h> // include standard integer number data types
16 #include <stddef.h> // include standard definition data types
17 #include <stdbool.h> // include standard boolean data types (true/false)
18 
19 
38 #ifndef __PSDCLD_VERSION
39  #define __PSDCLD_VERSION 914
40 #endif
41 
42 
55 enum NPNZ_STATUS_FLAGS_e
56 {
57  NPNZ_STATUS_CLEAR = 0b0000000000000000,
58  NPNZ_STATUS_SATUATION_MSK = 0b0000000000000011,
59  NPNZ_STATUS_LSAT_ACTIVE = 0b0000000000000001,
60  NPNZ_STATUS_LSAT_CLEAR = 0b0000000000000000,
61  NPNZ_STATUS_USAT_ACTIVE = 0b0000000000000010,
62  NPNZ_STATUS_USAT_CLEAR = 0b0000000000000000,
63  NPNZ_STATUS_AGC_DISABLE = 0b0000000000000000,
64  NPNZ_STATUS_AGC_ENABLED = 0b0000100000000000,
65  NPNZ_STATUS_TARGET_DEFAULT = 0b0000000000000000,
66  NPNZ_STATUS_TARGET_SWAPED = 0b0001000000000000,
67  NPNZ_STATUS_SOURCE_DEFAULT = 0b0000000000000000,
68  NPNZ_STATUS_SOURCE_SWAPED = 0b0010000000000000,
69  NPNZ_STATUS_INV_INPUT_OFF = 0b0000000000000000,
70  NPNZ_STATUS_INV_INPUT_ON = 0b0100000000000000,
71  NPNZ_STATUS_ENABLE_OFF = 0b0000000000000000,
72  NPNZ_STATUS_ENABLE_ON = 0b1000000000000000
73 };
74 typedef enum NPNZ_STATUS_FLAGS_e NPNZ_STATUS_FLAGS_t;
75 
76 
80 enum NPNZ_STATUS_SATURATION_e{
81  NPNZ_SAT_CLEAR = 0b0,
82  NPNZ_SAT_ACTIVE = 0b1
83 };
84 typedef enum NPNZ_STATUS_SATURATION_e NPNZ_STATUS_SATURATION_t;
85 extern volatile enum NPNZ_STATUS_SATURATION_e npnzEnumControlStatusSaturation;
86 
87 /* Control flags (bit-field) */
88 
92 enum NPNZ_STATUS_AGC_ENABLE_e{
93  NPNZ_AGC_DISABLED = 0b0,
94  NPNZ_AGC_ENABLED = 0b1
95 };
96 typedef enum NPNZ_STATUS_AGC_ENABLE_e NPNZ_STATUS_AGC_ENABLE_t;
97 extern volatile enum NPNZ_STATUS_AGC_ENABLE_e npnzEnumControlAgcEnable;
98 
99 
103 enum NPNZ_STATUS_SOURCE_SWAP_e{
104  NPNZ_SOURCE_DEFAULT = 0b0,
105  NPNZ_SOURCE_SWAPED = 0b1
106 };
107 typedef enum NPNZ_STATUS_SOURCE_SWAP_e NPNZ_STATUS_SOURCE_SWAP_t;
108 extern volatile enum NPNZ_STATUS_SOURCE_SWAP_e npnzEnumControlSourceSwap;
109 
110 
114 enum NPNZ_STATUS_TARGET_SWAP_e{
115  NPNZ_TARGET_DEFAULT = 0b0,
116  NPNZ_TARGET_SWAPED = 0b1
117 };
118 typedef enum NPNZ_STATUS_TARGET_SWAP_e NPNZ_STATUS_TARGET_SWAP_t;
119 extern volatile enum NPNZ_STATUS_TARGET_SWAP_e npnzEnumControlTargetSwap;
120 
121 
125 enum NPNZ_STATUS_INPUT_INV_e{
126  NPNZ_INPUT_DEFAULT = 0b0,
127  NPNZ_INPUT_INVERTED = 0b1
128 };
129 typedef enum NPNZ_STATUS_INPUT_INV_e NPNZ_STATUS_INPUT_INV_t;
130 extern volatile enum NPNZ_STATUS_INPUT_INV_e npnzEnumControlInputInversion;
131 
132 
136 enum NPNZ_STATUS_ENABLE_e{
137  NPNZ_DISABLED = 0b0,
138  NPNZ_ENABLED = 0b1
139 };
140 typedef enum NPNZ_STATUS_ENABLE_e NPNZ_STATUS_ENABLE_t;
141 extern volatile enum NPNZ_STATUS_ENABLE_e npnzEnumControlEnable;
142 
143 
148 {
149  volatile enum NPNZ_STATUS_FLAGS_e StatusWordFlags;
150  volatile enum NPNZ_STATUS_SATURATION_e flagSaturation;
151  volatile enum NPNZ_STATUS_AGC_ENABLE_e flagAgcControl;
152  volatile enum NPNZ_STATUS_SOURCE_SWAP_e flagSourceSwap;
153  volatile enum NPNZ_STATUS_TARGET_SWAP_e flagTargetSwap;
154  volatile enum NPNZ_STATUS_INPUT_INV_e flagCtrlInputInversion;
155  volatile enum NPNZ_STATUS_ENABLE_e flagControlEnable;
156 };
157 typedef struct NPNZ_FLAGS_s NPNZ_FLAGS_t;
158 extern volatile struct NPNZ_FLAGS_s npnzFlagList;
159 
160 
182 /* Controller status data structure */
184  union {
185  struct {
186  volatile bool lower_saturation_event : 1;
187  volatile bool upper_saturation_event : 1;
188  volatile unsigned : 1;
189  volatile unsigned : 1;
190  volatile unsigned : 1;
191  volatile unsigned : 1;
192  volatile unsigned : 1;
193  volatile unsigned : 1;
194 
195  volatile unsigned : 1;
196  volatile unsigned : 1;
197  volatile unsigned : 1;
198  volatile bool agc_enabled: 1;
199  volatile bool swap_target: 1;
200  volatile bool swap_source: 1;
201  volatile bool invert_input: 1;
202  volatile bool enabled : 1;
203  } __attribute__((packed))bits;
204  volatile uint16_t value;
205  };
206 } __attribute__((packed));
207 typedef struct NPNZ_STATUS_s NPNZ_STATUS_t;
208 
209 
225 /* Controller Input/Output Port */
226 struct NPNZ_PORT_s{
227 
228  volatile uint16_t* ptrAddress;
229  volatile int16_t NormScaler;
230  volatile fractional NormFactor;
231  volatile int16_t Offset;
232 
233 } __attribute__((packed));
234 typedef struct NPNZ_PORT_s NPNZ_PORT_t;
235 
236 
258 /* List of Controller Input/Output Ports */
260 
261  volatile struct NPNZ_PORT_s Source;
262  volatile struct NPNZ_PORT_s AltSource;
263  volatile struct NPNZ_PORT_s Target;
264  volatile struct NPNZ_PORT_s AltTarget;
265  volatile uint16_t* ptrControlReference;
266 
267 } __attribute__((packed));
268 typedef struct NPNZ_PORTS_s NPNZ_PORTS_t;
269 
270 
285 
286  volatile int32_t* ptrACoefficients;
287  volatile int32_t* ptrBCoefficients;
288  volatile fractional* ptrControlHistory;
289  volatile fractional* ptrErrorHistory;
290 
291  // Array size information
292  volatile uint16_t ACoefficientsArraySize;
293  volatile uint16_t BCoefficientsArraySize;
294  volatile uint16_t ControlHistoryArraySize;
295  volatile uint16_t ErrorHistoryArraySize;
296 
297  // Feedback scaling Input/Output Normalization
298  volatile int16_t normPreShift;
299  volatile int16_t normPostShiftA;
300  volatile int16_t normPostShiftB;
301  volatile int16_t normPostScaler;
302 
303  // P-Term Coefficients (for plant measurements only)
304  volatile int16_t PTermScaler;
305  volatile int16_t PTermFactor;
306 
307 } __attribute__((packed));
309 
310 
328 
329  volatile int16_t MinOutput;
330  volatile int16_t MaxOutput;
331  volatile int16_t AltMinOutput;
332  volatile int16_t AltMaxOutput;
333 
334 } __attribute__((packed));
335 typedef struct NPNZ_LIMITS_s NPNZ_LIMITS_t;
336 
337 
355 
356  volatile uint16_t* ptrADCTriggerARegister;
357  volatile uint16_t ADCTriggerAOffset;
358  volatile uint16_t* ptrADCTriggerBRegister;
359  volatile uint16_t ADCTriggerBOffset;
360 
361 } __attribute__((packed));
362 typedef struct NPNZ_ADC_TRGCTRL_s NPNZ_ADC_TRGCTRL_t;
363 
364 
382 
383  volatile uint16_t* ptrDProvControlInput;
384  volatile uint16_t* ptrDProvControlInputCompensated;
385  volatile uint16_t* ptrDProvControlError;
386  volatile uint16_t* ptrDProvControlOutput;
387 
388 } __attribute__((packed));
390 
391 
411 
412  volatile uint16_t ptrExtHookStartFunction;
413  volatile uint16_t ExtHookStartFunctionParam;
414 
415  volatile uint16_t ptrExtHookSourceFunction;
416  volatile uint16_t ExtHookSourceFunctionParam;
417 
420 
423 
424  volatile uint16_t ptrExtHookEndOfLoopFunction;
425  volatile uint16_t ExtHookEndOfLoopFunctionParam;
426 
427  volatile uint16_t ptrExtHookExitFunction;
428  volatile uint16_t ExtHookExitFunctionParam;
429 
430 } __attribute__((packed));
432 
433 
452 
453  volatile uint16_t AgcScaler;
454  volatile fractional AgcFactor;
455  volatile fractional AgcMedian;
456  volatile uint16_t ptrAgcObserverFunction;
457 
458 } __attribute__((packed));
460 
461 
475 
476  volatile uint16_t usrParam0;
477  volatile uint16_t usrParam1;
478  volatile uint16_t usrParam2;
479  volatile uint16_t usrParam3;
480  volatile uint16_t usrParam4;
481  volatile uint16_t usrParam5;
482  volatile uint16_t usrParam6;
483  volatile uint16_t usrParam7;
484 
485 } __attribute__((packed));
487 
488 
502 struct NPNZ16b_s {
503 
504  volatile struct NPNZ_STATUS_s status;
505  volatile struct NPNZ_PORTS_s Ports;
506  volatile struct NPNZ_FILTER_PARAMS_s Filter;
507  volatile struct NPNZ_GAIN_CONTROL_s GainControl;
508  volatile struct NPNZ_LIMITS_s Limits;
509  volatile struct NPNZ_ADC_TRGCTRL_s ADCTriggerControl;
510  volatile struct NPNZ_DATA_PROVIDERS_s DataProviders;
511  volatile struct NPNZ_EXTENSION_HOOKS_s ExtensionHooks;
512  volatile struct NPNZ_USER_DATA_BUFFER_s Advanced;
513 
514 } __attribute__((packed)) ;
515 typedef struct NPNZ16b_s NPNZ16b_t;
516 
517 /* ********************************************************************************/
518 
519 #endif // end of __SPECIAL_FUNCTION_LAYER_LIB_NPNZ16B_H__ header file section
520 
521 
522 //**********************************************************************************
523 // Download latest version of this tool here: https://microchip-pic-avr-tools.github.io/powersmart-dcld/
524 //**********************************************************************************
525 
Consolidated list of status bit value enumerations.
Definition: npnz16b.h:148
enum NPNZ_STATUS_FLAGS_e StatusWordFlags
List of all status and control flags of the NPNZ16b status word.
Definition: npnz16b.h:149
enum NPNZ_STATUS_SATURATION_e flagSaturation
List of all status and control flags of the NPNZ16b status word.
Definition: npnz16b.h:150
enum NPNZ_STATUS_ENABLE_e flagControlEnable
List of Supported Control Modes.
Definition: npnz16b.h:155
enum NPNZ_STATUS_SOURCE_SWAP_e flagSourceSwap
List of State Machine Operating State IDs.
Definition: npnz16b.h:152
enum NPNZ_STATUS_AGC_ENABLE_e flagAgcControl
List of all status and control flags of the NPNZ16b status word.
Definition: npnz16b.h:151
enum NPNZ_STATUS_INPUT_INV_e flagCtrlInputInversion
List of State Machine Operating State Return Values.
Definition: npnz16b.h:154
enum NPNZ_STATUS_TARGET_SWAP_e flagTargetSwap
List of State Machine Sub-State IDs.
Definition: npnz16b.h:153
Controller status word data structure allowing bit-wise access to status and control bits.
Definition: npnz16b.h:183
volatile bool enabled
Bit 15: enables/disables control loop execution.
Definition: npnz16b.h:202
struct NPNZ_STATUS_s::@132::@134 bits
Controller status bit-field for direct bit access.
volatile bool agc_enabled
Bit 11: when set, Adaptive Gain Control Modulation is enabled.
Definition: npnz16b.h:198
volatile unsigned
Bit 2: reserved.
Definition: npnz16b.h:188
volatile uint16_t value
Controller status full register access.
Definition: npnz16b.h:204
volatile bool lower_saturation_event
Bit 0: control loop is clamped at minimum output level.
Definition: npnz16b.h:186
volatile bool invert_input
Bit 14: when set, most recent error input value to controller is inverted.
Definition: npnz16b.h:201
volatile bool swap_target
Bit 12: when set, AltTarget is used as data output of controller.
Definition: npnz16b.h:199
volatile bool swap_source
Bit 13: when set, AltSource is used as data input to controller.
Definition: npnz16b.h:200
volatile bool upper_saturation_event
Bit 1: control loop is clamped at maximum output level.
Definition: npnz16b.h:187
Data structure defining parameters of a controller input or output port.
Definition: npnz16b.h:226
volatile fractional NormFactor
Q15 normalization factor.
Definition: npnz16b.h:230
volatile int16_t Offset
Value/signal offset of this port.
Definition: npnz16b.h:231
volatile uint16_t * ptrAddress
Pointer to register or variable where the value is read from (e.g. ADCBUFx) or written to (e....
Definition: npnz16b.h:228
volatile int16_t NormScaler
Bit-shift scaler of the Q15 normalization factor.
Definition: npnz16b.h:229
Data structure merging all defined controller input and output ports.
Definition: npnz16b.h:259
volatile uint16_t * ptrControlReference
Pointer to global variable of input register holding the controller reference value (e....
Definition: npnz16b.h:265
volatile struct NPNZ_PORT_s AltSource
Secondary data input port declaration.
Definition: npnz16b.h:262
volatile struct NPNZ_PORT_s AltTarget
Secondary data output port declaration.
Definition: npnz16b.h:264
volatile struct NPNZ_PORT_s Target
Primary data output port declaration.
Definition: npnz16b.h:263
volatile struct NPNZ_PORT_s Source
Primary data input port declaration.
Definition: npnz16b.h:261
Data structure for filter parameters such as pointer to history and coefficient arrays and number sca...
Definition: npnz16b.h:284
volatile uint16_t BCoefficientsArraySize
Size of the B coefficients array in X-space.
Definition: npnz16b.h:293
volatile fractional * ptrControlHistory
Pointer to n delay-line samples located in Y-space with first sample being the most recent.
Definition: npnz16b.h:288
volatile int16_t normPostShiftB
Normalization of B-term control output to Q15 (R/W)
Definition: npnz16b.h:300
volatile uint16_t ErrorHistoryArraySize
Size of the error history array in Y-space.
Definition: npnz16b.h:295
volatile int16_t PTermFactor
Q15 P-Term Coefficient Factor (R/W)
Definition: npnz16b.h:305
volatile int16_t normPostScaler
Control output normalization factor (Q15) (R/W)
Definition: npnz16b.h:301
volatile int32_t * ptrBCoefficients
Pointer to B coefficients located in X-space.
Definition: npnz16b.h:287
volatile int16_t PTermScaler
Q15 P-Term Coefficient Bit-Shift Scaler (R/W)
Definition: npnz16b.h:304
volatile int32_t * ptrACoefficients
Pointer to A coefficients located in X-space.
Definition: npnz16b.h:286
volatile uint16_t ACoefficientsArraySize
Size of the A coefficients array in X-space.
Definition: npnz16b.h:292
volatile uint16_t ControlHistoryArraySize
Size of the control history array in Y-space.
Definition: npnz16b.h:294
volatile int16_t normPostShiftA
Normalization of A-term control output to Q15 (R/W)
Definition: npnz16b.h:299
volatile int16_t normPreShift
Normalization of ADC-resolution to Q15 (R/W)
Definition: npnz16b.h:298
volatile fractional * ptrErrorHistory
Pointer to n+1 delay-line samples located in Y-space with first sample being the most recent.
Definition: npnz16b.h:289
Data strucure holding control output clamping threshold values.
Definition: npnz16b.h:327
volatile int16_t MaxOutput
Maximum output value used for clamping (R/W)
Definition: npnz16b.h:330
volatile int16_t AltMinOutput
Alternate minimum output value used for clamping (R/W)
Definition: npnz16b.h:331
volatile int16_t MinOutput
Minimum output value used for clamping (R/W)
Definition: npnz16b.h:329
volatile int16_t AltMaxOutput
Alternate maximum output value used for clamping (R/W)
Definition: npnz16b.h:332
Automatic ADC trigger placement parameters for primary ADC trigger A and secondary trigger B.
Definition: npnz16b.h:354
volatile uint16_t * ptrADCTriggerARegister
Pointer to ADC trigger #1 register (e.g. TRIG1)
Definition: npnz16b.h:356
volatile uint16_t * ptrADCTriggerBRegister
Pointer to ADC trigger #2 register (e.g. TRIG2)
Definition: npnz16b.h:358
volatile uint16_t ADCTriggerBOffset
ADC trigger #2 offset to compensate propagation delays.
Definition: npnz16b.h:359
volatile uint16_t ADCTriggerAOffset
ADC trigger #1 offset to compensate propagation delays.
Definition: npnz16b.h:357
Automated data provider pointers used to push most recent data points to user-defined variables.
Definition: npnz16b.h:381
volatile uint16_t * ptrDProvControlOutput
Pointer to external data buffer of most recent control output.
Definition: npnz16b.h:386
volatile uint16_t * ptrDProvControlInput
Pointer to external data buffer of most recent, raw control input.
Definition: npnz16b.h:383
volatile uint16_t * ptrDProvControlError
Pointer to external data buffer of most recent control error.
Definition: npnz16b.h:385
volatile uint16_t * ptrDProvControlInputCompensated
Pointer to external data buffer of most recent, compensated control input.
Definition: npnz16b.h:384
Set of function pointers and parameters used to tie in user-defined, external extension functions at ...
Definition: npnz16b.h:410
volatile uint16_t ptrExtHookPreAntiWindupFunction
Pointer to Function which will be called after the compensation filter computation is complete and be...
Definition: npnz16b.h:418
volatile uint16_t ExtHookPreTargetWriteFunctionParam
Parameter of function called (can be a variable or a pointer to a data structure)....
Definition: npnz16b.h:422
volatile uint16_t ExtHookEndOfLoopFunctionParam
Parameter of function called (can be a variable or a pointer to a data structure)....
Definition: npnz16b.h:425
volatile uint16_t ExtHookPreAntiWindupFunctionParam
Parameter of function called (can be a variable or a pointer to a data structure)....
Definition: npnz16b.h:419
volatile uint16_t ExtHookSourceFunctionParam
Parameter of function called (can be a variable or a pointer to a data structure)....
Definition: npnz16b.h:416
volatile uint16_t ptrExtHookSourceFunction
Pointer to Function which will be called after the source has been read and compensated....
Definition: npnz16b.h:415
volatile uint16_t ptrExtHookPreTargetWriteFunction
Pointer to Function which will be called before the most recent control output is written to target....
Definition: npnz16b.h:421
volatile uint16_t ptrExtHookStartFunction
Pointer to Function which will be called at the beginning of the control loop. This function pointer ...
Definition: npnz16b.h:412
volatile uint16_t ExtHookExitFunctionParam
Parameter of function called (can be a variable or a pointer to a data structure)....
Definition: npnz16b.h:428
volatile uint16_t ptrExtHookEndOfLoopFunction
Pointer to Function which is called at the end of the control loop but will be bypassed when the cont...
Definition: npnz16b.h:424
volatile uint16_t ExtHookStartFunctionParam
Parameter of function called (can be a variable or pointer to a data structure). This parameter is op...
Definition: npnz16b.h:413
volatile uint16_t ptrExtHookExitFunction
Pointer to Function which is called at the end of the control loop and will also be called when the c...
Definition: npnz16b.h:427
Data structure holding parameters required for adaptive or manual loop gain manipulation during runti...
Definition: npnz16b.h:451
volatile fractional AgcFactor
Q15 value of Adaptive Gain Modulation factor.
Definition: npnz16b.h:454
volatile fractional AgcMedian
Q15 value of Adaptive Gain Modulation nominal operating point.
Definition: npnz16b.h:455
volatile uint16_t ptrAgcObserverFunction
Function Pointer to Observer function updating the AGC modulation factor. This function pointer is st...
Definition: npnz16b.h:456
volatile uint16_t AgcScaler
Bit-shift scaler of Adaptive Gain Modulation factor.
Definition: npnz16b.h:453
Generic data buffer for undetermined use. These data buffers may be used by advanced control algorith...
Definition: npnz16b.h:474
volatile uint16_t usrParam6
generic 16-bit wide, user-defined parameter #7 for advanced control options
Definition: npnz16b.h:482
volatile uint16_t usrParam4
generic 16-bit wide, user-defined parameter #5 for advanced control options
Definition: npnz16b.h:480
volatile uint16_t usrParam0
generic 16-bit wide, user-defined parameter #1 for advanced control options
Definition: npnz16b.h:476
volatile uint16_t usrParam7
generic 16-bit wide, user-defined parameter #8 for advanced control options
Definition: npnz16b.h:483
volatile uint16_t usrParam2
generic 16-bit wide, user-defined parameter #3 for advanced control options
Definition: npnz16b.h:478
volatile uint16_t usrParam1
generic 16-bit wide, user-defined parameter #2 for advanced control options
Definition: npnz16b.h:477
volatile uint16_t usrParam3
generic 16-bit wide, user-defined parameter #4 for advanced control options
Definition: npnz16b.h:479
volatile uint16_t usrParam5
generic 16-bit wide, user-defined parameter #6 for advanced control options
Definition: npnz16b.h:481
Generic NPNZ16b Controller Object. This data structure is the main API data object providing single-p...
Definition: npnz16b.h:502
volatile struct NPNZ_STATUS_s status
Control Loop Status and Control flags.
Definition: npnz16b.h:504
volatile struct NPNZ_GAIN_CONTROL_s GainControl
Parameter section for advanced control options.
Definition: npnz16b.h:507
volatile struct NPNZ_EXTENSION_HOOKS_s ExtensionHooks
User extension function triggers using function pointers with parameters.
Definition: npnz16b.h:511
volatile struct NPNZ_LIMITS_s Limits
Input and output clamping values.
Definition: npnz16b.h:508
volatile struct NPNZ_DATA_PROVIDERS_s DataProviders
Automated data sources pushing recent data points to user-defined variables.
Definition: npnz16b.h:510
volatile struct NPNZ_ADC_TRGCTRL_s ADCTriggerControl
Automatic ADC trigger placement options for ADC Trigger A and B.
Definition: npnz16b.h:509
volatile struct NPNZ_FILTER_PARAMS_s Filter
Filter parameters such as pointer to history and coefficient arrays and number scaling.
Definition: npnz16b.h:506
volatile struct NPNZ_USER_DATA_BUFFER_s Advanced
Parameter section for advanced user control options.
Definition: npnz16b.h:512
volatile struct NPNZ_PORTS_s Ports
Controller input and output ports.
Definition: npnz16b.h:505