Skip to content

Commit

Permalink
mp debug trace log
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Dec 8, 2019
1 parent 1930c98 commit d13c931
Show file tree
Hide file tree
Showing 38 changed files with 1,250 additions and 507 deletions.
886 changes: 543 additions & 343 deletions modular-psu-firmware.eez-project

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions scripts/diode-tester-fast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from utime import ticks_ms, ticks_add, ticks_diff, sleep_ms
from eez import scpi, setU, getOutputMode, getI, dlogTraceData

U_STEP = 0.1
U_MAX = 40.0
TIME_STEP_MS = 4
MEASURE_DELAY_MS = 2
I_SET = 4.0
TRACE_FILE = "/diode-tester.dlog"

try:
scpi("OUTP 0")
scpi("INST ch1")
I_MAX = float(scpi("CURR? MAX"))
scpi("VOLT 0")
scpi("CURR " + str(I_SET))

scpi("SENS:DLOG:TRAC:X:UNIT VOLT")
scpi("SENS:DLOG:TRAC:X:STEP " + str(U_STEP))
scpi("SENS:DLOG:TRAC:X:RANG:MAX " + str(U_MAX))
scpi("SENS:DLOG:TRAC:Y1:UNIT AMPER")
scpi("SENS:DLOG:TRAC:Y1:RANG:MAX " + str(I_MAX))

# FILE_NAME_MAX_CHARS = 20
# fileName = scpi('query text, "Diode model", ' % FILE_NAME_MAX_CHARS)

scpi("INIT:DLOG:TRACE \"" + TRACE_FILE + "\"");

scpi("OUTP 1")

scpi("DISP:WINDOW:DLOG")

ch = 1

t = ticks_ms()
i = 0
while True:
u_set = i * U_STEP
if u_set > U_MAX:
break

setU(ch, u_set)

t = ticks_add(t, MEASURE_DELAY_MS)
sleep_ms(ticks_diff(t, ticks_ms()))

mode = getOutputMode(ch)
#if mode.startswith("CC"):
# break

curr = getI(ch)
dlogTraceData(curr)

i = i + 1

t = ticks_add(t, TIME_STEP_MS - MEASURE_DELAY_MS)
sleep_ms(ticks_diff(t, ticks_ms()))

print(i, u_set, curr, mode)
finally:
scpi("ABOR:DLOG")
scpi("OUTP 0")
81 changes: 39 additions & 42 deletions scripts/diode-tester.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,57 @@
#import ustruct
#from utime import ticks_ms, sleep_ms
from eez import scpi #, dlog
from utime import ticks_ms, ticks_add, ticks_diff, sleep_ms
from eez import scpi

U_STEP = 0.1
U_MAX = 40.0
#TIME_STEP_MS = 10
#MEASURE_DELAY_MS = 5
TIME_STEP_MS = 4
MEASURE_DELAY_MS = 2
I_SET = 4.0
TRACE_FILE = "/diode-tester.dlog"

scpi("INST ch1")
I_MAX = float(scpi("CURR? MAX"))
scpi("VOLT 0")
scpi("CURR " + str(I_SET))
try:
scpi("OUTP 0")
scpi("INST ch1")
I_MAX = float(scpi("CURR? MAX"))
scpi("VOLT 0")
scpi("CURR " + str(I_SET))

scpi("SENS:DLOG:TRAC:X:UNIT VOLT")
scpi("SENS:DLOG:TRAC:X:STEP " + str(U_STEP))
scpi("RANG:MAX " + str(U_MAX))
scpi("SENS:DLOG:TRAC:Y1:UNIT AMPER")
scpi("SENS:DLOG:TRAC:Y1:RANG:MAX " + str(I_MAX))
scpi("SENS:DLOG:TRAC:X:UNIT VOLT")
scpi("SENS:DLOG:TRAC:X:STEP " + str(U_STEP))
scpi("SENS:DLOG:TRAC:X:RANG:MAX " + str(U_MAX))
scpi("SENS:DLOG:TRAC:Y1:UNIT AMPER")
scpi("SENS:DLOG:TRAC:Y1:RANG:MAX " + str(I_MAX))

# FILE_NAME_MAX_CHARS = 20
# fileName = scpi('query text, "Diode model", ' % FILE_NAME_MAX_CHARS)
# FILE_NAME_MAX_CHARS = 20
# fileName = scpi('query text, "Diode model", ' % FILE_NAME_MAX_CHARS)

scpi("INIT:DLOG:TRACE \"" + TRACE_FILE + "\"");
scpi("INIT:DLOG:TRACE \"" + TRACE_FILE + "\"");

scpi("OUTP 1")
scpi("OUTP 1")

scpi("DISP:WINDOW:DLOG")
scpi("DISP:WINDOW:DLOG")

#t = ticks_ms()
i = 0
while True:
u_set = i * U_STEP
if u_set > U_MAX:
break
t = ticks_ms()
i = 0
while True:
u_set = i * U_STEP
if u_set > U_MAX:
break

scpi("VOLT " + str(u_set))
scpi("VOLT " + str(u_set))

#t = ticks_add(t, MEASURE_DELAY_MS)
#sleep_ms(ticks_diff(t, ticks_ms()))
t = ticks_add(t, MEASURE_DELAY_MS)
sleep_ms(ticks_diff(t, ticks_ms()))

mode = scpi("OUTP:MODE?")
#if mode.startswith("CC"):
# break
mode = scpi("OUTP:MODE?")
#if mode.startswith("CC"):
# break

scpi("DLOG:TRACE:DATA " + scpi("MEAS:CURR?"))
scpi("DLOG:TRACE:DATA " + scpi("MEAS:CURR?"))

#print(u_set)
#print(curr)
#print(mode)
i = i + 1

i = i + 1

#t = ticks_add(t, (TIME_STEP_MS - MEASURE_DELAY_MS) * 1000)
#sleep_ms(ticks_diff(t, ticks_ms()))

scpi("ABOR:DLOG")
scpi("OUTP 0")
t = ticks_add(t, TIME_STEP_MS - MEASURE_DELAY_MS)
sleep_ms(ticks_diff(t, ticks_ms()))
finally:
scpi("ABOR:DLOG")
scpi("OUTP 0")
4 changes: 4 additions & 0 deletions scripts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@

print(ustruct.pack('hhl', 1, 2, 3))
print(utime.ticks_ms())
print("sleep begin")
utime.sleep_ms(10000)
print("sleep end")
print(utime.ticks_ms())
139 changes: 134 additions & 5 deletions src/eez/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

#ifdef DEBUG

#include <eez/debug.h>

#include <cstdio>
#include <stdarg.h>
#include <string.h>

#include <eez/debug.h>
#include <eez/memory.h>

// TODO these includes should not be inside apps/psu
#include <eez/modules/psu/psu.h>

Expand All @@ -37,17 +38,145 @@ using namespace eez::psu;
namespace eez {
namespace debug {

static char *g_log = (char *)DEBUG_TRACE_LOG;
static uint32_t g_head;
static uint32_t g_tail;
static bool g_full;

static uint32_t g_numLines;
static uint32_t g_changed;

static uint32_t g_lastLineIndex;
static uint32_t g_lastLineCharPosition;

static uint32_t g_startPosition = 0;
static const uint32_t TRACE_LOG_PAGE_SIZE = 10;

void addCharToLog(char ch) {
*(g_log + g_head) = ch;

// advance pointer
if (g_full) {
g_tail = (g_tail + 1) % DEBUG_TRACE_LOG_SIZE;
}
g_head = (g_head + 1) % DEBUG_TRACE_LOG_SIZE;
g_full = g_head == g_tail;
}

void Trace(const char *format, ...) {
va_list args;
va_start(args, format);

//vprintf(format, args);

char buffer[896];
vsnprintf(buffer, 896, format, args);
Serial.write(buffer, strlen(buffer));

va_end(args);

for (char *p = buffer; *p; p++) {
if (*p == '\r') {
continue;
}
if (*p == '\n') {
addCharToLog(0);
} else if (*p == '\t') {
static const int TAB_SIZE = 4;
for (int i = 0; i < TAB_SIZE; i++) {
addCharToLog(' ');
}
} else {
addCharToLog(*p);
}
}

g_changed = true;

g_lastLineIndex = 0;
g_lastLineCharPosition = g_tail;
}

uint32_t getNumTraceLogLines() {
if (g_changed) {
g_changed = false;
g_numLines = 1;
uint32_t from = g_tail;
uint32_t to = g_head > 0 ? g_head - 1 : DEBUG_TRACE_LOG_SIZE - 1;
for (uint32_t i = from; i != to; i = (i + 1) % DEBUG_TRACE_LOG_SIZE) {
if (!g_log[i]) {
g_numLines++;
}
}
}
return g_numLines;
}

const char *getTraceLogLine(uint32_t lineIndex) {
if (lineIndex > g_lastLineIndex) {
do {
if (g_log[g_lastLineCharPosition] == 0) {
g_lastLineIndex++;
}

g_lastLineCharPosition = (g_lastLineCharPosition + 1) % DEBUG_TRACE_LOG_SIZE;
} while (g_lastLineIndex != lineIndex);
} else if (lineIndex < g_lastLineIndex) {
if (lineIndex == 0) {
g_lastLineIndex = 0;
g_lastLineCharPosition = g_tail;
} else {
g_lastLineCharPosition = (g_lastLineCharPosition + DEBUG_TRACE_LOG_SIZE - 2) % DEBUG_TRACE_LOG_SIZE;

do {
if (g_log[g_lastLineCharPosition] == 0) {
g_lastLineIndex--;
}

g_lastLineCharPosition = (g_lastLineCharPosition + DEBUG_TRACE_LOG_SIZE - 1) % DEBUG_TRACE_LOG_SIZE;
} while (g_lastLineIndex != lineIndex);

g_lastLineCharPosition = (g_lastLineCharPosition + 2) % DEBUG_TRACE_LOG_SIZE;
}
}

return g_log + g_lastLineCharPosition;
}

uint32_t getTraceLogStartPosition() {
uint32_t position = g_startPosition;
uint32_t count = eez::debug::getNumTraceLogLines();
if (count <= TRACE_LOG_PAGE_SIZE) {
position = 0;
} else if (position > count - TRACE_LOG_PAGE_SIZE) {
position = count - TRACE_LOG_PAGE_SIZE;
}
return position;
}

void setTraceLogStartPosition(uint32_t position) {
g_startPosition = position;
}

void resetTraceLogStartPosition() {
uint32_t count = eez::debug::getNumTraceLogLines();
if (count <= TRACE_LOG_PAGE_SIZE) {
g_startPosition = 0;
} else {
g_startPosition = count - TRACE_LOG_PAGE_SIZE;
}
}

uint32_t getTraceLogPageSize() {
return TRACE_LOG_PAGE_SIZE;
}

void onEncoder(int counter) {
#if defined(EEZ_PLATFORM_SIMULATOR)
counter = -counter;
#endif
int32_t newPosition = getTraceLogStartPosition() + counter;
if (newPosition < 0) {
newPosition = 0;
}
setTraceLogStartPosition(newPosition);
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
11 changes: 11 additions & 0 deletions src/eez/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ namespace debug {

void Trace(const char *format, ...);

uint32_t getNumTraceLogLines();
const char *getTraceLogLine(uint32_t lineIndex);

uint32_t getTraceLogStartPosition();
void setTraceLogStartPosition(uint32_t position);
void resetTraceLogStartPosition();

uint32_t getTraceLogPageSize();

void onEncoder(int couter);

} // namespace debug
} // namespace eez

Expand Down
5 changes: 5 additions & 0 deletions src/eez/gui/action_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,11 @@ void action_file_manager_sort_by() {
#endif
}

void action_show_debug_trace_log() {
eez::debug::resetTraceLogStartPosition();
pushPage(PAGE_ID_DEBUG_TRACE_LOG);
}

} // namespace gui
} // namespace eez

Expand Down
Loading

0 comments on commit d13c931

Please sign in to comment.