Subversion Repositories Code-Repo

Rev

Rev 234 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 234 Rev 237
Line 1... Line 1...
1
#include "defines.h"
1
#include "defines.h"
2
#include "TIMER4.h"
2
#include "TIMER4.h"
3
 
3
 
4
static TIMER4_DATA *timer_data_ptr;
4
static TIMER4_DATA *timer_data_ptr;
5
 
5
 
6
void TIMER4_Init(TIMER4_DATA *data, void (*callback)(void), uint32_t time_us) {
6
void TIMER4_Init(TIMER4_DATA *data, void (*callback_ms)(void),
-
 
7
        void (*callback_div)(void), uint32_t time_ms) {
-
 
8
    
7
    if (data != NULL) // if ptr is null, use existing data
9
    if (data != NULL) // if ptr is null, use existing data
8
        timer_data_ptr = data;
10
        timer_data_ptr = data;
9
 
11
 
-
 
12
    // The first callback function will be executed every ms
10
    timer_data_ptr->callback_function = callback;
13
    timer_data_ptr->callback_function_1 = callback_ms;
-
 
14
    // The second callback function will be executed at the multiplier specified
-
 
15
    timer_data_ptr->callback_function_2 = callback_div;
-
 
16
    timer_data_ptr->divider = time_ms;
-
 
17
    timer_data_ptr->count = 0;
11
 
18
 
12
    INTDisableInterrupts();
19
    INTDisableInterrupts();
13
 
20
 
14
    T4CON = 0x0;
21
    T4CON = 0x0;
15
 
22
 
16
    // PR4 is 16 bits wide, so we need to determine what pre-scaler to use
23
    // Set timer to trigger every millisecond
17
    uint16_t time;
24
    uint16_t time = 5000;
18
    if (time_us < 13107) {
-
 
19
        time = 5 * time_us;
-
 
20
        T4CONSET = 0x0040;         // Prescaler at 1:16
25
    T4CONSET = 0x0040;         // Prescaler at 1:16
21
    } else if (time_us < 26214) {
-
 
22
        time = 2.5 * time_us;
-
 
23
        T4CONSET = 0x0050;         // Prescaler at 1:32
-
 
24
    } else if (time_us < 52428) {
-
 
25
        time = 1.25 * time_us;
-
 
26
        T4CONSET = 0x0060;         // Prescaler at 1:64
-
 
27
    } else {
-
 
28
        time = 0.3125 * time_us;   // Minimum time_us of 209712
-
 
29
        T4CONSET = 0x0070;         // Prescaler at 1:256
-
 
30
    }
-
 
31
 
26
 
32
    Nop();
27
    Nop();
33
    TMR4 = 0x0;             // Clear timer register
28
    TMR4 = 0x0;             // Clear timer register
34
    PR4 = time;             // Load period register
29
    PR4 = time;             // Load period register
35
    IPC4SET = 0x0000000D;   // Set priority level = 3, sub-priority level = 1
30
    IPC4SET = 0x00000005;   // Set priority level = 1, sub-priority level = 1
36
    IFS0CLR = 0x00010000;   // Clear timer interrupt flag
31
    IFS0CLR = 0x00010000;   // Clear timer interrupt flag
37
    IEC0SET = 0x00010000;   // Enable timer interrupt
32
    IEC0SET = 0x00010000;   // Enable timer interrupt
38
 
33
 
39
    INTEnableInterrupts();
34
    INTEnableInterrupts();
40
}
35
}
Line 47... Line 42...
47
    T4CONCLR = 0x8000; // Stop timer
42
    T4CONCLR = 0x8000; // Stop timer
48
}
43
}
49
 
44
 
50
void __ISR(_TIMER_4_VECTOR, ipl4) __TIMER_4_Interrupt_Handler(void) {
45
void __ISR(_TIMER_4_VECTOR, ipl4) __TIMER_4_Interrupt_Handler(void) {
51
    // Call the saved callback function
46
    // Call the saved callback function
52
    (*timer_data_ptr->callback_function)();
47
    (*timer_data_ptr->callback_function_1)();
-
 
48
 
-
 
49
    if (timer_data_ptr->divider != 0 && timer_data_ptr->callback_function_2 != NULL) {
-
 
50
        if (timer_data_ptr->count == timer_data_ptr->divider) {
-
 
51
            (*timer_data_ptr->callback_function_2)();
-
 
52
            timer_data_ptr->count = 0;
-
 
53
        } else {
-
 
54
            timer_data_ptr->count++;
-
 
55
        }
-
 
56
    }
53
 
57
 
54
    IFS0CLR = 0x00010000; // Clear the timer interrupt flag
58
    IFS0CLR = 0x00010000; // Clear the timer interrupt flag
55
}
59
}