Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/mman.h>
- #include <native/task.h>
- #include <native/timer.h>
- #include <rtdk.h>
- #include "gpio-utils.h"
- int toggle = 0 ;
- int onOffTime ; // Time in micro sec to keep the signal on/off
- int gpio = 60;
- void fonction_periodique (void * arg)
- {
- int err;
- unsigned long depassements;
- rt_task_set_periodic(rt_task_self(), TM_NOW, 1000000000);
- rt_printf("[%lld] Timer programmé...\n", rt_timer_read());
- while ((err = rt_task_wait_period(& depassements)) == 0)
- {
- toggle = !toggle;
- gpio_set_value(gpio, toggle);
- // printf("...value set to %d...\n", toggle);
- // Pause for a while
- // usleep(onOffTime);
- rt_printf("[%lld]", rt_timer_read());
- if (depassements != 0)
- rt_printf(" Depassements : %lu", depassements);
- rt_printf("\n");
- }
- fprintf(stderr, "rt_task_wait_period(): %s\n",strerror(-err));
- exit(EXIT_FAILURE);
- }
- int main(int argc, char** argv)
- {
- //create a variable to store whether we are sending a '1' or a '0'
- char set_value[5];
- //Integer to keep track of whether we want on or off
- int gpio_fd;
- if (argc < 2)
- {
- printf("Usage: %s <on/off time in us>\n\n", argv[0]);
- printf("Toggle gpio 60 at the period given\n");
- exit(-1);
- } onOffTime = atoi(argv[1]);
- printf("**********************************\n"
- "* Welcome to PIN Blink program *\n"
- "* ....blinking gpio 60 *\n"
- "* ....period of %d us.........*\n"
- "**********************************\n", 2*onOffTime);
- //Using sysfs we need to write the gpio number to /sys/class/gpio/export
- //This will create the folder /sys/class/gpio/gpio60
- gpio_export(gpio);
- printf("...export file accessed, new pin now accessible\n");
- //SET DIRECTION
- gpio_set_dir(gpio, "out");
- printf("...direction set to output\n");
- gpio_fd = gpio_fd_open(gpio, O_RDONLY);
- RT_TASK task;
- mlockall(MCL_CURRENT|MCL_FUTURE);
- rt_print_auto_init(1);
- if (rt_task_spawn(& task, NULL, 0, 99,
- T_JOINABLE, fonction_periodique, NULL) != 0) {
- fprintf(stderr, "Impossible de creer la tache\n");
- exit(EXIT_FAILURE);
- }
- rt_task_join(& task);
- gpio_fd_close(gpio_fd);
- return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement