Project

General

Profile

Statistics
| Revision:

root / lab2 / timer.c @ 10

History | View | Annotate | Download (2.61 KB)

1
#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
    // 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
    return 1;
36
}
37

    
38
int (timer_subscribe_int)(uint8_t *bit_no) {
39
    /* To be implemented by the students */
40
    printf("%s is not yet implemented!\n", __func__);
41

    
42
    return 1;
43
}
44

    
45
int (timer_unsubscribe_int)() {
46
    /* To be implemented by the students */
47
    printf("%s is not yet implemented!\n", __func__);
48

    
49
    return 1;
50
}
51

    
52
void (timer_int_handler)() {
53
    /* To be implemented by the students */
54
    printf("%s is not yet implemented!\n", __func__);
55
}
56

    
57
int (timer_get_conf)(uint8_t timer, uint8_t *st) {
58
    // Write read-back command to TIMER_CTRL
59
    u32_t cmd = TIMER_RB_CMD | TIMER_RB_COUNT_ | TIMER_RB_SEL(timer);
60
    if(sys_outb(TIMER_CTRL, cmd)) return 1;
61

    
62
    int read_port;
63
    switch(timer) {
64
        case 0: read_port = TIMER_0; break;
65
        case 1: read_port = TIMER_1; break;
66
        case 2: read_port = TIMER_2; break;
67
        default: return 1;           break;
68
    }
69
    if(util_sys_inb(read_port, st)) return 1;
70
    return 0;
71
}
72

    
73
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
            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
            default: return 1; break;
84
        }
85
        if(timer_print_config(timer, field, conf)) return 1;
86
    }
87
    if(field == tsf_mode){
88
        conf.count_mode = (st & TIMER_MODE_MASK)>>TIMER_MODE_POS;
89
        if(timer_print_config(timer, field, conf)) return 1;
90
    }
91
    if(field == tsf_base){
92
        conf.bcd = st & TIMER_BCD;
93
        if(timer_print_config(timer, field, conf)) return 1;
94
    }
95
    return 0;
96
}