Need little help with pointer

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

Moderator: RoccoMarco

Post Reply
robert_o
Posts: 53
Joined: Fri Feb 17, 2017 11:25 pm
Has thanked: 1 time
Been thanked: 6 times

Need little help with pointer

Post 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.
Attachments
NUCLEO-STM32F411RE-USB-Rotary.zip
(21.88 KiB) Downloaded 346 times
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Need little help with pointer

Post by Giovanni »

Hi,

In the thread:

struct ROTARYDriver rotp = (struct ROTARYDriver *)arg;

and you have your pointer back.

Giovanni
robert_o
Posts: 53
Joined: Fri Feb 17, 2017 11:25 pm
Has thanked: 1 time
Been thanked: 6 times

Re: Need little help with pointer

Post 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;
^
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Need little help with pointer

Post by Giovanni »

I missed a *

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

Giovanni
robert_o
Posts: 53
Joined: Fri Feb 17, 2017 11:25 pm
Has thanked: 1 time
Been thanked: 6 times

Re: Need little help with pointer

Post 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.
Post Reply