Project

General

Profile

Statistics
| Revision:

root / lab2 / timer.c @ 27

History | View | Annotate | Download (3.52 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
    // Frequencies out this range are not supported (by limitation of hardware)
11
    if (freq > TIMER_FREQ || freq < TIMER_MIN_FREQ) {
12
        printf("%s: Frequency out of range, must be between %d and %d.\n", __func__, TIMER_MIN_FREQ, TIMER_FREQ);
13
        return 1;
14
    }
15

    
16
    uint8_t status = 0;
17
    if (timer_get_conf(timer, &status)) return 1;
18
    //Make command
19
    uint8_t write_cmd = 0;
20
    //Select timer
21
    switch(timer) {
22
        case 0: write_cmd |= TIMER_SEL0; break;
23
        case 1: write_cmd |= TIMER_SEL1; break;
24
        case 2: write_cmd |= TIMER_SEL2; 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
    if (sys_outb(TIMER_CTRL, write_cmd)) return 1;
33

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

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

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

    
51
    return 0;
52
}
53

    
54
int hook_id;
55

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

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

    
68
int no_interrupts = 0;
69
void (timer_int_handler)() {
70
    no_interrupts++;
71
}
72

    
73
int (timer_get_conf)(uint8_t timer, uint8_t *st) {
74
    if(st == NULL) return 1;
75
    // Write read-back command to TIMER_CTRL
76
    u32_t cmd = TIMER_RB_CMD | TIMER_RB_COUNT_ | TIMER_RB_SEL(timer);
77
    if(sys_outb(TIMER_CTRL, cmd)) return 1;
78
    int read_port;
79
    switch(timer) {
80
        case 0: read_port = TIMER_0; break;
81
        case 1: read_port = TIMER_1; break;
82
        case 2: read_port = TIMER_2; break;
83
        default: return 1;           break;
84
    }
85
    if(util_sys_inb(read_port, st)) return 1;
86
    return 0;
87
}
88

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