Revision 47
minor change
lab3/i8254.h | ||
---|---|---|
1 |
#ifndef _LCOM_I8254_H_ |
|
2 |
#define _LCOM_I8254_H_ |
|
3 |
|
|
4 |
#include <lcom/lcf.h> |
|
5 |
|
|
6 |
/** @defgroup i8254 i8254 |
|
7 |
* @{ |
|
8 |
* |
|
9 |
* Constants for programming the i8254 Timer. Needs to be completed. |
|
10 |
*/ |
|
11 |
|
|
12 |
#define TIMER_FREQ 1193182 /**< @brief clock frequency for timer in PC and AT */ |
|
13 |
#define TIMER_MIN_FREQ (TIMER_FREQ/UINT16_MAX) + ((TIMER_FREQ % UINT16_MAX) ? 1 : 0) /**< @brief mininum frequency for timer */ |
|
14 |
#define TIMER0_IRQ 0 /**< @brief Timer 0 IRQ line */ |
|
15 |
|
|
16 |
/* I/O port addresses */ |
|
17 |
|
|
18 |
#define TIMER_0 0x40 /**< @brief Timer 0 count register */ |
|
19 |
#define TIMER_1 0x41 /**< @brief Timer 1 count register */ |
|
20 |
#define TIMER_2 0x42 /**< @brief Timer 2 count register */ |
|
21 |
#define TIMER_CTRL 0x43 /**< @brief Control register */ |
|
22 |
|
|
23 |
#define SPEAKER_CTRL 0x61 /**< @brief Register for speaker control */ |
|
24 |
|
|
25 |
/* Timer control */ |
|
26 |
|
|
27 |
/* Timer selection: bits 7 and 6 */ |
|
28 |
|
|
29 |
#define TIMER_SEL0 0x00 /**< @brief Control Word for Timer 0 */ |
|
30 |
#define TIMER_SEL1 BIT(6) /**< @brief Control Word for Timer 1 */ |
|
31 |
#define TIMER_SEL2 BIT(7) /**< @brief Control Word for Timer 2 */ |
|
32 |
#define TIMER_RB_CMD (BIT(7) | BIT(6)) /**< @brief Read Back Command */ |
|
33 |
|
|
34 |
/* Register selection: bits 5 and 4 */ |
|
35 |
|
|
36 |
#define TIMER_LSB BIT(4) /**< @brief Initialize Counter LSB only */ |
|
37 |
#define TIMER_MSB BIT(5) /**< @brief Initialize Counter MSB only */ |
|
38 |
#define TIMER_LSB_MSB (TIMER_LSB | TIMER_MSB) /**< @brief Initialize LSB first and MSB afterwards */ |
|
39 |
#define TIMER_INMODE_MASK 0x30 //0011 0000 |
|
40 |
#define TIMER_INMODE_POS 4 |
|
41 |
|
|
42 |
/* Operating mode: bits 3, 2 and 1 */ |
|
43 |
|
|
44 |
#define TIMER_SQR_WAVE (BIT(2) | BIT(1)) /**< @brief Mode 3: square wave generator */ |
|
45 |
#define TIMER_RATE_GEN BIT(2) /**< @brief Mode 2: rate generator */ |
|
46 |
#define TIMER_MODE_MASK 0x0E /**< @brief Mask for mode */ |
|
47 |
#define TIMER_MODE_POS 1 /**< @brief Position of smallest bit from mode */ |
|
48 |
#define TIMER_MODE_2ALT 0x6 /**< @brief Alternative notation for mode 2 */ |
|
49 |
#define TIMER_MODE_3ALT 0x7 /**< @brief Alternative notation for mode 3 */ |
|
50 |
#define TIMER_MODE_RED2 0x03 /**< @brief Reduce 3-bit mode to 2-bit mode */ |
|
51 |
|
|
52 |
/* Counting mode: bit 0 */ |
|
53 |
|
|
54 |
#define TIMER_BCD 0x01 /**< @brief Count in BCD */ |
|
55 |
#define TIMER_BIN 0x00 /**< @brief Count in binary */ |
|
56 |
|
|
57 |
/* READ-BACK COMMAND FORMAT */ |
|
58 |
|
|
59 |
#define TIMER_RB_COUNT_ BIT(5) |
|
60 |
#define TIMER_RB_STATUS_ BIT(4) |
|
61 |
#define TIMER_RB_SEL(n) BIT((n) + 1) |
|
62 |
|
|
63 |
/**@}*/ |
|
64 |
|
|
65 |
#endif /* _LCOM_I8254_H */ |
|
0 | 66 |
lab3/kbc_func.c | ||
---|---|---|
1 |
#include "kbc_func.h" |
|
2 |
#include "kbc.h" |
|
3 |
|
|
4 |
#include <lcom/lcf.h> |
|
5 |
|
|
6 |
int (subscribe_kbc_interrupt)(uint8_t interrupt_bit, int *interrupt_id) { |
|
7 |
if (interrupt_id == NULL) return 1; |
|
8 |
*interrupt_id = interrupt_bit; |
|
9 |
return (sys_irqsetpolicy(KBC_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)); |
|
10 |
} |
|
11 |
|
|
12 |
int (unsubscribe_interrupt)(int *interrupt_id) { |
|
13 |
if (interrupt_id == NULL) return 1; |
|
14 |
return sys_irqrmpolicy(interrupt_id); |
|
15 |
} |
|
16 |
|
|
17 |
uint8_t scancode[2]; |
|
18 |
int two_byte_scancode = 0; |
|
19 |
int got_error = 0; |
|
20 |
|
|
21 |
void (kbc_ih)(void) { |
|
22 |
uint8_t status = 0; |
|
23 |
got_error = 0; |
|
24 |
|
|
25 |
if (util_sys_inb(STATUS_REG, &status)) { |
|
26 |
got_error = 1; |
|
27 |
return; |
|
28 |
} |
|
29 |
|
|
30 |
if (status & (TIME_OUT_REC | PARITY_ERROR)) { |
|
31 |
got_error = 1; |
|
32 |
return; |
|
33 |
} |
|
34 |
|
|
35 |
uint8_t byte = 0; |
|
36 |
|
|
37 |
if (util_sys_inb(OUTPUT_BUF, &byte)) { |
|
38 |
got_error = 1; |
|
39 |
return; |
|
40 |
} |
|
41 |
|
|
42 |
if (two_byte_scancode) { |
|
43 |
scancode[1] = byte; |
|
44 |
two_byte_scancode = 0; |
|
45 |
} else { |
|
46 |
scancode[0] = byte; |
|
47 |
two_byte_scancode = (byte == TWO_BYTE_CODE); |
|
48 |
} |
|
49 |
|
|
50 |
} |
|
51 |
|
|
52 |
int (kbd_poll)(uint8_t bytes[], uint8_t *size){ |
|
53 |
if(bytes == NULL || size == NULL) return 1; |
|
54 |
uint8_t c; |
|
55 |
if(kbc_read_byte(&c)) return 1; |
|
56 |
if(c == TWO_BYTE_CODE){ |
|
57 |
if(kbc_read_byte(&bytes[1])) return 1; |
|
58 |
bytes[0] = c; |
|
59 |
*size = 2; |
|
60 |
}else{ |
|
61 |
bytes[1] = 0; |
|
62 |
bytes[0] = c; |
|
63 |
*size = 1; |
|
64 |
} |
|
65 |
return 0; |
|
66 |
} |
|
67 |
|
|
68 |
int (kbc_read_cmd)(uint8_t *cmd){ |
|
69 |
if(kbc_issue_cmd(READ_KBC_CMD)) return 1; |
|
70 |
if(kbc_read_byte(cmd)) return 1; |
|
71 |
return 0; |
|
72 |
} |
|
73 |
|
|
74 |
int (kbc_change_cmd)(uint8_t cmd){ |
|
75 |
if(kbc_issue_cmd(WRITE_KBC_CMD)) return 1; |
|
76 |
if(sys_outb(KBC_CMD_ARG, cmd)) return 1; |
|
77 |
return 0; |
|
78 |
} |
|
79 |
|
|
80 |
int (kbc_restore_kbd)(){ |
|
81 |
uint8_t cmd = 0; |
|
82 |
if(kbc_read_cmd(&cmd)) return 1; |
|
83 |
cmd = (cmd | INT_KBD) & (~DIS_KBD); |
|
84 |
if(kbc_change_cmd(cmd)) return 1; |
|
85 |
return 0; |
|
86 |
} |
|
87 |
|
|
88 |
int (kbc_issue_cmd)(uint8_t cmd){ |
|
89 |
uint8_t stat; |
|
90 |
for(int i = 0; i < KBC_NUM_TRIES; ++i){ |
|
91 |
if(util_sys_inb(STATUS_REG, &stat)) return 1; |
|
92 |
if((stat&IN_BUF_FULL) == 0){ |
|
93 |
if(sys_outb(KBC_CMD, cmd)) return 1; |
|
94 |
return 0; |
|
95 |
} |
|
96 |
tickdelay(micros_to_ticks(DELAY)); |
|
97 |
} |
|
98 |
return 1; |
|
99 |
} |
|
100 |
|
|
101 |
int (kbc_read_byte)(uint8_t *byte){ |
|
102 |
uint8_t stat; |
|
103 |
while(true){ |
|
104 |
if(util_sys_inb(STATUS_REG, &stat)) return 1; |
|
105 |
if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)==0){ |
|
106 |
if(stat & (PARITY_ERROR | TIME_OUT_REC)) return 1; |
|
107 |
if(util_sys_inb(OUTPUT_BUF, byte)) return 1; |
|
108 |
else return 0; |
|
109 |
} |
|
110 |
tickdelay(micros_to_ticks(DELAY)); |
|
111 |
} |
|
112 |
} |
|
0 | 113 |
lab3/timer_func.c | ||
---|---|---|
1 |
#include "timer_func.h" |
|
2 |
|
|
3 |
#include <lcom/lcf.h> |
|
4 |
#include "i8254.h" |
|
5 |
|
|
6 |
int (subscribe_timer_interrupt)(uint8_t interrupt_bit, int *interrupt_id) { |
|
7 |
if (interrupt_id == NULL) return 1; |
|
8 |
*interrupt_id = interrupt_bit; |
|
9 |
return (sys_irqsetpolicy(TIMER0_IRQ, IRQ_REENABLE, interrupt_id)); |
|
10 |
} |
|
11 |
|
|
12 |
int no_interrupts = 0; |
|
13 |
void (timer_int_handler)() { |
|
14 |
no_interrupts++; |
|
15 |
} |
|
0 | 16 |
lab3/timer_func.h | ||
---|---|---|
1 |
#ifndef TIMER_FUNC_H_INCLUDED |
|
2 |
#define TIMER_FUNC_H_INCLUDED |
|
3 |
|
|
4 |
#include <stdint.h> |
|
5 |
|
|
6 |
int (subscribe_timer_interrupt)(uint8_t interrupt_bit, int *interrupt_id); |
|
7 |
|
|
8 |
#endif //TIMER_FUNC_H_INCLUDED |
|
0 | 9 |
Also available in: Unified diff