Page 1 of 1

Need little help with pointer

Posted: Tue Apr 25, 2017 1:01 pm
by robert_o
Hello everybody, i make good progress with chibios and i like it.
Now i am programming a universal button and rotary encoder driver without the use of interrupts.
I create a thread which reads the button input pins. There is my problem. I have a pointer to a struct which i want to pass to the THD_FUNCTION as argument. Now i do:

whre rotp is the struct of type ROTARYDriver .
struct ROTARYDriver {
rot_state_t state;
const ROTARYConfig *config;
};

void rotaryStart(ROTARYDriver *rotp, const ROTARYConfig *config) //Start the Rotary Driver
{
...configuration of inputs....
chThdCreateStatic(waButtonReadThread, sizeof(waButtonReadThread), NORMALPRIO + 5, ButtonReadThread, &rotp); //Start the Thread which reads the inputs
}

and in:
static THD_FUNCTION(ButtonReadThread, arg)
{
BUTTON = palReadLine(rotp->config->pinmap->BTN); // Here is the Problem. I need a connection from "arg" to "rotp" and i don't know how
}

This is basic C-Programming knowledge and not necessarily STM32 or Chibios but maybe somebody can help.

Re: Need little help with pointer

Posted: Tue Apr 25, 2017 1:52 pm
by Giovanni
Hi,

In the thread:

struct ROTARYDriver rotp = (struct ROTARYDriver *)arg;

and you have your pointer back.

Giovanni

Re: Need little help with pointer

Posted: Tue Apr 25, 2017 2:05 pm
by robert_o
I am sorry to say it doesn't work.

./userlib/src/rotary.c:90:38: error: invalid initializer
struct ROTARYDriver rotp = (struct ROTARYDriver *)arg;
^

Re: Need little help with pointer

Posted: Tue Apr 25, 2017 2:10 pm
by Giovanni
I missed a *

struct ROTARYDriver *rotp = (struct ROTARYDriver *)arg;

Giovanni

Re: Need little help with pointer

Posted: Tue Apr 25, 2017 2:10 pm
by robert_o
I found the solution:

I was wrong giving the address to the thread.
chThdCreateStatic(waButtonReadThread, sizeof(waButtonReadThread), NORMALPRIO + 5, ButtonReadThread, rotp);
is correct.
In the Thread:
struct ROTARYDriver *rotp = (struct ROTARYDriver *)arg;
and:
BUTTON = palReadLine(rotp->config->pinmap->BTN);

That works.
Thanks for pointing :) me in the right direction.