Page 1 of 1

ChibiOS Suspend and Resume thread

Posted: Sat Dec 01, 2012 4:51 pm
by jmarshellk
Hi everyone,

I'm new user of Chibi, and yet I'm working on my SCARA Robot project. My SCARA has two arms, and a servo motor to control each arm. I also use serial monitor two send command and receive a feedback. I use ATMega328 as the microcontroller and I'm trying to use ChibiOS in it. Without ChibiOS, my SCARA works well. But I face few problems while using Chibi to control the motor movement. I have several questions:
1. Is ATMega328's memory is too small to use ChibiOS?
2. I create 2 threads: one thread for each motor. But, I can suspend the thread and as a result, I can't control it anymore by using serial monitor. So, I'm trying to suspend both threads while they're not moving and resume it when I tell them to move. Would you please tell me how to make a suspend and resume command in Chibi?

Thank you so much for your help. :)

Re: ChibiOS Suspend and Resume thread

Posted: Sat Dec 01, 2012 6:06 pm
by Giovanni
Hi,

At least 2KB of RAM are required, better if 4KB, threads stacks take space.

In ChibiOS it is not possible to suspend a thread, threads can only suspend themselves and be resumed by other threads, you should reorganize your code to make threads take the decision to suspend.
The suspend can be done this way:

Code: Select all

Thread *tp = NULL;

msg_t mythread(void *p) {
  while (TRUE) {
    ...
    chSysLock();
    if (suspend_condition) {
      tp = chThdSelf();
      chSchGoSleep(THD_STATE_SUSPENDED);
    }
    chSysUnlock();
    ...
  }
}

/* This resumes the thread IF it is suspended.*/
chSysLock();
Thread *ntp = tp;
if (ntp) {
  tp = NULL;
  chSchWakeupS(ntp, RDY_OK);
}
chSysUnlock();


Alternatively you can use a binary semaphore (easy enough) or events (harder).

Giovanni

Re: ChibiOS Suspend and Resume thread

Posted: Sun Dec 02, 2012 6:15 am
by jmarshellk
Hi,

Thanks a lot for your help, it works!!
Grazie mille :D

Marshell