Skip to main content
Visitor II
December 11, 2024
Question

Changing STM32MP157D Linux system CLOCK_REALTIME to use the 64-bit Ethernet precision timer

  • December 11, 2024
  • 1 reply
  • 590 views

On an STM32MP157D-based system, I wish to condition the Linux CLOCK_REALTIME precision clock against an external clock source in a non-standard manner (ie. not using PTPD or similar).

To do this, I am using a wireless radio controlled by the CM4 core on the STM32MP157D to synchronize and condition the 64-bit Ethernet precision timer within a microsecond of a clock on the far end of the wireless connection. This code was adapted from an STM32F767 device, and the port was relatively easy to do.

Can I configure the Linux system to use the Ethernet precision timer rather than the default system timer for the CLOCK_REALTIME precision clock? If so, how might I configure this within the kernel or device tree files?

Alternatively, how might I adjust the CM4 ARM Cortex-A Generic Timer, which is likely used by default for CLOCK_REALTIME, so that it can be set and adjusted by code running within the CM4 core? Or is some other clock used by Linux for the CLOCK_REALTIME clock?

Thanks,
Mike

    This topic has been closed for replies.

    1 reply

    Visitor II
    December 11, 2024

    By the way, I can read the synchronized Ethernet precision timer from a Linux application using the /dev/ptp0 device using the code below. However, it would be best for my purposes if I could get the Linux system clock to use this timer for the CLOCK_REALTIME clock.

    #include <time.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <linux/ptp_clock.h>
    #include <sys/ioctl.h>
    
    #define FD_TO_CLOCKID(fd) ((~(unsigned int)(fd) << 3) | 3)
    
    int main() {
     // Open the PHC device (e.g., /dev/ptp0)
     int fd = open("/dev/ptp0", O_RDONLY);
     if (fd < 0) {
     perror("Failed to open PHC device");
     return -1;
     }
    
     // Convert file descriptor to clock ID
     clockid_t clk_id = FD_TO_CLOCKID(fd);
    
     // Use clock_gettime to fetch the current time from the PHC
     struct timespec ts;
     if (clock_gettime(clk_id, &ts) < 0) {
     perror("Failed to get PHC time");
     close(fd);
     return -1;
     }
    
     printf("PHC time: %lld.%09ld seconds\n", (long long)ts.tv_sec, ts.tv_nsec);
    
     // Close the PHC device
     close(fd);
     return 0;
    }