Skip to content

Commit 4fc2866

Browse files
committed
bare-arm: Clean up the code, make it run on an F405, and add a README.
This commit simplifies and cleans up the bare-arm port, and adds just enough system and library code to make it execute on an STM32F405 MCU. The mpconfigport.h configuration is simplified to just specify those configuration values that are different from the defaults. And the addition of -fdata-sections and -ffunction-sections means the final firmware is smaller than it previously was, by about 4200 bytes. A README is also added. Signed-off-by: Damien George <[email protected]>
1 parent 42cf77f commit 4fc2866

File tree

8 files changed

+453
-275
lines changed

8 files changed

+453
-275
lines changed

ports/bare-arm/Makefile

+37-30
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,59 @@
1+
# Include the core environment definitions; this will set $(TOP).
12
include ../../py/mkenv.mk
23

3-
# qstr definitions (must come before including py.mk)
4-
QSTR_DEFS = qstrdefsport.h
4+
# Include py core make definitions.
5+
include $(TOP)/py/py.mk
56

6-
# MicroPython feature configurations
7+
# Set makefile-level MicroPython feature configurations.
78
MICROPY_ROM_TEXT_COMPRESSION ?= 1
89

9-
# include py core make definitions
10-
include $(TOP)/py/py.mk
11-
10+
# Define toolchain and other tools.
1211
CROSS_COMPILE ?= arm-none-eabi-
12+
DFU ?= $(TOP)/tools/dfu.py
13+
PYDFU ?= $(TOP)/tools/pydfu.py
1314

14-
INC += -I.
15-
INC += -I$(TOP)
16-
INC += -I$(BUILD)
17-
18-
CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion
19-
CFLAGS = $(INC) -Wall -Werror -std=c99 -nostdlib $(CFLAGS_CORTEX_M4) $(COPT)
20-
CSUPEROPT = -Os # save some code space
15+
# Set CFLAGS.
16+
CFLAGS += -I. -I$(TOP) -I$(BUILD)
17+
CFLAGS += -Wall -Werror -std=c99 -nostdlib
18+
CFLAGS += -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -msoft-float
19+
CSUPEROPT = -Os # save some code space for performance-critical code
2120

22-
#Debugging/Optimization
21+
# Select debugging or optimisation build.
2322
ifeq ($(DEBUG), 1)
24-
CFLAGS += -O0 -ggdb
23+
CFLAGS += -Og
2524
else
2625
CFLAGS += -Os -DNDEBUG
26+
CFLAGS += -fdata-sections -ffunction-sections
2727
endif
2828

29-
LDFLAGS = -nostdlib -T stm32f405.ld -Map=$@.map --cref
30-
LIBS =
29+
# Set linker flags.
30+
LDFLAGS += -nostdlib -T stm32f405.ld --gc-sections
3131

32-
SRC_C = \
33-
main.c \
34-
# printf.c \
35-
string0.c \
36-
malloc0.c \
37-
gccollect.c \
32+
# Define the required source files.
33+
SRC_C += lib.c main.c system.c
3834

39-
SRC_S = \
40-
# startup_stm32f40xx.s \
41-
gchelper.s \
35+
# Define the required object files.
36+
OBJ += $(PY_CORE_O)
37+
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
4238

43-
OBJ = $(PY_CORE_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o))
44-
45-
all: $(BUILD)/firmware.elf
39+
# Define the top-level target, the main firmware.
40+
all: $(BUILD)/firmware.dfu
4641

4742
$(BUILD)/firmware.elf: $(OBJ)
4843
$(ECHO) "LINK $@"
49-
$(Q)$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)
44+
$(Q)$(LD) $(LDFLAGS) -o $@ $^
5045
$(Q)$(SIZE) $@
5146

47+
$(BUILD)/firmware.bin: $(BUILD)/firmware.elf
48+
$(ECHO) "Create $@"
49+
$(Q)$(OBJCOPY) -O binary -j .isr_vector -j .text -j .data $^ $@
50+
51+
$(BUILD)/firmware.dfu: $(BUILD)/firmware.bin
52+
$(ECHO) "Create $@"
53+
$(Q)$(PYTHON) $(DFU) -b 0x08000000:$^ $@
54+
55+
deploy: $(BUILD)/firmware.dfu
56+
$(Q)$(PYTHON) $(PYDFU) -u $^
57+
58+
# Include remaining core make rules.
5259
include $(TOP)/py/mkrules.mk

ports/bare-arm/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The bare-arm port
2+
=================
3+
4+
This port is intended to be the bare-minimum amount of code and configuration
5+
required to get MicroPython compiling and running on a bare-metal ARM-based
6+
target. No external dependencies or libraries are needed for this build and
7+
it shows exactly what hardware and system functionality MicroPython needs to
8+
run.
9+
10+
To build, simply run `make` in this directory. The output will be
11+
`build/firmware.elf` (and also corresponding `.bin` and `.dfu` files). This
12+
firmware can run on an STM32F405-based board (eg a PYBv1.x) and `make deploy`
13+
will program it to such an MCU when put in USB DFU mode. The output is a UART
14+
at 115200 baud, with TX on PA0.
15+
16+
There are some simple demonstration code strings (see `main.c`) which are
17+
compiled and executed when the firmware starts. They produce output on the
18+
system's stdout.
19+
20+
The size of the firmware (the machine code that is programmed to the
21+
microcontroller's flash/ROM) is currently around 61200 bytes.

ports/bare-arm/lib.c

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <string.h>
28+
29+
// These memory functions are needed when the garbage collector is disabled.
30+
// A full implementation should be provided, or the garbage collector enabled.
31+
// The functions here are very simple.
32+
33+
extern char _heap_start;
34+
35+
void *malloc(size_t n) {
36+
static char *cur_heap = NULL;
37+
if (cur_heap == NULL) {
38+
cur_heap = &_heap_start;
39+
}
40+
void *ptr = cur_heap;
41+
cur_heap += (n + 7) & ~7;
42+
return ptr;
43+
}
44+
45+
void *realloc(void *ptr, size_t size) {
46+
void *ptr2 = malloc(size);
47+
if (ptr && size) {
48+
memcpy(ptr2, ptr, size); // size may be greater than ptr's region, do copy anyway
49+
}
50+
return ptr2;
51+
}
52+
53+
void free(void *p) {
54+
}
55+
56+
// These standard string functions are needed by the runtime, and can be
57+
// provided either by the system or lib/libc/string0.c. The implementations
58+
// here are very simple.
59+
60+
int memcmp(const void *s1, const void *s2, size_t n) {
61+
const unsigned char *ss1 = s1, *ss2 = s2;
62+
while (n--) {
63+
int c = *ss1++ - *ss2++;
64+
if (c) {
65+
return c;
66+
}
67+
}
68+
return 0;
69+
}
70+
71+
void *memcpy(void *dest, const void *src, size_t n) {
72+
return memmove(dest, src, n);
73+
}
74+
75+
void *memmove(void *dest, const void *src, size_t n) {
76+
unsigned char *d = dest;
77+
const unsigned char *s = src;
78+
if (s < d && d < s + n) {
79+
// Need to copy backwards.
80+
d += n - 1;
81+
s += n - 1;
82+
while (n--) {
83+
*d-- = *s--;
84+
}
85+
} else {
86+
// Can copy forwards.
87+
while (n--) {
88+
*d++ = *s++;
89+
}
90+
}
91+
return dest;
92+
}
93+
94+
void *memset(void *s, int c, size_t n) {
95+
unsigned char *ss = s;
96+
while (n--) {
97+
*ss++ = c;
98+
}
99+
return s;
100+
}
101+
102+
char *strchr(const char *s, int c) {
103+
while (*s) {
104+
if (*s == c) {
105+
return (char *)s;
106+
}
107+
++s;
108+
}
109+
return NULL;
110+
}
111+
112+
int strcmp(const char *s1, const char *s2) {
113+
while (*s1 && *s2) {
114+
int c = *s1++ - *s2++;
115+
if (c) {
116+
return c;
117+
}
118+
}
119+
return *s1 - *s2;
120+
}
121+
122+
size_t strlen(const char *s) {
123+
const char *ss = s;
124+
while (*ss) {
125+
++ss;
126+
}
127+
return ss - s;
128+
}

0 commit comments

Comments
 (0)