Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
200 Kevin 1
#include <xc.h>
2
#include <plib.h>
3
#include "defines.h"
4
#include "BTN.h"
5
 
201 Kevin 6
static BTN_DATA *btn_data_ptr;
200 Kevin 7
 
201 Kevin 8
void BTN_Init(BTN_DATA *data, void (*callback_1)(void), void (*callback_2)(void), void (*callback_3)(void)) {
9
    btn_data_ptr = data;
10
 
11
    btn_data_ptr->callback_function_1 = callback_1;
12
    btn_data_ptr->callback_function_2 = callback_2;
13
    btn_data_ptr->callback_function_3 = callback_3;
200 Kevin 14
 
15
    BTN1_TRIS = 1;
16
    BTN2_TRIS = 1;
17
    BTN3_TRIS = 1;
18
 
19
    INTDisableInterrupts();
20
 
21
    CNCONSET = 0x8000;  // Turn on change notice interrupt
212 Kevin 22
#if defined CEREBOT_32MX7
200 Kevin 23
    CNENSET = 0x80300;  // Set interrupt on CN8/9/19
212 Kevin 24
#elif defined CEREBOT_MX7CK
25
    CNENSET = 0x00300;  // Set interrupt on CN8/9
26
#endif
200 Kevin 27
    int tmp = BTN1_PORT;
28
    tmp = BTN2_PORT;
29
    tmp = BTN3_PORT;
212 Kevin 30
    IPC6SET = 0xD0000;  // Set priority level = 3, sub-priority level = 1
200 Kevin 31
    IFS1CLR = 0x1;      // Clear interrupt flag
32
    IEC1SET = 0x1;      // Enable interrupt
33
 
34
    INTEnableInterrupts();
35
}
36
 
212 Kevin 37
void __ISR(_CHANGE_NOTICE_VECTOR, ipl3) __CN_Interrupt_Handler(void) {
201 Kevin 38
    // Upon interrupt, debounce input and call saved function
39
    if (BTN1_PORT == 1) {
40
        Delay_MS(BTN_DEBOUNCE_MS);
200 Kevin 41
        if (BTN1_PORT == 1) {
201 Kevin 42
            if (btn_data_ptr->callback_function_1 != NULL)
43
                (*btn_data_ptr->callback_function_1)();
200 Kevin 44
        }
201 Kevin 45
    }
46
    if (BTN2_PORT == 1) {
47
        Delay_MS(BTN_DEBOUNCE_MS);
200 Kevin 48
        if (BTN2_PORT == 1) {
201 Kevin 49
            if (btn_data_ptr->callback_function_2 != NULL)
50
                (*btn_data_ptr->callback_function_2)();
200 Kevin 51
        }
201 Kevin 52
    }
212 Kevin 53
#ifdef CEREBOT_32MX7
201 Kevin 54
    if (BTN3_PORT == 1) {
55
        Delay_MS(BTN_DEBOUNCE_MS);
200 Kevin 56
        if (BTN3_PORT == 1) {
201 Kevin 57
            if (btn_data_ptr->callback_function_3 != NULL)
58
                (*btn_data_ptr->callback_function_3)();
200 Kevin 59
        }
201 Kevin 60
    }
212 Kevin 61
#endif
200 Kevin 62
    IFS1CLR = 0x1; // Clear interrupt flag
63
}