OpenCores
URL https://opencores.org/ocsvn/xenie/xenie/trunk

Subversion Repositories xenie

[/] [xenie/] [trunk/] [examples/] [Eth_example/] [mb_fw/] [xenie_eth_test_womtd/] [src/] [timers.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 DFC
/******************************************************************************
2
**
3
** (C) Copyright 2017 DFC Design, s.r.o., Brno, Czech Republic
4
** Author: Marek Kvas (m.kvas@dspfpga.com)
5
**
6
****************************************************************************
7
**
8
** This file is part of Xenia Ethernet Example project.
9
**
10
** Xenia Ethernet Example project is free software: you can
11
** redistribute it and/or modify it under the terms of
12
** the GNU Lesser General Public License as published by the Free
13
** Software Foundation, either version 3 of the License, or
14
** (at your option) any later version.
15
**
16
** Xenia Ethernet Example project is distributed in the hope that
17
** it will be useful, but WITHOUT ANY WARRANTY; without even
18
** the implied warranty of MERCHANTABILITY or FITNESS FOR A
19
** PARTICULAR PURPOSE.  See the GNU Lesser General Public License
20
** for more details.
21
**
22
** You should have received a copy of the GNU Lesser General Public
23
** License along with Xenia Ethernet Example project.  If not,
24
** see <http://www.gnu.org/licenses/>.
25
****************************************************************************
26
*/
27
 
28
#include "timers.h"
29
 
30
 
31
#define PERIODIC_TIMER_NO       0
32
#define ONE_SHOT_TIMER_NO   1
33
 
34
#define PERIODIC_TIMER_RST_VAL (0xffffffff - (TIMER_FREQ/1000)) /* ms */
35
 
36
/* internal clock with millisecond step */
37
static volatile u32 ms_time = 0;
38
static volatile int one_shot_flag = 0;
39
 
40
static XTmrCtr* TmrCtrInst;
41
 
42
u32 timers_ms_now()
43
{
44
        return ms_time;
45
}
46
 
47
u32 Timers_ms_elapsed(u32 start)
48
{
49
        u32 now;
50
 
51
        now = timers_ms_now();
52
        return timers_ms_diff(start, now);
53
}
54
 
55
u32 timers_ms_diff(u32 start, u32 stop)
56
{
57
        u32 elapsed;
58
 
59
        if (stop >= start)
60
                elapsed = (stop - start);
61
        else
62
                elapsed = 0xffffffff - (start - stop);
63
 
64
        return elapsed;
65
}
66
 
67
u32 timers_ms_elapsed_update(u32 *start)
68
{
69
        u32 now;
70
        u32 elapsed;
71
 
72
        now = timers_ms_now();
73
        elapsed = timers_ms_diff(now, *start);
74
        *start = now;
75
        return elapsed;
76
}
77
 
78
/* Timers handler */
79
void timers_Handler(void *CallBackRef, u8 TmrCtrNumber)
80
{
81
        XTmrCtr *InstancePtr = (XTmrCtr *)CallBackRef;
82
 
83
        /* service the periodic counter */
84
        if (XTmrCtr_IsExpired(InstancePtr, PERIODIC_TIMER_NO)) {
85
                ms_time++;
86
        }
87
 
88
}
89
 
90
 
91
 
92
/* Initialize timers (interrupts are already set */
93
 
94
int timers_init(XTmrCtr* TmrCtrInstancePtr, u16 DeviceId)
95
{
96
        int Status;
97
 
98
        TmrCtrInst = TmrCtrInstancePtr;
99
 
100
        /*
101
         * Initialize the timer counter so that it's ready to use,
102
         * specify the device ID that is generated in xparameters.h
103
         */
104
        Status = XTmrCtr_Initialize(TmrCtrInst, DeviceId);
105
        if (Status != XST_SUCCESS) {
106
                return XST_FAILURE;
107
        }
108
 
109
        /*
110
         * Setup the handler for both halfs of timer
111
         */
112
        XTmrCtr_SetHandler(TmrCtrInst, timers_Handler,
113
                        TmrCtrInst);
114
 
115
        /*
116
         * Enable the interrupt
117
         */
118
 
119
        /* Set up periodic counter */
120
        XTmrCtr_SetOptions(TmrCtrInst, PERIODIC_TIMER_NO,
121
                                XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION);
122
        /* Set reset value determining timer period */
123
        XTmrCtr_SetResetValue(TmrCtrInst, PERIODIC_TIMER_NO, PERIODIC_TIMER_RST_VAL);
124
 
125
 
126
        /* Set up one-shot counter */
127
        XTmrCtr_SetOptions(TmrCtrInst, ONE_SHOT_TIMER_NO,
128
                        XTC_DOWN_COUNT_OPTION/*XTC_INT_MODE_OPTION*/);
129
 
130
 
131
        /*
132
         * Start the timer counter such that it's incrementing by default
133
         */
134
        XTmrCtr_Start(TmrCtrInst, PERIODIC_TIMER_NO);
135
 
136
        return XST_SUCCESS;
137
}
138
 
139
void timers_udelay(u32 usDelay)
140
{
141
        u32 timer_preset;
142
 
143
        /* calculate preset value */
144
        timer_preset = (TIMER_FREQ/1000000) * usDelay; /* us */
145
 
146
        /* Set counter initial value */
147
        XTmrCtr_SetResetValue(TmrCtrInst, ONE_SHOT_TIMER_NO, timer_preset);
148
 
149
        /* Start timer */
150
        XTmrCtr_Start(TmrCtrInst, ONE_SHOT_TIMER_NO);
151
 
152
        /* Wait for timer to elapse */
153
        while (XTmrCtr_GetValue(TmrCtrInst, ONE_SHOT_TIMER_NO));
154
 
155
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.