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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sw/] [host/] [wbsettime.cpp] - Blame information for rev 4

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

Line No. Rev Author Line
1 4 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    wbsettime
4
//
5
// Project:     OpenArty, an entirely open SoC based upon the Arty platform
6
//
7
// Purpose:     To set the on-board clock of an FPGA using the rtclight or
8
//              rtcclock FPGA protocols.
9
//
10
// Creator:     Dan Gisselquist, Ph.D.
11
//              Gisselquist Technology, LLC
12
//
13
////////////////////////////////////////////////////////////////////////////////
14
//
15
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
16
//
17
// This program is free software (firmware): you can redistribute it and/or
18
// modify it under the terms of  the GNU General Public License as published
19
// by the Free Software Foundation, either version 3 of the License, or (at
20
// your option) any later version.
21
//
22
// This program is distributed in the hope that it will be useful, but WITHOUT
23
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
24
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25
// for more details.
26
//
27
// You should have received a copy of the GNU General Public License along
28
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
29
// target there if the PDF file isn't present.)  If not, see
30
// <http://www.gnu.org/licenses/> for a copy.
31
//
32
// License:     GPL, v3, as defined and found on www.gnu.org,
33
//              http://www.gnu.org/licenses/gpl.html
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
        DEVBUS::BUSW    v;
61
 
62
        bool    set_time = true, read_hack = false;
63
 
64
        FPGAOPEN(m_fpga);
65
 
66
        signal(SIGSTOP, closeup);
67
        signal(SIGHUP, closeup);
68
 
69
 
70
        time_t  now, then;
71
 
72
        // We first wait for a second change, because we don't know how
73
        // much time this will take.
74
        DEVBUS::BUSW    clockword = 0l, dateword = 0l;
75
        clockword = m_fpga->readio(R_CLOCK);
76
 
77
        now = time(NULL);
78
        while(time(NULL) == now)
79
                ;
80
 
81
        if (set_time) {
82
                // Now, we have one second to set the time
83
                struct  tm      *tmp;
84
                int             sleepv = 0;
85
 
86
                then = now+1;
87
                tmp = localtime(&then);
88
                clockword &= ~0x03fffff;
89
 
90
                if (tmp->tm_sec != 0) {
91
                        // printf("THEN SECONDS = %d, ADDING %d\n",
92
                                // tmp->tm_sec, 60-tmp->tm_sec);
93
                        if (tmp->tm_sec < 58)
94
                                sleepv = 59 - tmp->tm_sec;
95
                        then += 60 - tmp->tm_sec;
96
                        tmp = localtime(&then);
97
                        // printf("Sleeping %d seconds (THEN->SEC = %d)\n", sleepv, tmp->tm_sec);
98
                }
99
 
100
                // printf("ORIGINAL : %02d:%02d:%02d\n", tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
101
                // Seconds
102
                clockword |= (((tmp->tm_sec)%10)&0x0f);
103
                clockword |= (((tmp->tm_sec)/10)&0x0f)<< 4;
104
                // Minutes
105
                clockword |= (((tmp->tm_min)%10)&0x0f)<< 8;
106
                clockword |= (((tmp->tm_min)/10)&0x0f)<<12;
107
                // Hours
108
                clockword |= (((tmp->tm_hour)%10)&0x0f)<<16;
109
                clockword |= (((tmp->tm_hour)/10)&0x0f)<<20;
110
 
111
                // Years
112
                dateword   = 0x00000000;
113
                dateword  |= (((tmp->tm_year+1900)/1000)&0x0f)<<28;
114
                dateword  |=((((tmp->tm_year+1900)/100 )%10)&0x0f)<<24;
115
                dateword  |=((((tmp->tm_year+1900)/10  )%10)&0x0f)<<20;
116
                dateword  |=((((tmp->tm_year+1900)     )%10)&0x0f)<<16;
117
                dateword  |= (((tmp->tm_mon +1)/10)&0x0f)<<12;
118
                dateword  |= (((tmp->tm_mon +1)%10)&0x0f)<< 8;
119
                dateword  |= (((tmp->tm_mday  )/10)&0x0f)<< 4;
120
                dateword  |= (((tmp->tm_mday  )%10)&0x0f);
121
                if (sleepv > 0)
122
                        sleep(sleepv);
123
 
124
                while(time(NULL) < then)
125
                        ;
126
                m_fpga->writeio(R_CLOCK, clockword);
127
 
128
                printf("Time set to   %06x\n", clockword & 0x03fffff);
129
#ifdef R_DATE   // If we have the date capability
130
                m_fpga->writeio(R_DATE, dateword);
131
                printf("Date set to %08x\n", dateword);
132
                printf("(Now reads %08x)\n", m_fpga->readio(R_DATE));
133
#endif // R_DATE
134
 
135
                now = then;
136
        } if (read_hack) {
137
                then = time(NULL) + 5;
138
                while(time(NULL) < then)
139
                        ;
140
                clockword = m_fpga->readio(R_CLOCK);
141
        }
142
 
143
        if (m_fpga->poll())
144
                printf("FPGA was interrupted\n");
145
        delete  m_fpga;
146
}
147
 

powered by: WebSVN 2.1.0

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