Skip to content

Commit cee6236

Browse files
committed
Initial commit
0 parents  commit cee6236

16 files changed

+490
-0
lines changed

.gdbinit

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
target extended-remote | openocd -c "gdb_port pipe" -f "openocd.cfg"
2+
monitor reset halt
3+
load
4+
monitor reset halt
5+
set step-mode on
6+
tui enable
7+
layout asm
8+
layout regs
9+

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
compile_commands.json
2+
build
3+
.clangd

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "buildsystem"]
2+
path = buildsystem
3+
url = [email protected]:Javier_varez/buildsystem.git

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
BUILD_SYSTEM_DIR := buildsystem
2+
include $(BUILD_SYSTEM_DIR)/top.mk
3+
include buildconfig/compiler.mk
4+
5+
include $(call all-makefiles-under, .)

app/build.mk

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
LOCAL_DIR := $(call current-dir)
2+
3+
include $(CLEAR_VARS)
4+
LOCAL_NAME := format
5+
TARGET_CFLAGS := \
6+
$(COMPILER_TRIPLE) \
7+
-mcpu=cortex-m3 \
8+
-mfloat-abi=soft \
9+
-mthumb
10+
LOCAL_CFLAGS := \
11+
$(COMPILER_CFLAGS) \
12+
$(TARGET_CFLAGS) \
13+
-Os \
14+
-g3 \
15+
-I$(LOCAL_DIR)/inc \
16+
-Wall \
17+
-Werror \
18+
-Wextra
19+
LOCAL_CXXFLAGS := \
20+
$(LOCAL_CFLAGS) \
21+
-fno-exceptions \
22+
-fno-rtti
23+
LOCAL_LDFLAGS := \
24+
$(COMPILER_LDFLAGS) \
25+
-Wl,--gc-sections
26+
LOCAL_LINKER_FILE := \
27+
$(LOCAL_DIR)/gcc.ld
28+
LOCAL_SRC := \
29+
$(LOCAL_DIR)/src/startup.cpp \
30+
$(LOCAL_DIR)/src/uart_logger.cpp \
31+
$(LOCAL_DIR)/src/main.cpp
32+
include $(BUILD_BINARY)

app/gcc.ld

+201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/* Linker script to configure memory regions.
2+
* Need modifying for a specific board.
3+
* FLASH.ORIGIN: starting address of flash
4+
* FLASH.LENGTH: length of flash
5+
* RAM.ORIGIN: starting address of RAM bank 0
6+
* RAM.LENGTH: length of RAM bank 0
7+
*/
8+
MEMORY
9+
{
10+
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 64K
11+
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
12+
}
13+
14+
/* Linker script to place sections and symbol values. Should be used together
15+
* with other linker script that defines memory regions FLASH and RAM.
16+
* It references following symbols, which must be defined in code:
17+
* Reset_Handler : Entry of reset handler
18+
*
19+
* It defines following symbols, which code can use without definition:
20+
* __exidx_start
21+
* __exidx_end
22+
* __copy_table_start__
23+
* __copy_table_end__
24+
* __zero_table_start__
25+
* __zero_table_end__
26+
* __etext
27+
* __data_start__
28+
* __preinit_array_start
29+
* __preinit_array_end
30+
* __init_array_start
31+
* __init_array_end
32+
* __fini_array_start
33+
* __fini_array_end
34+
* __data_end__
35+
* __bss_start__
36+
* __bss_end__
37+
* __end__
38+
* end
39+
* __HeapLimit
40+
* __StackLimit
41+
* __StackTop
42+
* __stack
43+
*/
44+
ENTRY(Reset_Handler)
45+
46+
SECTIONS
47+
{
48+
.text :
49+
{
50+
KEEP(*(.isr_vector))
51+
*(.text*)
52+
53+
KEEP(*(.init))
54+
KEEP(*(.fini))
55+
56+
/* .ctors */
57+
*crtbegin.o(.ctors)
58+
*crtbegin?.o(.ctors)
59+
*(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
60+
*(SORT(.ctors.*))
61+
*(.ctors)
62+
63+
/* .dtors */
64+
*crtbegin.o(.dtors)
65+
*crtbegin?.o(.dtors)
66+
*(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
67+
*(SORT(.dtors.*))
68+
*(.dtors)
69+
70+
*(.rodata*)
71+
72+
KEEP(*(.eh_frame*))
73+
} > FLASH
74+
75+
.ARM.extab :
76+
{
77+
*(.ARM.extab* .gnu.linkonce.armextab.*)
78+
} > FLASH
79+
80+
__exidx_start = .;
81+
.ARM.exidx :
82+
{
83+
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
84+
} > FLASH
85+
__exidx_end = .;
86+
87+
/* To copy multiple ROM to RAM sections,
88+
* uncomment .copy.table section and,
89+
* define __STARTUP_COPY_MULTIPLE in startup_ARMCMx.S */
90+
/*
91+
.copy.table :
92+
{
93+
. = ALIGN(4);
94+
__copy_table_start__ = .;
95+
LONG (__etext)
96+
LONG (__data_start__)
97+
LONG (__data_end__ - __data_start__)
98+
LONG (__etext2)
99+
LONG (__data2_start__)
100+
LONG (__data2_end__ - __data2_start__)
101+
__copy_table_end__ = .;
102+
} > FLASH
103+
*/
104+
105+
/* To clear multiple BSS sections,
106+
* uncomment .zero.table section and,
107+
* define __STARTUP_CLEAR_BSS_MULTIPLE in startup_ARMCMx.S */
108+
/*
109+
.zero.table :
110+
{
111+
. = ALIGN(4);
112+
__zero_table_start__ = .;
113+
LONG (__bss_start__)
114+
LONG (__bss_end__ - __bss_start__)
115+
LONG (__bss2_start__)
116+
LONG (__bss2_end__ - __bss2_start__)
117+
__zero_table_end__ = .;
118+
} > FLASH
119+
*/
120+
121+
/* Location counter can end up 2byte aligned with narrow Thumb code but
122+
__etext is assumed by startup code to be the LMA of a section in RAM
123+
which must be 4byte aligned */
124+
__etext = ALIGN (4);
125+
126+
.data : AT (__etext)
127+
{
128+
__data_start__ = .;
129+
*(vtable)
130+
*(.data*)
131+
132+
. = ALIGN(4);
133+
/* preinit data */
134+
PROVIDE_HIDDEN (__preinit_array_start = .);
135+
KEEP(*(.preinit_array))
136+
PROVIDE_HIDDEN (__preinit_array_end = .);
137+
138+
. = ALIGN(4);
139+
/* init data */
140+
PROVIDE_HIDDEN (__init_array_start = .);
141+
KEEP(*(SORT(.init_array.*)))
142+
KEEP(*(.init_array))
143+
PROVIDE_HIDDEN (__init_array_end = .);
144+
145+
146+
. = ALIGN(4);
147+
/* finit data */
148+
PROVIDE_HIDDEN (__fini_array_start = .);
149+
KEEP(*(SORT(.fini_array.*)))
150+
KEEP(*(.fini_array))
151+
PROVIDE_HIDDEN (__fini_array_end = .);
152+
153+
KEEP(*(.jcr*))
154+
. = ALIGN(4);
155+
/* All data end */
156+
__data_end__ = .;
157+
158+
} > RAM
159+
160+
.bss :
161+
{
162+
. = ALIGN(4);
163+
__bss_start__ = .;
164+
*(.bss*)
165+
*(COMMON)
166+
. = ALIGN(4);
167+
__bss_end__ = .;
168+
} > RAM
169+
170+
.heap (COPY):
171+
{
172+
__end__ = .;
173+
PROVIDE(end = .);
174+
*(.heap*)
175+
__HeapLimit = .;
176+
} > RAM
177+
178+
/* .stack_dummy section doesn't contains any symbols. It is only
179+
* used for linker to calculate size of stack sections, and assign
180+
* values to stack symbols later */
181+
.stack_dummy (COPY):
182+
{
183+
*(.stack*)
184+
} > RAM
185+
186+
/* Set stack top to end of RAM, and stack limit move down by
187+
* size of stack_dummy section */
188+
__StackTop = ORIGIN(RAM) + LENGTH(RAM);
189+
__StackInit = __StackTop - 1;
190+
__StackLimit = __StackTop - SIZEOF(.stack_dummy);
191+
PROVIDE(__stack = __StackTop);
192+
193+
/* Check if data + heap + stack exceeds RAM limit */
194+
ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack")
195+
196+
.intern_strings 0 (INFO):
197+
{
198+
*(.intern_strings*)
199+
}
200+
}
201+

app/inc/format.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef FORMAT_H_
2+
#define FORMAT_H_
3+
4+
#include <cstdint>
5+
6+
template<class Derived>
7+
class Logger {
8+
public:
9+
template<typename T>
10+
inline void sendRemainingArguments(const T& first_arg) {
11+
sendArgument(first_arg);
12+
}
13+
14+
template<typename T, typename ... Types>
15+
inline void sendRemainingArguments(const T& first_arg, Types... args) {
16+
sendArgument(first_arg);
17+
sendRemainingArguments(args...);
18+
}
19+
20+
inline void printfFmtValidator(const char* fmt, ...) __attribute__ ((format (printf, 2, 3))) {
21+
(void) fmt;
22+
}
23+
24+
private:
25+
template<typename T>
26+
inline void sendArgument(const T& argument) {
27+
Derived& derived = static_cast<Derived&>(*this);
28+
derived.addData(reinterpret_cast<const uint8_t*>(argument), sizeof(T));
29+
}
30+
};
31+
32+
#define LOG_DEBUG(logger, fmt) \
33+
{ \
34+
if (false) (logger)->printfFmtValidator(fmt); \
35+
__attribute__((section(".intern_strings"))) static const char string[] = fmt; \
36+
std::uint32_t fmt_id = reinterpret_cast<std::uint32_t>(string); \
37+
(logger)->sendRemainingArguments(fmt_id); \
38+
}
39+
40+
#define LOG_DEBUG_ARGS(logger, fmt, ...) \
41+
{ \
42+
if (false) (logger)->printfFmtValidator(fmt, __VA_ARGS__); \
43+
__attribute__((section(".intern_strings"))) static const char string[] = fmt; \
44+
std::uint32_t fmt_id = reinterpret_cast<std::uint32_t>(string); \
45+
(logger)->sendRemainingArguments(fmt_id, __VA_ARGS__); \
46+
}
47+
48+
#endif
49+

app/inc/uart_logger.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
#ifndef UART_LOGGER_H_
3+
#define UART_LOGGER_H_
4+
5+
#include "format.h"
6+
7+
class UartLogger: public Logger<UartLogger> {
8+
private:
9+
void addData(const uint8_t* data, uint32_t length);
10+
11+
friend Logger<UartLogger>;
12+
};
13+
14+
#endif // UART_LOGGER_H_

app/src/main.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
#include <cstdint>
3+
4+
#include "uart_logger.h"
5+
6+
template<std::uint32_t PTR>
7+
class RegAccess {
8+
public:
9+
static void writeRegister(std::uint32_t value) {
10+
*reinterpret_cast<volatile uint32_t*>(PTR) = value;
11+
}
12+
static std::uint32_t readRegister() {
13+
return *reinterpret_cast<volatile uint32_t*>(PTR);
14+
}
15+
};
16+
17+
constexpr uint32_t RCC_APB2_ENR = 0x40021018;
18+
constexpr uint32_t GPIO_PORTC = 0x40011000;
19+
constexpr uint32_t GPIO_CRH_OFFSET = 0x04;
20+
constexpr uint32_t GPIO_BSRR_OFFSET = 0x10;
21+
22+
int main() {
23+
UartLogger logger;
24+
LOG_DEBUG(&logger, "Ooopsie");
25+
LOG_DEBUG_ARGS(&logger, "Ooops%die2", 23);
26+
LOG_DEBUG_ARGS(&logger, "Ooops%die2", 123);
27+
RegAccess<RCC_APB2_ENR>::writeRegister(0x10);
28+
RegAccess<GPIO_PORTC + GPIO_CRH_OFFSET>::writeRegister(1 << 20);
29+
while (true) {
30+
RegAccess<GPIO_PORTC + GPIO_BSRR_OFFSET>::writeRegister(1 << 13);
31+
RegAccess<GPIO_PORTC + GPIO_BSRR_OFFSET>::writeRegister(1 << 29);
32+
}
33+
return 0;
34+
}
35+

0 commit comments

Comments
 (0)