Digital Power Starter Kit 3 Firmware
dsPIC33C Buck Converter Voltage Mode Control Example
main.c
1 /*
2  * File: main.c
3  * Author: M91406
4  *
5  * Created on July 8, 2019, 1:52 PM
6  */
7 
8 #include <xc.h> // include processor files - each processor file is guarded.
9 #include <stdint.h> // include standard integer data types
10 #include <stdbool.h> // include standard boolean data types
11 
12 #include "main.h" // include main header
13 
14 // Task managing variables
15 volatile bool run_main = true; // Flag allowing to terminate the main loop and restart the CPU
16 
17 #define TMR1_TIMEOUT 30000 // Timeout protection for Timer1 interrupt flag bit
18 volatile bool LOW_PRIORITY_GO = false; // Flag allowing low priority tasks to be executed
19 
20 /* PRIVATE FUNCTION CALL PROTOTYPES */
21 volatile uint16_t sysLowPriorityTasks_Execute(void);
22 volatile uint16_t __attribute__((always_inline)) sysHighPriorityTasks_Execute(void);
23 
24 
25 
80 int main(void) {
81 
82  volatile uint16_t retval = 1;
83  volatile uint16_t timeout = 0;
84 
85  // Initialize basic system configuration
86  retval &= SYSTEM_Initialize();
87 
88  // Initialize special, application-specific peripherals
90 
91  // Initialize software modules
92  retval &= sysUserTasks_Initialize();
93 
94  // Enable OS Timer
95  retval &= sysOsTimer_Enable(true, _OSTIMER_PRIORITY);
96 
97  // Last line of defense:
98  // when configuration of any block failed...
99  if (!retval)
100  CPU_RESET(); // reset the CPU and try again
101 
102 
103  // Main program execution
104  while (run_main) {
105 
106  // wait for timer1 to overrun
107  while ((!LOW_PRIORITY_GO) && (timeout++ < TMR1_TIMEOUT));
108  LOW_PRIORITY_GO = false;
109  timeout = 0; // Reset timeout counter
110 
111  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112  // Execute non-time critical, low-priority tasks
113 
114  retval &= sysLowPriorityTasks_Execute();
115 
116  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
117 
118  }
119 
120  CPU_RESET(); // if the firmware ever ends up here, reset the CPU
121 
122  return (0);
123 }
124 
125 
142 volatile uint16_t sysLowPriorityTasks_Execute(void)
143 {
144  volatile uint16_t retval=1;
145 
146  // Execute non-time critical, low-priority tasks
147  retval &= appLCD_Execute();
148  retval &= appLED_Execute();
149  retval &= appPushButton_Execute();
150 
151  return(retval);
152 }
153 
154 
172 volatile uint16_t sysHighPriorityTasks_Execute(void)
173 {
174  volatile uint16_t retval=1;
175 
176  // Execute high priority, time critical tasks
177  retval &= appPowerSupply_Execute(); // Execute power supply state machine
178  retval &= appFaultMonitor_Execute(); // Execute fault handler
179 
180  return(retval);
181 }
182 
183 
184 
194 void __attribute__((__interrupt__, context, no_auto_psv)) _OsTimerInterrupt(void)
195 {
196  volatile uint16_t retval=1;
197 
198  #if (DBGPIN1_ENABLE)
199  DBGPIN1_Set(); // Set the CPU debugging pin HIGH
200  #endif
201 
202  retval &= sysHighPriorityTasks_Execute(); // Execute list of high priority tasks
203 
204  LOW_PRIORITY_GO = true; // Set GO trigger for low priority tasks
205  _OSTIMER_IF = 0; // Reset the interrupt flag bit
206 
207  #if (DBGPIN1_ENABLE)
208  DBGPIN1_Clear(); // Clear the CPU debugging pin
209  #endif
210 
211  return;
212 }
213 
214 // ______________________________________
215 // end of file
216 
217 
218 
sysUserTasks_Initialize
volatile uint16_t sysUserTasks_Initialize(void)
Initializes the user-defined tasks.
Definition: system.c:102
_OSTIMER_IF
#define _OSTIMER_IF
interrupt flag bit
Definition: dpsk3_hwdescr.h:177
appPushButton_Execute
volatile uint16_t appPushButton_Execute(void)
Executes the USER push button monitor.
Definition: app_push_button.c:81
sysLowPriorityTasks_Execute
volatile uint16_t sysLowPriorityTasks_Execute(void)
Low priority task sequence executed after the high priority task sequence execution is complete.
Definition: main.c:142
sysOsTimer_Enable
volatile uint16_t sysOsTimer_Enable(volatile bool interrupt_enable, volatile uint8_t interrupt_priority)
Definition: init_timer1.c:81
DBGPIN1_Set
#define DBGPIN1_Set()
Macro instruction to set a pin state to logic HIGH.
Definition: dpsk3_hwdescr.h:199
_OSTIMER_PRIORITY
#define _OSTIMER_PRIORITY
interrupt priority (1 ... 7, default = 2)
Definition: dpsk3_hwdescr.h:178
_OsTimerInterrupt
#define _OsTimerInterrupt
Global state-machine peripheral assignments.
Definition: dpsk3_hwdescr.h:174
appLCD_Execute
volatile uint16_t appLCD_Execute(void)
Refreshes the LC display.
Definition: app_lcd.c:120
appLED_Execute
volatile uint16_t appLED_Execute(void)
Executes the debugging LED driver.
Definition: app_led.c:53
SYSTEM_Initialize
volatile uint16_t SYSTEM_Initialize(void)
Initializes essential chip resources.
Definition: system.c:28
appPowerSupply_Execute
volatile uint16_t appPowerSupply_Execute(void)
This is the top-level function call triggering the most recent state machine of all associated power ...
Definition: app_power_control.c:63
sysUserPeriperhals_Initialize
volatile uint16_t sysUserPeriperhals_Initialize(void)
Initializes the user-defined chip resources.
Definition: system.c:57
appFaultMonitor_Execute
volatile uint16_t appFaultMonitor_Execute(void)
Application wide fault object monitoring routine.
Definition: app_fault_monitor.c:56
sysHighPriorityTasks_Execute
volatile uint16_t sysHighPriorityTasks_Execute(void)
High priority task sequence executed at a fixed repetition frequency.
Definition: main.c:172
DBGPIN1_Clear
#define DBGPIN1_Clear()
Macro instruction to set a pin state to logic LOW.
Definition: dpsk3_hwdescr.h:200