Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable custom rtc clock source #25

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/libethercat/ec.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,17 @@ int ec_send_process_data(ec_t *pec);
*/
int ec_send_distributed_clocks_sync(ec_t *pec);

//! \brief Send distributed clock sync datagram
/*!
* \param pec Pointer to ethercat master structure,
* which you got from \link ec_open \endlink.
* \param act_rtc_time Current real-time clock value. If 0, the time of
* osal_timer_gettime_nsec() will be used. Otherwise
* the supplied time is used.
* \return 0 on success
*/
int ec_send_distributed_clocks_sync_with_rtc(ec_t *pec, osal_uint64_t act_rtc_time);

//! \brief Send broadcast read to ec state.
/*!
* \param[in] pec Pointer to ethercat master structure,
Expand Down
31 changes: 27 additions & 4 deletions src/ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@

// forward declaration
static void default_log_func(ec_t *pec, int lvl, const osal_char_t *format, ...) __attribute__ ((format (printf, 3, 4)));
static int ec_send_distributed_clocks_sync_intern(ec_t *pec, osal_uint64_t act_rtc_time);

//! calculate signed difference of 64-bit unsigned int's
/*!
Expand Down Expand Up @@ -1840,10 +1841,13 @@ static void cb_distributed_clocks(struct ec *pec, pool_entry_t *p_entry, ec_data

//! send distributed clock sync datagram
/*!
* \param pec ethercat master pointer
* \param pec ethercat master pointer
* \param act_rtc_time Current real-time clock value. If 0, the time of
* osal_timer_gettime_nsec() will be used. Otherwise
* the supplied time is used.
* \return 0 on success
*/
int ec_send_distributed_clocks_sync(ec_t *pec) {
int ec_send_distributed_clocks_sync_intern(ec_t *pec, osal_uint64_t act_rtc_time) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you do a forward declaration of the ..._intern function as static at the beginning of the file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course I can. What is the reason for this? Just out of interest.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to make it a little bit more compliant with coding standards like Misra-C or Autosar and to avoid warnings of some compilers when having no previous declaration. (and static to hide the symbol)

assert(pec != NULL);

int ret = EC_OK;
Expand Down Expand Up @@ -1891,8 +1895,6 @@ int ec_send_distributed_clocks_sync(ec_t *pec) {
p_dg->adr = ((osal_uint32_t)EC_REG_DCSYSTIME << 16u) | pec->dc.master_address;
}

osal_uint64_t act_rtc_time = osal_timer_gettime_nsec();

if (pec->dc.mode == dc_mode_ref_clock) {
if (pec->main_cycle_interval > 0) {
pec->dc.rtc_time += (osal_uint64_t)(pec->main_cycle_interval);
Expand All @@ -1917,6 +1919,27 @@ int ec_send_distributed_clocks_sync(ec_t *pec) {
return ret;
}

//! send distributed clock sync datagram
/*!
* \param pec ethercat master pointer
* \return 0 on success
*/
int ec_send_distributed_clocks_sync(ec_t *pec) {
return ec_send_distributed_clocks_sync_intern(pec, osal_timer_gettime_nsec());
}

//! send distributed clock sync datagram
/*!
* \param pec ethercat master pointer
* \param act_rtc_time Current real-time clock value. If 0, the time of
* osal_timer_gettime_nsec() will be used. Otherwise
* the supplied time is used.
* \return 0 on success
*/
int ec_send_distributed_clocks_sync_with_rtc(ec_t *pec, osal_uint64_t act_rtc_time) {
return ec_send_distributed_clocks_sync_intern(pec,act_rtc_time);
}

//! local callack for syncronous read/write
static void cb_brd_ec_state(struct ec *pec, pool_entry_t *p_entry, ec_datagram_t *p_dg) {
assert(pec != NULL);
Expand Down