1
0

6 Commits

Author SHA1 Message Date
9fbfac5fe1 feat(lab03): use timer on led on and off
+ separate timer function in dedicated class
2026-05-08 09:46:55 +02:00
22f923bfbe refactor(lab03): merge create and configure timer function 2026-05-08 09:46:55 +02:00
2d26f30adf feat(lab03): use epoll and timer for led 2026-05-08 09:46:55 +02:00
8561386973 feat(lab03): add epoll 2026-05-08 09:46:54 +02:00
5592c9c0fe fix(lab03): max value of nanosecond is 1s
Time is splited in second and nanosecond
2026-05-08 09:46:54 +02:00
ca8085ce09 feat(lab03): add timer + refactor base
- Split in several function
- Create timer
- Config timer

TODO: add epoll and connect the timer to this epoll
2026-05-08 09:46:54 +02:00
3 changed files with 143 additions and 30 deletions

View File

@@ -30,6 +30,11 @@
#include <sys/types.h> #include <sys/types.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h>
#include <sys/timerfd.h>
#include <sys/epoll.h>
#include "timer.h"
/* /*
* status led - gpioa.10 --> gpio10 * status led - gpioa.10 --> gpio10
@@ -39,9 +44,16 @@
#define GPIO_UNEXPORT "/sys/class/gpio/unexport" #define GPIO_UNEXPORT "/sys/class/gpio/unexport"
#define GPIO_LED "/sys/class/gpio/gpio10" #define GPIO_LED "/sys/class/gpio/gpio10"
#define LED "10" #define LED "10"
#define DEFAULT_TIME_MS 1000
#define DUTY_CYCLE_PERCENT 2
static int open_led() typedef struct {
{ long flash_period_ms;
int timer_fd;
int epoll_fd;
} ThreadData;
static int open_led() {
// unexport pin out of sysfs (reinitialization) // unexport pin out of sysfs (reinitialization)
int f = open(GPIO_UNEXPORT, O_WRONLY); int f = open(GPIO_UNEXPORT, O_WRONLY);
write(f, LED, strlen(LED)); write(f, LED, strlen(LED));
@@ -62,41 +74,89 @@ static int open_led()
return f; return f;
} }
int main(int argc, char* argv[]) void led_on(int led) {
{
long duty = 2; // %
long period = 1000; // ms
if (argc >= 2) period = atoi(argv[1]);
period *= 1000000; // in ns
// compute duty period...
long p1 = period / 100 * duty;
long p2 = period - p1;
int led = open_led();
pwrite(led, "1", sizeof("1"), 0); pwrite(led, "1", sizeof("1"), 0);
}
struct timespec t1; void led_off(int led) {
clock_gettime(CLOCK_MONOTONIC, &t1); pwrite(led, "0", sizeof("0"), 0);
}
static void* timer_thread(void* arg) {
ThreadData* data = (ThreadData*)arg;
int led = open_led();
led_off(led);
long time_on_ms = data->flash_period_ms / 100 * DUTY_CYCLE_PERCENT; // 2% duty
long time_off_ms = data->flash_period_ms - time_on_ms; // rest of the period
struct epoll_event ev;
int isLedOn = 0;
int k = 0; int k = 0;
while (1) { while(1) {
struct timespec t2;
clock_gettime(CLOCK_MONOTONIC, &t2);
long delta = int n = epoll_wait(data->epoll_fd, &ev, 1, -1);
(t2.tv_sec - t1.tv_sec) * 1000000000 + (t2.tv_nsec - t1.tv_nsec); if (n == -1) {
perror("epoll_wait failed");
int toggle = ((k == 0) && (delta >= p1)) | ((k == 1) && (delta >= p2)); break;
if (toggle) {
t1 = t2;
k = (k + 1) % 2;
if (k == 0)
pwrite(led, "1", sizeof("1"), 0);
else
pwrite(led, "0", sizeof("0"), 0);
} }
uint64_t val;
if (read(data->timer_fd, &val, sizeof(val)) != sizeof(val)) {
perror("read timerfd failed");
break;
}
int delay = 0;
if (isLedOn == 0) {
delay = time_on_ms; // 2% duty
led_on(led);
printf("ping %d\n", k++);
isLedOn = 1;
} else {
delay = time_off_ms; // rest of the period
led_off(led);
isLedOn = 0;
}
timer_set_time(&data->timer_fd, delay);
} }
return NULL;
}
int main(int argc, char* argv[]) {
ThreadData data;
pthread_t thread;
data.flash_period_ms = DEFAULT_TIME_MS;
// Create timerfd
data.timer_fd = timer_create_empty();
timer_set_time(&data.timer_fd, data.flash_period_ms);
// Create epoll instance
data.epoll_fd = epoll_create1(0);
if (data.epoll_fd == -1) {
perror("ERROR while create epoll");
exit(20);
}
timer_link_to_epoll(&data.timer_fd, &data.epoll_fd);
if (pthread_create(&thread, NULL, timer_thread, &data) != 0) {
perror("Failed to create timer thread");
exit(30);
}
pthread_join(thread, NULL);
return 0; return 0;
} }

View File

@@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/timerfd.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
int timer_create_empty() {
// Create timerfd
int timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
if (timer_fd == -1) {
perror("timerfd_create failed");
exit(10);
}
return timer_fd;
}
void timer_set_time(int* timer_fd, long period_ms) {
// https://www.man7.org/linux/man-pages/man3/itimerspec.3type.html
struct itimerspec its;
// Periodic interval
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
// Initial expiration
its.it_value.tv_sec = period_ms / 1000;
its.it_value.tv_nsec = (period_ms % 1000) * 1000000;
if (timerfd_settime(*timer_fd, 0, &its, NULL) == -1) {
perror("timerfd_settime failed");
exit(11);
}
}
void timer_link_to_epoll(int* timer_fd, int* epoll_fd) {
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = *timer_fd;
if (epoll_ctl(*epoll_fd, EPOLL_CTL_ADD, *timer_fd, &ev) == -1) {
perror("ERROR while add timerfd to epoll");
exit(21);
}
}

View File

@@ -0,0 +1,4 @@
int timer_create_empty();
void timer_set_time(int* timer_fd, long period_ms);
void timer_link_to_epoll(int* timer_fd, int* epoll_fd);