root / proj / src / interrupts_func.c @ 347
History | View | Annotate | Download (7.45 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 | 287 | up20180642 | #include "hltp.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 | 297 | up20180642 | /*
|
53 | 287 | up20180642 | static void process_received(const uint8_t *p, const size_t sz){
|
54 | void *q = NULL;
|
||
55 | hltp_type t = hltp_interpret(p, sz, &q);
|
||
56 | switch(t){
|
||
57 | case hltp_type_string:{
|
||
58 | char *s = q;
|
||
59 | printf("%s\n", s);
|
||
60 | } break;
|
||
61 | default: break;
|
||
62 | }
|
||
63 | }
|
||
64 | 297 | up20180642 | */
|
65 | 154 | up20180655 | int (subscribe_all)(void) { |
66 | |||
67 | /// Timer interrupt handling
|
||
68 | timer_id = 0;
|
||
69 | if(subscribe_timer_interrupt(TIMER0_IRQ, &timer_id)) {
|
||
70 | printf("%s: failed to subscribe timer interrupts.\n", __func__);
|
||
71 | return SBCR_ERROR;
|
||
72 | } |
||
73 | timer_subscribed = 1;
|
||
74 | |||
75 | /// Keyboard interrupt handling
|
||
76 | kbc_id = 0;
|
||
77 | if (subscribe_kbc_interrupt(KBC_IRQ, &kbc_id)) {
|
||
78 | printf("%s: failed to subscribe keyboard interrupts.\n", __func__);
|
||
79 | if (unsubscribe_all())
|
||
80 | 321 | up20180642 | printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n", __func__);
|
81 | 154 | up20180655 | return SBCR_ERROR;
|
82 | } |
||
83 | keyboard_subscribed = 1;
|
||
84 | |||
85 | /// Mouse interrupt handling
|
||
86 | mouse_id = 0;
|
||
87 | if (subscribe_mouse_interrupt(MOUSE_IRQ, &mouse_id)) { // subscribes mouse interrupts in exclusive mode |
||
88 | printf("%s: failed to subscribe mouse interrupts.\n", __func__);
|
||
89 | if (unsubscribe_all())
|
||
90 | 321 | up20180642 | printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n", __func__);
|
91 | 154 | up20180655 | return SBCR_ERROR;
|
92 | } |
||
93 | mouse_subscribed = 1;
|
||
94 | if (sys_irqdisable(&mouse_id)) { // temporarily disables our interrupts notifications |
||
95 | printf("%s: failed to disable mouse interrupts.\n", __func__);
|
||
96 | if (unsubscribe_all())
|
||
97 | 321 | up20180642 | printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n", __func__);
|
98 | 154 | up20180655 | return SBCR_ERROR;
|
99 | } |
||
100 | if (mouse_set_data_report(true)) { // enables mouse data reporting |
||
101 | printf("%s: failed to enable mouse data reporting.\n", __func__);
|
||
102 | if (unsubscribe_all())
|
||
103 | 321 | up20180642 | printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n", __func__);
|
104 | 154 | up20180655 | return SBCR_ERROR;
|
105 | } |
||
106 | if (sys_irqenable(&mouse_id)) { // re-enables our interrupts notifications |
||
107 | printf("%s: failed to enable mouse interrupts.\n", __func__);
|
||
108 | if (unsubscribe_all())
|
||
109 | 321 | up20180642 | printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n", __func__);
|
110 | 154 | up20180655 | return SBCR_ERROR;
|
111 | } |
||
112 | |||
113 | 263 | up20180642 | /// UART interrupt handling
|
114 | uart_id = 0;
|
||
115 | 272 | up20180642 | uart_set_bits_per_character(COM1_ADDR, 8);
|
116 | uart_set_stop_bits (COM1_ADDR, 2);
|
||
117 | uart_set_parity (COM1_ADDR, uart_parity_even); |
||
118 | 328 | up20180642 | uart_set_bit_rate (COM1_ADDR, 115200);
|
119 | 263 | up20180642 | uart_enable_int_rx (COM1_ADDR); |
120 | 275 | up20180642 | uart_enable_int_tx(COM1_ADDR); |
121 | 263 | up20180642 | if(subscribe_uart_interrupt(COM1_IRQ, &uart_id)) {
|
122 | printf("%s: failed to subscribe UART interrupts.\n", __func__);
|
||
123 | return SBCR_ERROR;
|
||
124 | } |
||
125 | uart_subscribed = 1;
|
||
126 | 297 | up20180642 | nctp_init(); |
127 | 263 | up20180642 | |
128 | 154 | up20180655 | return SUCCESS;
|
129 | } |
||
130 | |||
131 | int (unsubscribe_all)(void) { |
||
132 | int r = SUCCESS;
|
||
133 | |||
134 | // Unsubscribe Timer Interrupts
|
||
135 | if (timer_subscribed) {
|
||
136 | if (unsubscribe_interrupt(&timer_id)) {
|
||
137 | 321 | up20180642 | printf("%s: failed to unsubcribe timer interrupts.\n", __func__);
|
138 | 154 | up20180655 | r = UNSBCR_ERROR; |
139 | } |
||
140 | timer_subscribed = 0;
|
||
141 | } |
||
142 | |||
143 | // Unsubscribe Keyboard interrupts
|
||
144 | if (keyboard_subscribed) {
|
||
145 | if (unsubscribe_interrupt(&kbc_id)) {
|
||
146 | 321 | up20180642 | printf("%s: failed to unsubcribe keyboard interrupts.\n", __func__);
|
147 | 154 | up20180655 | r = UNSBCR_ERROR; |
148 | } |
||
149 | keyboard_subscribed = 0;
|
||
150 | } |
||
151 | |||
152 | // Unsubscribe Mouse Interrupts
|
||
153 | if (mouse_subscribed) {
|
||
154 | if (sys_irqdisable(&mouse_id)) { // temporarily disables our interrupts notifications |
||
155 | 321 | up20180642 | printf("%s: failed to disable mouse interrupts.\n", __func__);
|
156 | 154 | up20180655 | r = UNSBCR_ERROR; |
157 | } |
||
158 | if (mouse_set_data_report(false)) { // disables mouse data reporting |
||
159 | 321 | up20180642 | printf("%s: failed to disable mouse data reporting.\n", __func__);
|
160 | 154 | up20180655 | r = UNSBCR_ERROR; |
161 | } |
||
162 | if (sys_irqenable(&mouse_id)) { // re-enables our interrupts notifications |
||
163 | 321 | up20180642 | printf("%s: failed to enable mouse interrupts.\n", __func__);
|
164 | 154 | up20180655 | r = UNSBCR_ERROR; |
165 | } |
||
166 | if (unsubscribe_interrupt(&mouse_id)) { // unsubscribes interrupts |
||
167 | 321 | up20180642 | printf("%s: failed to unsubcribe mouse interrupts.\n", __func__);
|
168 | 154 | up20180655 | r = UNSBCR_ERROR; |
169 | } |
||
170 | mouse_subscribed = 0;
|
||
171 | } |
||
172 | |||
173 | 263 | up20180642 | // Unsubscribe UART interrupts
|
174 | if (uart_subscribed) {
|
||
175 | if (unsubscribe_interrupt(&uart_id)) {
|
||
176 | 321 | up20180642 | printf("%s: failed to unsubcribe UART interrupts.\n", __func__);
|
177 | 263 | up20180642 | r = UNSBCR_ERROR; |
178 | } |
||
179 | uart_enable_int_rx(COM1_ADDR); |
||
180 | uart_enable_int_tx(COM1_ADDR); |
||
181 | uart_subscribed = 0;
|
||
182 | } |
||
183 | 275 | up20180642 | nctp_free(); |
184 | 263 | up20180642 | |
185 | 154 | up20180655 | return r;
|
186 | } |
||
187 | |||
188 | 250 | up20180655 | void (interrupt_handler)(uint8_t handler) {
|
189 | 154 | up20180655 | if (handler >= 32) return; |
190 | 342 | up20180655 | if ((ih[handler]) == NULL) return; |
191 | 154 | up20180655 | (*ih[handler])(); |
192 | } |
||
193 | 250 | up20180655 | |
194 | 323 | up20180642 | int get_interrupts_vector(uint64_t *p){
|
195 | 304 | up20180642 | int r;
|
196 | |||
197 | *p = 0;
|
||
198 | |||
199 | int ipc_status;
|
||
200 | message msg; |
||
201 | if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
||
202 | printf("driver_receive failed with %d", r);
|
||
203 | return OTHER_ERROR;
|
||
204 | } |
||
205 | if (is_ipc_notify(ipc_status)) { /* received notification */ |
||
206 | switch (_ENDPOINT_P(msg.m_source)) {
|
||
207 | case HARDWARE: /* hardware interrupt notification */ |
||
208 | *p = msg.m_notify.interrupts; |
||
209 | return SUCCESS;
|
||
210 | default: break; /* no other notifications expected: do nothing */ |
||
211 | } |
||
212 | } |
||
213 | return SUCCESS;
|
||
214 | } |