Digital Power Starter Kit 3 Firmware  DM330017-3, Rev.3.0
dsPIC33C Buck Converter Peak Current Mode Control Example
dev_buck_converter.c
1 /*
2  * File: dev_buck_converter.c
3  * Author: M91406
4  *
5  * Created on July 9, 2019, 1:10 PM
6  */
7 
8 
9 #include <xc.h> // include processor files - each processor file is guarded.
10 #include <stdint.h> // include standard integer types header file
11 #include <stdbool.h> // include standard boolean types header file
12 
13 #include "dev_buck_typedef.h"
14 #include "dev_buck_opstates.h"
15 #include "dev_buck_pconfig.h"
16 
17 
30 volatile uint16_t drv_BuckConverter_Initialize(volatile struct BUCK_CONVERTER_s *buckInstance)
31 {
32  volatile uint16_t retval = 1;
33  volatile uint16_t _i=0;
34 
35  retval &= buckPWM_ModuleInitialize(buckInstance); // Initialize PWM Module
36  retval &= buckPWM_ChannelInitialize(buckInstance); // Initialize PWM Channel of Buck Converter
37 
38  retval &= buckADC_ModuleInitialize(); // Initialize ADC Module
39 
40  retval &= buckADC_ChannelInitialize(&buckInstance->feedback.ad_temp); // Initialize Temperature Channel
41  retval &= buckADC_ChannelInitialize(&buckInstance->feedback.ad_vin); // Initialize Input Voltage Channel
42  retval &= buckADC_ChannelInitialize(&buckInstance->feedback.ad_vout); // Initialize Output Voltage Channel
43 
44  for (_i=0; _i<buckInstance->set_values.no_of_phases; _i++) // Reset phase current values
45  retval &= buckADC_ChannelInitialize(&buckInstance->feedback.ad_isns[_i]); // Initialize Phase Current Channel
46 
47  retval &= buckGPIO_Initialize(buckInstance); // Initialize additional control IOs
48 
49  // Enable voltage loop controller
50  buckInstance->v_loop.controller->status.bits.enabled = false; // Disable voltage loop
51 
52  // Enabling inner control loops (current mode control only)
53  if (buckInstance->set_values.control_mode == BUCK_CONTROL_MODE_ACMC) { // In current mode...
54 
55  for (_i=0; _i<buckInstance->set_values.no_of_phases; _i++) // Reset phase current values
56  { buckInstance->i_loop[_i].controller->status.bits.enabled = false; } // Disable current loop
57 
58  }
59  else if (buckInstance->set_values.control_mode == BUCK_CONTROL_MODE_PCMC) { // In peak current mode...
60  /* do nothing */
61  }
62 
63  buckInstance->status.bits.enabled = false; // Disable Buck Converter
64  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_INITIALIZE; // Reset state machine
65 
66  return(retval);
67 }
68 
69 
86 volatile uint16_t drv_BuckConverter_Execute(volatile struct BUCK_CONVERTER_s *buckInstance)
87 {
88  volatile uint16_t retval=1;
89 
90  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
91  /* NULL POINTER PROTECTION */
92  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
93  // If no buck instance has been declared, leave here
94  if(buckInstance == NULL)
95  return(0);
96 
97  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
98  /* CAPTURE ENABLE PIN STATE IF ENABLED BY USER CODE */
99  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
100  if(buckInstance->gpio.EnableInput.enabled)
101  {
102  // Capture Enable Input pin status (1=high, 0=low)
103  uint16_t pin_state = buckGPIO_GetPinState(&buckInstance->gpio.EnableInput);
104 
105  if(!buckInstance->gpio.EnableInput.polarity)
106  // If POLARITY setting 0 = Active High (default)
107  buckInstance->status.bits.enabled = (bool)(pin_state == 1);
108  else
109  // If POLARITY setting 1 = Active Low
110  buckInstance->status.bits.enabled = (bool)(pin_state == 0);
111  }
112 
113  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
114  /* DISABLE/SUSPEND/FAULT-RESET */
115  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
116  // When enable status has changed from ENABLED to DISABLED or a fault condition
117  // is active, reset the state machine and hold it in RESET state
118  if ((!buckInstance->status.bits.enabled) || (buckInstance->status.bits.suspend) ||
119  (buckInstance->status.bits.fault_active))
120  {
121  if (!buckInstance->status.bits.ready)
122  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_INITIALIZE;
123  else
124  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_RESET;
125 
126  // Call most recent state
127  retval = BuckConverterStateMachine[buckInstance->state_id.bits.opstate_id](buckInstance);
128 
129  return((bool)(retval>0)); // Return
130  }
131  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
132 
133  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
134  /* FUNCTION CALL PROTECTION */
135  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
136  // If the state array pointer is out of range, roll over and start from first
137  // valid state
138  if(buckInstance->state_id.bits.opstate_id >= BuckStateList_size)
139  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_INITIALIZE;
140 
141  if (BuckConverterStateMachine[buckInstance->state_id.bits.opstate_id] == NULL)
142  return(0);
143  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
144 
145  // Execute state machine step
146  retval = BuckConverterStateMachine[buckInstance->state_id.bits.opstate_id](buckInstance);
147 
148  switch (retval)
149  {
150  /* If state machine state returns ERROR, switch to ERROR state in next execution cycle */
151  case BUCK_OPSRET_ERROR:
152 
153  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_INITIALIZE;
154  retval = 0;
155  break;
156 
157  /* IF state machine state signals state completion, move on to next state in line */
158  case BUCK_OPSRET_COMPLETE:
159 
160  // Increment main operating state pointer by one tick
161  buckInstance->state_id.value = (uint32_t)(buckInstance->state_id.bits.opstate_id++);
162 
163  // Check if new index is out of range, reset to RESET if so
164  if (buckInstance->state_id.bits.opstate_id >= BuckStateList_size)
165  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_RESET;
166 
167  retval = 1;
168  break;
169 
170  /* When state machine state returns REPEAT, the recent state function will be called again */
171  case BUCK_OPSRET_REPEAT:
172  // Do nothing, same state will be called next time
173  retval = 1;
174  break;
175 
176  /* When state machine state returns an unknown result, the
177  * state machine will be reset to INITIALIZE again */
178  default:
179  // In case an undefined return value has been received,
180  // REset state machine and start from scratch
181  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_INITIALIZE;
182  retval = 0;
183  break;
184  }
185 
186  return(retval);
187 }
188 
189 
201 volatile uint16_t drv_BuckConverter_Start(volatile struct BUCK_CONVERTER_s * buckInstance) {
202 
203  volatile uint16_t retval=1;
204  volatile uint16_t _i=0;
205 
206  // Disable control loops
207  buckInstance->v_loop.controller->status.bits.enabled = false; // Disable voltage loop
208  buckInstance->v_loop.ctrl_Reset(buckInstance->v_loop.controller); // Reset voltage loop histories
209 
210  if (buckInstance->set_values.control_mode == BUCK_CONTROL_MODE_ACMC) { // In current mode...
211 
212  for (_i=0; _i<buckInstance->set_values.no_of_phases; _i++)
213  {
214  buckInstance->i_loop[_i].controller->status.bits.enabled = false; // Disable current loop
215  buckInstance->i_loop[_i].ctrl_Reset(buckInstance->i_loop[_i].controller); // Reset current loop histories
216  }
217  }
218 
219  // Sequence PWM, DAC and ADC peripheral startup
220  retval &= buckPWM_Start(buckInstance); // Start PWM (All Outputs Disabled)
221  if (retval) buckInstance->status.bits.pwm_active = 1; // IF PWM startup was successful, set PWM_ACTIVE flag
222  if (buckInstance->set_values.control_mode == BUCK_CONTROL_MODE_PCMC)
223  { retval &= buckDAC_Start(buckInstance); } // Enable analog comparator + DAC instances
224  retval &= buckADC_Start(); // Start ADC
225 
226  // Enable buck converter and reset state machine to INITIALIZE
227  buckInstance->status.bits.enabled = true; // Enable Buck Converter
228  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_INITIALIZE; // Reset state machine
229 
230  return(retval);
231 }
232 
233 
246 volatile uint16_t drv_BuckConverter_Stop(volatile struct BUCK_CONVERTER_s *buckInstance) {
247 
248  volatile uint16_t retval=1;
249  volatile uint16_t _i=0;
250 
251  // Stop PWM completely (shuts down PWM generator)
252  retval &= buckPWM_Stop(buckInstance); // Stop PWM
253 
254  buckInstance->v_loop.controller->status.bits.enabled = false; // Disable voltage loop
255 
256  if (buckInstance->set_values.control_mode == BUCK_CONTROL_MODE_ACMC) {// In current mode...
257  for (_i=0; _i<buckInstance->set_values.no_of_phases; _i++)
258  { buckInstance->i_loop[_i].controller->status.bits.enabled = false; } // Disable current loop
259  }
260 
261  buckInstance->status.bits.enabled = false; // Disable Buck Converter
262  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_INITIALIZE; // Reset state machine
263 
264  return(retval);
265 }
266 
267 
279 volatile uint16_t drv_BuckConverter_Suspend(volatile struct BUCK_CONVERTER_s *buckInstance) {
280 
281  volatile uint16_t retval=1;
282 
283  buckInstance->status.bits.suspend = true; // Set SUSPEND bit terminating operation
284  retval &= drv_BuckConverter_Execute(buckInstance); // Enforce state switch immediately
285 
286  return(retval);
287 }
288 
289 
300 volatile uint16_t drv_BuckConverter_Resume(volatile struct BUCK_CONVERTER_s *buckInstance) {
301 
302  volatile uint16_t retval=1;
303 
304  buckInstance->status.bits.suspend = false; // Reset running state machine
305  retval &= drv_BuckConverter_Execute(buckInstance); // Enforce state switch immediately
306 
307  return(retval);
308 }
309 
310 // end of file
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 uint16_t drv_BuckConverter_Initialize(volatile struct BUCK_CONVERTER_s *buckInstance)
This function initializes all peripheral modules and their instances used by the power controller.
volatile uint16_t(* BuckConverterStateMachine[])(volatile struct BUCK_CONVERTER_s *buckInstance)
Function pointer array defining the state machine execution sequence
volatile uint16_t BuckStateList_size
Buck converter state machine function pointer array size
volatile uint16_t buckADC_ModuleInitialize(void)
This fucntion initializes the buck by resetting all its registers to default.
volatile uint16_t buckPWM_ChannelInitialize(volatile struct BUCK_CONVERTER_s *buckInstance)
This function initializes the output pins for the PWM output and the default buck PWM settings.
volatile uint16_t buckPWM_ModuleInitialize(volatile struct BUCK_CONVERTER_s *buckInstance)
Initializes the buck PWM module by resetting its registers to default.
volatile bool buckGPIO_GetPinState(volatile struct BUCK_GPIO_INSTANCE_s *buckGPIOInstance)
This function gets the state of the selected pin.
volatile uint16_t buckADC_Start(void)
This function enables the ADC module and starts the ADC cores analog inputs for the required input si...
volatile uint16_t buckPWM_Stop(volatile struct BUCK_CONVERTER_s *buckInstance)
This function stops the buck PWM output.
volatile uint16_t buckADC_ChannelInitialize(volatile struct BUCK_ADC_INPUT_SETTINGS_s *adcInstance)
This function initializes the settings for the ADC channel.
volatile uint16_t buckPWM_Start(volatile struct BUCK_CONVERTER_s *buckInstance)
This function enables the buck PWM operation.
volatile uint16_t buckGPIO_Initialize(volatile struct BUCK_CONVERTER_s *buckInstance)
This function initializes the buck input pins.
struct BUCK_CONVERTER_STATUS_s::@126::@128 bits
data structure for single bit addressing operations
struct BUCK_STATE_ID_s::@129::@131 bits
volatile uint32_t value
enum BUCK_CONTROL_MODE_e control_mode
Fundamental control mode.
volatile uint16_t no_of_phases
number of converter phases
volatile struct NPNZ16b_s * controller
pointer to control loop object data structure
void(* ctrl_Reset)(volatile struct NPNZ16b_s *)
Function pointer to RESET routine.
volatile struct BUCK_ADC_INPUT_SETTINGS_s ad_vin
ADC input sampling input voltage.
volatile struct BUCK_ADC_INPUT_SETTINGS_s ad_isns[BUCK_NO_OF_PHASES]
ADC input sampling phase current.
volatile struct BUCK_ADC_INPUT_SETTINGS_s ad_temp
ADC input sampling temperature.
volatile struct BUCK_ADC_INPUT_SETTINGS_s ad_vout
ADC input sampling output voltage.
volatile bool enabled
Specifies, if this IO is used or not.
volatile uint16_t polarity
Output polarity, where 0=ACTIVE HIGH, 1=ACTIVE_LOW.
volatile struct BUCK_GPIO_INSTANCE_s EnableInput
External ENABLE input.
BUCK control & monitoring data structure.
volatile struct BUCK_LOOP_SETTINGS_s i_loop[BUCK_NO_OF_PHASES]
BUCK Current control loop objects.
volatile struct BUCK_STATE_ID_s state_id
BUCK state machine operating state ID.
volatile struct BUCK_CONVERTER_STATUS_s status
BUCK operation status bits.
volatile struct BUCK_FEEDBACK_SETTINGS_s feedback
BUCK converter feedback settings.
volatile struct BUCK_GPIO_SETTINGS_s gpio
BUCK converter additional GPIO specification.
volatile struct BUCK_LOOP_SETTINGS_s v_loop
BUCK voltage control loop object.
volatile struct BUCK_CONVERTER_SETTINGS_s set_values
Control field for global access to references.
struct NPNZ_STATUS_s::@132::@134 bits
Controller status bit-field for direct bit access.
volatile struct NPNZ_STATUS_s status
Control Loop Status and Control flags.
Definition: npnz16b.h:504