Thread parameter - struct

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: barthess, RoccoMarco

WaL
Posts: 6
Joined: Tue May 15, 2018 11:52 am
Has thanked: 1 time

Thread parameter - struct

Postby WaL » Tue May 22, 2018 7:43 am

Hello!
I'm trying to pass parameters in the thread.
I already understood that this requires a structure, but then I stopped

Code: Select all

/*
 * Поток мигания светодиодом
 */
static THD_WORKING_AREA(waThreadBlink, 128);

static msg_t ThreadBlink(void *arg) {

  (void)arg;

  chRegSetThreadName("blinker");

  palSetPadMode(GPIOC, GPIOC_LED, PAL_MODE_OUTPUT_PUSHPULL);

  while (TRUE)
  {
    palTogglePad(GPIOC, GPIOC_LED);
    chThdSleepMilliseconds(arg->time2);
  }
  return (msg_t) 0;
}


Code: Select all

struct OWStruct
    {
      uint32_t timer;
      uint32_t time2;
    }OWStruct;
   


Code: Select all

OWStruct.time2=100;
chThdCreateStatic(waThreadBlink, sizeof(waThreadBlink), NORMALPRIO, ThreadBlink,&OWStruct);


I'm getting a compiler error
error: request for member 'time2' in something not a structure or union

rew
Posts: 238
Joined: Sat Jul 19, 2014 12:59 pm
Been thanked: 5 times

Re: Thread parameter - struct

Postby rew » Tue May 22, 2018 8:29 am

In the thread you need to add:
struct OWStruct *myarg = arg;

and then use "myarg" instead of "arg".

WaL
Posts: 6
Joined: Tue May 15, 2018 11:52 am
Has thanked: 1 time

Re: Thread parameter - struct

Postby WaL » Tue May 22, 2018 8:41 am

Thanks, it really work!

rew
Posts: 238
Joined: Sat Jul 19, 2014 12:59 pm
Been thanked: 5 times

Re: Thread parameter - struct

Postby rew » Tue May 22, 2018 6:07 pm

Oh, One more little "neatness" thingy....

the "(void) arg;" is there to tell the compiler: "I know what I'm doing, I declared the argument arg and I'm not going to use it. Please don't warn me about THAT!". Well... that's unnecessary when you ARE using it.

User avatar
Giovanni
Site Admin
Posts: 11076
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 367 times
Been thanked: 306 times
Contact:

Re: Thread parameter - struct

Postby Giovanni » Tue May 22, 2018 9:49 pm

Correct, it is there to suppress a warning, if you use the argument then there is no need for it.

Giovanni


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 2 guests