Skip to content

Commit 1b5590f

Browse files
Add elog flush over semihosting
Signed-off-by: Martin Ribelotta <martinribelotta@gmail.com>
1 parent a0ffbfc commit 1b5590f

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@
3535
*.lst
3636
*.nmp
3737
*.dep.mk
38+
39+
# VSCode C debug
40+
.vscode/.cortex-debug.*.json

.vscode/launch.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"cwd": "${workspaceRoot}",
9+
"executable": "${workspaceFolder}/out/elog-test.elf",
10+
"name": "Debug",
11+
"request": "launch",
12+
"type": "cortex-debug",
13+
"servertype": "openocd",
14+
"configFiles": [
15+
"interface/stlink.cfg",
16+
"target/stm32f1x.cfg"
17+
],
18+
"openOCDLaunchCommands": [
19+
"init",
20+
"arm semihosting enable"
21+
],
22+
"runToMain": true
23+
}
24+
]
25+
}

inc/elog.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ typedef struct
3333
long data[0];
3434
} elog_entry_t;
3535

36-
typedef void (elog_flush_func_t)(elog_entry_t *e);
36+
typedef void (elog_flush_func_t)(elog_entry_t *e, void *ctx);
3737

3838
extern elog_t *elog_init(void *arena, size_t size);
3939
extern int elog_put(elog_t *log, const char *const msg, int n, long args[]);
4040
extern elog_entry_t *elog_peek(elog_t *log);
41-
extern void elog_flush(elog_t *log, elog_flush_func_t func);
41+
extern void elog_flush(elog_t *log, elog_flush_func_t func, void *ctx);
4242

4343
#ifdef __cplusplus
4444
}

src/elog.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ int elog_put(elog_t *log, const char *const msg, int n, long args[])
2626
}
2727
}
2828

29-
void elog_flush(elog_t *log, elog_flush_func_t func)
29+
void elog_flush(elog_t *log, elog_flush_func_t func, void *ctx)
3030
{
3131
long off = 0;
3232
while (off < log->offset) {
33-
elog_entry_t *e = ((elog_entry_t*) log->buffer + off);
34-
func(e);
35-
off += e->len;
33+
elog_entry_t *e = ((elog_entry_t*) (log->buffer + off));
34+
size_t incr = sizeof(elog_entry_t) + e->len * sizeof(long);
35+
off += incr;
36+
func(e, ctx);
3637
}
3738
log->offset = 0;
3839
}

src/main.c

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
static elog_t *logger;
77
static char arena[1024];
88

9+
static void log_to_semihost(elog_entry_t *e, void *ctx)
10+
{
11+
int fd = *(int*) ctx;
12+
write(fd, e, sizeof(elog_entry_t) + e->len * sizeof(long));
13+
}
14+
915
int main()
1016
{
1117
static const char const b[] = "Hello world\n";
@@ -23,5 +29,12 @@ int main()
2329
ELOG(logger, "Error opening archive\n");
2430
writestr("Error opening file");
2531
}
32+
int log_fd = open("bin.log", SYS_OPEN_WOB);
33+
if (log_fd != -1) {
34+
elog_flush(logger, log_to_semihost, &log_fd);
35+
close(log_fd);
36+
} else {
37+
writestr("cannot open bin log for write");
38+
}
2639
return 0;
2740
}

0 commit comments

Comments
 (0)