| 1 |
4 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
//
|
| 3 |
51 |
dgisselq |
// Filename: wbsettime.cpp
|
| 4 |
4 |
dgisselq |
//
|
| 5 |
|
|
// Project: OpenArty, an entirely open SoC based upon the Arty platform
|
| 6 |
|
|
//
|
| 7 |
51 |
dgisselq |
// Purpose: To give a user access, via a command line program, to set the
|
| 8 |
|
|
// 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 |
4 |
dgisselq |
//
|
| 13 |
51 |
dgisselq |
//
|
| 14 |
4 |
dgisselq |
// Creator: Dan Gisselquist, Ph.D.
|
| 15 |
|
|
// Gisselquist Technology, LLC
|
| 16 |
|
|
//
|
| 17 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
| 18 |
|
|
//
|
| 19 |
51 |
dgisselq |
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
|
| 20 |
4 |
dgisselq |
//
|
| 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 |
|
|
// You should have received a copy of the GNU General Public License along
|
| 32 |
51 |
dgisselq |
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
|
| 33 |
4 |
dgisselq |
// target there if the PDF file isn't present.) If not, see
|
| 34 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
| 35 |
|
|
//
|
| 36 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
| 37 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
| 38 |
|
|
//
|
| 39 |
|
|
//
|
| 40 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
| 41 |
|
|
//
|
| 42 |
|
|
//
|
| 43 |
|
|
#include <stdio.h>
|
| 44 |
|
|
#include <stdlib.h>
|
| 45 |
|
|
#include <unistd.h>
|
| 46 |
|
|
#include <strings.h>
|
| 47 |
|
|
#include <ctype.h>
|
| 48 |
|
|
#include <string.h>
|
| 49 |
|
|
#include <signal.h>
|
| 50 |
|
|
#include <assert.h>
|
| 51 |
|
|
#include <time.h>
|
| 52 |
|
|
|
| 53 |
|
|
#include "port.h"
|
| 54 |
|
|
#include "llcomms.h"
|
| 55 |
|
|
#include "regdefs.h"
|
| 56 |
|
|
|
| 57 |
|
|
DEVBUS *m_fpga;
|
| 58 |
|
|
void closeup(int v) {
|
| 59 |
|
|
m_fpga->kill();
|
| 60 |
|
|
exit(0);
|
| 61 |
|
|
}
|
| 62 |
|
|
|
| 63 |
|
|
int main(int argc, char **argv) {
|
| 64 |
|
|
bool set_time = true, read_hack = false;
|
| 65 |
|
|
|
| 66 |
|
|
FPGAOPEN(m_fpga);
|
| 67 |
|
|
|
| 68 |
|
|
signal(SIGSTOP, closeup);
|
| 69 |
|
|
signal(SIGHUP, closeup);
|
| 70 |
|
|
|
| 71 |
|
|
|
| 72 |
|
|
time_t now, then;
|
| 73 |
|
|
|
| 74 |
|
|
// We first wait for a second change, because we don't know how
|
| 75 |
|
|
// much time this will take.
|
| 76 |
|
|
DEVBUS::BUSW clockword = 0l, dateword = 0l;
|
| 77 |
|
|
clockword = m_fpga->readio(R_CLOCK);
|
| 78 |
|
|
|
| 79 |
|
|
now = time(NULL);
|
| 80 |
|
|
while(time(NULL) == now)
|
| 81 |
|
|
;
|
| 82 |
|
|
|
| 83 |
|
|
if (set_time) {
|
| 84 |
|
|
// Now, we have one second to set the time
|
| 85 |
|
|
struct tm *tmp;
|
| 86 |
|
|
int sleepv = 0;
|
| 87 |
|
|
|
| 88 |
|
|
then = now+1;
|
| 89 |
|
|
tmp = localtime(&then);
|
| 90 |
|
|
clockword &= ~0x03fffff;
|
| 91 |
|
|
|
| 92 |
|
|
if (tmp->tm_sec != 0) {
|
| 93 |
|
|
// printf("THEN SECONDS = %d, ADDING %d\n",
|
| 94 |
|
|
// tmp->tm_sec, 60-tmp->tm_sec);
|
| 95 |
|
|
if (tmp->tm_sec < 58)
|
| 96 |
|
|
sleepv = 59 - tmp->tm_sec;
|
| 97 |
|
|
then += 60 - tmp->tm_sec;
|
| 98 |
|
|
tmp = localtime(&then);
|
| 99 |
|
|
// printf("Sleeping %d seconds (THEN->SEC = %d)\n", sleepv, tmp->tm_sec);
|
| 100 |
|
|
}
|
| 101 |
|
|
|
| 102 |
|
|
// printf("ORIGINAL : %02d:%02d:%02d\n", tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
|
| 103 |
|
|
// Seconds
|
| 104 |
|
|
clockword |= (((tmp->tm_sec)%10)&0x0f);
|
| 105 |
|
|
clockword |= (((tmp->tm_sec)/10)&0x0f)<< 4;
|
| 106 |
|
|
// Minutes
|
| 107 |
|
|
clockword |= (((tmp->tm_min)%10)&0x0f)<< 8;
|
| 108 |
|
|
clockword |= (((tmp->tm_min)/10)&0x0f)<<12;
|
| 109 |
|
|
// Hours
|
| 110 |
|
|
clockword |= (((tmp->tm_hour)%10)&0x0f)<<16;
|
| 111 |
|
|
clockword |= (((tmp->tm_hour)/10)&0x0f)<<20;
|
| 112 |
|
|
|
| 113 |
|
|
// Years
|
| 114 |
|
|
dateword = 0x00000000;
|
| 115 |
|
|
dateword |= (((tmp->tm_year+1900)/1000)&0x0f)<<28;
|
| 116 |
|
|
dateword |=((((tmp->tm_year+1900)/100 )%10)&0x0f)<<24;
|
| 117 |
|
|
dateword |=((((tmp->tm_year+1900)/10 )%10)&0x0f)<<20;
|
| 118 |
|
|
dateword |=((((tmp->tm_year+1900) )%10)&0x0f)<<16;
|
| 119 |
|
|
dateword |= (((tmp->tm_mon +1)/10)&0x0f)<<12;
|
| 120 |
|
|
dateword |= (((tmp->tm_mon +1)%10)&0x0f)<< 8;
|
| 121 |
|
|
dateword |= (((tmp->tm_mday )/10)&0x0f)<< 4;
|
| 122 |
|
|
dateword |= (((tmp->tm_mday )%10)&0x0f);
|
| 123 |
|
|
if (sleepv > 0)
|
| 124 |
|
|
sleep(sleepv);
|
| 125 |
|
|
|
| 126 |
|
|
while(time(NULL) < then)
|
| 127 |
|
|
;
|
| 128 |
|
|
m_fpga->writeio(R_CLOCK, clockword);
|
| 129 |
|
|
|
| 130 |
|
|
printf("Time set to %06x\n", clockword & 0x03fffff);
|
| 131 |
|
|
#ifdef R_DATE // If we have the date capability
|
| 132 |
|
|
m_fpga->writeio(R_DATE, dateword);
|
| 133 |
|
|
printf("Date set to %08x\n", dateword);
|
| 134 |
|
|
printf("(Now reads %08x)\n", m_fpga->readio(R_DATE));
|
| 135 |
|
|
#endif // R_DATE
|
| 136 |
|
|
|
| 137 |
|
|
now = then;
|
| 138 |
|
|
} if (read_hack) {
|
| 139 |
|
|
then = time(NULL) + 5;
|
| 140 |
|
|
while(time(NULL) < then)
|
| 141 |
|
|
;
|
| 142 |
|
|
clockword = m_fpga->readio(R_CLOCK);
|
| 143 |
|
|
}
|
| 144 |
|
|
|
| 145 |
|
|
if (m_fpga->poll())
|
| 146 |
|
|
printf("FPGA was interrupted\n");
|
| 147 |
|
|
delete m_fpga;
|
| 148 |
|
|
}
|
| 149 |
|
|
|