Project

General

Profile

Statistics
| Revision:

root / proj / src / interrupts_func.c @ 278

History | View | Annotate | Download (6.5 KB)

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