aes67_ptp.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * SPDX-FileCopyrightText: 2026 DatanoiseTV
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*
  7. * PTP (IEEE 1588) clock synchronization interface.
  8. *
  9. * Wraps ESP-IDF's PTP daemon and hardware timestamping on ESP32-P4 EMAC.
  10. * Provides clock status, grand master tracking, and the sample audio clock
  11. * (SAC) used for RTP timestamp generation.
  12. */
  13. #pragma once
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include "esp_err.h"
  17. #include "esp_eth.h"
  18. #include "aes67_config.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /* PTP lock states matching RAVENNA conventions */
  23. typedef enum {
  24. AES67_PTP_UNLOCKED = 0,
  25. AES67_PTP_LOCKING = 1,
  26. AES67_PTP_LOCKED = 2,
  27. } aes67_ptp_lock_state_t;
  28. /* PTP status information */
  29. typedef struct {
  30. aes67_ptp_lock_state_t lock_state;
  31. uint8_t grandmaster_id[8]; /* EUI-64 grand master identity */
  32. uint8_t domain;
  33. int32_t offset_ns; /* Current offset from master in ns */
  34. int32_t path_delay_ns; /* Mean path delay in ns */
  35. int32_t jitter_ns; /* Clock jitter estimate in ns */
  36. } aes67_ptp_status_t;
  37. /* Opaque PTP context */
  38. typedef struct aes67_ptp_ctx *aes67_ptp_handle_t;
  39. /* Callback for PTP lock state changes */
  40. typedef void (*aes67_ptp_lock_cb_t)(aes67_ptp_lock_state_t state, void *user_data);
  41. /**
  42. * Initialize the PTP subsystem. Uses ESP-IDF ptpd and hardware timestamping.
  43. */
  44. esp_err_t aes67_ptp_init(esp_eth_handle_t eth_handle, const aes67_ptp_config_t *config,
  45. aes67_ptp_handle_t *handle);
  46. /**
  47. * Start PTP synchronization.
  48. */
  49. esp_err_t aes67_ptp_start(aes67_ptp_handle_t handle);
  50. /**
  51. * Stop PTP synchronization.
  52. */
  53. esp_err_t aes67_ptp_stop(aes67_ptp_handle_t handle);
  54. /**
  55. * Destroy the PTP context.
  56. */
  57. esp_err_t aes67_ptp_destroy(aes67_ptp_handle_t handle);
  58. /**
  59. * Get current PTP status.
  60. */
  61. esp_err_t aes67_ptp_get_status(aes67_ptp_handle_t handle, aes67_ptp_status_t *status);
  62. /**
  63. * Get the current PTP time as a 64-bit nanosecond value.
  64. */
  65. esp_err_t aes67_ptp_get_time_ns(aes67_ptp_handle_t handle, uint64_t *time_ns);
  66. /**
  67. * Get the current PTP time as seconds + nanoseconds.
  68. */
  69. esp_err_t aes67_ptp_get_time(aes67_ptp_handle_t handle, uint32_t *sec, uint32_t *nsec);
  70. /**
  71. * Convert PTP time to an RTP media timestamp at the given sample rate.
  72. */
  73. uint32_t aes67_ptp_time_to_rtp_ts(uint64_t ptp_time_ns, uint32_t sample_rate);
  74. /**
  75. * Register a callback for PTP lock state transitions.
  76. */
  77. esp_err_t aes67_ptp_register_lock_cb(aes67_ptp_handle_t handle,
  78. aes67_ptp_lock_cb_t cb, void *user_data);
  79. /**
  80. * Get the grand master ID formatted as a string (e.g. "00-1A-2B-FF-FE-3C-4D-5E").
  81. */
  82. esp_err_t aes67_ptp_get_grandmaster_str(aes67_ptp_handle_t handle, char *buf, size_t len);
  83. #ifdef __cplusplus
  84. }
  85. #endif