Skip to content
Snippets Groups Projects
Commit 5ae48f33 authored by Andrea Parenti's avatar Andrea Parenti Committed by Jonathan Correa Magdalena
Browse files

Refactor / 1

parent 9fb30dec
No related branches found
No related tags found
2 merge requests!2Draft: Feat/p8003,!1First version
Pipeline #162659 failed
import os
from control import Timepix4control
APP_DIR = "/home/timepix4/application"
os.system('devmem2 0x80010004 w 0x2')
os.system('devmem2 0x80010004 w 0x0')
os.system('devmem2 0x80010004 w 0x2')
mytpx4 = Timepix4control(1)
os.system(APP_DIR + "/Timepix4_Init.py")
mytpx4.initialisechip()
mytpx4.configureGWT()
os.system(APP_DIR + "/sc_set_gwt_link_up.py -i 0x0 -l 0x0 -s 0x2")
os.system(APP_DIR + "/sc_set_gwt_link_up.py -i 0x1 -l 0x0 -s 0x2")
os.system('devmem2 0x80050000 w 0x2')
......@@ -15,6 +15,7 @@
from asyncio import create_subprocess_exec, subprocess # CancelledError
from time import time
from paramiko.client import SSHClient
from psutil import net_io_counters
from karabo.middlelayer import (
......@@ -65,6 +66,7 @@ class Tempus(Device):
__version__ = deviceVersion
COMMAND = "ssh"
APP_DIR = "/home/timepix4/application"
SCRIPT_DIR = "/home/timepix4/master"
def __init__(self, configuration):
......@@ -72,8 +74,9 @@ class Tempus(Device):
self.background_task = None
self.monitor_task = background(self._monitor())
self.ARGS = [f"root@{self.controllerIp}", "python3"]
self.client = None
self.channel = None
self.do_acquire = False
self.state = State.INIT
async def _monitor(self):
last_time = None
......@@ -204,18 +207,52 @@ class Tempus(Device):
@Slot(
displayedName="Initialize",
description="Run the initialization sequence. This can take about "
"30 s",
description="Run the initialization sequence. This will take about "
"45 s",
allowedStates={State.INIT, State.ON})
async def initialize(self):
self.state = State.CHANGING
self.status = "Initializing TEMPUS"
cmnd = [self.COMMAND, *self.ARGS, f"{self.SCRIPT_DIR}/karabo_init.py"]
self.commandLog = (
f"Initializing TEMPUS:\n{' '.join(cmnd)}\nThis can take about 30 "
"s")
self.background_task = background(
self._execute_command(cmnd, State.ON, "Initialized"))
"Initializing TEMPUS:\nThis will take about 45 s.\n\n")
self.background_task = background(self._initialize())
async def _initialize(self):
try:
await self.send_command(f"cd {self.SCRIPT_DIR}")
await self.send_command("python3")
await self.send_command("import os")
await self.send_command(
"from control import Timepix4control", sleep_time=2)
await self.send_command("os.system('devmem2 0x80010004 w 0x2')")
await self.send_command("os.system('devmem2 0x80010004 w 0x0')")
await self.send_command("os.system('devmem2 0x80010004 w 0x2')")
await self.send_command(
"mytpx4 = Timepix4control(1)", sleep_time=5)
await self.send_command(
f"os.system('{self.APP_DIR}/Timepix4_Init.py')", sleep_time=3)
await self.send_command("mytpx4.initialisechip()", sleep_time=1)
await self.send_command("mytpx4.configureGWT()", sleep_time=1)
await self.send_command(
f"os.system('{self.APP_DIR}/sc_set_gwt_link_up.py -i 0x0 "
"-l 0x0 -s 0x2')", sleep_time=15)
await self.send_command(
f"os.system('{self.APP_DIR}/sc_set_gwt_link_up.py -i 0x1 "
"-l 0x0 -s 0x2')", sleep_time=15)
await self.send_command("os.system('devmem2 0x80050000 w 0x2')")
self.status = "Initialized"
self.state = State.ON
except Exception as e:
self.logger.error(e)
self.state = State.ERROR
self.status = str(e)
self.commandLog = str(e)
@Slot(
displayedName="Reset Fast-Links",
......@@ -341,8 +378,49 @@ class Tempus(Device):
defaultValue=0.0001,
allowedStates={State.INIT, State.ON, State.ACTIVE})
async def onInitialization(self):
try:
self.client = SSHClient()
self.client.load_system_host_keys()
self.client.connect(
self.controllerIp, username="root", password="")
self.channel = self.client.invoke_shell()
msg = f"Connected to {self.controllerIp}"
self.logger.info(msg)
self.status = msg
self.state = State.INIT
except Exception as e:
self.client = None
self.channel = None
msg = f"Cannot connect to {self.controllerIp}"
self.status = msg
self.logger.error(f"{msg}: {e}")
async def onDestruction(self):
if self.channel:
self.channel.close()
if self.client:
self.client.close()
# De-initialize
cmnd = [
self.COMMAND, *self.ARGS, f"{self.SCRIPT_DIR}/karabo_deinit.py"]
await self._execute_command(cmnd, State.INIT, "")
async def send_command(self, cmnd, sleep_time=0.1):
self.logger.info(f"Executing: {cmnd}") # XXX debug
self.channel.send(cmnd + "\n")
await sleep(sleep_time)
recv_stderr = b""
while self.channel.recv_stderr_ready():
recv_stderr += self.channel.recv_stderr(1024)
if recv_stderr:
raise RuntimeError(recv_stderr.decode())
recv = b""
while self.channel.recv_ready():
recv += self.channel.recv(1024)
self.logger.info(recv) # XXX debug
self.commandLog = recv
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment