Skip to content

Commit

Permalink
Don't wait for openocd to start
Browse files Browse the repository at this point in the history
  • Loading branch information
lmbollen committed Jan 29, 2025
1 parent 1ef2881 commit 94f02d1
Showing 1 changed file with 26 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import System.Clock (Clock (Monotonic), diffTimeSpec, getTime, toNanoSecs)
import System.Exit
import System.FilePath
import System.IO
import System.Timeout (timeout)

import qualified Data.List as L

Expand All @@ -48,22 +47,15 @@ driverFunc testName targets = do
$ "Running Driver function for targets "
<> show ((\(_, info) -> info.deviceId) <$> targets)

projectDir <- liftIO $ findParentContaining "cabal.project"
let hitlDir = projectDir </> "_build/hitl/" <> testName
startTime <- liftIO $ getTime Monotonic

let
calcTimeSpentMs = (`div` 1000000) . toNanoSecs . diffTimeSpec startTime <$> getTime Monotonic

getTargetIndex :: HwTarget -> Int
getTargetIndex hwT = fromMaybe 9 $ L.findIndex (\di -> di.deviceId == idFromHwT hwT) demoRigInfo

tryWithTimeout :: String -> Int -> IO a -> IO a
tryWithTimeout actionName dur action = do
result <- timeout dur action
case result of
Nothing -> do
error $ "Timeout while performing action: " <> actionName
Just r -> pure r

initHwTargets :: VivadoM ()
initHwTargets = forM_ targets $ \(hwT, d) -> do
liftIO $ putStrLn $ "Preparing hardware target " <> show d.deviceId
Expand All @@ -79,20 +71,9 @@ driverFunc testName targets = do
let tclPort = 6666 + targetIndex
let telnetPort = 4444 + targetIndex

projectDir <- findParentContaining "cabal.project"
let
ocdStdout =
projectDir
</> "_build"
</> "hitl"
</> testName
</> "openocd-" <> show targetIndex <> "-stdout.log"
ocdStderr =
projectDir
</> "_build"
</> "hitl"
</> testName
</> "openocd-" <> show targetIndex <> "-stderr.log"
ocdStdout = hitlDir </> "openocd-" <> show targetIndex <> "-stdout.log"
ocdStderr = hitlDir </> "openocd-" <> show targetIndex <> "-stderr.log"
putStrLn $ "logging OpenOCD stdout to `" <> ocdStdout <> "`"
putStrLn $ "logging OpenOCD stderr to `" <> ocdStderr <> "`"

Expand All @@ -104,38 +85,32 @@ driverFunc testName targets = do
gdbPort
tclPort
telnetPort
hSetBuffering ocd.stderrHandle LineBuffering
tryWithTimeout "Waiting for OpenOCD to start" 15_000_000
$ expectLine ocd.stderrHandle openOcdWaitForHalt

let
ocdProcName = "OpenOCD (" <> show d.deviceId <> ")"
ocdClean2 = ocdClean1 >> awaitProcessTermination ocdProcName ocdPh (Just 5_000_000)

return ((gdbPort, ocd), ocdClean2)

initGdbs :: [Int] -> [IO (ProcessStdIoHandles, IO ())]
initGdbs gdbPorts = L.zipWith go gdbPorts targets
where
go :: Int -> (HwTarget, DeviceInfo) -> IO (ProcessStdIoHandles, IO ())
go gdbPort (hwT, d) = do
putStrLn $ "Starting GDB for target " <> show d.deviceId

(gdb, gdbPh, gdbClean1) <- Gdb.startGdbH
hSetBuffering gdb.stdinHandle LineBuffering
Gdb.setLogging gdb
$ "./_build/hitl/"
<> testName
<> "/gdb-out-"
<> show (getTargetIndex hwT)
<> ".log"
Gdb.setFile gdb $ firmwareBinariesDir "riscv32imc" Release </> "clock-control"
Gdb.setTarget gdb gdbPort
let
gdbProcName = "GDB (" <> show d.deviceId <> ")"
gdbClean2 = gdbClean1 >> awaitProcessTermination gdbProcName gdbPh (Just 5_000_000)

return (gdb, gdbClean2)
initGdb :: Int -> (HwTarget, DeviceInfo) -> IO (ProcessStdIoHandles, IO ())
initGdb gdbPort (hwT, d) = do
putStrLn $ "Starting GDB for target " <> show d.deviceId

(gdb, gdbPh, gdbClean1) <- Gdb.startGdbH
hSetBuffering gdb.stdinHandle LineBuffering
Gdb.setLogging gdb
$ "./_build/hitl/"
<> testName
<> "/gdb-out-"
<> show (getTargetIndex hwT)
<> ".log"
Gdb.setFile gdb $ firmwareBinariesDir "riscv32imc" Release </> "clock-control"
Gdb.setTarget gdb gdbPort
let
gdbProcName = "GDB (" <> show d.deviceId <> ")"
gdbClean2 = gdbClean1 >> awaitProcessTermination gdbProcName gdbPh (Just 5_000_000)

return (gdb, gdbClean2)

startTest :: (HwTarget, DeviceInfo) -> VivadoM ()
startTest (hwT, d) = do
Expand Down Expand Up @@ -207,9 +182,9 @@ driverFunc testName targets = do
liftIO $ putStrLn $ "Running cleanup for target " <> d.deviceId

initHwTargets
brackets (liftIO <$> initOpenOcds) (liftIO . snd) $ \initOcdsData -> do
let gdbPorts = fmap (fst . fst) initOcdsData
brackets (liftIO <$> initGdbs gdbPorts) (liftIO . snd) $ \initGdbsData -> do
let gdbPorts = L.take (L.length targets) [3333 ..]
brackets (liftIO <$> initOpenOcds) (liftIO . snd) $ \_initOcdsData -> do
brackets (liftIO <$> L.zipWith initGdb gdbPorts targets ) (liftIO . snd) $ \initGdbsData -> do
let gdbs = fmap fst initGdbsData
liftIO $ mapM_ ((errorToException =<<) . Gdb.loadBinary) gdbs
liftIO $ mapM_ ((errorToException =<<) . Gdb.compareSections) gdbs
Expand Down

0 comments on commit 94f02d1

Please sign in to comment.