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

Subversion Repositories xulalx25soc

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

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 17 dgisselq
#include "llcomms.h"
50
#include "usbi.h"
51 5 dgisselq
#include "port.h"
52
#include "regdefs.h"
53
 
54 17 dgisselq
FPGA    *m_fpga;
55 5 dgisselq
void    closeup(int v) {
56
        m_fpga->kill();
57
        exit(0);
58
}
59
 
60
int main(int argc, char **argv) {
61 7 dgisselq
        bool    set_time = true;
62 17 dgisselq
        int     skp=0, port = FPGAPORT;
63
        bool    use_usb = true;
64 5 dgisselq
 
65 17 dgisselq
        skp=1;
66
        for(int argn=0; argn<argc-skp; argn++) {
67
                if (argv[argn+skp][0] == '-') {
68
                        if (argv[argn+skp][1] == 'u')
69
                                use_usb = true;
70
                        else if (argv[argn+skp][1] == 'p') {
71
                                use_usb = false;
72
                                if (isdigit(argv[argn+skp][2]))
73
                                        port = atoi(&argv[argn+skp][2]);
74
                        }
75
                        skp++; argn--;
76
                } else
77
                        argv[argn] = argv[argn+skp];
78
        } argc -= skp;
79 5 dgisselq
 
80 17 dgisselq
        if (use_usb)
81
                m_fpga = new FPGA(new USBI());
82
        else
83
                m_fpga = new FPGA(new NETCOMMS(FPGAHOST, port));
84
 
85 5 dgisselq
        signal(SIGSTOP, closeup);
86
        signal(SIGHUP, closeup);
87
 
88
 
89
        time_t  now, then;
90
 
91
        // We first wait for a second change, because we don't know how
92
        // much time this will take.
93
        DEVBUS::BUSW    clockword = 0l, dateword = 0l;
94
        clockword = m_fpga->readio(R_CLOCK);
95
 
96
        now = time(NULL);
97
        while(time(NULL) == now)
98
                ;
99
 
100
        if (set_time) {
101
                // Now, we have one second to set the time
102
                struct  tm      *tmp;
103
                int             sleepv = 0;
104
 
105
                then = now+1;
106
                tmp = localtime(&then);
107
                clockword &= ~0x03fffff;
108
 
109
                if (tmp->tm_sec != 0) {
110
                        // printf("THEN SECONDS = %d, ADDING %d\n",
111
                                // tmp->tm_sec, 60-tmp->tm_sec);
112
                        if (tmp->tm_sec < 58)
113
                                sleepv = 59 - tmp->tm_sec;
114
                        then += 60 - tmp->tm_sec;
115
                        tmp = localtime(&then);
116 7 dgisselq
                        printf("Sleeping for %d seconds, so as to set time at the top of the minute\n", sleepv);
117 5 dgisselq
                }
118
 
119
                // printf("ORIGINAL : %02d:%02d:%02d\n", tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
120
                // Seconds
121
                clockword |= (((tmp->tm_sec)%10)&0x0f);
122
                clockword |= (((tmp->tm_sec)/10)&0x0f)<< 4;
123
                // Minutes
124
                clockword |= (((tmp->tm_min)%10)&0x0f)<< 8;
125
                clockword |= (((tmp->tm_min)/10)&0x0f)<<12;
126
                // Hours
127
                clockword |= (((tmp->tm_hour)%10)&0x0f)<<16;
128
                clockword |= (((tmp->tm_hour)/10)&0x0f)<<20;
129
 
130
                // Years
131
                dateword   = 0x00000000;
132
                dateword  |= (((tmp->tm_year+1900)/1000)&0x0f)<<28;
133
                dateword  |=((((tmp->tm_year+1900)/100 )%10)&0x0f)<<24;
134
                dateword  |=((((tmp->tm_year+1900)/10  )%10)&0x0f)<<20;
135
                dateword  |=((((tmp->tm_year+1900)     )%10)&0x0f)<<16;
136
                dateword  |= (((tmp->tm_mon +1)/10)&0x0f)<<12;
137
                dateword  |= (((tmp->tm_mon +1)%10)&0x0f)<< 8;
138
                dateword  |= (((tmp->tm_mday  )/10)&0x0f)<< 4;
139
                dateword  |= (((tmp->tm_mday  )%10)&0x0f);
140
                if (sleepv > 0)
141
                        sleep(sleepv);
142
 
143
                while(time(NULL) < then)
144
                        ;
145
                m_fpga->writeio(R_CLOCK, clockword);
146
 
147
                printf("Time set to   %06x\n", clockword & 0x03fffff);
148
#ifdef R_DATE   // If we have the date capability
149
                m_fpga->writeio(R_DATE, dateword);
150
                printf("Date set to %08x\n", dateword);
151
                printf("(Now reads %08x)\n", m_fpga->readio(R_DATE));
152
#endif // R_DATE
153
 
154
                now = then;
155
        }
156
 
157
        delete  m_fpga;
158
}
159
 

powered by: WebSVN 2.1.0

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