Project

General

Profile

Statistics
| Revision:

root / lab2 / timer.c @ 15

History | View | Annotate | Download (3.15 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
    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
        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
    }
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
    if (sys_outb(TIMER_CTRL, write_cmd)) return 1;
26

    
27
    // counter_init = clock/freq
28
    uint16_t counter_init = (uint16_t)(TIMER_FREQ / freq);
29

    
30
    int timer_port = 0;
31
    switch(timer) {
32
        case 0: timer_port = TIMER_0; break;
33
        case 1: timer_port = TIMER_1; break;
34
        case 2: timer_port = TIMER_2; break;
35
        default: return 1;            break;
36
    }
37
    uint8_t lsb = 0, msb = 0;
38
    if (util_get_LSB(counter_init, &lsb)) return 1;
39
    if (util_get_MSB(counter_init, &msb)) return 1;
40

    
41
    if (sys_outb(timer_port, lsb)) return 1;
42
    if (sys_outb(timer_port, msb)) return 1;
43

    
44
    //if (sys_outw(timer_port, counter_init)) return 1;
45

    
46
    return 0;
47
}
48

    
49
int (timer_subscribe_int)(uint8_t *bit_no) {
50
    /* To be implemented by the students */
51
    printf("%s is not yet implemented!\n", __func__);
52

    
53
    return 1;
54
}
55

    
56
int (timer_unsubscribe_int)() {
57
    /* To be implemented by the students */
58
    printf("%s is not yet implemented!\n", __func__);
59

    
60
    return 1;
61
}
62

    
63
void (timer_int_handler)() {
64
    /* To be implemented by the students */
65
    printf("%s is not yet implemented!\n", __func__);
66
}
67

    
68
int (timer_get_conf)(uint8_t timer, uint8_t *st) {
69
    // Write read-back command to TIMER_CTRL
70
    u32_t cmd = TIMER_RB_CMD | TIMER_RB_COUNT_ | TIMER_RB_SEL(timer);
71
    if(sys_outb(TIMER_CTRL, cmd)) return 1;
72

    
73
    int read_port;
74
    switch(timer) {
75
        case 0: read_port = TIMER_0; break;
76
        case 1: read_port = TIMER_1; break;
77
        case 2: read_port = TIMER_2; break;
78
        default: return 1;           break;
79
    }
80
    if(util_sys_inb(read_port, st)) return 1;
81
    return 0;
82
}
83

    
84
int (timer_display_conf)(uint8_t timer, uint8_t st, enum timer_status_field field) {
85
    union timer_status_field_val conf;
86
    uint8_t in_mode;
87
    if(field == tsf_initial){
88
        in_mode = (st & TIMER_INMODE_MASK) >> TIMER_INMODE_POS;
89
        switch(in_mode){
90
            case 0: conf.in_mode = INVAL_val    ; break; //000
91
            case 1: conf.in_mode = LSB_only     ; break; //001
92
            case 2: conf.in_mode = MSB_only     ; break; //010
93
            case 3: conf.in_mode = MSB_after_LSB; break; //011
94
            default: return 1; break;
95
        }
96
        if(timer_print_config(timer, field, conf)) return 1;
97
    }
98
    if(field == tsf_mode){
99
        conf.count_mode = (st & TIMER_MODE_MASK)>>TIMER_MODE_POS;
100
        if(timer_print_config(timer, field, conf)) return 1;
101
    }
102
    if(field == tsf_base){
103
        conf.bcd = st & TIMER_BCD;
104
        if(timer_print_config(timer, field, conf)) return 1;
105
    }
106
    return 0;
107
}