Skip to content

Commit

Permalink
bricks/_common/micropython: Use common pb_event_poll_hook.
Browse files Browse the repository at this point in the history
This lets us keep all MicroPython code in micropython.c and platform specifics in mphal.c.

This lets us do things like #117 in a platform-agnostic way.

This also renames pb_poll to the more descriptive pb_event_poll_hook.
  • Loading branch information
laurensvalk committed Nov 15, 2022
1 parent a2e3bd7 commit fba75ba
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
11 changes: 11 additions & 0 deletions bricks/_common/micropython.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
#include "py/stackctrl.h"
#include "py/stream.h"

// Implementation for MICROPY_EVENT_POLL_HOOK
void pb_event_poll_hook(void) {
while (pbio_do_one_event()) {
}

mp_handle_pending(true);

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

// callback for when stop button is pressed in IDE or on hub
void pbsys_main_stop_program(void) {

Expand Down
3 changes: 3 additions & 0 deletions bricks/_common/mphalport.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ void mp_hal_set_interrupt_char(int c);
#define mp_hal_ticks_cpu() 0
#define mp_hal_delay_us pbdrv_clock_delay_us
void pb_stack_get_info(char **sstack, char **estack);

// Platform-specific code to run on completing the poll hook.
void pb_event_poll_hook_leave(void);
4 changes: 2 additions & 2 deletions bricks/_common_stm32/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ static inline mp_uint_t disable_irq(void) {

#define MICROPY_EVENT_POLL_HOOK \
do { \
extern void pb_poll(void); \
pb_poll(); \
extern void pb_event_poll_hook(void); \
pb_event_poll_hook(); \
} while (0);

// We need to provide a declaration/definition of alloca()
Expand Down
8 changes: 1 addition & 7 deletions bricks/_common_stm32/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ void pb_stack_get_info(char **sstack, char **estack) {
*estack = (char *)&_estack;
}

// Implementation for MICROPY_EVENT_POLL_HOOK
void pb_poll(void) {
while (pbio_do_one_event()) {
}

mp_handle_pending(true);

void pb_event_poll_hook_leave(void) {
// There is a possible race condition where an interrupt occurs and sets the
// Contiki poll_requested flag after all events have been processed. So we
// have a critical section where we disable interrupts and check see if there
Expand Down
4 changes: 2 additions & 2 deletions bricks/ev3rt/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ static inline mp_uint_t disable_irq(void) {

#define MICROPY_EVENT_POLL_HOOK \
do { \
extern void pb_poll(void); \
pb_poll(); \
extern void pb_event_poll_hook(void); \
pb_event_poll_hook(); \
} while (0);

// We need to provide a declaration/definition of alloca()
Expand Down
8 changes: 1 addition & 7 deletions bricks/ev3rt/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
#include "kernel_cfg.h"
#include "kernel/task.h"


// Implementation for MICROPY_EVENT_POLL_HOOK
void pb_poll(void) {
while (pbio_do_one_event()) {
}

mp_handle_pending(true);
void pb_event_poll_hook_leave(void) {
}

// Runs MicroPython with the given program data.
Expand Down
6 changes: 2 additions & 4 deletions bricks/nxt/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ typedef long mp_off_t;

#define MICROPY_EVENT_POLL_HOOK \
do { \
extern void mp_handle_pending(bool); \
mp_handle_pending(true); \
extern int pbio_do_one_event(void); \
while (pbio_do_one_event()) { } \
extern void pb_event_poll_hook(void); \
pb_event_poll_hook(); \
} while (0);

#define MP_STATE_PORT MP_STATE_VM
Expand Down
3 changes: 3 additions & 0 deletions bricks/nxt/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include <base/drivers/bt.h>
#include "base/display.h"

void pb_event_poll_hook_leave(void) {
}

void pb_stack_get_info(char **sstack, char **estack) {
extern uint32_t __stack_start__;
extern uint32_t __stack_end__;
Expand Down

0 comments on commit fba75ba

Please sign in to comment.