Digital Power Starter Kit 3 Firmware
dsPIC33C Buck Converter Voltage 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 
50  buckInstance->v_loop.controller->status.bits.enabled = false; // Disable voltage loop
51 
52  if (buckInstance->set_values.control_mode == BUCK_CONTROL_MODE_ACMC) { // In current mode...
53 
54  for (_i=0; _i<buckInstance->set_values.no_of_phases; _i++) // Reset phase current values
55  { buckInstance->i_loop[_i].controller->status.bits.enabled = false; } // Disable current loop
56 
57  }
58 
59  buckInstance->status.bits.enabled = false; // Disable Buck Converter
60  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_INITIALIZE; // Reset state machine
61 
62  return(retval);
63 }
64 
65 
82 volatile uint16_t drv_BuckConverter_Execute(volatile struct BUCK_CONVERTER_s *buckInstance)
83 {
84  volatile uint16_t retval=1;
85 
86  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
87  /* NULL POINTER PROTECTION */
88  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
89  // If no buck instance has been declared, leave here
90  if(buckInstance == NULL)
91  return(0);
92 
93  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
94  /* CAPTURE ENABLE PIN STATE IF ENABLED BY USER CODE */
95  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
96  if(buckInstance->gpio.EnableInput.enabled)
97  {
98  // Capture Enable Input pin status (1=high, 0=low)
99  uint16_t pin_state = buckGPIO_GetPinState(&buckInstance->gpio.EnableInput);
100 
101  if(!buckInstance->gpio.EnableInput.polarity)
102  // If POLARITY setting 0 = Active High (default)
103  buckInstance->status.bits.enabled = (bool)(pin_state == 1);
104  else
105  // If POLARITY setting 1 = Active Low
106  buckInstance->status.bits.enabled = (bool)(pin_state == 0);
107  }
108 
109  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
110  /* DISABLE/SUSPEND/FAULT-RESET */
111  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
112  // When enable status has changed from ENABLED to DISABLED or a fault condition
113  // is active, reset the state machine and hold it in RESET state
114  if ((!buckInstance->status.bits.enabled) || (buckInstance->status.bits.suspend) ||
115  (buckInstance->status.bits.fault_active))
116  {
117  if (!buckInstance->status.bits.ready)
118  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_INITIALIZE;
119  else
120  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_RESET;
121 
122  // Call most recent state
123  retval = BuckConverterStateMachine[buckInstance->state_id.bits.opstate_id](buckInstance);
124 
125  return((bool)(retval>0)); // Return
126  }
127  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
128 
129  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
130  /* FUNCTION CALL PROTECTION */
131  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
132  // If the state array pointer is out of range, roll over and start from first
133  // valid state
134  if(buckInstance->state_id.bits.opstate_id >= BuckStateList_size)
135  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_INITIALIZE;
136 
137  if (BuckConverterStateMachine[buckInstance->state_id.bits.opstate_id] == NULL)
138  return(0);
139  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
140 
141  // Execute state machine step
142  retval = BuckConverterStateMachine[buckInstance->state_id.bits.opstate_id](buckInstance);
143 
144  switch (retval)
145  {
146  /* If state machine state returns ERROR, switch to ERROR state in next execution cycle */
147  case BUCK_OPSRET_ERROR:
148 
149  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_INITIALIZE;
150  retval = 0;
151  break;
152 
153  /* IF state machine state signals state completion, move on to next state in line */
154  case BUCK_OPSRET_COMPLETE:
155 
156  // Increment main operating state pointer by one tick
157  buckInstance->state_id.value = (uint32_t)(buckInstance->state_id.bits.opstate_id++);
158 
159  // Check if new index is out of range, reset to RESET if so
160  if (buckInstance->state_id.bits.opstate_id >= BuckStateList_size)
161  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_RESET;
162 
163  retval = 1;
164  break;
165 
166  /* When state machine state returns REPEAT, the recent state function will be called again */
167  case BUCK_OPSRET_REPEAT:
168  // Do nothing, same state will be called next time
169  retval = 1;
170  break;
171 
172  /* When state machine state returns an unknown result, the
173  * state machine will be reset to INITIALIZE again */
174  default:
175  // In case an undefined return value has been received,
176  // REset state machine and start from scratch
177  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_INITIALIZE;
178  retval = 0;
179  break;
180  }
181 
182  return(retval);
183 }
184 
185 
197 volatile uint16_t drv_BuckConverter_Start(volatile struct BUCK_CONVERTER_s * buckInstance) {
198 
199  volatile uint16_t retval=1;
200  volatile uint16_t _i=0;
201 
202  // Disable control loops
203  buckInstance->v_loop.controller->status.bits.enabled = false; // Disable voltage loop
204  buckInstance->v_loop.ctrl_Reset(buckInstance->v_loop.controller); // Reset voltage loop histories
205 
206  if (buckInstance->set_values.control_mode == BUCK_CONTROL_MODE_ACMC) { // In current mode...
207 
208  for (_i=0; _i<buckInstance->set_values.no_of_phases; _i++)
209  {
210  buckInstance->i_loop[_i].controller->status.bits.enabled = false; // Disable current loop
211  buckInstance->i_loop[_i].ctrl_Reset(buckInstance->i_loop[_i].controller); // Reset current loop histories
212  }
213  }
214 
215  // Sequence PWM and ADC peripheral startup
216  retval &= buckPWM_Start(buckInstance); // Start PWM (All Outputs Disabled)
217  if (retval) buckInstance->status.bits.pwm_active = 1; // IF PWM startup was successful, set PWM_ACTIVE flag
218  retval &= buckADC_Start(); // Start ADC
219 
220  // Enable buck converter and reset state machine to INITIALIZE
221  buckInstance->status.bits.enabled = true; // Enable Buck Converter
222  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_INITIALIZE; // Reset state machine
223 
224  return(retval);
225 }
226 
227 
240 volatile uint16_t drv_BuckConverter_Stop(volatile struct BUCK_CONVERTER_s *buckInstance) {
241 
242  volatile uint16_t retval=1;
243  volatile uint16_t _i=0;
244 
245  // Stop PWM completely (shuts down PWM generator)
246  retval &= buckPWM_Stop(buckInstance); // Stop PWM
247 
248  buckInstance->v_loop.controller->status.bits.enabled = false; // Disable voltage loop
249 
250  if (buckInstance->set_values.control_mode == BUCK_CONTROL_MODE_ACMC) {// In current mode...
251  for (_i=0; _i<buckInstance->set_values.no_of_phases; _i++)
252  { buckInstance->i_loop[_i].controller->status.bits.enabled = false; } // Disable current loop
253  }
254 
255  buckInstance->status.bits.enabled = false; // Disable Buck Converter
256  buckInstance->state_id.value = (uint32_t)BUCK_OPSTATE_INITIALIZE; // Reset state machine
257 
258  return(retval);
259 }
260 
261 
273 volatile uint16_t drv_BuckConverter_Suspend(volatile struct BUCK_CONVERTER_s *buckInstance) {
274 
275  volatile uint16_t retval=1;
276 
277  buckInstance->status.bits.suspend = true; // Set SUSPEND bit terminating operation
278  retval &= drv_BuckConverter_Execute(buckInstance); // Enforce state switch immediately
279 
280  return(retval);
281 }
282 
283 
294 volatile uint16_t drv_BuckConverter_Resume(volatile struct BUCK_CONVERTER_s *buckInstance) {
295 
296  volatile uint16_t retval=1;
297 
298  buckInstance->status.bits.suspend = false; // Reset running state machine
299  retval &= drv_BuckConverter_Execute(buckInstance); // Enforce state switch immediately
300 
301  return(retval);
302 }
303 
304 // end of file
BuckStateList_size
volatile uint16_t BuckStateList_size
Buck converter state machine function pointer array size
Definition: dev_buck_opstates.c:39
BuckConverterStateMachine
volatile uint16_t(* BuckConverterStateMachine[])(volatile struct BUCK_CONVERTER_s *buckInstance)
Buck converter state machine function pointer array.
buckADC_ModuleInitialize
volatile uint16_t buckADC_ModuleInitialize(void)
This fucntion initializes the buck by resetting all its registers to default.
Definition: dev_buck_pconfig.c:429
BUCK_CONVERTER_s::feedback
volatile struct BUCK_FEEDBACK_SETTINGS_s feedback
BUCK converter feedback settings.
Definition: dev_buck_typedef.h:507
BUCK_CONVERTER_SETTINGS_s::control_mode
enum BUCK_CONTROL_MODE_e control_mode
Fundamental control mode.
Definition: dev_buck_typedef.h:324
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
BUCK_CONVERTER_s::status
volatile struct BUCK_CONVERTER_STATUS_s status
BUCK operation status bits.
Definition: dev_buck_typedef.h:502
BUCK_CONVERTER_STATUS_s::fault_active
volatile bool fault_active
Bit #5: Flag bit indicating system is in enforced shut down mode (usually due to a fault condition)
Definition: dev_buck_typedef.h:213
BUCK_GPIO_INSTANCE_s::polarity
volatile uint16_t polarity
Output polarity, where 0=ACTIVE HIGH, 1=ACTIVE_LOW.
Definition: dev_buck_typedef.h:475
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
BUCK_CONVERTER_STATUS_s::suspend
volatile bool suspend
Bit #6: Control bit to put the converter in suspend mode (turned off while ENABLE bit is still on)
Definition: dev_buck_typedef.h:214
NPNZ16b_s::status
volatile struct NPNZ_STATUS_s status
Control Loop Status and Control flags.
Definition: npnz16b.h:504
BUCK_FEEDBACK_SETTINGS_s::ad_vout
volatile struct BUCK_ADC_INPUT_SETTINGS_s ad_vout
ADC input sampling output voltage.
Definition: dev_buck_typedef.h:452
drv_BuckConverter_Initialize
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.
Definition: dev_buck_converter.c:30
buckADC_ChannelInitialize
volatile uint16_t buckADC_ChannelInitialize(volatile struct BUCK_ADC_INPUT_SETTINGS_s *adcInstance)
This function initializes the settings for the ADC channel.
Definition: dev_buck_pconfig.c:528
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
BUCK_STATE_ID_s::value
volatile uint32_t value
Definition: dev_buck_typedef.h:245
buckPWM_Start
volatile uint16_t buckPWM_Start(volatile struct BUCK_CONVERTER_s *buckInstance)
This function enables the buck PWM operation.
Definition: dev_buck_pconfig.c:212
BUCK_GPIO_INSTANCE_s::enabled
volatile bool enabled
Specifies, if this IO is used or not.
Definition: dev_buck_typedef.h:472
NPNZ_STATUS_s::enabled
volatile bool enabled
Bit 15: enables/disables control loop execution.
Definition: npnz16b.h:202
BUCK_CONVERTER_STATUS_s::ready
volatile bool ready
Bit #0: status bit, indicating buck converter is initialized and ready to run.
Definition: dev_buck_typedef.h:208
BUCK_FEEDBACK_SETTINGS_s::ad_vin
volatile struct BUCK_ADC_INPUT_SETTINGS_s ad_vin
ADC input sampling input voltage.
Definition: dev_buck_typedef.h:451
BUCK_GPIO_SETTINGS_s::EnableInput
volatile struct BUCK_GPIO_INSTANCE_s EnableInput
External ENABLE input.
Definition: dev_buck_typedef.h:489
buckGPIO_GetPinState
volatile bool buckGPIO_GetPinState(volatile struct BUCK_GPIO_INSTANCE_s *buckGPIOInstance)
This function gets the state of the selected pin.
Definition: dev_buck_pconfig.c:723
buckPWM_Stop
volatile uint16_t buckPWM_Stop(volatile struct BUCK_CONVERTER_s *buckInstance)
This function stops the buck PWM output.
Definition: dev_buck_pconfig.c:281
BUCK_LOOP_SETTINGS_s::ctrl_Reset
void(* ctrl_Reset)(volatile struct NPNZ16b_s *)
Function pointer to RESET routine.
Definition: dev_buck_typedef.h:356
BUCK_CONVERTER_s::gpio
volatile struct BUCK_GPIO_SETTINGS_s gpio
BUCK converter additional GPIO specification.
Definition: dev_buck_typedef.h:510
buckGPIO_Initialize
volatile uint16_t buckGPIO_Initialize(volatile struct BUCK_CONVERTER_s *buckInstance)
This function initializes the buck input pins.
Definition: dev_buck_pconfig.c:754
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
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
BUCK_CONVERTER_STATUS_s::pwm_active
volatile bool pwm_active
Bit #2: indicating that PWM has been started and ADC triggers are generated.
Definition: dev_buck_typedef.h:210
buckPWM_ModuleInitialize
volatile uint16_t buckPWM_ModuleInitialize(volatile struct BUCK_CONVERTER_s *buckInstance)
Initializes the buck PWM module by resetting its registers to default.
Definition: dev_buck_pconfig.c:54
buckADC_Start
volatile uint16_t buckADC_Start(void)
This function enables the ADC module and starts the ADC cores analog inputs for the required input si...
Definition: dev_buck_pconfig.c:622
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_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_FEEDBACK_SETTINGS_s::ad_temp
volatile struct BUCK_ADC_INPUT_SETTINGS_s ad_temp
ADC input sampling temperature.
Definition: dev_buck_typedef.h:454
BUCK_CONVERTER_s::v_loop
volatile struct BUCK_LOOP_SETTINGS_s v_loop
BUCK voltage control loop object.
Definition: dev_buck_typedef.h:512
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_CONVERTER_SETTINGS_s::no_of_phases
volatile uint16_t no_of_phases
number of converter phases
Definition: dev_buck_typedef.h:325
buckPWM_ChannelInitialize
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.
Definition: dev_buck_pconfig.c:110
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_CONVERTER_STATUS_s::enabled
volatile bool enabled
Bit #15: Control bit enabling/disabling the charger port.
Definition: dev_buck_typedef.h:224