Skip to content

Commit

Permalink
Merged develop for release v0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Lehmann committed Sep 28, 2020
1 parent b6fea8c commit d99949b
Show file tree
Hide file tree
Showing 19 changed files with 599 additions and 525 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
usbtest/usbtest
.idea/
stRttLogger/stRttLogger
13 changes: 5 additions & 8 deletions accessport.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,13 @@ func (h *StLinkHandle) usbInitAccessPort(apNum byte) error {

log.Debugf("init ap_num = %d", apNum)

h.usbInitBuffer(transferRxEndpoint, 16)
ctx := h.initTransfer(transferRxEndpoint)

h.cmdbuf[h.cmdidx] = cmdDebug
h.cmdidx++
h.cmdbuf[h.cmdidx] = debugApiV2InitAccessPort
h.cmdidx++
h.cmdbuf[h.cmdidx] = apNum
h.cmdidx++
ctx.cmdBuffer.WriteByte(cmdDebug)
ctx.cmdBuffer.WriteByte(debugApiV2InitAccessPort)
ctx.cmdBuffer.WriteByte(apNum)

retVal := h.usbTransferErrCheck(h.databuf, 2)
retVal := h.usbTransferErrCheck(ctx, 2)

if retVal != nil {
return errors.New("could not init access port on device")
Expand Down
59 changes: 31 additions & 28 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,40 +136,40 @@ const (
cmdGetTargetVoltage = 0xF7
)

const (
//STLINK_DEBUG_GETSTATUS = 0x01
//STLINK_DEBUG_FORCEDEBUG = 0x02
//STLINK_DEBUG_APIV1_RESETSYS = 0x03
//STLINK_DEBUG_APIV1_READALLREGS = 0x04
//STLINK_DEBUG_APIV1_READREG = 0x05
//STLINK_DEBUG_APIV1_WRITEREG = 0x06
// ST-Link debug commands
const (
//debugEnterJTag = 0x00
//debugGetStatus = 0x01
//debugForceDebug = 0x02
//debugApiV1ResetSys = 0x03
//debugApiV1ReadAllRegs = 0x04
//debugApiV1ReadReg = 0x05
//debugApiV1WriteReg = 0x06
debugReadMem32Bit = 0x07
debugWriteMem32Bit = 0x08
//STLINK_DEBUG_RUNCORE = 0x09
//STLINK_DEBUG_STEPCORE = 0x0a
//STLINK_DEBUG_APIV1_SETFP = 0x0b
//debugRunCore = 0x09
//debugStepCore = 0x0a
//debugApiV1SetFP = 0x0b
debugReadMem8Bit = 0x0c
debugWriteMem8Bit = 0x0d
//STLINK_DEBUG_APIV1_CLEARFP = 0x0e
//STLINK_DEBUG_APIV1_WRITEDEBUGREG = 0x0f
//STLINK_DEBUG_APIV1_SETWATCHPOINT = 0x10
//STLINK_DEBUG_ENTER_JTAG_RESET = 0x00
debugEnterSwdNoReset = 0xa3
debugEnterJTagNoReset = 0xa4
//debugApiV1ClearFP = 0x0e
//debugApiV1WriteDebugReg = 0x0f
//debugApiV1SetWatchPoint = 0x10
debugApiV1Enter = 0x20
debugExit = 0x21
debugReadCoreId = 0x22
debugApiV2Enter = 0x30
debugApiV2ReadIdCodes = 0x31
//STLINK_DEBUG_APIV2_RESETSYS = 0x32
//STLINK_DEBUG_APIV2_READREG = 0x33
//STLINK_DEBUG_APIV2_WRITEREG = 0x34
//STLINK_DEBUG_APIV2_WRITEDEBUGREG = 0x35
//STLINK_DEBUG_APIV2_READDEBUGREG = 0x36
//STLINK_DEBUG_APIV2_READALLREGS = 0x3A
debugApiV2GetLastRWStatus = 0x3B
debugApiV2DriveNrst = 0x3C
debugApiV2GetLastRWStatus2 = 0x3E
//debugApiV2ResetSys = 0x32
//debugApiV2ReadReg = 0x33
//debugApiV2WriteReg = 0x34
//debugApiV2WriteDebugReg = 0x35
//debugApiV2ReadDebugReg = 0x36
//debugApiV2ReadAllRegs = 0x3A
debugApiV2GetLastRWStatus = 0x3B
debugApiV2DriveNrst = 0x3C
debugApiV2GetLastRWStatus2 = 0x3E

debugApiV2StartTraceRx = 0x40
debugApiV2StopTraceRx = 0x41
debugApiV2GetTraceNB = 0x42
Expand All @@ -181,9 +181,12 @@ const (
debugApiV2WriteMem16Bit = 0x48
debugApiV2InitAccessPort = 0x4B
debugApiV2CloseAccessPortDbg = 0x4C
//STLINK_DEBUG_APIV2_DRIVE_NRST_LOW = 0x00
//STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH = 0x01
//STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE = 0x02
//STLINK_DEBUG_APIV2_DRIVE_NRST_LOW = 0x00
//STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH = 0x01
//STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE = 0x02

debugEnterSwdNoReset = 0xa3
debugEnterJTagNoReset = 0xa4

debugApiV3SetComFreq = 0x61
debugApiV3GetComFreq = 0x62
Expand Down
37 changes: 37 additions & 0 deletions cpus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2020 Sebastian Lehmann. All rights reserved.
// Use of this source code is governed by a GNU-style
// license that can be found in the LICENSE file.

// this code is mainly inspired and based on the openocd project source code
// for detailed information see

// https://sourceforge.net/p/openocd/code

package gostlink

type StmCpuInfo struct {
RamStart uint64
RamSize uint64
}

var supportedStmCpus = map[string]StmCpuInfo{
"STM32F030F4": {0x20000000, 0x1000},
"STM32F030K6": {0x20000000, 0x1000},
"STM32F030C6": {0x20000000, 0x1000},
"STM32F030C8": {0x20000000, 0x2000},
"STM32F030R8": {0x20000000, 0x2000},
"STM32F030CC": {0x20000000, 0x8000},
"STM32F030RC": {0x20000000, 0x8000},
"STM32F070F6": {0x20000000, 0x2000},
"STM32F070C6": {0x20000000, 0x2000},
"STM32F070CB": {0x20000000, 0x4000},
"STM32F070RB": {0x20000000, 0x4000},
}

func GetCpuInformation(cpuId string) *StmCpuInfo {
if val, ok := supportedStmCpus[cpuId]; ok {
return &val
} else {
return nil
}
}
14 changes: 8 additions & 6 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,29 @@ func newUsbError(msg string, code usbErrorCode) error {
Converts an STLINK status code held in the first byte of a response
to an gostlink library error, logs any error/wait status as debug output.
*/
func (h *StLinkHandle) usbErrorCheck() error {
func (h *StLinkHandle) usbErrorCheck(ctx* transferCtx) error {

errorStatus := ctx.dataBuffer.Bytes()[0]

if h.stMode == StLinkModeDebugSwim {
switch h.databuf[0] {
switch errorStatus {
case swimErrorOk:
return nil

case swimErrorBusy:
return newUsbError("swim is busy", usbErrorWait)

default:
return newUsbError(fmt.Sprintf("unknown/unexpected STLINK status code 0x%x", h.databuf[0]), usbErrorFail)
return newUsbError(fmt.Sprintf("unknown/unexpected STLINK status code 0x%x", errorStatus), usbErrorFail)
}
}

/* TODO: no error checking yet on api V1 */
if h.version.jtagApi == jTagApiV1 {
h.databuf[0] = debugErrorOk
errorStatus = debugErrorOk
}

switch h.databuf[0] {
switch errorStatus {
case debugErrorOk:
return nil

Expand Down Expand Up @@ -109,6 +111,6 @@ func (h *StLinkHandle) usbErrorCheck() error {
return newUsbError("STLINK_BAD_AP_ERROR", usbErrorFail)

default:
return newUsbError(fmt.Sprintf("unknown/unexpected STLINK status code 0x%x", h.databuf[0]), usbErrorFail)
return newUsbError(fmt.Sprintf("unknown/unexpected STLINK status code 0x%x", errorStatus), usbErrorFail)
}
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ go 1.14
require (
github.com/boljen/go-bitmap v0.0.0-20151001105940-23cd2fb0ce7d
github.com/google/gousb v2.1.0+incompatible
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/sirupsen/logrus v1.6.0
github.com/x-cray/logrus-prefixed-formatter v0.5.2
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de // indirect
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,27 @@ github.com/google/gousb v2.1.0+incompatible h1:ApzMDjF3FeO219QwWybJxYfFhXQzPLOEy
github.com/google/gousb v2.1.0+incompatible/go.mod h1:Tl4HdAs1ThE3gECkNwz+1MWicX6FXddhJEw7L8jRDiI=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw=
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/x-cray/logrus-prefixed-formatter v0.5.2 h1:00txxvfBM9muc0jiLIEAkAcIMJzfthRT6usrui8uGmg=
github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de h1:ikNHVSjEfnvz6sxdSPCaPt572qowuyMDMJLLm3Db3ig=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
10 changes: 10 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ import (
"os"

"github.com/sirupsen/logrus"
"github.com/x-cray/logrus-prefixed-formatter"
)

const MaxLogLevel = logrus.DebugLevel

func init() {
formatter := &prefixed.TextFormatter{
DisableColors: false,
TimestampFormat: "15:04:05",
FullTimestamp: true,
ForceFormatting: true,
}

logrus.SetFormatter(formatter)

logrus.SetOutput(os.Stdout)
logrus.SetLevel(MaxLogLevel)
}
Loading

0 comments on commit d99949b

Please sign in to comment.