datetime.datetime.now().strftime("%d%m%Y_%H%M%S") + ".log", filemode='w') logger = logging.getLogger("config_fpga") if trimble_utils.set_clock_trimble(): logger.info("Trimble GPS clock successfully detected.") logger.info( "Successfully updated system clock to gps time from trimble.") else: logger.info( "Unable to read time from trimble. Using RPi system clock which is unrealiable" ) i2c_bus = smbus.SMBus(1) rtc = ds3231.DS3231(i2c_bus, 0x68) snap_board = parameters["snap-board"] firmware = parameters["firmware"] synthesizer = parameters["synthesizer"] adcs = parameters["adcs"] pfb = parameters["pfb"] test_vector_generator = parameters["test-vector-generator"] xcorr = parameters["xcorr"] output_control = parameters["output-control"] packetiser = parameters["packetiser"] baseband = parameters["baseband"] harddrives = parameters["hard-drives"] spectra = parameters["spectra"] ethernet = parameters["ethernet"]
parser.add_argument("--enable-12-hour-mode", action="store_true", help="") parser.add_argument("--set-system-clock-from-rtc", action="store_true", help="") parser.add_argument( "--timestamp", action="store_true", help="Return the DS3231 timekeeping registers as a unix timestamp") args = parser.parse_args() rtc = None try: if args.i2c_bus != None: bus = smbus.SMBus(args.i2c_bus) rtc = ds3231.DS3231(bus) else: print("Please specify the i2c bus the RTC is connected too.") exit(1) if args.get_seconds: print(rtc.get_seconds()) if args.set_seconds != None: if args.set_seconds >= 0 and args.set_seconds <= 60: rtc.set_seconds(args.set_seconds) else: print("Seconds need to be in the range 0 to 60") exit(1) if args.get_minutes:
# Last NTP check started at 1605987450 (21st November) # Check at 1607715597 when DS3231 read 1607715599 # Drift was 2 in 1728147 secs: 1.15730895577749ppm (11th December) # Cal factor adjusted to -12 (== -1.2ppm) from machine import I2C, RTC, Pin from network import WLAN, AP_IF, STA_IF import ds3231 i2c = I2C(0, scl=22, sda=21) ds = ds3231.DS3231(i2c) rtc = RTC() ds.alarm1_tm ds.rtc_tm import settings import wifi wifi_settings = settings.load_settings("wifi.json") ip_addr = wifi.connect_sta(wifi_settings['SSID'], wifi_settings['Password'], wifi_settings['Hostname']) import ntptime ntp_settings = settings.load_settings("ntp.json") ntptime.ntp_query("192.168.16.10") ds.rtc import settings import wifi wifi_settings = settings.load_settings("wifi.json") ip_addr = wifi.connect_sta(wifi_settings['SSID'], wifi_settings['Password'], wifi_settings['Hostname']) print(''.join('{:02x} '.format(x) for x in ds.read_ds3231_rtc()))
def main(): # LED output - turn it on whilst we're booting... led = Pin(2, Pin.OUT) led.value(1) # Initialise the DS3231 battery-backed RTC i2c = I2C(0, scl=22, sda=21) ds = ds3231.DS3231(i2c) print("DS3231 time : {}".format(ds.rtc_tm)) print("Hands position: {}".format(ds.alarm1_tm)) # Initialise the mechanical clock clock = dgclock.DGClock("clock.json", ds.alarm1) # Read the config file, and initialise hands at last known position # Intialise the display ui = dgui.DGUI(clock.hands_tm) # Read the WiFi settings wifi_settings = settings.load_settings("wifi.json") network = wifi.wifi(wifi_settings) # Read the NTP server to use ntp_settings = settings.load_settings("ntp.json") next_ntp_sync = ds.rtc + 30 # First sync attempt after 30 seconds set_time = 0 # Resetting the DS RTC not needed try: while True: # Tell the UI what the time is ui.now_tm = ds.rtc_tod_tm now = ds.rtc_tod # Move the clock to show current TOD unless stopped if ui.mode == 'Normal' or ui.mode == 'Set': clock.move(now) # Update the non-volatile copy of the hand position ds.alarm1_tm = clock.hands_tm sleep_ms(10) # Allow time for this to complete - DS3231 write can fail otherwise # LED states if clock.mode == "Run" and ui.now_tm[3] == 22 and ui.now_tm[4] == 0: # Run mode - on at 22:00:00 until 22:01:00 led.value(1) elif clock.mode == "Wait": # Wait mode - on led.value(1) elif clock.mode == "Fast" and clock.hands_tm[5] % 2 == 0: # Fast mode - on for even seconds led.value(1) else: # Otherwise off led.value(0) # Tell the UI where the clock thinks the hands are ui.hands_tm = clock.hands_tm ui.clock_mode = clock.mode # Check that we have a WiFi connection, and attempt to reconnect if it's dropped network.connect() # Handle any button presses if ui.handle_buttons(): # Adjust hands was selected, so copy from UI to the clock clock.hands_tm = ui.time_to_set # Update the screen ui.update_screen() # Periodically re-sync the clocks if ds.rtc > next_ntp_sync: print("Querying {}".format(ntp_settings['NTP'])) (ntp_time, millis, ticks) = ntptime.ntp_query(ntp_settings['NTP']) old_time = ds.rtc if ntp_time is not None: #ds.rtc = ntp_time # Copy the received time into the RTC as quickly as possible to minimise error error_ms = 1000 - millis # Convert fractional part to error in milliseconds set_at_ticks = ticks_add(ticks, error_ms) set_time = ntp_time + 1 print("Got {}.{:03d} @ {} - error in milliseconds {} - setting {} @ {}".format(ntp_time, millis, ticks, error_ms, set_time, set_at_ticks)) next_ntp_sync = ntp_time + 3654 # Just a bit less than once an hour #print("Sync (disabled) RTC to NTP - {} (delta {})".format(ds.rtc_tod_tm, old_time - ntp_time)) else: ui.ntp_sync = False next_ntp_sync = ds.rtc + 321 # Just a bit more than five minutes print("NTP sync failed at {}".format(ui.now_tm)) else: gc.collect() # Don't waste time garbage collecting if we're also setting the clock if set_time > 0: tick_err = ticks_diff(ticks_ms(), set_at_ticks) if -100 < tick_err and tick_err < 100: # Allow a 100ms "buffer" ds.rtc = set_time ui.ntp_sync = True print("Set DS RTC {} ({}) @ {}".format(set_time, ds.rtc_tm, ticks_ms())) set_time = 0 elif tick_err > 100: # Missed - try again on the next second set_time += 1 set_at_ticks = ticks_add(set_at_ticks, 1000) print("Missed the ticks - trying again @ {}".format(set_at_ticks)) else: print("Tick error {}".format(tick_err)) except KeyboardInterrupt: # Try to relinquish the I2C bus print("Hands left at : {}".format(ds.alarm1_tm)) i2c.deinit() ui.tft.deinit()
import onewire DEFAULT_I2C_ADDR = 0x27 TEMP_LOW = 20 TEMP_HIGH = 45 SPEED_LOW = 20 SPEED_HIGH = 100 temp = 15 led = Pin(2, Pin.OUT) led.value(1) CONFIG_BTN = Pin(34, Pin.IN) i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000) real_clock = ds3231.DS3231(i2c) ds_sensor = ds18x20.DS18X20(onewire.OneWire(Pin(4))) ds_dev = ds_sensor.scan()[0] lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16) if CONFIG_BTN.value() == 0: lcd.putstr('Config mode') sleep(0.5) import web_server else: lcd.putstr("Temp control\nStart online!") fan1 = PWM(Pin(5)) fan2 = PWM(Pin(18)) fan3 = PWM(Pin(19))
f_fpga_temp.close() f_therms_time_sys_start.close() f_therms_time_gps_start.close() f_therms_time_rtc_start.close() f_therms_time_sys_stop.close() f_therms_time_gps_stop.close() f_therms_time_rtc_stop.close() for f in f_therms_temp: f.close() return None #======================================================================= if __name__ == '__main__': i2c_bus = smbus.SMBus(1) rtc = ds3231.DS3231(i2c_bus) rtc.set_system_clock_from_rtc() # Parse options parser = ArgumentParser() parser.add_argument( 'configfile', type=str, help='yaml file with configuration options for PRIZMs DAQ') args = parser.parse_args() params = None with open(args.configfile, 'r') as cf: params = yaml.load(cf.read(), yaml.FullLoader) # Create log file