Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Wrong timestamp on event read #38

Open
albinpopote opened this issue Aug 25, 2023 · 0 comments
Open

Wrong timestamp on event read #38

albinpopote opened this issue Aug 25, 2023 · 0 comments

Comments

@albinpopote
Copy link

albinpopote commented Aug 25, 2023

Hi,

I'm working on a python project on my Orange PC2 board with gpiod event on pin 40 (gpiochip1 - 199).
I got event when pin is linked to VCC (with Pull down), the event type is good but the timestamp is wrong as you can see below:

Got event: 
        Type: 1 (<class 'int'>)
        Timestamp: 1970-01-01 00:29:55.042645 (<class 'datetime.datetime'>)

I made some tests and I don't known if there has any change made under the linux kernel but the original timestamp returned in the gpio_line_event_read_fd() function is not a timestamp but seems to be the time of the event since the OS uptime:

def gpiod_line_event_read_fd(fd: int, event: gpiod_line_event) -> int:
    """
    @brief Read the last GPIO event directly from a file descriptor.

    @param fd:    File descriptor.
    @param event: Buffer in which the event data will be stored.

    @return 0 if the event was read correctly, -1 on error.

    Users who directly poll the file descriptor for incoming events can also
    directly read the event data from it using this routine. This function
    translates the kernel representation of the event to the libgpiod format.
    """
    evdata = gpioevent_data()

    try:
        rd = os_read(fd, sizeof(evdata))
    except OSError:
        return -1

    if len(rd) != sizeof(evdata):
        set_errno(EIO)
        return -1

    memmove(pointer(evdata), rd, sizeof(evdata))

    event.event_type = (
        GPIOD_LINE_EVENT_RISING_EDGE
        if evdata.id == GPIOEVENT_EVENT_RISING_EDGE
        else GPIOD_LINE_EVENT_FALLING_EDGE
    )
    print(f"Original Event timestamp: {evdata.timestamp}")
    print(f"Event timestamp (seconds only): {evdata.timestamp // 1000000000}")

    sec = evdata.timestamp // 1_000_000_000
    event.ts = datetime(year=1970, month=1, day=1) + timedelta(
        days=sec // 86400,
        seconds=sec % 86400,
        microseconds=(evdata.timestamp % 1_000_000_000) // 1000,
    )

    return 0

Got the following result:

Waiting for event
Original Event timestamp: 2712700685261
Event timestamp (seconds only): 2712
System uptime: 2712.71
Got event: 
        Type: 1 (<class 'int'>)
        Timestamp: 1970-01-01 00:45:12.700685 (<class 'datetime.datetime'>)
Got Event: RISING with timestamp: 1970-01-01 00:45:12.700685

I propose of the following fix options:

  • keep the timestamp attribute and change the datetime to the OS uptime in place of 1970-01-01
  • replace the timestamp attribute to a "since_boot_time" (or other attribute name) with only the timedelta object (and let the developper work with in his program/script)

I don't have any board to test if this issue is specific to my OPi PC2 board...
Used Versions:

  • gpiod: 1.5.4
  • Kernel: 6.1.30-sunxi64
  • libgpiod2: 1.6.3-1+b3

PS: I tried with the gpiomon tool to check if i got the same result: True

root@OPI:~# cat /proc/uptime && gpiomon -B pull-up gpiochip1 199
4948.04 19430.06
event:  RISING EDGE offset: 199 timestamp: [    4948.135970804]

Thks
Regards,
Albin ^^

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant