Skip to content

Commit a0ffbfc

Browse files
First functional version of logger (only in ram)
Signed-off-by: Martin Ribelotta <martinribelotta@gmail.com>
1 parent 47987ed commit a0ffbfc

File tree

6 files changed

+196
-3
lines changed

6 files changed

+196
-3
lines changed

Makefile

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ BIN=$(patsubst %.elf, %.bin, $(ELF))
2222
HEX=$(patsubst %.elf, %.hex, $(ELF))
2323
LST=$(patsubst %.elf, %.lst, $(ELF))
2424
NMP=$(patsubst %.elf, %.nmp, $(ELF))
25+
BINLOG=$(DEST)/log_text.bin
2526

2627
CFLAGS:=$(ARCH)
2728
CFLAGS+=$(addprefix -I, $(INCLUDES))
@@ -72,7 +73,7 @@ NM=$(CROSS)nm
7273
RM=rm -fr
7374
MD=mkdir -p
7475

75-
all: $(ELF) $(BIN) $(HEX) $(LST) $(NMP)
76+
all: $(ELF) $(LST) $(NMP) $(BINLOG) $(BIN) $(HEX)
7677

7778
OBJS:=$(addprefix $(DEST)/, $(patsubst %.c, %.o, $(notdir $(CSRC))))
7879
DEPS:=$(addsuffix %.dep.mk, $(OBJS))
@@ -98,6 +99,12 @@ $(ELF): $(OBJ)
9899
@echo LD $@
99100
$(Q)$(LD) -o $@ $(LDFLAGS) $(OBJS)
100101

102+
$(BINLOG): $(ELF)
103+
@echo EXTRACT LOGS $@
104+
$(Q)$(OCP) --dump-section .elog=$@ $<
105+
@echo LOGS NOLOAD ELF $<
106+
@$(OCP) --set-section-flags .elog=noload $<
107+
101108
$(BIN): $(ELF)
102109
@echo BIN $<
103110
$(Q)$(OCP) -O binary $< $@

inc/elog-cpp.h

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#ifndef __ELOG_CPP_H__
2+
#define __ELOG_CPP_H__
3+
4+
#define ELOG_NARG(...) \
5+
ELOG_NARG_(__VA_ARGS__, ELOG_RSEQ_N())
6+
#define ELOG_NARG_(...) \
7+
ELOG_ARG_N(__VA_ARGS__)
8+
#define ELOG_ARG_N( \
9+
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
10+
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
11+
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
12+
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
13+
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
14+
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
15+
_61,_62,_63,N,...) N
16+
#define ELOG_RSEQ_N() \
17+
63,62,61,60, \
18+
59,58,57,56,55,54,53,52,51,50, \
19+
49,48,47,46,45,44,43,42,41,40, \
20+
39,38,37,36,35,34,33,32,31,30, \
21+
29,28,27,26,25,24,23,22,21,20, \
22+
19,18,17,16,15,14,13,12,11,10, \
23+
9,8,7,6,5,4,3,2,1,0
24+
25+
#define FIRST_(a, ...) a
26+
#define SECOND_(a, b, ...) b
27+
28+
#define FIRST(...) FIRST_(__VA_ARGS__,)
29+
#define SECOND(...) SECOND_(__VA_ARGS__,)
30+
31+
#define EMPTY()
32+
33+
#define EVAL(...) EVAL1024(__VA_ARGS__)
34+
#define EVAL1024(...) EVAL512(EVAL512(__VA_ARGS__))
35+
#define EVAL512(...) EVAL256(EVAL256(__VA_ARGS__))
36+
#define EVAL256(...) EVAL128(EVAL128(__VA_ARGS__))
37+
#define EVAL128(...) EVAL64(EVAL64(__VA_ARGS__))
38+
#define EVAL64(...) EVAL32(EVAL32(__VA_ARGS__))
39+
#define EVAL32(...) EVAL16(EVAL16(__VA_ARGS__))
40+
#define EVAL16(...) EVAL8(EVAL8(__VA_ARGS__))
41+
#define EVAL8(...) EVAL4(EVAL4(__VA_ARGS__))
42+
#define EVAL4(...) EVAL2(EVAL2(__VA_ARGS__))
43+
#define EVAL2(...) EVAL1(EVAL1(__VA_ARGS__))
44+
#define EVAL1(...) __VA_ARGS__
45+
46+
#define DEFER1(m) m EMPTY()
47+
#define DEFER2(m) m EMPTY EMPTY()()
48+
49+
#define IS_PROBE(...) SECOND(__VA_ARGS__, 0)
50+
#define PROBE() ~, 1
51+
52+
#define CAT(a,b) a ## b
53+
54+
#define NOT(x) IS_PROBE(CAT(_NOT_, x))
55+
#define _NOT_0 PROBE()
56+
57+
#define BOOL(x) NOT(NOT(x))
58+
59+
#define IF_ELSE(condition) _IF_ELSE(BOOL(condition))
60+
#define _IF_ELSE(condition) CAT(_IF_, condition)
61+
62+
#define _IF_1(...) __VA_ARGS__ _IF_1_ELSE
63+
#define _IF_0(...) _IF_0_ELSE
64+
65+
#define _IF_1_ELSE(...)
66+
#define _IF_0_ELSE(...) __VA_ARGS__
67+
68+
#define HAS_ARGS(...) BOOL(FIRST(_END_OF_ARGUMENTS_ __VA_ARGS__)())
69+
#define _END_OF_ARGUMENTS_() 0
70+
71+
#define MAP(m, first, ...) \
72+
m(first) \
73+
IF_ELSE(HAS_ARGS(__VA_ARGS__))( \
74+
DEFER2(_MAP)()(m, __VA_ARGS__) \
75+
)( \
76+
/* Do nothing, just terminate */ \
77+
)
78+
#define _MAP() MAP
79+
80+
#define _long_cast_apply(x) ((long) (x)),
81+
#define long_cast_apply(...) EVAL(MAP(_long_cast_apply, __VA_ARGS__))
82+
83+
#endif /* __ELOG_CPP_H__ */

inc/elog.h

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef __ELOG_H__
2+
#define __ELOG_H__
3+
4+
#include <stddef.h>
5+
6+
#include "elog-cpp.h"
7+
8+
#define ELOG(o, msg, ...) do { \
9+
__attribute__((section("elog"))) static const char p_msg[] = msg; \
10+
IF_ELSE(HAS_ARGS(__VA_ARGS__))( \
11+
elog_put(o, p_msg, ELOG_NARG(__VA_ARGS__), (long[]){ long_cast_apply(__VA_ARGS__) }); \
12+
)( \
13+
elog_put(o, p_msg, 0, (long[]){}); \
14+
) \
15+
} while(0)
16+
17+
#define ELOG_BUFFER(name) ((elog_t*)(name))
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
typedef struct {
24+
size_t buflen;
25+
size_t offset;
26+
char buffer[0];
27+
} elog_t;
28+
29+
typedef struct
30+
{
31+
size_t msgid;
32+
size_t len;
33+
long data[0];
34+
} elog_entry_t;
35+
36+
typedef void (elog_flush_func_t)(elog_entry_t *e);
37+
38+
extern elog_t *elog_init(void *arena, size_t size);
39+
extern int elog_put(elog_t *log, const char *const msg, int n, long args[]);
40+
extern elog_entry_t *elog_peek(elog_t *log);
41+
extern void elog_flush(elog_t *log, elog_flush_func_t func);
42+
43+
#ifdef __cplusplus
44+
}
45+
#endif
46+
47+
#endif /* __ELOG_H__ */

lib/armv7m-generic.ld

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ MEMORY
2424
{
2525
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K
2626
ROM (rx) : ORIGIN = 0x08000000, LENGTH = 64K
27+
LOG (r) : ORIGIN = 0xF0000000, LENGTH = 1024M
2728
}
2829

2930
_estack = ORIGIN(RAM) + LENGTH(RAM);
@@ -123,6 +124,12 @@ SECTIONS
123124
end = .;
124125
} >RAM
125126

127+
128+
.elog :
129+
{
130+
*(elog* elog.*)
131+
} >LOG
132+
126133
/* Remove information from the standard libraries */
127134
/DISCARD/ :
128135
{

src/elog.c

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <elog.h>
2+
3+
#include <string.h>
4+
5+
elog_t *elog_init(void *arena, size_t size)
6+
{
7+
elog_t *ptr = (elog_t*) arena;
8+
ptr->buflen = size;
9+
ptr->offset = 0;
10+
return ptr;
11+
}
12+
13+
int elog_put(elog_t *log, const char *const msg, int n, long args[])
14+
{
15+
elog_entry_t *e = (elog_entry_t*) (log->buffer + log->offset);
16+
long esize = n * sizeof(long);
17+
long newoff = log->offset + sizeof(elog_entry_t) + esize;
18+
if (newoff < log->buflen) {
19+
e->msgid = (size_t) msg;
20+
e->len = n;
21+
memcpy(e->data, args, esize);
22+
log->offset = newoff;
23+
return 1;
24+
} else {
25+
return 0;
26+
}
27+
}
28+
29+
void elog_flush(elog_t *log, elog_flush_func_t func)
30+
{
31+
long off = 0;
32+
while (off < log->offset) {
33+
elog_entry_t *e = ((elog_entry_t*) log->buffer + off);
34+
func(e);
35+
off += e->len;
36+
}
37+
log->offset = 0;
38+
}

src/main.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
#include <ucmsis.h>
2-
#include <stddef.h>
32
#include <usemihosting.h>
43

4+
#include <elog.h>
5+
6+
static elog_t *logger;
7+
static char arena[1024];
8+
59
int main()
610
{
711
static const char const b[] = "Hello world\n";
8-
int fd = open("tmp.txt", SYS_OPEN_WO);
12+
logger = elog_init(arena, sizeof(arena));
13+
ELOG(logger, "Hello world %d\n", 10);
14+
const char *const filename = "tmp.txt";
15+
int fd = open(filename, SYS_OPEN_WO);
16+
ELOG(logger, "Open file %s with fd=%d\n", filename, fd);
917
if (fd != -1) {
18+
ELOG(logger, "Writing message to file\n");
1019
write(fd, b, sizeof(b));
20+
ELOG(logger, "Closing..\n");
1121
close(fd);
1222
} else {
23+
ELOG(logger, "Error opening archive\n");
1324
writestr("Error opening file");
1425
}
1526
return 0;

0 commit comments

Comments
 (0)