Skip to content

ports/mimxrt:Add thread support. #13755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ports/mimxrt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ SRC_C += \
systick.c \
ticks.c \
tusb_port.c \
mpthreadport.c \
pybthread.c\
gccollect.c\


SHARED_SRC_C += \
shared/libc/printf.c \
Expand Down
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/ADAFRUIT_METRO_M7/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ MICROPY_HW_FLASH_SIZE ?= 0x800000 # 8MB
MICROPY_PY_NETWORK_NINAW10 ?= 1
MICROPY_PY_SSL ?= 1
MICROPY_SSL_MBEDTLS ?= 1

CFLAGS += -DMICROPY_PY_THREAD=0
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ else
JLINK_CONNECTION_SETTINGS = -USB
endif

CFLAGS += -DMICROPY_PY_THREAD=0

deploy_jlink: $(BUILD)/firmware.hex
$(Q)$(TOUCH) $(JLINK_COMMANDER_SCRIPT)
$(ECHO) "ExitOnError 1" > $(JLINK_COMMANDER_SCRIPT)
Expand Down
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/MIMXRT1015_EVK/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ MICROPY_HW_FLASH_TYPE = qspi_nor_flash
MICROPY_HW_FLASH_SIZE = 0x1000000 # 16MB

MICROPY_BOOT_BUFFER_SIZE = (32 * 1024)

CFLAGS += -DMICROPY_PY_THREAD=0
3 changes: 3 additions & 0 deletions ports/mimxrt/boards/MIMXRT1170_EVK/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-1070evk"

#if MICROPY_PY_THREAD
#else
#define MICROPY_EVENT_POLL_HOOK \
do { \
extern void mp_handle_pending(bool); \
mp_handle_pending(true); \
} while (0);
#endif

// MIMXRT1170_EVK has 2 user LEDs
#define MICROPY_HW_LED1_PIN (pin_GPIO_AD_04)
Expand Down
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/MIMXRT1170_EVK/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ MICROPY_PY_LWIP = 1
MICROPY_PY_SSL = 1
MICROPY_SSL_MBEDTLS = 1

CFLAGS += -DMICROPY_PY_THREAD=1

FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py

CFLAGS += -DCPU_MIMXRT1176DVMAA_cm7 \
Expand Down
1 change: 1 addition & 0 deletions ports/mimxrt/boards/OLIMEX_RT1010/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ MICROPY_HW_FLASH_SIZE = 0x200000 # 2MB
MICROPY_HW_FLASH_RESERVED ?= 0x1000 # 4KB

CFLAGS += -DMICROPY_HW_FLASH_DQS=kFlexSPIReadSampleClk_LoopbackInternally
CFLAGS += -DMICROPY_PY_THREAD=0

SRC_C += \
hal/flexspi_nor_flash.c \
50 changes: 50 additions & 0 deletions ports/mimxrt/gccollect.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "py/gc.h"
#include "py/mpthread.h"
#include "shared/runtime/gchelper.h"
#include "shared/runtime/softtimer.h"
#include "gccollect.h"

void gc_collect(void) {
// start the GC
gc_collect_start();

// trace the stack and registers
gc_helper_collect_regs_and_stack();

// trace root pointers from any threads
#if MICROPY_PY_THREAD
mp_thread_gc_others();
#endif

// trace soft timer nodes
soft_timer_gc_mark_all();

// end the GC
gc_collect_end();
}
44 changes: 44 additions & 0 deletions ports/mimxrt/gccollect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_STM32_GCCOLLECT_H
#define MICROPY_INCLUDED_STM32_GCCOLLECT_H

// variables defining memory layout
// (these probably belong somewhere else...)
extern uint32_t _etext;
extern uint32_t _sidata;
extern uint32_t _ram_start;
extern uint32_t _sdata;
extern uint32_t _edata;
extern uint32_t _sbss;
extern uint32_t _ebss;
extern uint32_t _heap_start;
extern uint32_t _heap_end;
extern uint32_t _sstack;
extern uint32_t _estack;
extern uint32_t _ram_end;

#endif // MICROPY_INCLUDED_STM32_GCCOLLECT_H
19 changes: 12 additions & 7 deletions ports/mimxrt/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
#include "systick.h"
#include "extmod/modnetwork.h"

#if MICROPY_PY_THREAD
static pyb_thread_t pyb_thread_main;
#endif

extern uint8_t _sstack, _estack, _gc_heap_start, _gc_heap_end;

void board_init(void);
Expand All @@ -64,7 +68,9 @@ int main(void) {
board_init();
ticks_init();
pendsv_init();

#if MICROPY_PY_THREAD
pyb_thread_init(&pyb_thread_main);
#endif
#if MICROPY_PY_LWIP
// lwIP doesn't allow to reinitialise itself by subsequent calls to this function
// because the system timeout list (next_timeout) is only ever reset by BSS clearing.
Expand Down Expand Up @@ -96,6 +102,11 @@ int main(void) {
led_init();
#endif

// Python threading init
#if MICROPY_PY_THREAD
mp_thread_init();
#endif

mp_stack_set_top(&_estack);
mp_stack_set_limit(&_estack - &_sstack - 1024);

Expand Down Expand Up @@ -163,12 +174,6 @@ int main(void) {
return 0;
}

void gc_collect(void) {
gc_collect_start();
gc_helper_collect_regs_and_stack();
gc_collect_end();
}

void nlr_jump_fail(void *val) {
for (;;) {
}
Expand Down
27 changes: 27 additions & 0 deletions ports/mimxrt/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ uint32_t trng_random_u32(void);
#define MICROPY_PY_HASHLIB_SHA1 (MICROPY_PY_SSL)
// #define MICROPY_PY_CRYPTOLIB (MICROPY_PY_SSL)

#ifndef MICROPY_PY_THREAD
#define MICROPY_PY_THREAD (1)
#endif


#ifndef MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
#define MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE (1)
#endif
Expand Down Expand Up @@ -182,6 +187,24 @@ extern const struct _mp_obj_type_t mp_network_cyw43_type;

#define MP_STATE_PORT MP_STATE_VM

#if MICROPY_PY_THREAD

#define MICROPY_EVENT_POLL_HOOK \
do { \
extern void mp_handle_pending(bool); \
mp_handle_pending(true); \
if (pyb_thread_enabled) { \
MP_THREAD_GIL_EXIT(); \
pyb_thread_yield(); \
MP_THREAD_GIL_ENTER(); \
} else { \
__WFE(); \
} \
} while (0);

#define MICROPY_THREAD_YIELD() pyb_thread_yield()

#else
// Miscellaneous settings
#ifndef MICROPY_EVENT_POLL_HOOK
#define MICROPY_EVENT_POLL_HOOK \
Expand All @@ -192,6 +215,10 @@ extern const struct _mp_obj_type_t mp_network_cyw43_type;
} while (0);
#endif

#define MICROPY_THREAD_YIELD()

#endif

#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1))

#define MP_HAL_CLEANINVALIDATE_DCACHE(addr, size) \
Expand Down
20 changes: 19 additions & 1 deletion ports/mimxrt/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "ticks.h"
#include "tusb.h"
#include "fsl_snvs_lp.h"

#include "led.h"
#ifndef MICROPY_HW_STDIN_BUFFER_LEN
#define MICROPY_HW_STDIN_BUFFER_LEN 512
#endif
Expand Down Expand Up @@ -112,6 +112,24 @@ int mp_hal_stdin_rx_chr(void) {
}
}

NORETURN void boardctrl_fatal_error(const char *msg) {
for (volatile uint delay = 0; delay < 10000000; delay++) {
}
led_state(1, 1);

mp_hal_stdout_tx_strn("\nFATAL ERROR:\n", 14);
mp_hal_stdout_tx_strn(msg, strlen(msg));
for (uint i = 0;;) {
led_toggle(1);
for (volatile uint delay = 0; delay < 10000000; delay++) {
}
if (i >= 16) {
// to conserve power
__WFI();
}
}
}

mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t ret = len;
bool did_write = false;
Expand Down
2 changes: 1 addition & 1 deletion ports/mimxrt/mphalport.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,5 @@ void mp_hal_generate_laa_mac(int idx, uint8_t buf[6]);
void mp_hal_get_mac(int idx, uint8_t buf[6]);
void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest);
void mp_hal_get_unique_id(uint8_t id[]);

NORETURN void boardctrl_fatal_error(const char *msg);
#endif // MICROPY_INCLUDED_MIMXRT_MPHALPORT_H
99 changes: 99 additions & 0 deletions ports/mimxrt/mpthreadport.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <stdio.h>

#include "py/runtime.h"
#include "py/gc.h"
#include "py/mpthread.h"
#include "gccollect.h"

#if MICROPY_PY_THREAD

// the mutex controls access to the linked list
static mp_thread_mutex_t thread_mutex;

void mp_thread_init(void) {
mp_thread_mutex_init(&thread_mutex);
mp_thread_set_state(&mp_state_ctx.thread);
}

void mp_thread_gc_others(void) {
mp_thread_mutex_lock(&thread_mutex, 1);
for (pyb_thread_t *th = pyb_thread_all; th != NULL; th = th->all_next) {
gc_collect_root((void **)&th, 1);
gc_collect_root(&th->arg, 1);
gc_collect_root(&th->stack, 1);
if (th != pyb_thread_cur) {
gc_collect_root(th->stack, th->stack_len);
}
}
mp_thread_mutex_unlock(&thread_mutex);
}

mp_uint_t mp_thread_get_id(void) {
return (uint32_t)pyb_thread_cur;
}

mp_uint_t mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) {
if (*stack_size == 0) {
*stack_size = 4096; // default stack size
} else if (*stack_size < 2048) {
*stack_size = 2048; // minimum stack size
}

// round stack size to a multiple of the word size
size_t stack_len = *stack_size / sizeof(uint32_t);
*stack_size = stack_len * sizeof(uint32_t);

// allocate stack and linked-list node (must be done outside thread_mutex lock)
uint32_t *stack = m_new(uint32_t, stack_len);
pyb_thread_t *th = m_new_obj(pyb_thread_t);

mp_thread_mutex_lock(&thread_mutex, 1);

// create thread
uint32_t id = pyb_thread_new(th, stack, stack_len, entry, arg);
if (id == 0) {
mp_thread_mutex_unlock(&thread_mutex);
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("can't create thread"));
}

mp_thread_mutex_unlock(&thread_mutex);

// adjust stack_size to provide room to recover from hitting the limit
*stack_size -= 1024;

return id;
}

void mp_thread_start(void) {
}

void mp_thread_finish(void) {
}

#endif // MICROPY_PY_THREAD
Loading