| Digital Power Starter Kit 3 Firmware dsPIC33C Boost Converter Voltage Mode Control Example |

dev_boost_converter.c
1 /*
2 * File: dev_boost_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_boost_typedef.h"
14 #include "dev_boost_opstates.h"
15 #include "dev_boost_pconfig.h"
16
17 /*******************************************************************************
18 * @fn volatile uint16_t drv_BoostConverter_Initialize(volatile struct BOOST_CONVERTER_s *boostInstance)
19 * @ingroup lib-layer-boost-converter-functions-public
20 * @brief This function initializes all peripheral modules and their instances used by the power controller
21 * @param boostInstance Pointer to a Boost Converter data object of type struct BOOST_CONVERTER_s
22 * @return unsigned integer (0=failure, 1=success)
23 *
24 * @details
25 * This function initializes the PWM module, PWM channels, ADC channels for temperature,
26 * input voltage, output voltage and phase current. The boost IO pins are also initialize
27 * while keeping the boost converter operation disabled. The state machine is set to Initialize.
28 *********************************************************************************/
29
30 volatile uint16_t drv_BoostConverter_Initialize(volatile struct BOOST_CONVERTER_s *boostInstance)
31 {
32 volatile uint16_t retval = 1;
33 volatile uint16_t _i=0;
34
35 retval &= boostPWM_ModuleInitialize(boostInstance); // Initialize PWM Module
36 retval &= boostPWM_ChannelInitialize(boostInstance); // Initialize PWM Channel of Boost Converter
37
38 retval &= boostADC_ModuleInitialize(); // Initialize ADC Module
39
40 retval &= boostADC_ChannelInitialize(&boostInstance->feedback.ad_temp); // Initialize Temperature Channel
41 retval &= boostADC_ChannelInitialize(&boostInstance->feedback.ad_vin); // Initialize Input Voltage Channel
42 retval &= boostADC_ChannelInitialize(&boostInstance->feedback.ad_vout); // Initialize Output Voltage Channel
43
44 for (_i=0; _i<boostInstance->set_values.no_of_phases; _i++) // Reset phase current values
45 retval &= boostADC_ChannelInitialize(&boostInstance->feedback.ad_isns[_i]); // Initialize Phase Current Channel
46
47 retval &= boostGPIO_Initialize(boostInstance); // Initialize additional control IOs
48
49
50 boostInstance->v_loop.controller->status.bits.enabled = false; // Disable voltage loop
51
52 if (boostInstance->set_values.control_mode == BOOST_CONTROL_MODE_ACMC) { // In current mode...
53
54 for (_i=0; _i<boostInstance->set_values.no_of_phases; _i++) // Reset phase current values
55 { boostInstance->i_loop[_i].controller->status.bits.enabled = false; } // Disable current loop
56
57 }
58
59 boostInstance->status.bits.enabled = false; // Disable Boost Converter
60 boostInstance->state_id.value = (uint32_t)BOOST_OPSTATE_INITIALIZE; // Reset state machine
61
62 return(retval);
63 }
64
65 /*******************************************************************************
66 * @fn volatile uint16_t drv_BoostConverter_Execute(volatile struct BOOST_CONVERTER_s * boostInstance)
67 * @ingroup lib-layer-boost-converter-functions-public
68 * @brief This function is the main boost converter state machine executing the most recent state
69 * @param boostInstance Pointer to a Boost Converter data object of type struct BOOST_CONVERTER_s
70 * @return 0 = BOOST_OPSRET_REPEAT
71 * @return 1 = BOOST_OPSRET_COMPLETE
72 * @return 2 = BOOST_OPSRET_REPEAT
73 *
74 * @details
75 * This function performs tasks in the state machine.
76 * - If state machine state returns ERROR, switch to ERROR state in next execution cycle
77 * - If state machine state signals state completion, move on to next state in line
78 * - When state machine state returns REPEAT, the recent state function will be called again
79 * - When state machine state returns an unknown result, the state machine will be reset to INITIALIZE again
80 *********************************************************************************/
81
82 volatile uint16_t drv_BoostConverter_Execute(volatile struct BOOST_CONVERTER_s *boostInstance)
83 {
84 volatile uint16_t retval=1;
85
86 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
87 /* NULL POINTER PROTECTION */
88 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
89 // If no boost instance has been declared, leave here
90 if(boostInstance == NULL)
91 return(0);
92
93 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
94 /* CAPTURE ENABLE PIN STATE IF ENABLED BY USER CODE */
95 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
96 if(boostInstance->gpio.EnableInput.enabled)
97 {
98 // Capture Enable Input pin status (1=high, 0=low)
99 uint16_t pin_state = boostGPIO_GetPinState(&boostInstance->gpio.EnableInput);
100
101 if(!boostInstance->gpio.EnableInput.polarity)
102 // If POLARITY setting 0 = Active High (default)
103 boostInstance->status.bits.enabled = (bool)(pin_state == 1);
104 else
105 // If POLARITY setting 1 = Active Low
106 boostInstance->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 ((!boostInstance->status.bits.enabled) || (boostInstance->status.bits.suspend) ||
115 (boostInstance->status.bits.fault_active))
116 {
117 if (!boostInstance->status.bits.ready)
118 boostInstance->state_id.value = (uint32_t)BOOST_OPSTATE_INITIALIZE;
119 else
120 boostInstance->state_id.value = (uint32_t)BOOST_OPSTATE_RESET;
121
122 // Call most recent state
123 retval = BoostConverterStateMachine[boostInstance->state_id.bits.opstate_id](boostInstance);
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(boostInstance->state_id.bits.opstate_id >= BoostStateList_size)
135 boostInstance->state_id.value = (uint32_t)BOOST_OPSTATE_INITIALIZE;
136
137 if (BoostConverterStateMachine[boostInstance->state_id.bits.opstate_id] == NULL)
138 return(0);
139 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
140
141 // Execute state machine step
142 retval = BoostConverterStateMachine[boostInstance->state_id.bits.opstate_id](boostInstance);
143
144 switch (retval)
145 {
146 /* If state machine state returns ERROR, switch to ERROR state in next execution cycle */
147 case BOOST_OPSRET_ERROR:
148
149 boostInstance->state_id.value = (uint32_t)BOOST_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 BOOST_OPSRET_COMPLETE:
155
156 // Increment main operating state pointer by one tick
157 boostInstance->state_id.value = (uint32_t)(boostInstance->state_id.bits.opstate_id++);
158
159 // Check if new index is out of range, reset to RESET if so
160 if (boostInstance->state_id.bits.opstate_id >= BoostStateList_size)
161 boostInstance->state_id.value = (uint32_t)BOOST_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 BOOST_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 boostInstance->state_id.value = (uint32_t)BOOST_OPSTATE_INITIALIZE;
178 retval = 0;
179 break;
180 }
181
182 return(retval);
183 }
184
185 /*******************************************************************************
186 * @fn volatile uint16_t drv_BoostConverter_Start(volatile struct BOOST_CONVERTER_s * boostInstance)
187 * @ingroup lib-layer-boost-converter-functions-public
188 * @brief This function starts the boost converter
189 * @param boostInstance Pointer to a Boost Converter data object of type struct BOOST_CONVERTER_s
190 * @return unsigned integer (0=failure, 1=success)
191 *
192 * @details
193 * This function starts the boost converter operation by enabling the the boost PWM and ADC peripherals,
194 * enabling the boost converter and reseting the state machine to Initialize.
195 *********************************************************************************/
196
197 volatile uint16_t drv_BoostConverter_Start(volatile struct BOOST_CONVERTER_s * boostInstance) {
198
199 volatile uint16_t retval=1;
200 volatile uint16_t _i=0;
201
202 // Disable control loops
203 boostInstance->v_loop.controller->status.bits.enabled = false; // Disable voltage loop
204 boostInstance->v_loop.ctrl_Reset(boostInstance->v_loop.controller); // Reset voltage loop histories
205
206 if (boostInstance->set_values.control_mode == BOOST_CONTROL_MODE_ACMC) { // In current mode...
207
208 for (_i=0; _i<boostInstance->set_values.no_of_phases; _i++)
209 {
210 boostInstance->i_loop[_i].controller->status.bits.enabled = false; // Disable current loop
211 boostInstance->i_loop[_i].ctrl_Reset(boostInstance->i_loop[_i].controller); // Reset current loop histories
212 }
213 }
214
215 // Sequence PWM and ADC peripheral startup
216 retval &= boostPWM_Start(boostInstance); // Start PWM (All Outputs Disabled)
217 if (retval) boostInstance->status.bits.pwm_active = 1; // IF PWM startup was successful, set PWM_ACTIVE flag
218 retval &= boostADC_Start(); // Start ADC
219
220 // Enable boost converter and reset state machine to INITIALIZE
221 boostInstance->status.bits.enabled = true; // Enable Boost Converter
222 boostInstance->state_id.value = (uint32_t)BOOST_OPSTATE_INITIALIZE; // Reset state machine
223
224 return(retval);
225 }
226
227 /*******************************************************************************
228 * @fn volatile uint16_t drv_BoostConverter_Stop(volatile struct BOOST_CONVERTER_s * boostInstance)
229 * @ingroup lib-layer-boost-converter-functions-public
230 * @brief This function stop the boost converter opration
231 * @param boostInstance Pointer to a Boost Converter data object of type struct BOOST_CONVERTER_s
232 * @return unsigned integer (0=failure, 1=success)
233 *
234 * @details
235 * This function stops the boost converter operation by shutting down the PWM generator,
236 * disabling the voltage/current loop and reset the state machine to Initialize.
237 *
238 *********************************************************************************/
239
240 volatile uint16_t drv_BoostConverter_Stop(volatile struct BOOST_CONVERTER_s *boostInstance) {
241
242 volatile uint16_t retval=1;
243 volatile uint16_t _i=0;
244
245 // Stop PWM completely (shuts down PWM generator)
246 retval &= boostPWM_Stop(boostInstance); // Stop PWM
247
248 boostInstance->v_loop.controller->status.bits.enabled = false; // Disable voltage loop
249
250 if (boostInstance->set_values.control_mode == BOOST_CONTROL_MODE_ACMC) {// In current mode...
251 for (_i=0; _i<boostInstance->set_values.no_of_phases; _i++)
252 { boostInstance->i_loop[_i].controller->status.bits.enabled = false; } // Disable current loop
253 }
254
255 boostInstance->status.bits.enabled = false; // Disable Boost Converter
256 boostInstance->state_id.value = (uint32_t)BOOST_OPSTATE_INITIALIZE; // Reset state machine
257
258 return(retval);
259 }
260
261 /*******************************************************************************
262 * @ingroup lib-layer-boost-converter-functions-public
263 * @fn volatile uint16_t drv_BoostConverter_Suspend(volatile struct BOOST_CONVERTER_s * boostInstance)
264 * @brief This function suspends the operation of the boost converter
265 * @param boostInstance Pointer to a Boost Converter data object of type struct BOOST_CONVERTER_s
266 * @return unsigned integer (0=failure, 1=success)
267 *
268 * @details
269 * This function sets the suspend bit terminating operation. This bit will be evaluated in the
270 * State machine tasks which eventually shuts down the operation of the boost converter.
271 *********************************************************************************/
272
273 volatile uint16_t drv_BoostConverter_Suspend(volatile struct BOOST_CONVERTER_s *boostInstance) {
274
275 volatile uint16_t retval=1;
276
277 boostInstance->status.bits.suspend = true; // Set SUSPEND bit terminating operation
278 retval &= drv_BoostConverter_Execute(boostInstance); // Enforce state switch immediately
279
280 return(retval);
281 }
282
283 /*******************************************************************************
284 * @fn volatile uint16_t drv_BoostConverter_Resume(volatile struct BOOST_CONVERTER_s * boostInstance)
285 * @ingroup lib-layer-boost-converter-functions-public
286 * @brief This function resume the operation of the boost converter
287 * @param boostInstance Pointer to a Boost Converter data object of type struct BOOST_CONVERTER_s
288 * @return unsigned integer (0=failure, 1=success)
289 *
290 * @details
291 * This function executes the latest task in the state machine enforcing state switch immediately.
292 *********************************************************************************/
293
294 volatile uint16_t drv_BoostConverter_Resume(volatile struct BOOST_CONVERTER_s *boostInstance) {
295
296 volatile uint16_t retval=1;
297
298 boostInstance->status.bits.suspend = false; // Reset running state machine
299 retval &= drv_BoostConverter_Execute(boostInstance); // Enforce state switch immediately
300
301 return(retval);
302 }
303
304 // end of file
BOOST_CONVERTER_SETTINGS_s::control_mode
enum BOOST_CONTROL_MODE_e control_mode
Fundamental control mode.
Definition: dev_boost_typedef.h:350
BOOST_LOOP_SETTINGS_s::controller
volatile struct NPNZ16b_s * controller
pointer to control loop object data structure
Definition: dev_boost_typedef.h:380
BOOST_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_boost_typedef.h:214
BOOST_FEEDBACK_SETTINGS_s::ad_vout
volatile struct BOOST_ADC_INPUT_SETTINGS_s ad_vout
ADC input sampling output voltage.
Definition: dev_boost_typedef.h:479
BOOST_CONVERTER_s::v_loop
volatile struct BOOST_LOOP_SETTINGS_s v_loop
BOOST voltage control loop object.
Definition: dev_boost_typedef.h:539
BOOST_CONVERTER_SETTINGS_s::no_of_phases
volatile uint16_t no_of_phases
number of converter phases
Definition: dev_boost_typedef.h:351
BOOST_STATE_ID_s::value
volatile uint32_t value
Definition: dev_boost_typedef.h:246
BOOST_FEEDBACK_SETTINGS_s::ad_temp
volatile struct BOOST_ADC_INPUT_SETTINGS_s ad_temp
ADC input sampling temperature.
Definition: dev_boost_typedef.h:481
BOOST_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_boost_typedef.h:215
BOOST_CONVERTER_STATUS_s::enabled
volatile bool enabled
Bit #15: Control bit enabling/disabling the charger port.
Definition: dev_boost_typedef.h:225
BOOST_LOOP_SETTINGS_s::ctrl_Reset
void(* ctrl_Reset)(volatile struct NPNZ16b_s *)
Function pointer to RESET routine.
Definition: dev_boost_typedef.h:383
BOOST_GPIO_INSTANCE_s::enabled
volatile bool enabled
Specifies, if this IO is used or not.
Definition: dev_boost_typedef.h:499
BOOST_CONVERTER_STATUS_s::ready
volatile bool ready
Bit #0: status bit, indicating boost converter is initialized and ready to run.
Definition: dev_boost_typedef.h:209
BOOST_FEEDBACK_SETTINGS_s::ad_vin
volatile struct BOOST_ADC_INPUT_SETTINGS_s ad_vin
ADC input sampling input voltage.
Definition: dev_boost_typedef.h:478
BOOST_CONVERTER_STATUS_s::pwm_active
volatile bool pwm_active
Bit #2: indicating that PWM has been started and ADC triggers are generated.
Definition: dev_boost_typedef.h:211
BOOST_STATE_ID_s::bits
struct BOOST_STATE_ID_s::@372::@373 bits
BOOST_CONVERTER_s::i_loop
volatile struct BOOST_LOOP_SETTINGS_s i_loop[BOOST_MPHASE_COUNT]
BOOST Current control loop objects.
Definition: dev_boost_typedef.h:540
NPNZ16b_s::status
volatile struct NPNZ_STATUS_s status
Control Loop Status and Control flags.
Definition: npnz16b.h:504
BOOST_GPIO_INSTANCE_s::polarity
volatile uint16_t polarity
Output polarity, where 0=ACTIVE HIGH, 1=ACTIVE_LOW.
Definition: dev_boost_typedef.h:502
BOOST_CONVERTER_s::feedback
volatile struct BOOST_FEEDBACK_SETTINGS_s feedback
BOOST converter feedback settings.
Definition: dev_boost_typedef.h:534
BOOST_CONVERTER_s::status
volatile struct BOOST_CONVERTER_STATUS_s status
BOOST operation status bits.
Definition: dev_boost_typedef.h:529
BOOST_CONVERTER_s::gpio
volatile struct BOOST_GPIO_SETTINGS_s gpio
BOOST converter additional GPIO specification.
Definition: dev_boost_typedef.h:537
BOOST_CONVERTER_s::state_id
volatile struct BOOST_STATE_ID_s state_id
BOOST state machine operating state ID.
Definition: dev_boost_typedef.h:530
BOOST_GPIO_SETTINGS_s::EnableInput
volatile struct BOOST_GPIO_INSTANCE_s EnableInput
External ENABLE input.
Definition: dev_boost_typedef.h:516
BOOST_CONVERTER_s
BOOST control & monitoring data structure.
Definition: dev_boost_typedef.h:527
BOOST_FEEDBACK_SETTINGS_s::ad_isns
volatile struct BOOST_ADC_INPUT_SETTINGS_s ad_isns[BOOST_MPHASE_COUNT]
ADC input sampling phase current.
Definition: dev_boost_typedef.h:480
BOOST_CONVERTER_s::set_values
volatile struct BOOST_CONVERTER_SETTINGS_s set_values
Control field for global access to references.
Definition: dev_boost_typedef.h:532
NPNZ_STATUS_s::enabled
volatile bool enabled
Bit 15: enables/disables control loop execution.
Definition: npnz16b.h:202