crash core 1

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
arj
Posts: 9
Joined: Wed Feb 03, 2021 5:45 pm

crash core 1

Post by arj » Tue Feb 09, 2021 9:20 am

Here is my code that demonstrates the core 1 crashing problem. It's connected to a WS2812 LED strip. Enter R,G,B for a static colour or S to animate the colours running from core 1. With line 109 (pixels_set(i, rgb)) commented out it works without any problem. When line 109 is not commented out it crashes core 1 after 28 times around the loop. I'm assuming the problem is because the code running in core 1 is accessing the state machine used for the LED control?

Code: Select all

import array
import time
import rp2
import random
import select
import sys
import utime
import _thread
from machine import Pin

from sys import stdin, exit
from _thread import start_new_thread
from utime import sleep



################################################################################
# Configure the number of WS2812 LEDs.
NUM_LEDS   = 60
PIN_NUM    = 6
brightness = 0.2   # <<< Any value between 0.0 and 1.0
version    = "PicoLED V1.00 enter R,G,B or S to run sparkle on core 1"


################################################################################
# global variables to share between both threads/processors
# 
terminateThread = False



##########################################################################
@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24)
def ws2812():
    T1 = 2
    T2 = 5
    T3 = 3
    wrap_target()
    label("bitloop")
    out(x, 1)               .side(0)    [T3 - 1]
    jmp(not_x, "do_zero")   .side(1)    [T1 - 1]
    jmp("bitloop")          .side(1)    [T2 - 1]
    label("do_zero")
    nop()                   .side(0)    [T2 - 1]
    wrap()
 

# Create the StateMachine with the ws2812 program, outputting on pin
sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(PIN_NUM))
 
# Start the StateMachine, it will wait for data on its FIFO.
sm.active(1)

# Display a pattern on the LEDs via an array of LED RGB values.
ar = array.array("I", [0 for _ in range(NUM_LEDS)])



##########################################################################
def pixels_show():
    dimmer_ar = array.array("I", [0 for _ in range(NUM_LEDS)])
    for i,c in enumerate(ar):
        r = int(((c >> 8) & 0xFF) * brightness)
        g = int(((c >> 16) & 0xFF) * brightness)
        b = int((c & 0xFF) * brightness)
        dimmer_ar[i] = (g<<16) + (r<<8) + b
    sm.put(dimmer_ar, 8)
    time.sleep_ms(10)

def pixels_set(i, color):
    ar[i] = (color[1]<<16) + (color[0]<<8) + color[2]

def pixels_fill(color):
    for i in range(len(ar)):
        pixels_set(i, color)
    pixels_show()
    

##########################################################################
BLACK  = (0, 0, 0)
RED    = (255, 0, 0)
ORANGE = (255, 150, 0)
YELLOW = (255, 240, 0)
GREEN  = (0, 255, 0)
CYAN   = (0, 255, 255)
BLUE   = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE  = (255, 255, 255)
COL1   = (255, 124, 23)

COLORS = (BLACK, RED, ORANGE, YELLOW, GREEN, CYAN, BLUE, PURPLE, WHITE)



##########################################################################
def ModeSparkle(wait_ms):
    global terminateThread
    count=1
    
    while True:
        for i in range(NUM_LEDS):
            if terminateThread == True:
                print("Ending ModeSparkle")
                return
            r = random.randint(0,255)
            g = random.randint(0,255)
            b = random.randint(0,255)
            rgb = [r,g,b]
            pixels_set(i, rgb)      # line 109
        pixels_show()
        print ("Sparkle ", count)
        count=count+1
        time.sleep_ms(wait_ms)
                   

try:
    print(version)
    while True:
        buffLine = input(":").lower()

        # Static colours    
        if (buffLine == "?"):
            print(version)

        if (buffLine.startswith("r")):
            terminateThread = True 
            pixels_fill(RED)

        if (buffLine.startswith("g")):
            terminateThread = True 
            pixels_fill(GREEN)

        if (buffLine.startswith("b")):
            terminateThread = True 
            pixels_fill(BLUE)
            
        if (buffLine.startswith("k")):
            terminateThread = True 
            pixels_fill(BLACK)
            
        if (buffLine.startswith("w")):
            terminateThread = True 
            pixels_fill(WHITE)

        if (buffLine.startswith("s")):
            terminateThread = True
            time.sleep_ms(100)
            terminateThread = False
            
            try:
                lastthread = _thread.start_new_thread(ModeSparkle, (500,))
            except:
                print("Error starting thread")


except KeyboardInterrupt:
    print("Ctrl+C")
    terminateThread = True   #terminate thread


terminateThread = True
print("Program END")

Post Reply