{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module STLink.Commands
( getVersion
, ResponseVersion(..)
, getCurrentMode
, STLinkMode(..)
, getCurrentVoltage
, enterMode
, DebugMode(..)
, getStatus
, DebugStatus
, haltCore
, runCore
, stepCore
, resetCore
, readReg
, writeReg
, readMem
, writeMem
)
where
import Data.Binary.Get
import Data.Binary.Put
import Data.Bits
import Data.Word
import Data.Semigroup ( (<>) )
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import Control.Applicative
import STLink.Driver
bitsOn :: (Num a, Bits a) => Int -> a
bitsOn n = f n 0
where
f 0 m = shiftR m 1
f l m = f (l - 1) (shift (m .|. 1) 1)
selectBits :: (Integral a, Bits a, Integral n) => (Int, Int) -> a -> n
selectBits (p, w) n = fromIntegral $ shiftR n p .&. bitsOn w
class InCommand c where
type InResponse c :: *
inCommandEncoding :: c -> Put
inResponseSize :: c -> Int
inResponseEncoding :: c -> Get (InResponse c)
runInCommand :: c -> STLink (InResponse c)
runInCommand c =
STLink $ \h ->
runGet (inResponseEncoding c) . BL.fromStrict <$>
inCommand
h
(BL.toStrict . runPut . inCommandEncoding $ c)
(inResponseSize c)
class OutCommand c where
type OutData c :: *
outCommandEncoding :: c -> Put
outDataEncoding :: OutData c -> Put
runOutCommand :: c -> OutData c -> STLink ()
runOutCommand c d =
STLink $ \h ->
outCommand
h
(BL.toStrict . runPut . outCommandEncoding $ c)
(BL.toStrict . runPut . outDataEncoding @c $ d)
data CommandVersion =
CommandVersion
deriving (Show, Eq)
data ResponseVersion = ResponseVersion
{ versionStlink :: Int
, versionJtag :: Int
, versionSwim :: Int
} deriving (Show, Eq)
instance InCommand CommandVersion where
type InResponse CommandVersion = ResponseVersion
inCommandEncoding _ = putWord8 0xf1
inResponseSize _ = 6
inResponseEncoding _ = do
n <- getWord16le
pure $
ResponseVersion
(selectBits (12, 4) n)
(selectBits (6, 6) n)
(selectBits (0, 6) n)
getVersion :: STLink ResponseVersion
getVersion = runInCommand CommandVersion
data CommandGetMode =
CommandGetMode
deriving (Show, Eq)
data STLinkMode
= ModeDFU
| ModeMass
| ModeDebug
| ModeSWIM
| ModeBootloader
deriving (Show, Eq)
instance InCommand CommandGetMode where
type InResponse CommandGetMode = STLinkMode
inCommandEncoding _ = putWord8 0xf5
inResponseSize _ = 2
inResponseEncoding _ =
getWord8 >>= \case
0x0 -> pure ModeDFU
0x1 -> pure ModeMass
0x2 -> pure ModeDebug
0x3 -> pure ModeSWIM
0x4 -> pure ModeBootloader
_ -> empty
getCurrentMode :: STLink STLinkMode
getCurrentMode = runInCommand CommandGetMode
data CommandGetVoltage =
CommandGetVoltage
deriving (Show, Eq)
instance InCommand CommandGetVoltage where
type InResponse CommandGetVoltage = Float
inCommandEncoding _ = putWord8 0xf7
inResponseSize _ = 8
inResponseEncoding _ = do
divisor <- fromIntegral <$> getInt32le
coef <- fromIntegral <$> getInt32le
pure $ 2 * coef * (1.2 / divisor)
getCurrentVoltage :: STLink Float
getCurrentVoltage = runInCommand CommandGetVoltage
putDebug :: Put -> Put
putDebug = (putWord8 0xf2 *>)
putSWIM :: Put -> Put
putSWIM = (putWord8 0xf2 *>)
data CommandModeEnter =
CommandModeEnter DebugMode
deriving (Show, Eq)
data DebugMode
= DebugJTAG
| DebugSWD
| DebugSWIM
deriving (Show, Eq)
instance InCommand CommandModeEnter where
type InResponse CommandModeEnter = ()
inCommandEncoding (CommandModeEnter m) =
case m of
DebugJTAG -> putDebug $ putWord16be 0x3000
DebugSWD -> putDebug $ putWord16be 0x30a3
DebugSWIM -> putSWIM $ putWord8 0x00
inResponseSize _ = 2
inResponseEncoding _ = pure ()
enterMode :: DebugMode -> STLink ()
enterMode = runInCommand . CommandModeEnter
data CommandGetStatus =
CommandGetStatus
deriving (Show, Eq)
data DebugStatus
= DebugStatusRunning
| DebugStatusHalted
deriving (Show, Eq)
instance InCommand CommandGetStatus where
type InResponse CommandGetStatus = DebugStatus
inCommandEncoding CommandGetStatus = putDebug $ putWord8 0x01
inResponseSize _ = 2
inResponseEncoding _ =
getWord8 >>= \case
0x80 -> pure DebugStatusRunning
0x81 -> pure DebugStatusHalted
_ -> empty
getStatus :: STLink DebugStatus
getStatus = runInCommand CommandGetStatus
data CommandExecution
= ExecuteRun
| ExecuteHalt
| ExecuteReset
| ExecuteStep
deriving (Eq, Show)
instance InCommand CommandExecution where
type InResponse CommandExecution = ()
inCommandEncoding s =
case s of
ExecuteRun -> putDebug $ putWord8 0x09
ExecuteHalt -> putDebug $ putWord8 0x02
ExecuteReset -> putWord8 0x32
ExecuteStep -> putDebug $ putWord8 0x0a
inResponseSize _ = 2
inResponseEncoding _ = pure ()
haltCore :: STLink ()
haltCore = runInCommand ExecuteHalt
runCore :: STLink ()
runCore = runInCommand ExecuteRun
resetCore :: STLink ()
resetCore = runInCommand ExecuteReset
stepCore :: STLink ()
stepCore = runInCommand ExecuteStep
type Reg = Word8
data CommandReadReg =
CommandReadReg Reg
deriving (Eq, Show)
instance InCommand CommandReadReg where
type InResponse CommandReadReg = Word32
inCommandEncoding (CommandReadReg n) =
putDebug $ putWord8 0x33 *> putWord8 n
inResponseSize _ = 8
inResponseEncoding _ = getWord32le *> getWord32le
readReg :: Reg -> STLink Word32
readReg = runInCommand . CommandReadReg
data CommandWriteReg =
CommandWriteReg Reg Word32
instance InCommand CommandWriteReg where
type InResponse CommandWriteReg = ()
inCommandEncoding (CommandWriteReg n v) =
putDebug $ putWord8 0x34 *> putWord8 n *> putWord32le v
inResponseSize _ = 2
inResponseEncoding _ = pure ()
writeReg :: Reg -> Word32 -> STLink ()
writeReg n v = runInCommand $ CommandWriteReg n v
data CommandReadMem =
CommandReadMem Word32 Word16
deriving (Eq, Show)
correctLength :: (Eq n, Num n) => n -> n
correctLength 1 = 2
correctLength x = x
correctBS :: BS.ByteString -> BS.ByteString
correctBS s | BS.length s == 1 = s <> "\0"
| otherwise = s
instance InCommand CommandReadMem where
type InResponse CommandReadMem = BS.ByteString
inCommandEncoding (CommandReadMem a l) = putDebug $ do
putWord8 0x0c
putWord32le a
putWord16le (correctLength l)
inResponseSize (CommandReadMem _ l) =
fromIntegral (correctLength l)
inResponseEncoding (CommandReadMem _ l) =
getByteString (fromIntegral l)
readMem :: Word32 -> Word16 -> STLink BS.ByteString
readMem a l = runInCommand (CommandReadMem a l)
data CommandWriteMem =
CommandWriteMem Word32 Word16
deriving (Eq, Show)
instance OutCommand CommandWriteMem where
type OutData CommandWriteMem = BS.ByteString
outCommandEncoding (CommandWriteMem a l) = putDebug $ do
putWord8 0x0d
putWord32le a
putWord16le (correctLength l)
outDataEncoding = putByteString . correctBS
writeMem :: Word32 -> BS.ByteString -> STLink ()
writeMem a d = runOutCommand (CommandWriteMem a (fromIntegral $ BS.length d)) d