Project

General

Profile

Statistics
| Revision:

root / lab2 / timer.c @ 24

History | View | Annotate | Download (3.34 KB)

1
#include <lcom/lcf.h>
2
#include <lcom/timer.h>
3

    
4
#include <stdint.h>
5

    
6
#include "i8254.h"
7

    
8
int no_interrupts = 0;
9

    
10
int (timer_set_frequency)(uint8_t timer, uint32_t freq) {
11

    
12
    // Frequencies out this range are not supported (by limitation of hardware)
13
    if (freq > TIMER_FREQ || freq < TIMER_MIN_FREQ) return 1;
14

    
15
    uint8_t status = 0;
16
    if (timer_get_conf(timer, &status)) return 1;
17
    //Make command
18
    uint8_t write_cmd = 0;
19
    //Select timer
20
    switch(timer) {
21
        case 0: write_cmd |= TIMER_SEL0; break;
22
        case 1: write_cmd |= TIMER_SEL1; break;
23
        case 2: write_cmd |= TIMER_SEL2; break;
24
        default: return 1;               break;
25
    }
26
    //Change both LSB and MSB
27
    write_cmd |= TIMER_LSB_MSB;
28
    //Keep 4 least significant bits
29
    write_cmd |= (status & (TIMER_MODE_MASK | TIMER_BCD));
30
    //Write cmd
31
    if (sys_outb(TIMER_CTRL, write_cmd)) return 1;
32

    
33
    // counter_init = clock/freq
34
    uint16_t counter_init = (uint16_t)(TIMER_FREQ / freq);
35

    
36
    int timer_port = 0;
37
    switch(timer) {
38
        case 0: timer_port = TIMER_0; break;
39
        case 1: timer_port = TIMER_1; break;
40
        case 2: timer_port = TIMER_2; break;
41
        default: return 1;            break;
42
    }
43
    uint8_t lsb = 0, msb = 0;
44
    if (util_get_LSB(counter_init, &lsb)) return 1;
45
    if (util_get_MSB(counter_init, &msb)) return 1;
46

    
47
    if (sys_outb(timer_port, lsb)) return 1;
48
    if (sys_outb(timer_port, msb)) return 1;
49

    
50
    return 0;
51
}
52

    
53
int hook_id = 2;
54

    
55
int (timer_subscribe_int)(uint8_t *bit_no) {
56
    if(sys_irqsetpolicy(TIMER0_IRQ, IRQ_REENABLE, &hook_id)) return 1;
57
    *bit_no = hook_id;
58
    return 0;
59
}
60

    
61
int (timer_unsubscribe_int)() {
62
    if(sys_irqrmpolicy(&hook_id)) return 1;
63
    return 0;
64
}
65

    
66
void (timer_int_handler)() {
67
    no_interrupts++;
68
}
69

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

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

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