All of lore.kernel.org
 help / color / mirror / Atom feed
* [Xenomai] Toggling GPIO RT Xenomai on Dragonboard 41c
@ 2017-06-21  8:10 eladyaakovi
  2017-06-21 12:35 ` Giulio Moro
  0 siblings, 1 reply; 7+ messages in thread
From: eladyaakovi @ 2017-06-21  8:10 UTC (permalink / raw)
  To: xenomai, xenomai-request


Hi, I wrote a code that up and down (toggling) gpio.
I want to do the same with RT function.

Here pessudo code:

void not_rt_func(void *arg)
{
time_t t = time(NULL) + 10;
while (time(NULL) < t) {
                //gpio up
sleep(1000);
//gpio down
                sleep(1000);
}
return NULL;
}


That code worked well (with up and down function, we used files:



        FILE *dir;
 dir = fopen("/sys/class/gpio/gpio13/value","w");







        fprintf(dir, "%d", 1);  //gpio up

        fprintf(dir, "%d", 0);  //gpio down






        fclose(dir);

)


I want to make the same as real time.
I saw examples worked with PWM but it is too complicated to my "problem".
I just want to make this toggling with a RT process (and realtime up and down GPIO)
He used device driver, should i use it too ?
Can't we work without device driver ?

if we might use it, so i thought to write it like this:



user function:

int dev = open("/dev/rtdm/my_gpio",O_RDWR);
RT_TASK rt_task;




void rt_func(void *arg)
{
time_t t = time(NULL) + 10;
while (time(NULL) < t) {
ioctl(dev, GPIO_UP,NULL); //gpio up
rt_task_sleep(1000);
ioctl(dev, GPIO_DOWN,NULL); //gpio down
rt_task_sleep(1000);
}
return NULL;
}



device driver:
struct gpio_data {
int gpio;
};

static int gpio_ioctl_rt(struct rtdm_fd *fd, unsigned int request, void __user *arg)
{
struct gpio_data*ctx = rtdm_fd_to_private(fd);
switch (request) {
case GPIO_UP:
if (ctx->gpio > 0)
return -EBUSY;
gpio_set_value(ctx->gpio, 1);
case GPIO_DOWN:
if (ctx->gpio == 0)
return -EBUSY;
gpio_set_value(ctx->gpio, 0);
default:
return -EINVAL;
}
}





Do you think it is OK ?


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Xenomai] Toggling GPIO RT Xenomai on Dragonboard 41c
  2017-06-21  8:10 [Xenomai] Toggling GPIO RT Xenomai on Dragonboard 41c eladyaakovi
@ 2017-06-21 12:35 ` Giulio Moro
  2017-06-22 11:45   ` eladyaakovi
  0 siblings, 1 reply; 7+ messages in thread
From: Giulio Moro @ 2017-06-21 12:35 UTC (permalink / raw)
  To: eladyaakovi, xenomai, xenomai-request

As you ask "Can't we work without device driver ?": here is a driver-less solution which may work on your platform (if it supports memmapping the GPIO banks) and may suit your needs.

I have some working user-space code to read and toggle GPIOs on a BeagleBone Black, which simply uses memory mapping and is real-time safe.
It consists of these files:
https://github.com/BelaPlatform/Bela/blob/master/include/Gpio.h
https://github.com/BelaPlatform/Bela/blob/master/include/GPIOcontrol.h
https://github.com/BelaPlatform/Bela/blob/master/core/GPIOcontrol.cpp

The GPIOcontrol stuff (by Derek Molloy) exports the pin through the sysfs interface, but then in Gpio.h the actual memory mapping takes place.
You will have to replace these constants with the ones for your architecture (the technical reference manual for your hardware should give you the correct addresses):

static const uint32_t GPIO_SIZE =  0x198;
static const uint32_t GPIO_DATAIN = (0x138 / 4);
static const uint32_t GPIO_CLEARDATAOUT = (0x190 / 4);
static const uint32_t GPIO_SETDATAOUT = (0x194 / 4);
static const uint32_t GPIO_ADDRESSES[4] = {
	0x44E07000,
	0x4804C000,
	0x481AC000,
	0x481AE000,
};

Hope this helps,
Giulio

________________________________________
From: Xenomai <xenomai-bounces@xenomai.org> on behalf of eladyaakovi@campus.technion.ac.il <eladyaakovi@campus.technion.ac.il>
Sent: 21 June 2017 09:10
To: xenomai@xenomai.org; xenomai-request@xenomai.org
Subject: [Xenomai] Toggling GPIO RT Xenomai on Dragonboard 41c

Hi, I wrote a code that up and down (toggling) gpio.
I want to do the same with RT function.

Here pessudo code:

void not_rt_func(void *arg)
{
time_t t = time(NULL) + 10;
while (time(NULL) < t) {
                //gpio up
sleep(1000);
//gpio down
                sleep(1000);
}
return NULL;
}


That code worked well (with up and down function, we used files:



        FILE *dir;
 dir = fopen("/sys/class/gpio/gpio13/value","w");







        fprintf(dir, "%d", 1);  //gpio up

        fprintf(dir, "%d", 0);  //gpio down






        fclose(dir);

)


I want to make the same as real time.
I saw examples worked with PWM but it is too complicated to my "problem".
I just want to make this toggling with a RT process (and realtime up and down GPIO)
He used device driver, should i use it too ?
Can't we work without device driver ?

if we might use it, so i thought to write it like this:



user function:

int dev = open("/dev/rtdm/my_gpio",O_RDWR);
RT_TASK rt_task;




void rt_func(void *arg)
{
time_t t = time(NULL) + 10;
while (time(NULL) < t) {
ioctl(dev, GPIO_UP,NULL); //gpio up
rt_task_sleep(1000);
ioctl(dev, GPIO_DOWN,NULL); //gpio down
rt_task_sleep(1000);
}
return NULL;
}



device driver:
struct gpio_data {
int gpio;
};

static int gpio_ioctl_rt(struct rtdm_fd *fd, unsigned int request, void __user *arg)
{
struct gpio_data*ctx = rtdm_fd_to_private(fd);
switch (request) {
case GPIO_UP:
if (ctx->gpio > 0)
return -EBUSY;
gpio_set_value(ctx->gpio, 1);
case GPIO_DOWN:
if (ctx->gpio == 0)
return -EBUSY;
gpio_set_value(ctx->gpio, 0);
default:
return -EINVAL;
}
}





Do you think it is OK ?

_______________________________________________
Xenomai mailing list
Xenomai@xenomai.org
https://xenomai.org/mailman/listinfo/xenomai


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Xenomai] Toggling GPIO RT Xenomai on Dragonboard 41c
  2017-06-21 12:35 ` Giulio Moro
@ 2017-06-22 11:45   ` eladyaakovi
  2017-06-22 12:30     ` Giulio Moro
  0 siblings, 1 reply; 7+ messages in thread
From: eladyaakovi @ 2017-06-22 11:45 UTC (permalink / raw)
  To: Giulio Moro, xenomai, xenomai-request

Can you explain please why this is not writing to a file:


time_t t = time(NULL) + time_to_run;

while (time(NULL) <  t){

   fprintf(dir,"%d",1);

   rt_task_sleep(1000);

}


not writing 1 to the file ?

dir is a file descriptor.


(without the rt_task_sleep(1000) and the while it works fine).

any suggestions what can i do ?

thanks.

________________________________
מאת: Giulio Moro <g.moro@qmul.ac.uk>
‏‏נשלח: יום רביעי 21 יוני 2017 15:35:49
‏‏אל: Elad Yaakovi; xenomai@xenomai.org; xenomai-request@xenomai.org
‏‏נושא: Re: Toggling GPIO RT Xenomai on Dragonboard 41c

As you ask "Can't we work without device driver ?": here is a driver-less solution which may work on your platform (if it supports memmapping the GPIO banks) and may suit your needs.

I have some working user-space code to read and toggle GPIOs on a BeagleBone Black, which simply uses memory mapping and is real-time safe.
It consists of these files:
https://github.com/BelaPlatform/Bela/blob/master/include/Gpio.h
https://github.com/BelaPlatform/Bela/blob/master/include/GPIOcontrol.h
https://github.com/BelaPlatform/Bela/blob/master/core/GPIOcontrol.cpp

The GPIOcontrol stuff (by Derek Molloy) exports the pin through the sysfs interface, but then in Gpio.h the actual memory mapping takes place.
You will have to replace these constants with the ones for your architecture (the technical reference manual for your hardware should give you the correct addresses):

static const uint32_t GPIO_SIZE =  0x198;
static const uint32_t GPIO_DATAIN = (0x138 / 4);
static const uint32_t GPIO_CLEARDATAOUT = (0x190 / 4);
static const uint32_t GPIO_SETDATAOUT = (0x194 / 4);
static const uint32_t GPIO_ADDRESSES[4] = {
        0x44E07000,
        0x4804C000,
        0x481AC000,
        0x481AE000,
};

Hope this helps,
Giulio

________________________________________
From: Xenomai <xenomai-bounces@xenomai.org> on behalf of eladyaakovi@campus.technion.ac.il <eladyaakovi@campus.technion.ac.il>
Sent: 21 June 2017 09:10
To: xenomai@xenomai.org; xenomai-request@xenomai.org
Subject: [Xenomai] Toggling GPIO RT Xenomai on Dragonboard 41c

Hi, I wrote a code that up and down (toggling) gpio.
I want to do the same with RT function.

Here pessudo code:

void not_rt_func(void *arg)
{
time_t t = time(NULL) + 10;
while (time(NULL) < t) {
                //gpio up
sleep(1000);
//gpio down
                sleep(1000);
}
return NULL;
}


That code worked well (with up and down function, we used files:



        FILE *dir;
 dir = fopen("/sys/class/gpio/gpio13/value","w");







        fprintf(dir, "%d", 1);  //gpio up

        fprintf(dir, "%d", 0);  //gpio down






        fclose(dir);

)


I want to make the same as real time.
I saw examples worked with PWM but it is too complicated to my "problem".
I just want to make this toggling with a RT process (and realtime up and down GPIO)
He used device driver, should i use it too ?
Can't we work without device driver ?

if we might use it, so i thought to write it like this:



user function:

int dev = open("/dev/rtdm/my_gpio",O_RDWR);
RT_TASK rt_task;




void rt_func(void *arg)
{
time_t t = time(NULL) + 10;
while (time(NULL) < t) {
ioctl(dev, GPIO_UP,NULL); //gpio up
rt_task_sleep(1000);
ioctl(dev, GPIO_DOWN,NULL); //gpio down
rt_task_sleep(1000);
}
return NULL;
}



device driver:
struct gpio_data {
int gpio;
};

static int gpio_ioctl_rt(struct rtdm_fd *fd, unsigned int request, void __user *arg)
{
struct gpio_data*ctx = rtdm_fd_to_private(fd);
switch (request) {
case GPIO_UP:
if (ctx->gpio > 0)
return -EBUSY;
gpio_set_value(ctx->gpio, 1);
case GPIO_DOWN:
if (ctx->gpio == 0)
return -EBUSY;
gpio_set_value(ctx->gpio, 0);
default:
return -EINVAL;
}
}





Do you think it is OK ?

_______________________________________________
Xenomai mailing list
Xenomai@xenomai.org
https://xenomai.org/mailman/listinfo/xenomai

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Xenomai] Toggling GPIO RT Xenomai on Dragonboard 41c
  2017-06-22 11:45   ` eladyaakovi
@ 2017-06-22 12:30     ` Giulio Moro
  2017-06-22 15:05       ` eladyaakovi
  0 siblings, 1 reply; 7+ messages in thread
From: Giulio Moro @ 2017-06-22 12:30 UTC (permalink / raw)
  To: eladyaakovi, xenomai

> Can you explain please why this is not writing to a file:

Is this related to "Re: Toggling GPIO RT Xenomai on Dragonboard 41c" or should it go under a separate heading?

> (without the rt_task_sleep(1000) and the while it works fine).

Many things could be wrong with this. 
If `time_to_run` is very small (or zero), then the condition is never met. On the other hand , I have no idea what time(NULL) does. Why don't you add a `printf()` inside the loop to see if it gets called once at least? 
Also, I have no idea why you would try to write one byte to disk every 1 microsecond and how / whether your system would be capable of dealing with it.

Are you using some Xenomai wrappers or is `fprintf` actually calling the stdlib `fprintf` ? In the latter case, then I see this code as potentially hanging your system if it actually were to run and `time_to_run` is not small enough.

Just guessing, because we do not see a larger portion of your code.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Xenomai] Toggling GPIO RT Xenomai on Dragonboard 41c
  2017-06-22 12:30     ` Giulio Moro
@ 2017-06-22 15:05       ` eladyaakovi
  2017-06-22 15:12         ` Giulio Moro
  0 siblings, 1 reply; 7+ messages in thread
From: eladyaakovi @ 2017-06-22 15:05 UTC (permalink / raw)
  To: Giulio Moro, xenomai

Is there any other sleep function for real time ?

this sleep is not working well

oscilloscope measured 30ms.

and it is not changing when I change the values.

________________________________
מאת: Giulio Moro <g.moro@qmul.ac.uk>
‏‏נשלח: יום חמישי 22 יוני 2017 15:30:03
‏‏אל: Elad Yaakovi; xenomai@xenomai.org
‏‏נושא: Re: Toggling GPIO RT Xenomai on Dragonboard 41c

> Can you explain please why this is not writing to a file:

Is this related to "Re: Toggling GPIO RT Xenomai on Dragonboard 41c" or should it go under a separate heading?

> (without the rt_task_sleep(1000) and the while it works fine).

Many things could be wrong with this.
If `time_to_run` is very small (or zero), then the condition is never met. On the other hand , I have no idea what time(NULL) does. Why don't you add a `printf()` inside the loop to see if it gets called once at least?
Also, I have no idea why you would try to write one byte to disk every 1 microsecond and how / whether your system would be capable of dealing with it.

Are you using some Xenomai wrappers or is `fprintf` actually calling the stdlib `fprintf` ? In the latter case, then I see this code as potentially hanging your system if it actually were to run and `time_to_run` is not small enough.

Just guessing, because we do not see a larger portion of your code.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Xenomai] Toggling GPIO RT Xenomai on Dragonboard 41c
  2017-06-22 15:05       ` eladyaakovi
@ 2017-06-22 15:12         ` Giulio Moro
  2017-06-22 15:21           ` eladyaakovi
  0 siblings, 1 reply; 7+ messages in thread
From: Giulio Moro @ 2017-06-22 15:12 UTC (permalink / raw)
  To: eladyaakovi, xenomai

> Is there any other sleep function for real time ?

That is the correct sleep function for real time, and the sleep time is expressed in nanoseconds, so you are trying to sleep for 1 microsecond, which is well beyond Xenomai's capabilities on many (all?) platforms. Minimum latency values are around 30 - 60 microseconds, if I am not mistaken.

>> Are you using some Xenomai wrappers or is `fprintf` actually calling the stdlib `fprintf` ?

if that is a stdlib `fprintf` then you have no timing guarantees there, because your thread switches to secondary mode (linux mode) and so you can expect ANY delay instead of the requested one. That delay would then be due to the `rt_task_sleep()` but to the `fprintf`.
Then, again, I have no idea what your file descriptor is, what `fprintf` is or how you are measuring this "30ms" delay, so I cannot help further.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Xenomai] Toggling GPIO RT Xenomai on Dragonboard 41c
  2017-06-22 15:12         ` Giulio Moro
@ 2017-06-22 15:21           ` eladyaakovi
  0 siblings, 0 replies; 7+ messages in thread
From: eladyaakovi @ 2017-06-22 15:21 UTC (permalink / raw)
  To: Giulio Moro, xenomai

Thanks for your answer,

the fprintf is not an issue (I have tried with rt_printf too)

rt_task_sleep(1000000000)

rt_task_sleep(1000)

oscilloscope measure 30 ms for both.

________________________________
מאת: Giulio Moro <g.moro@qmul.ac.uk>
‏‏נשלח: יום חמישי 22 יוני 2017 18:12:51
‏‏אל: Elad Yaakovi; xenomai@xenomai.org
‏‏נושא: Re: Toggling GPIO RT Xenomai on Dragonboard 41c

> Is there any other sleep function for real time ?

That is the correct sleep function for real time, and the sleep time is expressed in nanoseconds, so you are trying to sleep for 1 microsecond, which is well beyond Xenomai's capabilities on many (all?) platforms. Minimum latency values are around 30 - 60 microseconds, if I am not mistaken.

>> Are you using some Xenomai wrappers or is `fprintf` actually calling the stdlib `fprintf` ?

if that is a stdlib `fprintf` then you have no timing guarantees there, because your thread switches to secondary mode (linux mode) and so you can expect ANY delay instead of the requested one. That delay would then be due to the `rt_task_sleep()` but to the `fprintf`.
Then, again, I have no idea what your file descriptor is, what `fprintf` is or how you are measuring this "30ms" delay, so I cannot help further.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-06-22 15:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-21  8:10 [Xenomai] Toggling GPIO RT Xenomai on Dragonboard 41c eladyaakovi
2017-06-21 12:35 ` Giulio Moro
2017-06-22 11:45   ` eladyaakovi
2017-06-22 12:30     ` Giulio Moro
2017-06-22 15:05       ` eladyaakovi
2017-06-22 15:12         ` Giulio Moro
2017-06-22 15:21           ` eladyaakovi

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.