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

Subversion Repositories xulalx25soc

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

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