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

Subversion Repositories amber

[/] [amber/] [trunk/] [sw/] [boot-loader-ethmac/] [timer.c] - Diff between revs 78 and 80

Show entire file | Details | Blame | View Log

Rev 78 Rev 80
Line 48... Line 48...
 
 
/* Global variable for the current time */
/* Global variable for the current time */
time_t* current_time_g;
time_t* current_time_g;
 
 
 
 
time_t* init_timer()
/* Create a timer object */
 
time_t* new_timer()
{
{
    time_t* timer;
    time_t* timer;
    timer= malloc(sizeof(time_t));
    timer= malloc(sizeof(time_t));
    timer->milliseconds = 0;
    timer->milliseconds = 0;
    timer->seconds      = 0;
    timer->seconds      = 0;
Line 63... Line 64...
 
 
/*Initialize a global variable that keeps
/*Initialize a global variable that keeps
  track of the current up time. Timers are
  track of the current up time. Timers are
  compared against this running value.
  compared against this running value.
*/
*/
void init_current_time()
void init_timer()
{
{
 
    /* Initialize the current time object */
 
    current_time_g = new_timer();
 
 
    /* Configure timer 0 */
    /* Configure timer 0 */
    /* Counts down from this value and generates an interrupt every 1/100 seconds */
    /* Counts down from this value and generates an interrupt every 1/100 seconds */
    *(unsigned int *) ( ADR_AMBER_TM_TIMER0_LOAD ) = 1562; // 16-bit, x 256
    *(unsigned int *) ( ADR_AMBER_TM_TIMER0_LOAD ) = 1562; // 16-bit, x 256
    *(unsigned int *) ( ADR_AMBER_TM_TIMER0_CTRL ) = 0xc8;  // enable[7], periodic[6],
    *(unsigned int *) ( ADR_AMBER_TM_TIMER0_CTRL ) = 0xc8;  // enable[7], periodic[6],
 
 
    /* Enable timer 0 interrupt */
    /* Enable timer 0 interrupt */
    *(unsigned int *) ( ADR_AMBER_IC_IRQ0_ENABLESET ) = 0x020;
    *(unsigned int *) ( ADR_AMBER_IC_IRQ0_ENABLESET ) = 0x020;
 
}
 
 
    current_time_g = malloc(sizeof(time_t));
 
    current_time_g->milliseconds = 0;
/* Set the timer to current time plus a number of milliseconds */
    current_time_g->seconds = 0;
void set_timer (time_t* timer, int milliseconds)
 
{
 
    int seconds = _div(milliseconds, 1000);
 
    int mseconds = milliseconds - seconds*1000;  /* milliseconds % 1000  */
 
 
 
    if (current_time_g->milliseconds >= (1000 - mseconds)) {
 
        timer->seconds      = current_time_g->seconds + 1;
 
        timer->milliseconds = current_time_g->milliseconds + mseconds - 1000;
 
        }
 
    else {
 
        timer->seconds      = current_time_g->seconds;
 
        timer->milliseconds = current_time_g->milliseconds + mseconds;
}
}
 
 
 
   timer->seconds += seconds;
 
}
 
 
 
 
void timer_interrupt()
void timer_interrupt()
{
{
    /* Clear timer 0 interrupt in timer */
    /* Clear timer 0 interrupt in timer */
Line 118... Line 136...
    else
    else
        return 0;
        return 0;
}
}
 
 
 
 
/* Set the timer to current time plus 5 seconds */
 
void set_timer (time_t* timer, int milliseconds)
 
{
 
    int seconds = _div(milliseconds, 1000);
 
    int mseconds = milliseconds - seconds*1000;  /* milliseconds % 1000  */
 
 
 
 
 
    if (current_time_g->milliseconds >= (1000 - mseconds)) {
 
        timer->seconds      = current_time_g->seconds + 1;
 
        timer->milliseconds = current_time_g->milliseconds + mseconds - 1000;
 
        }
 
    else {
 
        timer->seconds      = current_time_g->seconds;
 
        timer->milliseconds = current_time_g->milliseconds + mseconds;
 
        }
 
 
 
   timer->seconds += seconds;
 
}
 
 
 
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

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