Skip to content

Commit 603ebde

Browse files
committed
pbdrv/stack: Move stack info to driver.
This lets us move it away from MicroPython's mphal so we can further unify all embedded ports.
1 parent 0663e76 commit 603ebde

File tree

18 files changed

+81
-31
lines changed

18 files changed

+81
-31
lines changed

bricks/_common/micropython.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <stdio.h>
88
#include <string.h>
99

10+
#include <pbdrv/stack.h>
11+
1012
#include <pbio/button.h>
1113
#include <pbio/main.h>
1214
#include <pbio/os.h>
@@ -350,7 +352,7 @@ void pbsys_main_run_program(pbsys_main_program_t *program) {
350352
// Note: stack control relies on main thread being initialised above
351353
char *sstack;
352354
char *estack;
353-
pb_stack_get_info(&sstack, &estack);
355+
pbdrv_stack_get_info(&sstack, &estack);
354356
#if PYBRICKS_OPT_USE_ESTACK
355357
mp_stack_set_top(estack);
356358
#else

bricks/_common/mphalport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ void mp_hal_set_interrupt_char(int c);
88
#define mp_hal_ticks_us pbdrv_clock_get_us
99
#define mp_hal_ticks_cpu() 0
1010
#define mp_hal_delay_us pbdrv_clock_delay_us
11-
void pb_stack_get_info(char **sstack, char **estack);
1211

1312
// Platform-specific code to run on completing the poll hook.
1413
void pb_event_poll_hook_leave(void);

bricks/_common/sources.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
182182
drv/resistor_ladder/resistor_ladder.c \
183183
drv/sound/sound_nxt.c \
184184
drv/sound/sound_stm32_hal_dac.c \
185+
drv/stack/stack_embedded.c \
185186
drv/uart/uart_debug_first_port.c \
186187
drv/uart/uart_ev3.c \
187188
drv/uart/uart_ev3_pru.c \

bricks/_common_stm32/mphalport.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717
#include "py/mpconfig.h"
1818
#include "py/stream.h"
1919

20-
void pb_stack_get_info(char **sstack, char **estack) {
21-
extern uint32_t _estack;
22-
extern uint32_t _sstack;
23-
*sstack = (char *)&_sstack;
24-
*estack = (char *)&_estack;
25-
}
26-
2720
// using "internal" pbdrv variable
2821
extern volatile uint32_t pbdrv_clock_ticks;
2922

bricks/ev3/mphalport.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@
1818
#include "py/mpconfig.h"
1919
#include "py/stream.h"
2020

21-
void pb_stack_get_info(char **sstack, char **estack) {
22-
extern uint32_t _estack;
23-
extern uint32_t _sstack;
24-
*sstack = (char *)&_sstack;
25-
*estack = (char *)&_estack;
26-
}
27-
2821
// Core delay function that does an efficient sleep and may switch thread context.
2922
// If IRQs are enabled then we must have the GIL.
3023
void mp_hal_delay_ms(mp_uint_t Delay) {

bricks/nxt/mphalport.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@
1818
#include "py/mpconfig.h"
1919
#include "py/stream.h"
2020

21-
void pb_stack_get_info(char **sstack, char **estack) {
22-
extern uint32_t __stack_start__;
23-
extern uint32_t __stack_end__;
24-
*sstack = (char *)&__stack_start__;
25-
*estack = (char *)&__stack_end__;
26-
}
27-
2821
// TODO
2922
static bool interrupts_get(void) {
3023
return true;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2025 The Pybricks Authors
3+
4+
#include <pbdrv/config.h>
5+
6+
#if PBDRV_CONFIG_STACK_EMBEDDED
7+
8+
#include <stdint.h>
9+
10+
void pbdrv_stack_get_info(char **sstack, char **estack) {
11+
// Defined in linker script.
12+
extern uint32_t _estack;
13+
extern uint32_t _sstack;
14+
*sstack = (char *)&_sstack;
15+
*estack = (char *)&_estack;
16+
}
17+
18+
#endif // PBDRV_CONFIG_STACK_EMBEDDED

lib/pbio/include/pbdrv/stack.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2025 The Pybricks Authors
3+
4+
#ifndef _PBDRV_STACK_H_
5+
#define _PBDRV_STACK_H_
6+
7+
#include <stddef.h>
8+
9+
#include <pbdrv/config.h>
10+
11+
#if PBDRV_CONFIG_STACK
12+
13+
/**
14+
* Gets the stack information.
15+
*
16+
* @param [out] sstack The start of the stack.
17+
* @param [out] estack The end of the stack.
18+
*/
19+
void pbdrv_stack_get_info(char **sstack, char **estack);
20+
21+
#else // PBDRV_CONFIG_STACK
22+
23+
static inline void pbdrv_stack_get_info(char **sstack, char **estack) {
24+
*sstack = NULL;
25+
*estack = NULL;
26+
}
27+
28+
#endif // PBDRV_CONFIG_STACK
29+
30+
#endif // _PBDRV_STACK_H_

lib/pbio/platform/city_hub/pbdrvconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
#define PBDRV_CONFIG_UART_STM32F0 (1)
7474
#define PBDRV_CONFIG_UART_STM32F0_NUM_UART (2)
7575

76+
#define PBDRV_CONFIG_STACK (1)
77+
#define PBDRV_CONFIG_STACK_EMBEDDED (1)
78+
7679
#define PBDRV_CONFIG_WATCHDOG (1)
7780
#define PBDRV_CONFIG_WATCHDOG_STM32 (1)
7881

lib/pbio/platform/essential_hub/pbdrvconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@
103103
#define PBDRV_CONFIG_USB_PROD_STR LEGO_USB_PROD_STR_TECHNIC_SMALL_HUB " + Pybricks"
104104
#define PBDRV_CONFIG_USB_STM32F4 (1)
105105

106+
#define PBDRV_CONFIG_STACK (1)
107+
#define PBDRV_CONFIG_STACK_EMBEDDED (1)
108+
106109
#define PBDRV_CONFIG_WATCHDOG (1)
107110
#define PBDRV_CONFIG_WATCHDOG_STM32 (1)
108111

0 commit comments

Comments
 (0)