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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [sw/] [wbsettime.cpp] - Blame information for rev 7

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

Line No. Rev Author Line
1 5 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    wbsettime.cpp
4
//
5
// Project:     XuLA2 board
6
//
7
// Purpose:     To give a user access, via a command line program, to set the
8 7 dgisselq
//              real time clock within the FPGA.  Note, however, that the RTC
9
//      clock device only sets the subseconds field when you program it at the
10
//      top of a minute.  This program, therefore, will wait 'til the top of a 
11
//      minute to set the clock.  It can be annoying, but ... it works.
12 5 dgisselq
//
13
//
14
// Creator:     Dan Gisselquist, Ph.D.
15
//              Gisselquist Technology, LLC
16
//
17
////////////////////////////////////////////////////////////////////////////////
18
//
19
// Copyright (C) 2015, Gisselquist Technology, LLC
20
//
21
// This program is free software (firmware): you can redistribute it and/or
22
// modify it under the terms of  the GNU General Public License as published
23
// by the Free Software Foundation, either version 3 of the License, or (at
24
// your option) any later version.
25
//
26
// This program is distributed in the hope that it will be useful, but WITHOUT
27
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
28
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
29
// for more details.
30
//
31
// License:     GPL, v3, as defined and found on www.gnu.org,
32
//              http://www.gnu.org/licenses/gpl.html
33
//
34
//
35
////////////////////////////////////////////////////////////////////////////////
36
//
37
//
38
//
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <unistd.h>
42
#include <strings.h>
43
#include <ctype.h>
44
#include <string.h>
45
#include <signal.h>
46
#include <assert.h>
47
#include <time.h>
48
 
49
#include "port.h"
50
#include "llcomms.h"
51
#include "regdefs.h"
52
 
53
DEVBUS  *m_fpga;
54
void    closeup(int v) {
55
        m_fpga->kill();
56
        exit(0);
57
}
58
 
59
int main(int argc, char **argv) {
60 7 dgisselq
        bool    set_time = true;
61 5 dgisselq
 
62
        FPGAOPEN(m_fpga);
63
 
64
        signal(SIGSTOP, closeup);
65
        signal(SIGHUP, closeup);
66
 
67
 
68
        time_t  now, then;
69
 
70
        // We first wait for a second change, because we don't know how
71
        // much time this will take.
72
        DEVBUS::BUSW    clockword = 0l, dateword = 0l;
73
        clockword = m_fpga->readio(R_CLOCK);
74
 
75
        now = time(NULL);
76
        while(time(NULL) == now)
77
                ;
78
 
79
        if (set_time) {
80
                // Now, we have one second to set the time
81
                struct  tm      *tmp;
82
                int             sleepv = 0;
83
 
84
                then = now+1;
85
                tmp = localtime(&then);
86
                clockword &= ~0x03fffff;
87
 
88
                if (tmp->tm_sec != 0) {
89
                        // printf("THEN SECONDS = %d, ADDING %d\n",
90
                                // tmp->tm_sec, 60-tmp->tm_sec);
91
                        if (tmp->tm_sec < 58)
92
                                sleepv = 59 - tmp->tm_sec;
93
                        then += 60 - tmp->tm_sec;
94
                        tmp = localtime(&then);
95 7 dgisselq
                        printf("Sleeping for %d seconds, so as to set time at the top of the minute\n", sleepv);
96 5 dgisselq
                }
97
 
98
                // printf("ORIGINAL : %02d:%02d:%02d\n", tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
99
                // Seconds
100
                clockword |= (((tmp->tm_sec)%10)&0x0f);
101
                clockword |= (((tmp->tm_sec)/10)&0x0f)<< 4;
102
                // Minutes
103
                clockword |= (((tmp->tm_min)%10)&0x0f)<< 8;
104
                clockword |= (((tmp->tm_min)/10)&0x0f)<<12;
105
                // Hours
106
                clockword |= (((tmp->tm_hour)%10)&0x0f)<<16;
107
                clockword |= (((tmp->tm_hour)/10)&0x0f)<<20;
108
 
109
                // Years
110
                dateword   = 0x00000000;
111
                dateword  |= (((tmp->tm_year+1900)/1000)&0x0f)<<28;
112
                dateword  |=((((tmp->tm_year+1900)/100 )%10)&0x0f)<<24;
113
                dateword  |=((((tmp->tm_year+1900)/10  )%10)&0x0f)<<20;
114
                dateword  |=((((tmp->tm_year+1900)     )%10)&0x0f)<<16;
115
                dateword  |= (((tmp->tm_mon +1)/10)&0x0f)<<12;
116
                dateword  |= (((tmp->tm_mon +1)%10)&0x0f)<< 8;
117
                dateword  |= (((tmp->tm_mday  )/10)&0x0f)<< 4;
118
                dateword  |= (((tmp->tm_mday  )%10)&0x0f);
119
                if (sleepv > 0)
120
                        sleep(sleepv);
121
 
122
                while(time(NULL) < then)
123
                        ;
124
                m_fpga->writeio(R_CLOCK, clockword);
125
 
126
                printf("Time set to   %06x\n", clockword & 0x03fffff);
127
#ifdef R_DATE   // If we have the date capability
128
                m_fpga->writeio(R_DATE, dateword);
129
                printf("Date set to %08x\n", dateword);
130
                printf("(Now reads %08x)\n", m_fpga->readio(R_DATE));
131
#endif // R_DATE
132
 
133
                now = then;
134
        }
135
 
136
        delete  m_fpga;
137
}
138
 

powered by: WebSVN 2.1.0

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