Chibios freezes on arduino mega 2560

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderator: tfAteba

Post Reply
Vinny
Posts: 3
Joined: Fri Jan 09, 2015 11:50 am

Chibios freezes on arduino mega 2560

Post by Vinny »

Hi guys, im trying to implement chibios on an arduino mega 2560 and i have difficulties with it freezing.
I know it sounds weird but it only runs the code once and stops.
No idea why...

BTW im a dead rookie sandwich :D I have no idea what im doing, im really just improvising right now so... be easy on me. :oops:
Thanks!

Code:

Code: Select all


#include <ChibiOS_AVR.h>
#include <Keypad.h>
#include <Password.h>
#include <DHT22.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

//---------------------Define-----------------------------

#define ONE_WIRE_BUS 22
OneWire oneWire(ONE_WIRE_BUS);
#define DHT22A_PIN 23
DHT22 DHT22A(DHT22A_PIN);
#define DHT22B_PIN 24
DHT22 DHT22B(DHT22B_PIN);

//-------------------Misc. Parameters------------------------------

unsigned int auxFan = 25;
unsigned int mainFan = 26;
unsigned int lightPin = 27;
volatile boolean auxFanOn = false;
volatile boolean mainFanOn = false;
volatile boolean lightsOn = false;
volatile boolean TempSensorError = false;
volatile boolean PWScreen = false;
volatile boolean screenOn = false;
volatile float avgTempC;
volatile int DHT22AHum;
volatile int DHT22BHum;
volatile char key;
volatile uint32_t count = 0;

//--------------------------------Dallas Temperature Addresses--------

DallasTemperature sensors(&oneWire);
DeviceAddress Temp1 = {
  0x28, 0xAE, 0x23, 0x3C, 0x05, 0x00, 0x00, 0x88};
DeviceAddress Temp2 = {
  0x28, 0xCC, 0x5F, 0x3C, 0x05, 0x00, 0x00, 0x69};
DeviceAddress Temp3 = {
  0x28, 0xF2, 0x2A, 0x35, 0x05, 0x00, 0x00, 0xBB};
DeviceAddress Temp4 = {
  0x28, 0xA7, 0x63, 0x3C, 0x05, 0x00, 0x00, 0xF7};

//---------------------------------LCD Setup-------------------------------

LiquidCrystal_I2C lcd(0x27,20,4);

//----------------------------------- keypad & PW setup-------------------------

String newPasswordString;
char newPassword[6];

Password password = Password( "123456" );

byte maxPasswordLength = 6;
byte currentPasswordLength = 0;
const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
  {
    '1','2','3','A'  }
  ,
  {
    '4','5','6','B'  }
  ,
  {
    '7','8','9','C'  }
  ,
  {
    '*','0','#','D'  }
};
byte rowPins[ROWS] = {
  11,10,9,8};
byte colPins[COLS] = {
  7,6,5,4};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


//--------------------------------Thread 1 - read DHT22's ----------------------------------------------

static WORKING_AREA(waThread1, 100);

static msg_t Thread1(void *arg) {

  DHT22_ERROR_t errorCode;
  chThdSleep(2000);
  errorCode = DHT22A.readData();
  errorCode = DHT22B.readData();
  DHT22AHum = DHT22A.getHumidity();
  DHT22BHum = DHT22B.getHumidity();
  chThdSleep(100);
  if(PWScreen == false) {
    lcd.setCursor(0,0);
    lcd.print("Hum: A:");
    lcd.println(DHT22AHum);
    lcd.print(" B:");
    lcd.println(DHT22BHum);
    Serial.println(DHT22BHum);

  }
}

//------------------------Thread 2 - Read Temps ---------------------------------------------

static WORKING_AREA(waThread2, 100);

static msg_t Thread2(void *arg) {

  sensors.requestTemperatures();
  lcd.clear();
  chThdSleep(1000);
  float TempC1 = sensors.getTempC(Temp1);
  float TempC2 = sensors.getTempC(Temp2);
  float TempC3 = sensors.getTempC(Temp3);
  float TempC4 = sensors.getTempC(Temp4);
  avgTempC = (TempC1 + TempC2  + TempC4)/3;
  chThdSleep(100);
  if(PWScreen == false) {
    lcd.setCursor(0,1);
    lcd.print("Avg Temp:");
    lcd.print(avgTempC);
    Serial.print(avgTempC);

  }
}

//------------------------------Thread 3 - Temp Control ------------------------------

static WORKING_AREA(waThread3, 100);

static msg_t Thread3(void *arg) {
  chThdSleep(1000);
  if (avgTempC <= 10 || avgTempC >= 50)
  {
    digitalWrite(lightPin, HIGH);
    lightsOn = false;
    TempSensorError = true;
    lcd.setCursor(0,3);
    lcd.print("Defective T sensor!");
    Serial.print("Defective T sensor!");
  }
}

//----------------------------------Thread 4 - Temp Control ---------------------------

static WORKING_AREA(waThread4, 100);

static msg_t Thread4(void *arg) {
  chThdSleep(1000);
  if(avgTempC < 23 && auxFanOn == true && TempSensorError == false || avgTempC < 23 && mainFanOn == true && TempSensorError == false)
  {
    digitalWrite(auxFan, HIGH);
    digitalWrite(mainFan, HIGH);
    mainFanOn = false;
    auxFanOn = false;
    lcd.setCursor(0,3);
    lcd.print ("Fans  Off");
  }
}

//---------------------------Thread 5 - Temp Control --------------------------------

static WORKING_AREA(waThread5, 100);

static msg_t Thread5(void *arg) {
  chThdSleep(1000);
  if(avgTempC >= 23 && auxFanOn == false && mainFanOn == false && TempSensorError == false || avgTempC == 24 && auxFanOn == false && mainFanOn == false && TempSensorError == false)
  {
    digitalWrite(auxFan, LOW);
    lcd.setCursor(0,3);
    lcd.print ("\nAux Fan On");
    auxFanOn = true;
  }
}

//--------------------------Thread 6 - Temp control ---------------------------------

static WORKING_AREA(waThread6, 100);

static msg_t Thread6(void *arg) {
  chThdSleep(1000);
  if(avgTempC >= 25 && mainFanOn == false && TempSensorError == false)
  {
    digitalWrite(auxFan, HIGH);   
    chThdSleep(500);
    digitalWrite(mainFan, LOW);
    lcd.setCursor(0,3);
    lcd.print ("Main Fan ON");
    auxFanOn = false;
    mainFanOn = true; 
  }
}

//----------------------Thread 7 ---------------------------
static WORKING_AREA(waThread7, 100);

static msg_t Thread7(void *arg) {

  chThdSleep(1000);



}

//-------------------------------------------Setup--------------------------------------------------

void setup() {

  Serial.begin(9600);

  lcd.init();   
  lcd.backlight();
  lcd.clear();
  sensors.begin();
  sensors.setResolution(10);
  pinMode(auxFan, OUTPUT);
  digitalWrite(auxFan, HIGH);
  pinMode(mainFan, OUTPUT);
  digitalWrite(mainFan, HIGH);
  pinMode(lightPin, OUTPUT);
  digitalWrite(lightPin, LOW);

  chBegin(mainThread);
  while(1) {}

}


//----------------------------Main Thread--------------------------------------------------

void mainThread() {

  chThdCreateStatic(waThread1, sizeof(waThread1),
  NORMALPRIO + 1, Thread1, NULL);

  chThdCreateStatic(waThread2, sizeof(waThread2),
  NORMALPRIO + 1, Thread2, NULL);

  chThdCreateStatic(waThread3, sizeof(waThread3),
  NORMALPRIO + 1, Thread3, NULL); 

  chThdCreateStatic(waThread4, sizeof(waThread4),
  NORMALPRIO + 1, Thread4, NULL); 

  chThdCreateStatic(waThread5, sizeof(waThread5),
  NORMALPRIO + 1, Thread5, NULL);                         

  chThdCreateStatic(waThread6, sizeof(waThread6),
  NORMALPRIO + 1, Thread6, NULL);   

  chThdCreateStatic(waThread7, sizeof(waThread7),
  NORMALPRIO + 1, Thread7, NULL);                         

  for (;;)
  {

    noInterrupts();
    count++;
    interrupts();
  }
}

//------------------------------------------------------------------------------
void loop() {
  // not used
}

utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: Chibios freezes on arduino mega 2560

Post by utzig »

You should definitely put a chThdSleep(1) or some small sleep on the for loop in the end of main(). Also if noInterrupts/interrupts are most probably not required there.

Cheers,
Fabio Utzig
Vinny
Posts: 3
Joined: Fri Jan 09, 2015 11:50 am

Re: Chibios freezes on arduino mega 2560

Post by Vinny »

utzig wrote:You should definitely put a chThdSleep(1) or some small sleep on the for loop in the end of main(). Also if noInterrupts/interrupts are most probably not required there.

Cheers,
Fabio Utzig



Thanks for the quick answer fabio.

Here's what i did:

Code: Select all

void mainThread() {

  chThdCreateStatic(waThread1, sizeof(waThread1),
  NORMALPRIO + 1, Thread1, NULL);

  chThdCreateStatic(waThread2, sizeof(waThread2),
  NORMALPRIO + 1, Thread2, NULL);

  chThdCreateStatic(waThread3, sizeof(waThread3),
  NORMALPRIO + 1, Thread3, NULL); 

  chThdCreateStatic(waThread4, sizeof(waThread4),
  NORMALPRIO + 1, Thread4, NULL); 

  chThdCreateStatic(waThread5, sizeof(waThread5),
  NORMALPRIO + 1, Thread5, NULL);                         

  chThdCreateStatic(waThread6, sizeof(waThread6),
  NORMALPRIO + 1, Thread6, NULL);   

  chThdCreateStatic(waThread7, sizeof(waThread7),
  NORMALPRIO + 1, Thread7, NULL);                         

chThdSleep(10);
  for (;;)
   {
    count++;
  }
}


Still doesnt work.
It only runs once and stops. but it runs the whole code with no problems.
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: Chibios freezes on arduino mega 2560

Post by utzig »

Ah, your threads don't have an infinite loop!!!

When you write a thread function you have to enclose it in an infinite loop. So instead of:

Code: Select all

static msg_t Thread1(void *arg) {

  DHT22_ERROR_t errorCode;
  chThdSleep(2000);
  errorCode = DHT22A.readData();
  errorCode = DHT22B.readData();
  DHT22AHum = DHT22A.getHumidity();
  DHT22BHum = DHT22B.getHumidity();
  chThdSleep(100);
  if(PWScreen == false) {
    lcd.setCursor(0,0);
    lcd.print("Hum: A:");
    lcd.println(DHT22AHum);
    lcd.print(" B:");
    lcd.println(DHT22BHum);
    Serial.println(DHT22BHum);

  }
}


It should be:

Code: Select all

static msg_t Thread1(void *arg) {

  DHT22_ERROR_t errorCode;

  while (TRUE) {
    chThdSleep(2000);
    errorCode = DHT22A.readData();
    errorCode = DHT22B.readData();
    DHT22AHum = DHT22A.getHumidity();
    DHT22BHum = DHT22B.getHumidity();
    chThdSleep(100);
    if(PWScreen == false) {
      lcd.setCursor(0,0);
      lcd.print("Hum: A:");
      lcd.println(DHT22AHum);
      lcd.print(" B:");
      lcd.println(DHT22BHum);
      Serial.println(DHT22BHum);
    } 
  }
}


Same applies for your other threads!

Cheers,
Fabio Utzig
Vinny
Posts: 3
Joined: Fri Jan 09, 2015 11:50 am

Re: Chibios freezes on arduino mega 2560

Post by Vinny »

Its working!

Thanks Fabio youre awesome, man!

:D
Post Reply