Skip to content

Instantly share code, notes, and snippets.

@zdavkeos
Created July 6, 2013 16:51
Show Gist options
  • Save zdavkeos/5940480 to your computer and use it in GitHub Desktop.
Save zdavkeos/5940480 to your computer and use it in GitHub Desktop.
Simple PPSAPI client for FreeBSD
// ppsapi_client.c
// Zach Davis 07-06-2013
// (C) Same license as FreeBSD
/*
* Monitors a PPS port for events
*
* See sample Makefile at the bottom
*/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/timepps.h>
int main(int argc, char **argv)
{
int fd;
pps_handle_t ph;
pps_params_t parms;
int caps;
pps_info_t pinfo;
struct timespec timeout = { 0 };
if (argc != 2) {
printf("Usage: %s [serial port]\n\n", argv[0]);
exit(0);
}
fd = open(argv[1], O_RDWR, 0);
if (fd < 1) {
perror("Error opening file");
exit(-1);
}
if (time_pps_create(fd, &ph) != 0) {
printf("Failed to create pps handle\n");
exit(-1);
}
time_pps_getcap(ph, &caps);
printf("Capabilities: 0x%X\n", caps);
printf("CANWAIT: %d\n", (caps & PPS_CANWAIT) == 0);
printf("CAPTUREASSERT: %d\n", (caps & PPS_CAPTUREASSERT) == 0);
printf("OFFSETASSERT: %d\n", (caps & PPS_OFFSETASSERT) == 0);
time_pps_getparams(ph, &parms);
parms.assert_offset.tv_sec = 0;
parms.assert_offset.tv_nsec = 0;
parms.mode |= PPS_CAPTUREASSERT | PPS_OFFSETASSERT;
time_pps_setparams(ph, &parms);
printf("assert_seq, assert_time, clear_seq, clear_time, mode\n");
while (1) {
if (0 != time_pps_fetch(ph, PPS_TSFMT_TSPEC, &pinfo, &timeout)) {
perror("Error fetching info");
break;
}
printf("%d, %d.%09ld, %d, %d.%09ld, %x\n",
pinfo.assert_sequence, pinfo.assert_tu.tspec.tv_sec,
pinfo.assert_tu.tspec.tv_nsec, pinfo.clear_sequence,
pinfo.clear_tu.tspec.tv_sec, pinfo.clear_tu.tspec.tv_nsec,
pinfo.current_mode);
sleep(1);
}
time_pps_destroy(ph);
close(fd);
return 0;
}
/*
# Makefile for ppsapi_client
all: ppsapi_client
ppsapi_client: ppsapi_client.c
${CC} ${CFLAGS} -Wall -o ppsapi_client ppsapi_client.c
.PHONY: clean
clean:
rm -f *.o *~ ppsapi_client
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment