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

Subversion Repositories amber

[/] [amber/] [trunk/] [sw/] [boot-loader-ethmac/] [timer.c] - Blame information for rev 61

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 61 csantifort
/*----------------------------------------------------------------
2
//                                                              //
3
//  boot-loader-ethmac.c                                        //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  The main functions for the boot loader application. This    //
10
//  application is embedded in the FPGA's SRAM and is used      //
11
//  to load larger applications into the DDR3 memory on         //
12
//  the development board.                                      //
13
//                                                              //
14
//  Author(s):                                                  //
15
//      - Conor Santifort, csantifort.amber@gmail.com           //
16
//                                                              //
17
//////////////////////////////////////////////////////////////////
18
//                                                              //
19
// Copyright (C) 2011 Authors and OPENCORES.ORG                 //
20
//                                                              //
21
// This source file may be used and distributed without         //
22
// restriction provided that this copyright statement is not    //
23
// removed from the file and that any derivative work contains  //
24
// the original copyright notice and the associated disclaimer. //
25
//                                                              //
26
// This source file is free software; you can redistribute it   //
27
// and/or modify it under the terms of the GNU Lesser General   //
28
// Public License as published by the Free Software Foundation; //
29
// either version 2.1 of the License, or (at your option) any   //
30
// later version.                                               //
31
//                                                              //
32
// This source is distributed in the hope that it will be       //
33
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
34
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
35
// PURPOSE.  See the GNU Lesser General Public License for more //
36
// details.                                                     //
37
//                                                              //
38
// You should have received a copy of the GNU Lesser General    //
39
// Public License along with this source; if not, download it   //
40
// from http://www.opencores.org/lgpl.shtml                     //
41
//                                                              //
42
----------------------------------------------------------------*/
43
 
44
#include "amber_registers.h"
45
#include "utilities.h"
46
#include "timer.h"
47
 
48
 
49
/* Global variable for the current time */
50
time_t* current_time_g;
51
 
52
 
53
time_t* init_timer()
54
{
55
    time_t* timer;
56
    timer= malloc(sizeof(time_t));
57
    timer->milliseconds = 0;
58
    timer->seconds      = 0;
59
    return timer;
60
}
61
 
62
 
63
 
64
/*Initialize a global variable that keeps
65
  track of the current up time. Timers are
66
  compared against this running value.
67
*/
68
void init_current_time()
69
{
70
    /* Configure timer 0 */
71
    /* Counts down from this value and generates an interrupt every 1/100 seconds */
72
    *(unsigned int *) ( ADR_AMBER_TM_TIMER0_LOAD ) = 1562; // 16-bit, x 256
73
    *(unsigned int *) ( ADR_AMBER_TM_TIMER0_CTRL ) = 0xc8;  // enable[7], periodic[6], 
74
 
75
    /* Enable timer 0 interrupt */
76
    *(unsigned int *) ( ADR_AMBER_IC_IRQ0_ENABLESET ) = 0x020;
77
 
78
    current_time_g = malloc(sizeof(time_t));
79
    current_time_g->milliseconds = 0;
80
    current_time_g->seconds = 0;
81
}
82
 
83
 
84
 
85
void timer_interrupt()
86
{
87
    /* Clear timer 0 interrupt in timer */
88
    *(unsigned int *) ( ADR_AMBER_TM_TIMER0_CLR ) = 1;
89
 
90
    /* Disable timer 0 interrupt in interrupt controller */
91
    *(unsigned int *) ( ADR_AMBER_IC_IRQ0_ENABLECLR ) = 0x020;
92
 
93
    /* Update the global current_time variable */
94
    if (current_time_g->milliseconds == 990) {
95
        current_time_g->milliseconds = 0;
96
        current_time_g->seconds++;
97
        }
98
    else {
99
        current_time_g->milliseconds+=10;
100
        }
101
 
102
    /* Enable timer 0 interrupt in interrupt controller */
103
    *(unsigned int *) ( ADR_AMBER_IC_IRQ0_ENABLESET ) = 0x020;
104
}
105
 
106
 
107
/* Return true if timer has expired */
108
int timer_expired(time_t * expiry_time)
109
{
110
    if (!expiry_time->seconds && !expiry_time->milliseconds)
111
        /* timer is not set */
112
        return 0;
113
    else if (expiry_time->seconds < current_time_g->seconds)
114
        return 1;
115
    else if ((expiry_time->seconds      == current_time_g->seconds) &&
116
             (expiry_time->milliseconds < current_time_g->milliseconds))
117
        return 1;
118
    else
119
        return 0;
120
}
121
 
122
 
123
/* Set the timer to current time plus 5 seconds */
124
void set_timer (time_t* timer, int milliseconds)
125
{
126
    int seconds = _div(milliseconds, 1000);
127
    int mseconds = milliseconds - seconds*1000;  /* milliseconds % 1000  */
128
 
129
 
130
    if (current_time_g->milliseconds >= (1000 - mseconds)) {
131
        timer->seconds      = current_time_g->seconds + 1;
132
        timer->milliseconds = current_time_g->milliseconds + mseconds - 1000;
133
        }
134
    else {
135
        timer->seconds      = current_time_g->seconds;
136
        timer->milliseconds = current_time_g->milliseconds + mseconds;
137
        }
138
 
139
   timer->seconds += seconds;
140
}
141
 

powered by: WebSVN 2.1.0

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