Need little help with pointer

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

Moderators: RoccoMarco, barthess

robert_o
Posts: 48
Joined: Fri Feb 17, 2017 11:25 pm
Has thanked: 1 time
Been thanked: 5 times

Need little help with pointer

Postby robert_o » Tue Apr 25, 2017 1:01 pm

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 145 times

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

Re: Need little help with pointer

Postby Giovanni » Tue Apr 25, 2017 1:52 pm

Hi,

In the thread:

struct ROTARYDriver rotp = (struct ROTARYDriver *)arg;

and you have your pointer back.

Giovanni

robert_o
Posts: 48
Joined: Fri Feb 17, 2017 11:25 pm
Has thanked: 1 time
Been thanked: 5 times

Re: Need little help with pointer

Postby robert_o » Tue Apr 25, 2017 2:05 pm

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: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Need little help with pointer

Postby Giovanni » Tue Apr 25, 2017 2:10 pm

I missed a *

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

Giovanni

robert_o
Posts: 48
Joined: Fri Feb 17, 2017 11:25 pm
Has thanked: 1 time
Been thanked: 5 times

Re: Need little help with pointer

Postby robert_o » Tue Apr 25, 2017 2:10 pm

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.


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 28 guests