Digital Power Starter Kit 3 Firmware  DM330017-3, Rev.3.0
dsPIC33C Buck Converter Peak Current 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 
195 void __attribute__((__interrupt__, context, no_auto_psv)) _OsTimerInterrupt(void)
196 {
197  volatile uint16_t retval=1;
198 
199  #if (DBGPIN1_ENABLE)
200  DBGPIN1_Set(); // Set the CPU debugging pin HIGH
201  #endif
202 
203  retval &= sysHighPriorityTasks_Execute(); // Execute list of high priority tasks
204 
205  LOW_PRIORITY_GO = true; // Set GO trigger for low priority tasks
206  _OSTIMER_IF = 0; // Reset the interrupt flag bit
207 
208  #if (DBGPIN1_ENABLE)
209  DBGPIN1_Clear(); // Clear the CPU debugging pin
210  #endif
211 
212  return;
213 }
214 
215 // ______________________________________
216 // end of file
217 
218 
219 
int main(void)
Application main function executed after device comes out of RESET.
Definition: main.c:80
volatile uint16_t sysLowPriorityTasks_Execute(void)
Low priority task sequence executed after the high priority task sequence execution is complete.
Definition: main.c:142
volatile uint16_t sysHighPriorityTasks_Execute(void)
High priority task sequence executed at a fixed repetition frequency.
Definition: main.c:172
volatile uint16_t SYSTEM_Initialize(void)
Initializes essential chip resources.
Definition: system.c:28
volatile uint16_t sysUserPeriperhals_Initialize(void)
Initializes the user-defined chip resources.
Definition: system.c:57
volatile uint16_t sysOsTimer_Enable(volatile bool interrupt_enable, volatile uint8_t interrupt_priority)
Definition: init_timer1.c:81
volatile uint16_t sysUserTasks_Initialize(void)
Initializes the user-defined tasks.
Definition: system.c:102
#define _OsTimerInterrupt
Global state-machine peripheral assignments.
#define _OSTIMER_PRIORITY
interrupt priority (1 ... 7, default = 2)
#define _OSTIMER_IF
interrupt flag bit
#define DBGPIN1_Set()
Macro instruction to set a pin state to logic HIGH.
#define DBGPIN1_Clear()
Macro instruction to set a pin state to logic LOW.
volatile uint16_t appFaultMonitor_Execute(void)
Application wide fault object monitoring routine.
volatile uint16_t appLCD_Execute(void)
Refreshes the LC display.
Definition: app_lcd.c:120
volatile uint16_t appLED_Execute(void)
Executes the debugging LED driver.
Definition: app_led.c:61
volatile uint16_t appPowerSupply_Execute(void)
This is the top-level function call triggering the most recent state machine of all associated power ...
volatile uint16_t appPushButton_Execute(void)
Executes the USER push button monitor.