Send and receive in thread
Posted: Wed Dec 05, 2012 4:45 am
Hi all, I need help about MailBox. I create two thread. One post message, another fetch message, when it receive message, it with turn on or turn off LED3 (I'm using STM32l-Discovery). Here the code I used and it done well (LED3 on or off as I expected).
Now I want threads post and fetch message, too. I means thread LEDS fetchs message, control LED3 and post message; thread LEDC fetchs message, control LED4 and post message. I edited code like that:
But this case it didn't work, both LED3 and LED4 didn't blink.
I don't know how to solve. Please help me. Thanks in advance.
Code: Select all
#include "ch.h"
#include "hal.h"
// Thread fetch message
static WORKING_AREA(waLEDS, 128);
static msg_t LEDS(void * arg) {
Mailbox* mbox = (Mailbox *)arg;
msg_t msg, result;
while(TRUE) {
result = chMBFetch(mbox, &msg, TIME_INFINITE);
if(result == RDY_OK) {
if(msg == 1)
palSetPad(GPIOB, GPIOB_LED3);
if(msg == 2)
palClearPad(GPIOB, GPIOB_LED3);
}
}
}
// Thread post message
static WORKING_AREA(waLEDC, 128);
static msg_t LEDC(void * arg) {
Mailbox* mbox = (Mailbox *)arg;
while(TRUE) {
chMBPost(mbox, 1, TIME_INFINITE);
chThdSleepMilliseconds(1000);
chMBPost(mbox, 2, TIME_INFINITE);
chThdSleepMilliseconds(1000);
}
}
int main(void) {
Mailbox mbox;
msg_t mbox_buffer[1];
chMBInit(&mbox, mbox_buffer, 1);
halInit();
chSysInit();
chThdCreateStatic(waLEDS, sizeof(waLEDS), NORMALPRIO, LEDS, (void *)&mbox);
chThdCreateStatic(waLEDC, sizeof(waLEDC), NORMALPRIO, LEDC, (void *)&mbox);
while (TRUE) {
chThdSleepMilliseconds(500);
}
}
Now I want threads post and fetch message, too. I means thread LEDS fetchs message, control LED3 and post message; thread LEDC fetchs message, control LED4 and post message. I edited code like that:
Code: Select all
#include "ch.h"
#include "hal.h"
// Thread fetch message
static WORKING_AREA(waLEDS, 128);
static msg_t LEDS(void * arg) {
Mailbox* mbox = (Mailbox *)arg;
msg_t msg, result;
while(TRUE) {
result = chMBFetch(mbox, &msg, TIME_INFINITE);
if(result == RDY_OK) {
if(msg == 1)
palSetPad(GPIOB, GPIOB_LED3);
if(msg == 2)
palClearPad(GPIOB, GPIOB_LED3);
}
chMBPost(mbox, 3, TIME_INFINITE);
chThdSleepMilliseconds(1000);
chMBPost(mbox, 4, TIME_INFINITE);
chThdSleepMilliseconds(1000);
}
}
// Thread post message
static WORKING_AREA(waLEDC, 128);
static msg_t LEDC(void * arg) {
Mailbox* mbox = (Mailbox *)arg;
msg_t msg, result;
while(TRUE) {
result = chMBFetch(mbox, &msg, TIME_INFINITE);
if(result == RDY_OK) {
if(msg == 3)
palSetPad(GPIOB, GPIOB_LED3);
if(msg == 4)
palClearPad(GPIOB, GPIOB_LED3);
}
chMBPost(mbox, 1, TIME_INFINITE);
chThdSleepMilliseconds(1000);
chMBPost(mbox, 2, TIME_INFINITE);
chThdSleepMilliseconds(1000);
}
}
int main(void) {
Mailbox mbox;
msg_t mbox_buffer[1];
chMBInit(&mbox, mbox_buffer, 1);
halInit();
chSysInit();
chThdCreateStatic(waLEDS, sizeof(waLEDS), NORMALPRIO, LEDS, (void *)&mbox);
chThdCreateStatic(waLEDC, sizeof(waLEDC), NORMALPRIO, LEDC, (void *)&mbox);
while (TRUE) {
chThdSleepMilliseconds(500);
}
}
But this case it didn't work, both LED3 and LED4 didn't blink.
I don't know how to solve. Please help me. Thanks in advance.