I'm beginner in ChibiOs. My question is how to send information(structure) to a task?
I need send msg data from ledoff to Thread3 if data arrived from serial.
Code: Select all
#include "ch.h"
#include "ch.hpp"
#include "hal.h"
#include "string.h"
using namespace chibios_rt;
static virtual_timer_t vt2;
uint16_t cb[50];
uint8_t cb_p=0;
struct messageData
{
int timeStamp;
char cmd;
uint8_t tmr;
};
static THD_WORKING_AREA(waThread2, 128);
static THD_WORKING_AREA(waThread3, 128);
// This function will become the code for thread 2
static void Thread3(void *arg)
{
(void)arg;
messageData *msg;
thread_t *tp;
chRegSetThreadName("Yellow Blinker");
int i=0;
while (TRUE)
{
// >>>>>>>>>> Need the msg pointer for processing <<<<<<<<<<<<<<
tp=chMsgWait();
msg=(messageData*)chMsgGet(tp);
if (msg->cmd=='B')
{
for (i=0;i<msg->tmr;i++)
{
palClearPad(GPIOD, GPIOD_LED6);
chThdSleepMilliseconds( 250 ); // delay 250mS
palSetPad(GPIOD, GPIOD_LED6);
chThdSleepMilliseconds( 50 ); // delay 50mS
}
chMsgRelease(tp,(msg_t)&msg);
}
else
if (msg->cmd=='V')
{
palClearPad(GPIOD, GPIOD_LED6);
chThdSleepSeconds(msg->tmr); // delay tmr*s
palSetPad(GPIOD, GPIOD_LED6);
chMsgRelease(tp,(msg_t)&msg);
}
else
chMsgRelease(tp,(msg_t)&msg);
}
// return 0;
}
// This function will become the code for thread 2
static void Thread2(void *arg)
{
(void)arg;
chRegSetThreadName("Green Blinker");
while (TRUE)
{
palClearPad(GPIOD, GPIOD_LED4);
chThdSleepMilliseconds( 2000 ); // delay 2000mS
palSetPad(GPIOD, GPIOD_LED4);
chThdSleepMilliseconds( 50 ); // delay 50mS
}
// return 0;
}
static void ledoff(void *p) {
(void)p;
messageData msg;
if (cb_p>0)
{
if (cb[0]==(uint16_t)'B')
{
msg.cmd = (uint16_t)'B';
msg.tmr=200;
msg.timeStamp=chVTGetSystemTime();
chMsgSend(?????,(msg_t)msg);
// >>>>>>>>>>> Need to send msg pointer to send to Thread3 <<<<<<<<<<<
}
if (cb[0]==(uint16_t)'V')
{
msg.cmd = (uint16_t)'V';
msg.tmr=100;
msg.timeStamp=chVTGetSystemTime();
chMsgSend(?????,(msg_t)msg);
// >>>>>>>>>>> Need to send msg pointer to send to Thread3 <<<<<<<<<<<
}
uartStartSend(&UARTD2, cb_p-1, cb);
cb_p=0;
}
palClearPad(GPIOD, GPIOD_LED5);
}
/*
* This callback is invoked when a character is received but the application
* was not ready to receive it, the character is passed as parameter.
*/
static void rxchar(UARTDriver *uartp, uint16_t c) {
(void)uartp;
(void)c;
/* Flashing the LED each time a character is received.*/
if (cb_p<50)
{
cb[cb_p]=c;
cb_p++;
}
palSetPad(GPIOD, GPIOD_LED5);
chSysLockFromISR();
// reset timer if more data arrived
chVTResetI(&vt2);
// Set timer to 200 tick and call 'ledoff' on timeout
chVTSetI(&vt2, MS2ST(200), ledoff, NULL);
chSysUnlockFromISR();
}
/*
* This callback is invoked when a receive buffer has been completely written.
*/
static void rxend(UARTDriver *uartp) {
(void)uartp;
}
/*
* UART driver configuration structure.
*/
static UARTConfig uartcfg = {
NULL,
NULL,
rxend,
rxchar,
NULL,
115200,
0,
USART_CR2_LINEN,
0
};
/*
* Application entry point.
*/
int main(void) {
const char *st;
halInit();
System::init();
uartStart(&UARTD2, &uartcfg);
palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));
/*
* Serves timer events.
*/
thread_t *ctp=chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL);
chThdCreateStatic(waThread3, sizeof(waThread3), NORMALPRIO, Thread3, NULL);
st=chRegGetThreadNameX(ctp);
uartStartSend(&UARTD2, 16, st);
while (true) {
palClearPad(GPIOD, GPIOD_LED3);
chThdSleepMilliseconds( 500 ); // delay 500mS
palSetPad(GPIOD, GPIOD_LED3);
chThdSleepMilliseconds( 500 ); // delay 500mS
}
return 0;
}