7 years ago.

LPC1768 freezes

I am writing some code where I need to run 2 things at the same time: 1) Recieving data from xbee serial connection 2) Run LED sequences depending on the recieved data

I can recieve the information fine and it is correct, But when the data is passed to the thread the program stops and the serial connection is unresponsive. To control the LEDs I am using SPI (WS2812 & PixelArray Librarys).

I need the LED sequence to keep running until new data is recieved, hence using thread. I have tested the function solidPulse and that code works when it is not called by RTOS leading me to the conclusion the problem is due to the rtos library.

EDIT: I have managed to test the thread and it is running. the main loop appears to have stopped and is not recieving any serial data.

Code

#include "mbed.h"
#include "rtos.h"
#include "xbee.h"
#include "CMDLIB.h"
#include "WS2812.h"
#include "PixelArray.h"

Serial pc(USBTX, USBRX); // tx, rx

CMDLIB cmd;
xbee xbee(p28,p27,p29,38400);
PixelArray px1(LED_COUNT1);
WS2812 set1(p5, 144, 3, 11, 10, 11);
Thread ledthread;

int ledFunction;
int colour1[3],colour2[3],speed,intensity;

void solidPulse(int colour, int speed, int intensity){
    int period = speed / intensity;
    int inten = intensity * INTENSITY_STEP;
    px1.SetAll(colour);
    set1.write(px1.getBuf());
    for(int a = 255; a > inten; a--){
        px1.SetAllI(a);
        set1.write(px1.getBuf());
        wait_ms(period);
    }
    for(int a = inten; a < 255; a++){
        px1.SetAllI(a);
        set1.write(px1.getBuf());
        wait_ms(period);
    }

}

void ledThread(){
    while(1){
        switch(ledFunction){
            case 0: //Off
                offLED();
                break;
            case 1: //Solid
                solidPulse(cmd.RGB2HEX(colour1[0], colour1[1], colour1[2]), speed, intensity);
                break;
            case 2: //Solid Pulse
                solidPulse(cmd.RGB2HEX(colour1[0], colour1[1], colour1[2]), speed, intensity);
                break;
            default:
                break;
        }
        wait_ms(100);
    }
}

void setup(){
    set1.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
    set2.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
    xbee.setup(startChar, endChar);
    pc.baud(38400);
    muxcon = 0;
    ledFunction = 0;
    for(int j = 0; j < 3; j++){
        colour1[j] = 0;
        colour2[j] = 0;
    }
    speed = 100;
    intensity = 80;
    
    for (int i = 1; i < LED_COUNT1; i++) {
        px1.Set(i, 0xffffff);
    }
    set1.write(px1.getBuf());
}

int main() {
    setup();
    ledthread.start(ledThread);
    string recData;
    while(true){
        //Get Data and save to variables
    }
}

Im not entirely sure whats wrong, ive been looking at other posts which say the problem could be with SPI and RTOS but I couldnt see how to test/fix it.

Could you give me an insight to the cause and the solution?

Be the first to answer this question.