From 3bd96650567d9ad02dc77e8c442e55eecca90058 Mon Sep 17 00:00:00 2001 From: Aleksander Mistewicz Date: Mon, 5 Feb 2018 18:37:15 +0100 Subject: [PATCH] Add GetCurrent to Interface GetCurrent method of STM interface fetches the value of the measured current drawn by DUT. It sends "current" to STM's UART and parses the response, which is expected to be an integer. A new internal helper function, sendAndReceive, is added. Change-Id: Ifb8b634b5b432685d6b69dc76ebc8f4e9f8fa189 --- sw/nanopi/cmd/stm/stm.go | 8 ++++++++ sw/nanopi/stm/stm.go | 49 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/sw/nanopi/cmd/stm/stm.go b/sw/nanopi/cmd/stm/stm.go index 366da41..03c0e12 100644 --- a/sw/nanopi/cmd/stm/stm.go +++ b/sw/nanopi/cmd/stm/stm.go @@ -18,6 +18,7 @@ package main import ( "flag" + "fmt" "log" "time" @@ -27,6 +28,7 @@ import ( var ( ts, dut, tick bool tickDuration time.Duration + cur bool ) func setFlags() { @@ -34,6 +36,7 @@ func setFlags() { flag.BoolVar(&ts, "ts", false, "connect SD card to test server") flag.BoolVar(&dut, "dut", false, "connect SD card to DUT") flag.BoolVar(&tick, "tick", false, "power off the DUT, wait 'm' seconds and switch it on again") + flag.BoolVar(&cur, "cur", false, "get reading of the current drawn by DUT") } func checkErr(ctx string, err error) { @@ -63,4 +66,9 @@ func main() { if tick { checkErr("failed to tick the power supply: ", dev.PowerTick(tickDuration)) } + if cur { + i, err := dev.GetCurrent() + checkErr("failed to read the power consumption: ", err) + fmt.Println(i) + } } diff --git a/sw/nanopi/stm/stm.go b/sw/nanopi/stm/stm.go index 671075a..92cdbe4 100644 --- a/sw/nanopi/stm/stm.go +++ b/sw/nanopi/stm/stm.go @@ -21,6 +21,7 @@ import ( "bufio" "fmt" "io" + "strconv" "strings" "sync" "time" @@ -71,6 +72,7 @@ type UserInterface interface { PowerTick(d time.Duration) (err error) DUT() (err error) TS() (err error) + GetCurrent() (value int, err error) } // AdminInterface contains methods of STM that are intended to @@ -150,6 +152,28 @@ func (stm *STM) readResponse() (string, error) { return string(response), nil } +// sendAndReceive writes cmd, optionally reads a response if withResponse is set to true, +// and checks for confirmation of successful execution. +func (stm *STM) sendAndReceive(cmd string, withResponse bool) (response string, err error) { + stm.mux.Lock() + defer stm.mux.Unlock() + _, err = io.WriteString(stm.port, cmd+"\n") + if err != nil { + return "", fmt.Errorf("failed to write a command: %s", err) + } + if withResponse { + response, err = stm.readResponse() + if err != nil { + return "", err + } + } + err = stm.checkOK() + if err != nil { + return "", err + } + return response, nil +} + // checkOK reads a line from the buffer and rises an error // if it is not an expected confirmation response. // @@ -165,15 +189,10 @@ func (stm *STM) checkOK() error { return nil } -// executeCommand sends a prepared cmd string and checks the response. +// executeCommand sends a prepared cmd string and checks for OK response. func (stm *STM) executeCommand(cmd string) (err error) { - stm.mux.Lock() - defer stm.mux.Unlock() - _, err = io.WriteString(stm.port, cmd+"\n") - if err != nil { - return fmt.Errorf("failed to write a command: %s", err) - } - return stm.checkOK() + _, err = stm.sendAndReceive(cmd, false) + return } // noEcho requires special handling as there are more possible responses. @@ -230,3 +249,17 @@ func (stm *STM) DUT() error { func (stm *STM) TS() error { return stm.executeCommand("ts") } + +// GetCurrent reads value of current drawn by DUT. +// The value is in milliamperes [mA]. +func (stm *STM) GetCurrent() (int, error) { + str, err := stm.sendAndReceive("current", true) + if err != nil { + return 0, err + } + i, err := strconv.Atoi(str) + if err != nil { + return 0, err + } + return i, nil +} -- 2.7.4