Page 1 of 1
Question to Mailbox
Posted: Mon Jul 23, 2012 10:17 pm
by stormracer
Hello,
i've a little problem with mailboxes. I set up a small example project, for testing. It only works for some seconds and than stops completely. There are three threads. One only toggle a led. The second should send some text to the third. When it stops, no led is blinking.
The source is attached to this post. I think that the buffer for the mailbox doesn't get cleared, after fetching a message. But i'm not sure...
Christian
Re: Question to Mailbox
Posted: Tue Jul 24, 2012 2:32 am
by Abhishek
Hi,
The source isn't attached; it can't be seen.
This possibly looks like a stack overflow. Try increasing workarea size, see if it helps. If not, then it might be due to what you suspect.
Best Regards.
Re: Question to Mailbox
Posted: Tue Jul 24, 2012 6:55 am
by barthess
Check if you correctly initialize mailbox. There is no possibility to overflow/underflow mailbox using API.
Re: Question to Mailbox
Posted: Tue Jul 24, 2012 8:04 am
by stormracer
Hello,
sry the attachment gets lost. Now a another try...
I hope my initialization of the mailbox is right. Is there a generally cue how large the workarea size should be?
Christian
Attaching the file doesn't work. The system doesn't accept the extension (*.c; *.txt). Which extensions are allowed?
Code: Select all
#include "ch.h"
#include "hal.h"
#include "test.h"
#include "chprintf.h"
#include "chmboxes.h"
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {
(void)arg;
chRegSetThreadName("blinker");
char msg[20];
while (TRUE) {
palSetPad(GPIOD, GPIOD_LED3);
chThdSleepMilliseconds(100);
palClearPad(GPIOD, GPIOD_LED3);
chThdSleepMilliseconds(100);
chMBFetch((Mailbox *)arg, (msg_t*)&msg, 0);
}
return 0;
}
static WORKING_AREA(waThread2, 128);
static msg_t Thread2(void *arg) {
(void)arg;
chRegSetThreadName("blinker2");
char msg[] = "test Nachricht";
while (TRUE) {
palSetPad(GPIOD, GPIOD_LED5);
chThdSleepMilliseconds(200);
palClearPad(GPIOD, GPIOD_LED5);
chThdSleepMilliseconds(200);
chMBPost((Mailbox *)arg, (msg_t)&msg, 0);
}
return 0;
}
static WORKING_AREA(waThread3, 128);
static msg_t Thread3(void *arg) {
(void)arg;
chRegSetThreadName("blinker3");
while (TRUE) {
palSetPad(GPIOD, GPIOD_LED6);
chThdSleepMilliseconds(500);
palClearPad(GPIOD, GPIOD_LED6);
chThdSleepMilliseconds(500);
}
return 0;
}
/*
* Application entry point.
*/
int main(void) {
halInit();
chSysInit();
Mailbox mailbox;
char buf[100];
chMBInit(&mailbox, (msg_t*)buf, sizeof(buf));
/*
* Creates the example thread.
*/
chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, &mailbox);
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, &mailbox);
chThdCreateStatic(waThread3, sizeof(waThread3), NORMALPRIO, Thread3, NULL);
while (TRUE) {
chThdSleepMilliseconds(500);
}
}
Re: Question to Mailbox
Posted: Tue Jul 24, 2012 8:08 am
by barthess
stormracer wrote: Is there a generally cue how large the workarea size should be?
The good starting value for Cortex-M3 is 256 bytes. Later you can optimize it like described in manual on official chibios page.
stormracer wrote:Which extensions are allowed?
If I know correct *.zip and *.png, probably *.jpg.
Re: Question to Mailbox
Posted: Tue Jul 24, 2012 8:11 am
by barthess
stormracer wrote:chMBInit(&mailbox, (msg_t*)buf, sizeof(buf));
chMBInit(&mailbox, (msg_t*)buf, sizeof(buf)/sizeof(msg_t));
and better to use "uint8_t" instead of "char" because some compilers interpret it like one byte, some other like two bytes.
Re: Question to Mailbox
Posted: Tue Jul 24, 2012 8:13 am
by Giovanni
There are several errors:
Code: Select all
char buf[100];
chMBInit(&mailbox, (msg_t*)buf, sizeof(buf));
The 3rd parameter is the number of msg_t not the size of the array.
Code: Select all
chMBFetch((Mailbox *)arg, (msg_t*)&msg, 0);
chMBPost((Mailbox *)arg, (msg_t)&msg, 0);
The 3rd parameter (zero) is equivalent to TIME_IMMEDIATE, both calls do not wait internally. You should use TIME_INFINITE instead.
Both things are documented in the kernel reference manual.
Giovanni
Re: Question to Mailbox
Posted: Tue Jul 24, 2012 8:49 am
by stormracer
Hi,
thanks for the fast answers.
The zero in chMBPost/Fetch was a try, normally i use TIME_INFINITE.
Thanks for the hint with msg_t and the third parameter. Now i found out that it is a multiple of Int32.
I try it later today.
Thanks, Christian
Re: Question to Mailbox
Posted: Sun Jul 29, 2012 3:04 pm
by jscott
Did you ever get your example working?
Can you post a final version?
-John