Project

General

Profile

Statistics
| Revision:

root / proj / src / project / src / interrupts_func.c @ 380

History | View | Annotate | Download (8.08 KB)

1
#include <lcom/lcf.h>
2

    
3
#include "interrupts_func.h"
4
#include "timer.h"
5
#include "keyboard.h"
6
#include "mouse.h"
7
#include "hltp.h"
8
#include "utils.h"
9
#include "errors.h"
10
#include "rtc.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
static int uart_subscribed = 0, uart_id;
19

    
20
static int rtc_subscribed = 0, rtc_id;
21

    
22
static void (*const ih[])(void) =   {    timer_int_handler,
23
                                         kbc_ih,
24
                                         NULL,
25
                                         NULL,
26
                                         NULL,
27
                                         NULL,
28
                                         NULL,
29
                                         NULL,
30
                                         rtc_ih,
31
                                         NULL,
32
                                         NULL,
33
                                         NULL,
34
                                         mouse_ih,
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
                                         NULL,
53
                                         NULL,
54
                                     };
55
/*
56
static void process_received(const uint8_t *p, const size_t sz){
57
    void *q = NULL;
58
    hltp_type t = hltp_interpret(p, sz, &q);
59
    switch(t){
60
        case hltp_type_string:{
61
            char *s = q;
62
            printf("%s\n", s);
63
        } break;
64
        default: break;
65
    }
66
}
67
*/
68
int (subscribe_all)(void) {
69

    
70
    /// Timer interrupt handling
71
    timer_id = 0;
72
    if(subscribe_timer_interrupt(TIMER0_IRQ, &timer_id)) {
73
        printf("%s: failed to subscribe timer interrupts.\n", __func__);
74
        return SBCR_ERROR;
75
    }
76
    timer_subscribed = 1;
77

    
78
    /// Keyboard interrupt handling
79
    kbc_id = 0;
80
    if (subscribe_kbc_interrupt(KBC_IRQ, &kbc_id)) {
81
        printf("%s: failed to subscribe keyboard interrupts.\n", __func__);
82
        if (unsubscribe_all())
83
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n", __func__);
84
        return SBCR_ERROR;
85
    }
86
    keyboard_subscribed = 1;
87

    
88
    /// Mouse interrupt handling
89
    mouse_id = 0;
90
    if (subscribe_mouse_interrupt(MOUSE_IRQ, &mouse_id)) { // subscribes mouse interrupts in exclusive mode
91
        printf("%s: failed to subscribe mouse interrupts.\n", __func__);
92
        if (unsubscribe_all())
93
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n", __func__);
94
        return SBCR_ERROR;
95
    }
96
    mouse_subscribed = 1;
97
    if (sys_irqdisable(&mouse_id)) { // temporarily disables our interrupts notifications
98
        printf("%s: failed to disable mouse interrupts.\n", __func__);
99
        if (unsubscribe_all())
100
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n", __func__);
101
        return SBCR_ERROR;
102
    }
103
    if (mouse_set_data_report(true)) { // enables mouse data reporting
104
        printf("%s: failed to enable mouse data reporting.\n", __func__);
105
        if (unsubscribe_all())
106
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n", __func__);
107
        return SBCR_ERROR;
108
    }
109
    if (sys_irqenable(&mouse_id)) { // re-enables our interrupts notifications
110
        printf("%s: failed to enable mouse interrupts.\n", __func__);
111
        if (unsubscribe_all())
112
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n", __func__);
113
        return SBCR_ERROR;
114
    }
115

    
116
    if (subscribe_rtc_interrupt(RTC_IRQ, &rtc_id)){
117
        printf("%s: failed to enable rtc interrupts.\n", __func__);
118
        if (unsubscribe_all())
119
            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n", __func__);
120
        return SBCR_ERROR;
121
    }
122
    rtc_subscribed = 1;
123
    rtc_set_updates_int(1);
124

    
125

    
126

    
127
    /// UART interrupt handling
128
    uart_id = 0;
129
    uart_set_bits_per_character(COM1_ADDR, 8);
130
    uart_set_stop_bits         (COM1_ADDR, 2);
131
    uart_set_parity            (COM1_ADDR, uart_parity_even);
132
    uart_set_bit_rate          (COM1_ADDR, 19200);
133
    uart_enable_int_rx (COM1_ADDR);
134
    uart_enable_int_tx(COM1_ADDR);
135
    if(subscribe_uart_interrupt(COM1_IRQ, &uart_id)) {
136
        printf("%s: failed to subscribe UART interrupts.\n", __func__);
137
        return SBCR_ERROR;
138
    }
139
    uart_subscribed = 1;
140
    nctp_init();
141

    
142
    return SUCCESS;
143
}
144

    
145
int (unsubscribe_all)(void) {
146
    int r = SUCCESS;
147

    
148
    // Unsubscribe Timer Interrupts
149
    if (timer_subscribed) {
150
        if (unsubscribe_interrupt(&timer_id)) {
151
            printf("%s: failed to unsubcribe timer interrupts.\n", __func__);
152
            r = UNSBCR_ERROR;
153
        }
154
        timer_subscribed = 0;
155
    }
156

    
157
    // Unsubscribe Keyboard interrupts
158
    if (keyboard_subscribed) {
159
        if (unsubscribe_interrupt(&kbc_id)) {
160
            printf("%s: failed to unsubcribe keyboard interrupts.\n", __func__);
161
            r = UNSBCR_ERROR;
162
        }
163
        keyboard_subscribed = 0;
164
    }
165

    
166
    // Unsubscribe Mouse Interrupts
167
    if (mouse_subscribed) {
168
        if (sys_irqdisable(&mouse_id)) { // temporarily disables our interrupts notifications
169
            printf("%s: failed to disable mouse interrupts.\n", __func__);
170
            r = UNSBCR_ERROR;
171
        }
172
        if (mouse_set_data_report(false)) { // disables mouse data reporting
173
            printf("%s: failed to disable mouse data reporting.\n", __func__);
174
            r = UNSBCR_ERROR;
175
        }
176
        if (sys_irqenable(&mouse_id)) { // re-enables our interrupts notifications
177
            printf("%s: failed to enable mouse interrupts.\n", __func__);
178
            r = UNSBCR_ERROR;
179
        }
180
        if (unsubscribe_interrupt(&mouse_id)) { // unsubscribes interrupts
181
            printf("%s: failed to unsubcribe mouse interrupts.\n", __func__);
182
            r = UNSBCR_ERROR;
183
        }
184
        mouse_subscribed = 0;
185
    }
186

    
187
    if (rtc_subscribed) {
188
        if (unsubscribe_interrupt(&rtc_id)) {
189
            printf("%s: failed to unsubcribe RTC interrupts.\n", __func__);
190
            r = UNSBCR_ERROR;
191
        }
192
        rtc_subscribed = 0;
193
    }
194

    
195
    // Unsubscribe UART interrupts
196
    if (uart_subscribed) {
197
        if (unsubscribe_interrupt(&uart_id)) {
198
            printf("%s: failed to unsubcribe UART interrupts.\n", __func__);
199
            r = UNSBCR_ERROR;
200
        }
201
        uart_enable_int_rx(COM1_ADDR);
202
        uart_enable_int_tx(COM1_ADDR);
203
        uart_subscribed = 0;
204
    }
205
    nctp_free();
206

    
207
    return r;
208
}
209

    
210
void (interrupt_handler)(uint8_t handler) {
211
    if (handler >= 32)              return;
212
    if ((ih[handler]) == NULL)      return;
213
    (*ih[handler])();
214
}
215

    
216
int get_interrupts_vector(uint64_t *p){
217
    int r;
218

    
219
    *p = 0;
220

    
221
    int ipc_status;
222
    message msg;
223
    if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
224
        printf("driver_receive failed with %d", r);
225
        return OTHER_ERROR;
226
    }
227
    if (is_ipc_notify(ipc_status)) { /* received notification */
228
        switch (_ENDPOINT_P(msg.m_source)) {
229
            case HARDWARE: /* hardware interrupt notification */
230
                *p = msg.m_notify.interrupts;
231
                return SUCCESS;
232
            default: break; /* no other notifications expected: do nothing */
233
        }
234
    }
235
    return SUCCESS;
236
}