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

rtos_timer.c
1 /*
2 * File: init_timer1.c
3 * Author: M91406
4 *
5 * Created on July 8, 2019, 2:47 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 #include <stddef.h> // include standard definition data types
12
13 #include "config/hal.h" // include application hardware abstraction layer header file
14 #include "rtos_timer.h" // include RTOS timer initialization header file
15 #include "common/p33c_pral/xc16_pral.h" // include common peripheral abstraction layer driver header file
16
17 /***********************************************************************************
18 * @ingroup os-timer-initialization
19 * @fn uint16_t osTimer_Initialize(void)
20 * @brief Initializes the timer used as time-base for the task scheduler
21 * @return unsigned integer
22 * @return 0=failure
23 * @return 1=success
24 *
25 * @details
26 * The internal PLL is set up to operate the CPU at maximum speed of 100 MIPS
27 * (100 MHz instruction cycle). The task scheduler runs on a specific, usually
28 * fixed time base specified in the state machine section of the microcontroller
29 * abstraction layer (e.g. 100us).
30 *
31 * This routine initializes a device timer exclusively used for that purpose
32 * which is not shared with other tasks/peripherals to ensure stable, deterministic
33 * behavior of the firmware. The timer does not use interrupts. The scheduler
34 * task execution period is controlled by polling on the timer overrun bit, allowing
35 * some more relaxed timing while not putting additional burden on the CPU.
36 *
37 **********************************************************************************/
38
39 volatile uint16_t osTimer_Initialize (void)
40 {
41 volatile uint16_t retval=1;
42 volatile struct P33C_TIMER_MODULE_s* tmr;
43
44 tmr = p33c_TimerModule_GetHandle();
45
46 tmr->TxCON.bits.TON = 0; // Timer1 On: Stops 16-bit Timer1 during configuration
47 tmr->TxCON.bits.TSIDL = 0; // Timer1 Stop in Idle Mode: Continues module operation in Idle mode
48 tmr->TxCON.bits.TMWDIS = 0; // Asynchronous Timer1 Write Disable: Back-to-back writes are enabled in Asynchronous mode
49 tmr->TxCON.bits.TMWIP = 0; // Asynchronous Timer1 Write in Progress: Write to the timer in Asynchronous mode is complete
50 tmr->TxCON.bits.PRWIP = 0; // Asynchronous Period Write in Progress: Write to the Period register in Asynchronous mode is complete
51 tmr->TxCON.bits.TECS = 0b11; // Timer1 Extended Clock Select: FRC clock
52 tmr->TxCON.bits.TGATE = 0; // Timer1 Gated Time Accumulation Enable: Gated time accumulation is disabled when TCS = 0
53 tmr->TxCON.bits.TCKPS = 0; // Timer1 Input Clock Prescale Select: 1:1
54 tmr->TxCON.bits.TSYNC = 0; // Timer1 External Clock Input Synchronization Select: Does not synchronize the External Clock input
55 tmr->TxCON.bits.TCS = 0; // Timer1 Clock Source Select: Internal peripheral clock
56
57 // Reset Timer Counter Register TMR to Zero;
58 tmr->TMRx.value = 0x0000;
59
60 //Period = 0.0001 s; Frequency = 100000000 Hz; PR 9999;
61 tmr->PRx.value = MAIN_EXEC_PER;
62
63 // Reset interrupt and interrupt flag bit
64 _OSTIMER_IP = _OSTIMER_PRIORITY; // Set interrupt priority to DEFAULT
65 _OSTIMER_IF = 0; // Reset interrupt flag bit
66 _OSTIMER_IE = 0; // Disable Timer1 interrupt
67
68 return(retval);
69 }
70
71 /***********************************************************************************
72 * @fn uint16_t osTimer_Enable(volatile bool interrupt_enable, volatile uint8_t interrupt_priority)
73 * @brief Enables the timer used as time-base for the task scheduler
74 * @param interrupt_enable: Control flag of type boolean enabling or disabling the timer interrupt
75 * @param interrupt_priority: Interrupt Service Routine priority level of the timer interrupt (if enabled) between level 0 and 6
76 * @return unsigned integer (0=failure, 1=success)
77 *
78 * @details
79 * Once the task scheduler time base has been initialized, this function can be
80 * used to enable the timer and start the scheduled task execution.
81 *
82 **********************************************************************************/
83
84 volatile uint16_t osTimer_Enable (volatile bool interrupt_enable, volatile uint8_t interrupt_priority)
85 {
86 volatile uint16_t retval=1;
87 volatile struct P33C_TIMER_MODULE_s* tmr;
88
89 // Enable Timer1
90 _OSTIMER_IP = interrupt_priority; // Set interrupt priority to zero
91 _OSTIMER_IF = 0; // Reset interrupt flag bit
92 _OSTIMER_IE = interrupt_enable; // Enable/Disable Timer1 interrupt
93
94 // Capture timer module
95 tmr = p33c_TimerModule_GetHandle();
96
97 tmr->TxCON.bits.TON = 1; // Timer1 On: Starts 16-bit Timer
98 retval &= tmr->TxCON.bits.TON; // Check timer enable bit for verification
99
100 return(retval);
101 }
102
103 // end of file
P33C_TIMER_MODULE_s::TxCON
union P33C_TIMER_MODULE_s::@336 TxCON
P33C_TIMER_MODULE_s::TMRx
union P33C_TIMER_MODULE_s::@337 TMRx
_OSTIMER_IP
#define _OSTIMER_IP
interrupt priority register
Definition: dpsk3_hwdescr.h:175
P33C_TIMER_MODULE_s::value
uint16_t value
Definition: p33c_timer.h:42
P33C_TIMER_MODULE_s
Definition: p33c_timer.h:38
_OSTIMER_IF
#define _OSTIMER_IF
interrupt flag bit
Definition: dpsk3_hwdescr.h:177
_OSTIMER_IE
#define _OSTIMER_IE
interrupt enable bit
Definition: dpsk3_hwdescr.h:176
_OSTIMER_PRIORITY
#define _OSTIMER_PRIORITY
interrupt priority (1 ... 7, default = 2)
Definition: dpsk3_hwdescr.h:178
P33C_TIMER_MODULE_s::bits
struct tagT1CONBITS bits
Definition: p33c_timer.h:41
P33C_TIMER_MODULE_s::PRx
union P33C_TIMER_MODULE_s::@338 PRx
MAIN_EXEC_PER
#define MAIN_EXEC_PER
Global state-machine user-settings conversion macros.
Definition: dpsk3_hwdescr.h:160