Project

General

Profile

Statistics
| Revision:

root / proj / src / interrupts_func.c @ 284

History | View | Annotate | Download (6.48 KB)

1 178 up20180642
#include <lcom/lcf.h>
2
3 154 up20180655
#include "interrupts_func.h"
4
#include "timer.h"
5
#include "keyboard.h"
6
#include "mouse.h"
7 282 up20180642
#include "uart.h"
8 154 up20180655
#include "utils.h"
9
#include "errors.h"
10
11
static int timer_subscribed = 0, timer_id;
12
13
static int keyboard_subscribed = 0, kbc_id;
14
15
static int mouse_subscribed = 0, mouse_id;
16
17 263 up20180642
static int uart_subscribed = 0, uart_id;
18
19 154 up20180655
static void (*const ih[])(void) =   {    timer_int_handler,
20
                                         kbc_ih,
21
                                         NULL,
22
                                         NULL,
23
                                         NULL,
24
                                         NULL,
25
                                         NULL,
26
                                         NULL,
27
                                         NULL,
28
                                         NULL,
29
                                         NULL,
30
                                         NULL,
31
                                         mouse_ih,
32
                                         NULL,
33
                                         NULL,
34
                                         NULL,
35
                                         NULL,
36
                                         NULL,
37
                                         NULL,
38
                                         NULL,
39
                                         NULL,
40
                                         NULL,
41
                                         NULL,
42
                                         NULL,
43
                                         NULL,
44
                                         NULL,
45
                                         NULL,
46
                                         NULL,
47
                                         NULL,
48
                                         NULL,
49
                                         NULL,
50
                                         NULL,
51
                                     };
52
53
int (subscribe_all)(void) {
54
55
    /// Timer interrupt handling
56
    timer_id = 0;
57
    if(subscribe_timer_interrupt(TIMER0_IRQ, &timer_id)) {
58
        printf("%s: failed to subscribe timer interrupts.\n", __func__);
59
        return SBCR_ERROR;
60
    }
61
    timer_subscribed = 1;
62
63
    /// Keyboard interrupt handling
64
    kbc_id = 0;
65
    if (subscribe_kbc_interrupt(KBC_IRQ, &kbc_id)) {
66
        printf("%s: failed to subscribe keyboard interrupts.\n", __func__);
67
        if (unsubscribe_all())
68
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n");
69
        return SBCR_ERROR;
70
    }
71
    keyboard_subscribed = 1;
72
73
    /// Mouse interrupt handling
74
    mouse_id = 0;
75
    if (subscribe_mouse_interrupt(MOUSE_IRQ, &mouse_id)) { // subscribes mouse interrupts in exclusive mode
76
        printf("%s: failed to subscribe mouse interrupts.\n", __func__);
77
        if (unsubscribe_all())
78
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n");
79
        return SBCR_ERROR;
80
    }
81
    mouse_subscribed = 1;
82
    if (sys_irqdisable(&mouse_id)) { // temporarily disables our interrupts notifications
83
        printf("%s: failed to disable mouse interrupts.\n", __func__);
84
        if (unsubscribe_all())
85
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n");
86
        return SBCR_ERROR;
87
    }
88
    if (mouse_set_data_report(true)) { // enables mouse data reporting
89
        printf("%s: failed to enable mouse data reporting.\n", __func__);
90
        if (unsubscribe_all())
91
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n");
92
        return SBCR_ERROR;
93
    }
94
    if (sys_irqenable(&mouse_id)) { // re-enables our interrupts notifications
95
        printf("%s: failed to enable mouse interrupts.\n", __func__);
96
        if (unsubscribe_all())
97
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n");
98
        return SBCR_ERROR;
99
    }
100
101 263 up20180642
    /// UART interrupt handling
102
    uart_id = 0;
103 272 up20180642
    uart_set_bits_per_character(COM1_ADDR, 8);
104
    uart_set_stop_bits         (COM1_ADDR, 2);
105
    uart_set_parity            (COM1_ADDR, uart_parity_even);
106
    uart_set_bit_rate          (COM1_ADDR, 38400);
107 263 up20180642
    uart_enable_int_rx (COM1_ADDR);
108 275 up20180642
    uart_enable_int_tx(COM1_ADDR);
109 263 up20180642
    if(subscribe_uart_interrupt(COM1_IRQ, &uart_id)) {
110
        printf("%s: failed to subscribe UART interrupts.\n", __func__);
111
        return SBCR_ERROR;
112
    }
113
    uart_subscribed = 1;
114 275 up20180642
    nctp_init();
115 263 up20180642
116 154 up20180655
    return SUCCESS;
117
}
118
119
int (unsubscribe_all)(void) {
120
    int r = SUCCESS;
121
122
    // Unsubscribe Timer Interrupts
123
    if (timer_subscribed) {
124
        if (unsubscribe_interrupt(&timer_id)) {
125
            printf("%s: failed to unsubcribe timer interrupts.\n");
126
            r = UNSBCR_ERROR;
127
        }
128
        timer_subscribed = 0;
129
    }
130
131
    // Unsubscribe Keyboard interrupts
132
    if (keyboard_subscribed) {
133
        if (unsubscribe_interrupt(&kbc_id)) {
134
            printf("%s: failed to unsubcribe keyboard interrupts.\n");
135
            r = UNSBCR_ERROR;
136
        }
137
        keyboard_subscribed = 0;
138
    }
139
140
    // Unsubscribe Mouse Interrupts
141
    if (mouse_subscribed) {
142
        if (sys_irqdisable(&mouse_id)) { // temporarily disables our interrupts notifications
143
            printf("%s: failed to disable mouse interrupts.\n");
144
            r = UNSBCR_ERROR;
145
        }
146
        if (mouse_set_data_report(false)) { // disables mouse data reporting
147
            printf("%s: failed to disable mouse data reporting.\n");
148
            r = UNSBCR_ERROR;
149
        }
150
        if (sys_irqenable(&mouse_id)) { // re-enables our interrupts notifications
151
            printf("%s: failed to enable mouse interrupts.\n");
152
            r = UNSBCR_ERROR;
153
        }
154
        if (unsubscribe_interrupt(&mouse_id)) { // unsubscribes interrupts
155
            printf("%s: failed to unsubcribe mouse interrupts.\n");
156
            r = UNSBCR_ERROR;
157
        }
158
        mouse_subscribed = 0;
159
    }
160
161 263 up20180642
    // Unsubscribe UART interrupts
162
    if (uart_subscribed) {
163
        if (unsubscribe_interrupt(&uart_id)) {
164
            printf("%s: failed to unsubcribe UART interrupts.\n");
165
            r = UNSBCR_ERROR;
166
        }
167
        uart_enable_int_rx(COM1_ADDR);
168
        uart_enable_int_tx(COM1_ADDR);
169
        uart_subscribed = 0;
170
    }
171 275 up20180642
    nctp_free();
172 263 up20180642
173 154 up20180655
    return r;
174
}
175
176 250 up20180655
void (interrupt_handler)(uint8_t handler) {
177 154 up20180655
    if (handler >= 32)              return;
178
    if ((*ih[handler]) == NULL)     return;
179
    (*ih[handler])();
180
}
181 250 up20180655
182
void (timer_manager)() {
183
184
}
185
186
void (mouse_manager)() {
187
188
}