summaryrefslogtreecommitdiffstats
path: root/mig_test/software/libhal/timer.c
blob: 789c5655e69d50e66dc902ee8651a4d60ecbf97a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "peripherie.h"

#include "timer.h"

////////////////////////////////////////
// timer functions
// timer 0.0 is used for usleep
// timer 0.0 is used for msleep
// timer 0.0 is used for sleep (via msleep)
//
// timer 0.0 ticks with milliseconds
// timer 0.1 ticks with seconds


// wait for a given time in micro seconds
void usleep(uint32_t usec)
{
    uint32_t tcr;

    // 1 usec = 6
    timer0->e[0].reload = (F_CPU/TIMER_PRESCALER/1000000)*usec;
    timer0->e[0].ctrl   = TIMER_ENABLE | TIMER_LOAD;

    do 
    {
        tcr = timer0->e[0].ctrl;
    } while ( (tcr & TIMER_ENABLE));
}


// wait for given time in milli seconds
void msleep(uint32_t msec)
{
    uint32_t tcr;

    // some values for 50MHz @ Spartan 3e
    // 1 msec    = 6250
    // 167 msec  = 2**20 (20 bit counter) 391 slices
    // 2684 msec = 2**24 (24 bit counter) 450 slices
    //           = 2**32 (32 bit counter) 572 slices
    // some values for 52MHz @ Spartan 6
    // 1 msec    = 6500
    // 161 msec  = 2**20 (20 bit counter)
    // 2581 msec = 2**24 (24 bit counter) 450 slices
    // 660 sec   = 2**32 (32 bit counter) 572 slices
    timer0->e[0].reload = (F_CPU/TIMER_PRESCALER/1000)*msec;
    timer0->e[0].ctrl   = TIMER_ENABLE | TIMER_LOAD;

    do 
    {
        tcr = timer0->e[0].ctrl;
    } while ( (tcr & TIMER_ENABLE));
}


// wait for given time in seconds
void sleep(uint32_t sec)
{
    uint32_t timer;

    for (timer=0; timer<sec; timer++)
    {
        msleep( 100);
        msleep( 100);
        msleep( 100);
        msleep( 100);
        msleep( 100);
        msleep( 100);
        msleep( 100);
        msleep( 100);
        msleep( 100);
        msleep( 100);
    }
}



// deliver the milliseconds from timer 0.0
uint32_t msecs( void)
{
    return( timer0->e[0].value);
}


// deliver the seconds from timer 0.1
uint32_t seconds( void)
{
    return( timer0->e[1].value);
}


// deliver the time (in seconds and fraction) from timer
uint32_t get_time( void)
{
    uint32_t value;

    TIMER_STOP;

    // combine values (seconds.milliseconds)
    value = timer0->e[1].value * 1000 + timer0->e[0].value;

    TIMER_RUN;

    return( value);
}


// just a loop
void wait( uint32_t value)
{
    uint32_t i;

    for (i=0; i<value; i++) {}
}


// initialisation for the timer
void timer_init( void)
{
    timer0->scaler_reload = TIMER_PRESCALER-1; // set prescaler
    
    // set timer 0.1 in chain mode to timer 0.0
    // so it counts in seconds
    timer0->e[1].reload = 0xffffffff;
    timer0->e[1].ctrl   = TIMER_ENABLE | TIMER_RESTART | TIMER_LOAD | TIMER_CHAIN;
    
    // set timer 0.0 to free running in msec 
    timer0->e[0].reload = (F_CPU/TIMER_PRESCALER/CLOCKS_PER_SECOND);
    timer0->e[0].ctrl   = TIMER_ENABLE | TIMER_RESTART | TIMER_LOAD;
}


OpenPOWER on IntegriCloud