Skip to content

Commit 5472924

Browse files
committed
minimal: Add enough code to run minimal build on STM32F4xx hardware.
Minimal support code for a Cortex-M CPU is added, along with set-up code for an STM32F4xx MCU, including a UART for a REPL. Tested on a pyboard. Code size is 77592 bytes.
1 parent dd0a0f7 commit 5472924

6 files changed

Lines changed: 234 additions & 63 deletions

File tree

minimal/Makefile

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ INC += -I../stmhal
1919
INC += -I$(BUILD)
2020

2121
ifeq ($(CROSS), 1)
22+
DFU = ../tools/dfu.py
23+
PYDFU = ../tools/pydfu.py
2224
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
2325
CFLAGS = $(INC) -Wall -Werror -ansi -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) $(COPT)
2426
else
@@ -49,20 +51,28 @@ SRC_C = \
4951
lib/libc/string0.c \
5052
lib/mp-readline/readline.c \
5153

52-
SRC_S = \
53-
54-
# startup_stm32f40xx.s \
55-
# gchelper.s \
56-
57-
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o))
54+
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
5855

56+
ifeq ($(CROSS), 1)
57+
all: $(BUILD)/firmware.dfu
58+
else
5959
all: $(BUILD)/firmware.elf
60+
endif
6061

6162
$(BUILD)/firmware.elf: $(OBJ)
6263
$(ECHO) "LINK $@"
6364
$(Q)$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)
6465
$(Q)$(SIZE) $@
6566

67+
$(BUILD)/firmware.dfu: $(BUILD)/firmware.elf
68+
$(ECHO) "Create $@"
69+
$(Q)$(OBJCOPY) -O binary -j .isr_vector -j .text -j .data $^ $(BUILD)/firmware.bin
70+
$(Q)$(PYTHON) $(DFU) -b 0x08000000:$(BUILD)/firmware.bin $@
71+
72+
deploy: $(BUILD)/firmware.dfu
73+
$(ECHO) "Writing $< to the board"
74+
$(Q)$(PYTHON) $(PYDFU) -u $<
75+
6676
# Run emulation build on a POSIX system with suitable terminal settings
6777
run:
6878
stty raw opost -echo

minimal/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# The minimal port
2+
3+
This port is intended to be a minimal MicroPython port that actually runs.
4+
It can run under Linux (or similar) and on any STM32F4xx MCU (eg the pyboard).
5+
6+
## Building and running Linux version
7+
8+
By default the port will be built for the host machine:
9+
10+
$ make
11+
12+
To run a small test script do:
13+
14+
$ make run
15+
16+
## Building for an STM32 MCU
17+
18+
The Makefile has the ability to build for a Cortex-M CPU, and by default
19+
includes some start-up code for an STM32F4xx MCU and also enables a UART
20+
for communication. To build:
21+
22+
$ make CROSS=1
23+
24+
If you previously built the Linux version, you will need to first run
25+
`make clean` to get rid of incompatible object files.
26+
27+
Building will produce the build/firmware.dfu file which can be programmed
28+
to an MCU using:
29+
30+
$ make CROSS=1 deploy
31+
32+
This version of the build will work out-of-the-box on a pyboard (and
33+
anything similar), and will give you a MicroPython REPL on UART1 at 9600
34+
baud. Pin PA13 will also be driven high, and this turns on the red LED on
35+
the pyboard.

minimal/main.c

Lines changed: 157 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,161 @@ void MP_WEAK __assert_func(const char *file, int line, const char *func, const c
9494
}
9595
#endif
9696

97-
#if !MICROPY_MIN_USE_STDOUT
98-
void _start(void) {main(0, NULL);}
97+
#if MICROPY_MIN_USE_CORTEX_CPU
98+
99+
// this is a minimal IRQ and reset framework for any Cortex-M CPU
100+
101+
extern uint32_t _estack, _sidata, _sdata, _edata, _sbss, _ebss;
102+
103+
void Reset_Handler(void) __attribute__((naked));
104+
void Reset_Handler(void) {
105+
// set stack pointer
106+
asm volatile ("ldr sp, =_estack");
107+
// copy .data section from flash to RAM
108+
for (uint32_t *src = &_sidata, *dest = &_sdata; dest < &_edata;) {
109+
*dest++ = *src++;
110+
}
111+
// zero out .bss section
112+
for (uint32_t *dest = &_sbss; dest < &_ebss;) {
113+
*dest++ = 0;
114+
}
115+
// jump to board initialisation
116+
void _start(void);
117+
_start();
118+
}
119+
120+
void Default_Handler(void) {
121+
for (;;) {
122+
}
123+
}
124+
125+
uint32_t isr_vector[] __attribute__((section(".isr_vector"))) = {
126+
(uint32_t)&_estack,
127+
(uint32_t)&Reset_Handler,
128+
(uint32_t)&Default_Handler, // NMI_Handler
129+
(uint32_t)&Default_Handler, // HardFault_Handler
130+
(uint32_t)&Default_Handler, // MemManage_Handler
131+
(uint32_t)&Default_Handler, // BusFault_Handler
132+
(uint32_t)&Default_Handler, // UsageFault_Handler
133+
0,
134+
0,
135+
0,
136+
0,
137+
(uint32_t)&Default_Handler, // SVC_Handler
138+
(uint32_t)&Default_Handler, // DebugMon_Handler
139+
0,
140+
(uint32_t)&Default_Handler, // PendSV_Handler
141+
(uint32_t)&Default_Handler, // SysTick_Handler
142+
};
143+
144+
void _start(void) {
145+
// when we get here: stack is initialised, bss is clear, data is copied
146+
147+
// SCB->CCR: enable 8-byte stack alignment for IRQ handlers, in accord with EABI
148+
*((volatile uint32_t*)0xe000ed14) |= 1 << 9;
149+
150+
// initialise the cpu and peripherals
151+
#if MICROPY_MIN_USE_STM32_MCU
152+
void stm32_init(void);
153+
stm32_init();
154+
#endif
155+
156+
// now that we have a basic system up and running we can call main
157+
main(0, NULL);
158+
159+
// we must not return
160+
for (;;) {
161+
}
162+
}
163+
164+
#endif
165+
166+
#if MICROPY_MIN_USE_STM32_MCU
167+
168+
// this is minimal set-up code for an STM32 MCU
169+
170+
typedef struct {
171+
volatile uint32_t CR;
172+
volatile uint32_t PLLCFGR;
173+
volatile uint32_t CFGR;
174+
volatile uint32_t CIR;
175+
uint32_t _1[8];
176+
volatile uint32_t AHB1ENR;
177+
volatile uint32_t AHB2ENR;
178+
volatile uint32_t AHB3ENR;
179+
uint32_t _2;
180+
volatile uint32_t APB1ENR;
181+
volatile uint32_t APB2ENR;
182+
} periph_rcc_t;
183+
184+
typedef struct {
185+
volatile uint32_t MODER;
186+
volatile uint32_t OTYPER;
187+
volatile uint32_t OSPEEDR;
188+
volatile uint32_t PUPDR;
189+
volatile uint32_t IDR;
190+
volatile uint32_t ODR;
191+
volatile uint16_t BSRRL;
192+
volatile uint16_t BSRRH;
193+
volatile uint32_t LCKR;
194+
volatile uint32_t AFR[2];
195+
} periph_gpio_t;
196+
197+
typedef struct {
198+
volatile uint32_t SR;
199+
volatile uint32_t DR;
200+
volatile uint32_t BRR;
201+
volatile uint32_t CR1;
202+
} periph_uart_t;
203+
204+
#define USART1 ((periph_uart_t*) 0x40011000)
205+
#define GPIOA ((periph_gpio_t*) 0x40020000)
206+
#define GPIOB ((periph_gpio_t*) 0x40020400)
207+
#define RCC ((periph_rcc_t*) 0x40023800)
208+
209+
// simple GPIO interface
210+
#define GPIO_MODE_IN (0)
211+
#define GPIO_MODE_OUT (1)
212+
#define GPIO_MODE_ALT (2)
213+
#define GPIO_PULL_NONE (0)
214+
#define GPIO_PULL_UP (0)
215+
#define GPIO_PULL_DOWN (1)
216+
void gpio_init(periph_gpio_t *gpio, int pin, int mode, int pull, int alt) {
217+
gpio->MODER = (gpio->MODER & ~(3 << (2 * pin))) | (mode << (2 * pin));
218+
// OTYPER is left as default push-pull
219+
// OSPEEDR is left as default low speed
220+
gpio->PUPDR = (gpio->PUPDR & ~(3 << (2 * pin))) | (pull << (2 * pin));
221+
gpio->AFR[pin >> 3] = (gpio->AFR[pin >> 3] & ~(15 << (4 * (pin & 7)))) | (alt << (4 * (pin & 7)));
222+
}
223+
#define gpio_get(gpio, pin) ((gpio->IDR >> (pin)) & 1)
224+
#define gpio_set(gpio, pin, value) do { gpio->ODR = (gpio->ODR & ~(1 << (pin))) | (value << pin); } while (0)
225+
#define gpio_low(gpio, pin) do { gpio->BSRRH = (1 << (pin)); } while (0)
226+
#define gpio_high(gpio, pin) do { gpio->BSRRL = (1 << (pin)); } while (0)
227+
228+
void stm32_init(void) {
229+
// basic MCU config
230+
RCC->CR |= (uint32_t)0x00000001; // set HSION
231+
RCC->CFGR = 0x00000000; // reset all
232+
RCC->CR &= (uint32_t)0xfef6ffff; // reset HSEON, CSSON, PLLON
233+
RCC->PLLCFGR = 0x24003010; // reset PLLCFGR
234+
RCC->CR &= (uint32_t)0xfffbffff; // reset HSEBYP
235+
RCC->CIR = 0x00000000; // disable IRQs
236+
237+
// leave the clock as-is (internal 16MHz)
238+
239+
// enable GPIO clocks
240+
RCC->AHB1ENR |= 0x00000003; // GPIOAEN, GPIOBEN
241+
242+
// turn on an LED! (on pyboard it's the red one)
243+
gpio_init(GPIOA, 13, GPIO_MODE_OUT, GPIO_PULL_NONE, 0);
244+
gpio_high(GPIOA, 13);
245+
246+
// enable UART1 at 9600 baud (TX=B6, RX=B7)
247+
gpio_init(GPIOB, 6, GPIO_MODE_ALT, GPIO_PULL_NONE, 7);
248+
gpio_init(GPIOB, 7, GPIO_MODE_ALT, GPIO_PULL_NONE, 7);
249+
RCC->APB2ENR |= 0x00000010; // USART1EN
250+
USART1->BRR = (104 << 4) | 3; // 16MHz/(16*104.1875) = 9598 baud
251+
USART1->CR1 = 0x0000200c; // USART enable, tx enable, rx enable
252+
}
253+
99254
#endif

minimal/mpconfigport.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj;
8383
#define MICROPY_MIN_USE_STDOUT (1)
8484
#endif
8585

86+
#ifdef __thumb__
87+
#define MICROPY_MIN_USE_CORTEX_CPU (1)
88+
#define MICROPY_MIN_USE_STM32_MCU (1)
89+
#endif
90+
8691
#define MP_STATE_PORT MP_STATE_VM
8792

8893
#define MICROPY_PORT_ROOT_POINTERS \

minimal/stm32f405.ld

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,31 @@
66
MEMORY
77
{
88
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 0x100000 /* entire flash, 1 MiB */
9-
FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 0x004000 /* sector 0, 16 KiB */
10-
FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 0x080000 /* sectors 5,6,7,8, 4*128KiB = 512 KiB (could increase it more) */
119
CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 0x010000 /* 64 KiB */
1210
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x020000 /* 128 KiB */
1311
}
1412

1513
/* top end of the stack */
1614
_estack = ORIGIN(RAM) + LENGTH(RAM);
1715

18-
/* RAM extents for the garbage collector */
19-
_ram_end = ORIGIN(RAM) + LENGTH(RAM);
20-
_heap_end = 0x2001c000; /* tunable */
21-
2216
/* define output sections */
2317
SECTIONS
2418
{
25-
/* The startup code goes first into FLASH */
26-
.isr_vector :
27-
{
28-
. = ALIGN(4);
29-
KEEP(*(.isr_vector)) /* Startup code */
30-
31-
. = ALIGN(4);
32-
} >FLASH_ISR
33-
3419
/* The program code and other data goes into FLASH */
3520
.text :
3621
{
3722
. = ALIGN(4);
23+
KEEP(*(.isr_vector)) /* isr vector table */
3824
*(.text) /* .text sections (code) */
3925
*(.text*) /* .text* sections (code) */
4026
*(.rodata) /* .rodata sections (constants, strings, etc.) */
4127
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
42-
/* *(.glue_7) */ /* glue arm to thumb code */
43-
/* *(.glue_7t) */ /* glue thumb to arm code */
4428

4529
. = ALIGN(4);
4630
_etext = .; /* define a global symbol at end of code */
4731
_sidata = _etext; /* This is used by the startup in order to initialize the .data secion */
48-
} >FLASH_TEXT
49-
50-
/*
51-
.ARM.extab :
52-
{
53-
*(.ARM.extab* .gnu.linkonce.armextab.*)
5432
} >FLASH
5533

56-
.ARM :
57-
{
58-
__exidx_start = .;
59-
*(.ARM.exidx*)
60-
__exidx_end = .;
61-
} >FLASH
62-
*/
63-
6434
/* This is the initialized data section
6535
The program executes knowing that the data is in the RAM
6636
but the loader puts the initial values in the FLASH (inidata).
@@ -69,7 +39,6 @@ SECTIONS
6939
{
7040
. = ALIGN(4);
7141
_sdata = .; /* create a global symbol at data start; used by startup code in order to initialise the .data section in RAM */
72-
_ram_start = .; /* create a global symbol at ram start for garbage collector */
7342
*(.data) /* .data sections */
7443
*(.data*) /* .data* sections */
7544

@@ -90,28 +59,5 @@ SECTIONS
9059
_ebss = .; /* define a global symbol at bss end; used by startup code */
9160
} >RAM
9261

93-
/* this is to define the start of the heap, and make sure we have a minimum size */
94-
.heap :
95-
{
96-
. = ALIGN(4);
97-
_heap_start = .; /* define a global symbol at heap start */
98-
} >RAM
99-
100-
/* this just checks there is enough RAM for the stack */
101-
.stack :
102-
{
103-
. = ALIGN(4);
104-
} >RAM
105-
106-
/* Remove information from the standard libraries */
107-
/*
108-
/DISCARD/ :
109-
{
110-
libc.a ( * )
111-
libm.a ( * )
112-
libgcc.a ( * )
113-
}
114-
*/
115-
11662
.ARM.attributes 0 : { *(.ARM.attributes) }
11763
}

minimal/uart_core.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,25 @@
55
* Core UART functions to implement for a port
66
*/
77

8+
#if MICROPY_MIN_USE_STM32_MCU
9+
typedef struct {
10+
volatile uint32_t SR;
11+
volatile uint32_t DR;
12+
} periph_uart_t;
13+
#define USART1 ((periph_uart_t*)0x40011000)
14+
#endif
15+
816
// Receive single character
917
int mp_hal_stdin_rx_chr(void) {
1018
unsigned char c = 0;
1119
#if MICROPY_MIN_USE_STDOUT
1220
int r = read(0, &c, 1);
1321
(void)r;
22+
#elif MICROPY_MIN_USE_STM32_MCU
23+
// wait for RXNE
24+
while ((USART1->SR & (1 << 5)) == 0) {
25+
}
26+
c = USART1->DR;
1427
#endif
1528
return c;
1629
}
@@ -20,5 +33,12 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
2033
#if MICROPY_MIN_USE_STDOUT
2134
int r = write(1, str, len);
2235
(void)r;
36+
#elif MICROPY_MIN_USE_STM32_MCU
37+
while (len--) {
38+
// wait for TXE
39+
while ((USART1->SR & (1 << 7)) == 0) {
40+
}
41+
USART1->DR = *str++;
42+
}
2343
#endif
2444
}

0 commit comments

Comments
 (0)