root / lab2 / timer.c @ 10
History | View | Annotate | Download (2.61 KB)
1 | 4 | up20180655 | #include <lcom/lcf.h> |
---|---|---|---|
2 | #include <lcom/timer.h> |
||
3 | |||
4 | #include <stdint.h> |
||
5 | |||
6 | #include "i8254.h" |
||
7 | |||
8 | int (timer_set_frequency)(uint8_t timer, uint32_t freq) {
|
||
9 | |||
10 | 10 | up20180655 | // counter_init = clock/feq
|
11 | |||
12 | uint8_t status = 0;
|
||
13 | |||
14 | if (timer_get_conf(timer, &status)) return 1; |
||
15 | //Make command
|
||
16 | uint8_t write_cmd = 0;
|
||
17 | //Select timer
|
||
18 | switch(timer) {
|
||
19 | case 0: write_cmd |= TIMER_SEL0; |
||
20 | break;
|
||
21 | case 1: write_cmd |= TIMER_SEL1; |
||
22 | break;
|
||
23 | case 2: write_cmd |= TIMER_SEL2; |
||
24 | break;
|
||
25 | default: return 1; break; |
||
26 | } |
||
27 | //Change both LSB and MSB
|
||
28 | write_cmd |= TIMER_LSB_MSB; |
||
29 | //Keep 4 least significant bits
|
||
30 | write_cmd |= (status & (TIMER_MODE_MASK | TIMER_BCD)); |
||
31 | //Write cmd
|
||
32 | sys_outb(TIMER_CTRL, ) |
||
33 | |||
34 | |||
35 | 5 | up20180642 | return 1; |
36 | 4 | up20180655 | } |
37 | |||
38 | int (timer_subscribe_int)(uint8_t *bit_no) {
|
||
39 | /* To be implemented by the students */
|
||
40 | 5 | up20180642 | printf("%s is not yet implemented!\n", __func__);
|
41 | 4 | up20180655 | |
42 | 5 | up20180642 | return 1; |
43 | 4 | up20180655 | } |
44 | |||
45 | int (timer_unsubscribe_int)() {
|
||
46 | 5 | up20180642 | /* To be implemented by the students */
|
47 | printf("%s is not yet implemented!\n", __func__);
|
||
48 | 4 | up20180655 | |
49 | 5 | up20180642 | return 1; |
50 | 4 | up20180655 | } |
51 | |||
52 | void (timer_int_handler)() {
|
||
53 | 5 | up20180642 | /* To be implemented by the students */
|
54 | printf("%s is not yet implemented!\n", __func__);
|
||
55 | 4 | up20180655 | } |
56 | |||
57 | int (timer_get_conf)(uint8_t timer, uint8_t *st) {
|
||
58 | 9 | up20180642 | // Write read-back command to TIMER_CTRL
|
59 | 5 | up20180642 | u32_t cmd = TIMER_RB_CMD | TIMER_RB_COUNT_ | TIMER_RB_SEL(timer); |
60 | 10 | up20180655 | if(sys_outb(TIMER_CTRL, cmd)) return 1; |
61 | 4 | up20180655 | |
62 | 5 | up20180642 | int read_port;
|
63 | 7 | up20180655 | switch(timer) {
|
64 | 5 | up20180642 | case 0: read_port = TIMER_0; break; |
65 | case 1: read_port = TIMER_1; break; |
||
66 | case 2: read_port = TIMER_2; break; |
||
67 | 6 | up20180642 | default: return 1; break; |
68 | 5 | up20180642 | } |
69 | if(util_sys_inb(read_port, st)) return 1; |
||
70 | return 0; |
||
71 | 4 | up20180655 | } |
72 | |||
73 | 6 | up20180642 | int (timer_display_conf)(uint8_t timer, uint8_t st, enum timer_status_field field) { |
74 | union timer_status_field_val conf;
|
||
75 | uint8_t in_mode; |
||
76 | if(field == tsf_all || field == tsf_initial){
|
||
77 | in_mode = (st & TIMER_INMODE_MASK) >> TIMER_INMODE_POS; |
||
78 | switch(in_mode){
|
||
79 | 9 | up20180642 | case 0: conf.in_mode = INVAL_val ; break; //000 |
80 | case 1: conf.in_mode = LSB_only ; break; //001 |
||
81 | case 2: conf.in_mode = MSB_only ; break; //010 |
||
82 | case 3: conf.in_mode = MSB_after_LSB; break; //011 |
||
83 | 6 | up20180642 | default: return 1; break; |
84 | } |
||
85 | if(timer_print_config(timer, field, conf)) return 1; |
||
86 | 5 | up20180642 | } |
87 | 8 | up20180642 | if(field == tsf_mode){
|
88 | 6 | up20180642 | conf.count_mode = (st & TIMER_MODE_MASK)>>TIMER_MODE_POS; |
89 | if(timer_print_config(timer, field, conf)) return 1; |
||
90 | } |
||
91 | 8 | up20180642 | if(field == tsf_base){
|
92 | 6 | up20180642 | conf.bcd = st & TIMER_BCD; |
93 | if(timer_print_config(timer, field, conf)) return 1; |
||
94 | } |
||
95 | return 0; |
||
96 | } |