Project

General

Profile

Statistics
| Revision:

root / lab2 / timer.c @ 12

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