← index #15982Issue #17968
Related · high · value 3.741
QUERY · ISSUE

Thread on Core 1 on PICO hangs when accessing io

openby kpg141260opened 2024-10-09updated 2025-02-04
bugport-rp2needs-info

Port, board and/or hardware

Raspberry Pi Pico

MicroPython version

MicroPython v1.23.0 on 2024-06-02; Raspberry Pi Pico with RP2040

Reproduction

import os
import time
import _thread


def core1_thread():
    try:
        print("entering core1_thread")
        text = "some text to log"
        # Open log file in append mode
        f = open("test.log", "a")
        f.write(text)
    except OSError as ex:
        print(ex)
    finally:
        f.close()


def create_log():
    """
    Check if the log file exists. If the log file does not exist, create the log file and write a header line.

    Parameters:
        None

    Returns:
        None
    """
    _log_fn = "/test.log"
    try:
        # File exists
        os.stat(_log_fn)
        return
    except OSError:
        idx = _log_fn.rfind("/")
        dir = _log_fn[1:idx]
        if dir and not dir in os.listdir():
            os.mkdir(dir)
        # File does not exist
        try:
            t = f"file {_log_fn} created\n"
            with open(_log_fn, "a") as f:
                f.write(t)
                print(t.rstrip())
        except OSError as ex:
            raise OSError(f"exception {ex}")


def main():
    create_log()
    thread = _thread.start_new_thread(core1_thread, ())

if __name__ == "__main__":
    main()
    time.sleep(1)  # if 'mpremote run', allow thread to run before we soft reset

Expected behaviour

Expected to see 'some text to log' in file test.log on pico.

Observed behaviour

After starting the thread it hangs after printing 'entering core1_thread'.

Additional Information

No, I've provided everything above.

Code of Conduct

Yes, I agree

CANDIDATE · ISSUE

rp2: No Pin IRQ on core 1 (on core 0: OK)

openby GitHubsSilverBulletopened 2025-08-21updated 2026-03-09
bugport-rp2

Port, board and/or hardware

RP2, PICO and PICO2, RP2040 and RP2350

MicroPython version

MicroPython v1.26.0 on 2025-08-09; Raspberry Pi Pico2 with RP2350

No Pin() IRQ when using the second core (_thread).
Primary core works as intended.

Reproduction

#!micropython
# -*- coding: UTF-8 -*-
# vim:fileencoding=UTF-8:ts=4
#╭──────────────────────────────────────────────────────────────────────────────╮
#│      PICO_THREAD_PIN_IRQ_TEST.PY                                             │
#│      Board:       PICO2/RP2350                                               │
#│      Micropython: 3.4.0; MicroPython v1.26.0 on 2025-08-09                   │
#│      Testing:     Pin.irq() on both cores                                    │
#│                   using a bridge between GPIO16 and GPIO17                   │
#│                   GPIO16 runs a slow PWM                                     │
#│                   GPIO17 acts as an input with IRQ                           │
#╰──────────────────────────────────────────────────────────────────────────────╯

import _thread
from sys import version
from array import array
from machine import Pin, PWM
from time import sleep_ms

_SIO_BASE   = const(0xd0000000)
_CPUID      = const(0x000 >>2)
_THREAD_HLT = const(0)
_THREAD_RUN = const(1)
_THREAD_FIN = const(2)

@micropython.viper
def cpuid() -> int:
    sio: ptr32 = ptr32(_SIO_BASE)
    return sio[_CPUID]

def core1(data,hard):

    def cb_core1(pin):
        print('cb_core1(): IRQ on core:', cpuid(), end='  ')
        print('count:', data[1])
        if data[1]:
            data[1] -= 1

    print('core1() is running on core:', cpuid())
    pin = Pin(17, Pin.IN, Pin.PULL_UP)
    data[1] = 5
    pin.irq(handler=cb_core1, trigger=Pin.IRQ_FALLING, hard=hard)
    while data[0] == _THREAD_RUN and data[1]:
        sleep_ms(10)
    pin.irq(None)
    pin.init()
    data[0] = _THREAD_FIN
    return

def core0():

    def cb_core0(pin):
        print('cb_core0(): IRQ on core:', cpuid(), end='  ')
        print('count:', data[1])
        if data[1]:
            data[1] -= 1

    print(version)
    data = array('I', (_THREAD_HLT,0))          # [0]state   [1] counter
    pwm = PWM(16, freq=10, duty_u16=2**15)
    print('Testing hard=False on core 0')
    pin = Pin(17, Pin.IN, None)
    data[1] = 5
    pin.irq(handler=cb_core0, trigger=Pin.IRQ_FALLING, hard=False)
    while data[1]:
        sleep_ms(10)
    pin.irq(None)
    pin.init()

    print('Testing hard=True on core 0')
    pin = Pin(17, Pin.IN, None)
    data[1] = 5
    pin.irq(handler=cb_core0, trigger=Pin.IRQ_FALLING, hard=True)
    while data[1]:
        sleep_ms(10)
    pin.irq(None)
    pin.init()

    print('Testing hard=False on core 1')
    data[0] = _THREAD_RUN
    _thread.start_new_thread(core1, (data,False))
    loops = 100
    while (loops:= loops-1) and data[0] != _THREAD_FIN:
        sleep_ms(10)
    data[0] = _THREAD_HLT
    while data[0] != _THREAD_FIN: pass

    print('Testing hard=True on core 1')
    data[0] = _THREAD_RUN
    _thread.start_new_thread(core1, (data,True))
    loops = 100
    while (loops:= loops-1) and data[0] != _THREAD_FIN:
        sleep_ms(10)
    data[0] = _THREAD_HLT
    while data[0] != _THREAD_FIN: pass

    pwm.deinit()

core0()
3.4.0; MicroPython v1.26.0 on 2025-08-09
Testing hard=False on core 0
cb_core0(): IRQ on core: 0  count: 5
cb_core0(): IRQ on core: 0  count: 4
cb_core0(): IRQ on core: 0  count: 3
cb_core0(): IRQ on core: 0  count: 2
cb_core0(): IRQ on core: 0  count: 1
Testing hard=True on core 0
cb_core0(): IRQ on core: 0  count: 5
cb_core0(): IRQ on core: 0  count: 4
cb_core0(): IRQ on core: 0  count: 3
cb_core0(): IRQ on core: 0  count: 2
cb_core0(): IRQ on core: 0  count: 1
Testing hard=False on core 1
core1() is running on core: 1
Testing hard=True on core 1
core1() is running on core: 1


### Expected behaviour

Well, should fire IRQ/callback events when running on core 1.

### Observed behaviour

Does not.

### Additional Information

No, I've provided everything above.

### Code of Conduct

Yes, I agree

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied