Commit 8f2add6b authored by Aurelio Colosimo's avatar Aurelio Colosimo

general redefinition of timers to achieve ms granularity

Signed-off-by: Aurelio Colosimo's avatarAurelio Colosimo <aurelio@aureliocolosimo.it>
parent e138d5e1
......@@ -41,36 +41,40 @@ int posix_timer_init(struct pp_instance *ppi)
}
int posix_timer_start(uint32_t interval, struct pp_timer *tm)
int posix_timer_start(uint32_t interval_ms, struct pp_timer *tm)
{
time_t now;
now = time(NULL);
tm->start = (uint32_t)now;
tm->interval = interval;
struct timespec now;
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
tm->start = ((uint64_t)(now->tv_sec)) * 1000 +
(now->tv_nsec / 1000000);
tm->interval_ms = interval_ms;
return 0;
}
int posix_timer_stop(struct pp_timer *tm)
{
tm->interval = 0;
tm->interval_ms = 0;
tm->start = 0;
return 0;
}
int posix_timer_expired(struct pp_timer *tm)
{
time_t now;
struct timespec now;
uint64_t now_ms;
if (tm->start == 0) {
PP_PRINTF("%p Warning: posix_timer_expired: timer not started\n",tm);
return 0;
}
now = time(NULL);
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
now_ms = ((uint64_t)(now->tv_sec)) * 1000 +
(now->tv_nsec / 1000000);
if (tm->start + tm->interval < (uint32_t)now) {
if (now_ms > tm->start + tm->interval) {
tm->start = now;
return 1;
}
......@@ -80,18 +84,14 @@ int posix_timer_expired(struct pp_timer *tm)
void posix_timer_adjust_all(struct pp_instance *ppi, int32_t diff)
{
int i;
for (i = 0; i < PP_TIMER_ARRAY_SIZE; i++) {
ppi->timers[i]->start += diff;
}
/* Do nothing, clock_gettime uses MONOTONIC_RAW tstamps */
}
int pp_timer_init(struct pp_instance *ppi)
__attribute__((alias("posix_timer_init")));
int pp_timer_start(uint32_t interval, struct pp_timer *tm)
int pp_timer_start(uint32_t interval_ms, struct pp_timer *tm)
__attribute__((alias("posix_timer_start")));
int pp_timer_stop(struct pp_timer *tm)
......
......@@ -36,7 +36,7 @@ void spec_main_loop(struct pp_instance *ppi)
/* Wait for a packet or for the timeout */
while (delay_ms && !minic_poll_rx()) {
timer_delay(1000);
timer_delay(1);
delay_ms--;
}
if (!minic_poll_rx()) {
......
......@@ -2,6 +2,7 @@
#include <ppsi/diag.h>
#include <pps_gen.h>
#include <syscon.h>
#include <ptpd_netif.h>
static struct pp_timer spec_timers[PP_TIMER_ARRAY_SIZE];
......@@ -15,16 +16,16 @@ int spec_timer_init(struct pp_instance *ppi)
return 0;
}
int spec_timer_start(uint32_t interval, struct pp_timer *tm)
int spec_timer_start(uint32_t interval_ms, struct pp_timer *tm)
{
tm->start = spec_time();
tm->interval = interval;
tm->start = ptpd_netif_get_msec_tics();
tm->interval_ms = interval_ms;
return 0;
}
int spec_timer_stop(struct pp_timer *tm)
{
tm->interval = 0;
tm->interval_ms = 0;
tm->start = 0;
return 0;
......@@ -39,9 +40,9 @@ int spec_timer_expired(struct pp_timer *tm)
return 0;
}
now = spec_time();
now = ptpd_netif_get_msec_tics();
if (tm->start + tm->interval <= now) {
if (now > tm->start + tm->interval_ms) {
tm->start = now;
return 1;
}
......@@ -64,7 +65,7 @@ uint32_t spec_timer_get_msec_tics(void)
int pp_timer_init(struct pp_instance *ppi)
__attribute__((alias("spec_timer_init")));
int pp_timer_start(uint32_t interval, struct pp_timer *tm)
int pp_timer_start(uint32_t interval_ms, struct pp_timer *tm)
__attribute__((alias("spec_timer_start")));
int pp_timer_stop(struct pp_timer *tm)
......
......@@ -94,8 +94,8 @@ struct pp_frgn_master {
* for timer expiration computation. Both are seconds
*/
struct pp_timer {
uint32_t start;
uint32_t interval;
uint64_t start;
uint32_t interval_ms;
};
......@@ -260,7 +260,7 @@ extern int pp_send_packet(struct pp_instance *ppi, void *pkt, int len,
/* Timers */
extern int pp_timer_init(struct pp_instance *ppi); /* initializes timer common
structure */
extern int pp_timer_start(uint32_t interval, struct pp_timer *tm);
extern int pp_timer_start(uint32_t interval_ms, struct pp_timer *tm);
extern int pp_timer_stop(struct pp_timer *tm);
extern int pp_timer_expired(struct pp_timer *tm); /* returns 1 when expired */
/* pp_adj_timers is called after pp_set_tstamp and must be defined for those
......
......@@ -68,8 +68,8 @@ void st_com_restart_annrec_timer(struct pp_instance *ppi)
if (DSPOR(ppi)->logAnnounceInterval < 0)
PP_PRINTF("Error: logAnnounceInterval < 0");
pp_timer_start((DSPOR(ppi)->announceReceiptTimeout) <<
DSPOR(ppi)->logAnnounceInterval,
pp_timer_start(((DSPOR(ppi)->announceReceiptTimeout) <<
DSPOR(ppi)->logAnnounceInterval) * 1000,
ppi->timers[PP_TIMER_ANN_RECEIPT]);
}
......
......@@ -29,13 +29,14 @@ int pp_master(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (ppi->is_new_state) {
DSPOR(ppi)->portState = PPS_MASTER;
pp_timer_start(1 << DSPOR(ppi)->logSyncInterval,
pp_timer_start((1 << DSPOR(ppi)->logSyncInterval) * 1000,
ppi->timers[PP_TIMER_SYNC]);
pp_timer_start(1 << DSPOR(ppi)->logAnnounceInterval,
pp_timer_start((1 << DSPOR(ppi)->logAnnounceInterval) * 1000,
ppi->timers[PP_TIMER_ANN_INTERVAL]);
pp_timer_start(1 << DSPOR(ppi)->logMinPdelayReqInterval,
pp_timer_start(
(1 << DSPOR(ppi)->logMinPdelayReqInterval) * 1000,
ppi->timers[PP_TIMER_PDELAYREQ]);
/* Send an announce immediately, when becomes master */
if (msg_issue_announce(ppi) < 0)
......
......@@ -12,7 +12,8 @@ int pp_passive(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (ppi->is_new_state) {
DSPOR(ppi)->portState = PPS_PASSIVE;
pp_timer_start(1 << DSPOR(ppi)->logMinPdelayReqInterval,
pp_timer_start(
(1 << DSPOR(ppi)->logMinPdelayReqInterval) * 1000,
ppi->timers[PP_TIMER_PDELAYREQ]);
st_com_restart_annrec_timer(ppi);
......
......@@ -38,11 +38,13 @@ int pp_slave(struct pp_instance *ppi, unsigned char *pkt, int plen)
st_com_restart_annrec_timer(ppi);
if (OPTS(ppi)->e2e_mode)
pp_timer_start(1 << DSPOR(ppi)->logMinDelayReqInterval,
ppi->timers[PP_TIMER_DELAYREQ]);
pp_timer_start(
(1 << DSPOR(ppi)->logMinDelayReqInterval) * 1000,
ppi->timers[PP_TIMER_DELAYREQ]);
else
pp_timer_start(1 << DSPOR(ppi)->logMinPdelayReqInterval,
ppi->timers[PP_TIMER_PDELAYREQ]);
pp_timer_start(
(1 << DSPOR(ppi)->logMinPdelayReqInterval) * 1000,
ppi->timers[PP_TIMER_PDELAYREQ]);
}
if (st_com_check_record_update(ppi))
......
......@@ -13,7 +13,7 @@ int wr_calibrated(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (ppi->is_new_state) {
DSPOR(ppi)->wrPortState = WRS_CALIBRATED;
ppi->next_delay = PP_DEFAULT_NEXT_DELAY_MS;
pp_timer_start(DSPOR(ppi)->wrStateTimeout / 1000,
pp_timer_start(DSPOR(ppi)->wrStateTimeout,
ppi->timers[PP_TIMER_WRS_CALIBRATED]);
}
......@@ -46,6 +46,6 @@ state_updated:
pp_timer_stop(ppi->timers[PP_TIMER_WRS_CALIBRATED]);
ret:
ppi->next_delay = PP_DEFAULT_NEXT_DELAY_MS;
ppi->next_delay = DSPOR(ppi)->wrStateTimeout;
return 0;
}
......@@ -16,8 +16,11 @@ int wr_calibration(struct pp_instance *ppi, unsigned char *pkt, int plen)
DSPOR(ppi)->wrPortState = WRS_CALIBRATION;
e = msg_issue_wrsig(ppi, CALIBRATE);
pp_timer_start(DSPOR(ppi)->calPeriod / 1000,
pp_timer_start(DSPOR(ppi)->calPeriod,
ppi->timers[PP_TIMER_WRS_CALIBRATION]);
if (DSPOR(ppi)->calibrated) {
DSPOR(ppi)->wrPortState = WRS_CALIBRATION_2;
}
}
if (pp_timer_expired(ppi->timers[PP_TIMER_WRS_CALIBRATION])) {
......@@ -127,7 +130,7 @@ state_updated:
if (ppi->next_state != ppi->state)
pp_timer_stop(ppi->timers[PP_TIMER_WRS_CALIBRATION]);
ppi->next_delay = PP_DEFAULT_NEXT_DELAY_MS;
ppi->next_delay = DSPOR(ppi)->wrStateTimeout;
return e;
}
......@@ -14,7 +14,7 @@ int wr_locked(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (ppi->is_new_state) {
DSPOR(ppi)->portState = PPS_UNCALIBRATED;
DSPOR(ppi)->wrPortState = WRS_LOCKED;
pp_timer_start(DSPOR(ppi)->wrStateTimeout / 1000,
pp_timer_start(DSPOR(ppi)->wrStateTimeout,
ppi->timers[PP_TIMER_WRS_LOCKED]);
e = msg_issue_wrsig(ppi, LOCKED);
......@@ -47,7 +47,7 @@ state_updated:
if (ppi->next_state != ppi->state)
pp_timer_stop(ppi->timers[PP_TIMER_WRS_LOCKED]);
ppi->next_delay = PP_DEFAULT_NEXT_DELAY_MS;
ppi->next_delay = DSPOR(ppi)->wrStateTimeout;
return e;
}
......@@ -16,7 +16,7 @@ int wr_m_lock(struct pp_instance *ppi, unsigned char *pkt, int plen)
DSPOR(ppi)->wrPortState = WRS_M_LOCK;
DSPOR(ppi)->wrMode = WR_MASTER;
e = msg_issue_wrsig(ppi, LOCK);
pp_timer_start(WR_M_LOCK_TIMEOUT_MS / 1000,
pp_timer_start(WR_M_LOCK_TIMEOUT_MS,
ppi->timers[PP_TIMER_WRS_M_LOCK]);
}
......@@ -46,7 +46,7 @@ state_updated:
if (ppi->next_state != ppi->state)
pp_timer_stop(ppi->timers[PP_TIMER_WRS_M_LOCK]);
ppi->next_delay = PP_DEFAULT_NEXT_DELAY_MS;
ppi->next_delay = DSPOR(ppi)->wrStateTimeout;
return e;
}
......@@ -17,7 +17,7 @@ int wr_present(struct pp_instance *ppi, unsigned char *pkt, int plen)
DSPOR(ppi)->portState = PPS_UNCALIBRATED;
DSPOR(ppi)->wrPortState = WRS_PRESENT;
DSPOR(ppi)->wrMode = WR_SLAVE;
pp_timer_start(DSPOR(ppi)->wrStateTimeout / 1000,
pp_timer_start(DSPOR(ppi)->wrStateTimeout,
ppi->timers[PP_TIMER_WRS_PRESENT]);
st_com_restart_annrec_timer(ppi);
e = msg_issue_wrsig(ppi, SLAVE_PRESENT);
......@@ -54,7 +54,7 @@ state_updated:
pp_timer_stop(ppi->timers[PP_TIMER_ANN_RECEIPT]);
}
ppi->next_delay = PP_DEFAULT_NEXT_DELAY_MS;
ppi->next_delay = DSPOR(ppi)->wrStateTimeout;
return e;
}
......@@ -14,14 +14,17 @@ int wr_resp_calib_req(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (ppi->is_new_state) {
DSPOR(ppi)->wrPortState = WRS_RESP_CALIB_REQ;
ppi->next_delay = PP_DEFAULT_NEXT_DELAY_MS;
if (DSPOR(ppi)->otherNodeCalSendPattern)
if (DSPOR(ppi)->otherNodeCalSendPattern) {
wr_calibration_pattern_enable(ppi, 0, 0, 0);
pp_timer_start(WR_M_LOCK_TIMEOUT_MS / 1000,
ppi->timers[PP_TIMER_WRS_RESP_CALIB_REQ]);
pp_timer_start(
DSPOR(ppi)->otherNodeCalPeriod / 1000,
ppi->timers[PP_TIMER_WRS_RESP_CALIB_REQ]);
}
}
if (pp_timer_expired(ppi->timers[PP_TIMER_WRS_RESP_CALIB_REQ])) {
if ((DSPOR(ppi)->otherNodeCalSendPattern) &&
(pp_timer_expired(ppi->timers[PP_TIMER_WRS_RESP_CALIB_REQ]))) {
if (DSPOR(ppi)->wrMode == WR_MASTER)
ppi->next_state = PPS_MASTER;
else
......@@ -53,7 +56,7 @@ state_updated:
if (ppi->next_state != ppi->state)
pp_timer_stop(ppi->timers[PP_TIMER_WRS_M_LOCK]);
ppi->next_delay = PP_DEFAULT_NEXT_DELAY_MS;
ppi->next_delay = DSPOR(ppi)->wrStateTimeout;
return e;
}
......@@ -14,7 +14,7 @@ int wr_s_lock(struct pp_instance *ppi, unsigned char *pkt, int plen)
DSPOR(ppi)->wrPortState = WRS_S_LOCK;
ppi->next_delay = PP_DEFAULT_NEXT_DELAY_MS;
wr_locking_enable(ppi);
pp_timer_start(WR_S_LOCK_TIMEOUT_MS / 1000,
pp_timer_start(WR_S_LOCK_TIMEOUT_MS,
ppi->timers[PP_TIMER_WRS_S_LOCK]);
}
......@@ -34,7 +34,7 @@ state_updated:
if (ppi->next_state != ppi->state)
pp_timer_stop(ppi->timers[PP_TIMER_WRS_S_LOCK]);
ppi->next_delay = PP_DEFAULT_NEXT_DELAY_MS;
ppi->next_delay = DSPOR(ppi)->wrStateTimeout;
return e;
}
......@@ -22,7 +22,7 @@
#define WR_DEFAULT_CAL_PATTERN 0x3E0 /* 1111100000 */
#define WR_DEFAULT_CAL_PATTERN_LEN 0xA /* 10 bits */
#define WR_DEFAULT_STATE_TIMEOUT_MS 2000 /* [ms] (was 300) */
#define WR_DEFAULT_STATE_TIMEOUT_MS 300 /* [ms] */
#define WR_M_LOCK_TIMEOUT_MS 10000
#define WR_S_LOCK_TIMEOUT_MS 10000
#define WR_DEFAULT_STATE_REPEAT 3
......
......@@ -59,8 +59,8 @@ void st_com_restart_annrec_timer(struct pp_instance *ppi)
if (DSPOR(ppi)->logAnnounceInterval < 0)
PP_PRINTF("Error: logAnnounceInterval < 0");
pp_timer_start((DSPOR(ppi)->announceReceiptTimeout) <<
DSPOR(ppi)->logAnnounceInterval,
pp_timer_start(((DSPOR(ppi)->announceReceiptTimeout) <<
DSPOR(ppi)->logAnnounceInterval) * 1000,
ppi->timers[PP_TIMER_ANN_RECEIPT]);
}
......
......@@ -22,13 +22,14 @@ int pp_master(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (ppi->is_new_state) {
DSPOR(ppi)->portState = PPS_MASTER;
pp_timer_start(1 << DSPOR(ppi)->logSyncInterval,
pp_timer_start((1 << DSPOR(ppi)->logSyncInterval) * 1000,
ppi->timers[PP_TIMER_SYNC]);
pp_timer_start(1 << DSPOR(ppi)->logAnnounceInterval,
pp_timer_start((1 << DSPOR(ppi)->logAnnounceInterval) * 1000,
ppi->timers[PP_TIMER_ANN_INTERVAL]);
pp_timer_start(1 << DSPOR(ppi)->logMinPdelayReqInterval,
pp_timer_start(
(1 << DSPOR(ppi)->logMinPdelayReqInterval) * 1000,
ppi->timers[PP_TIMER_PDELAYREQ]);
/* Send an announce immediately, when becomes master */
if (msg_issue_announce(ppi) < 0)
......
......@@ -12,7 +12,8 @@ int pp_passive(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (ppi->is_new_state) {
DSPOR(ppi)->portState = PPS_PASSIVE;
pp_timer_start(1 << DSPOR(ppi)->logMinPdelayReqInterval,
pp_timer_start(
(1 << DSPOR(ppi)->logMinPdelayReqInterval) * 1000,
ppi->timers[PP_TIMER_PDELAYREQ]);
st_com_restart_annrec_timer(ppi);
......
......@@ -36,11 +36,13 @@ int pp_slave(struct pp_instance *ppi, unsigned char *pkt, int plen)
st_com_restart_annrec_timer(ppi);
if (OPTS(ppi)->e2e_mode)
pp_timer_start(1 << DSPOR(ppi)->logMinDelayReqInterval,
ppi->timers[PP_TIMER_DELAYREQ]);
pp_timer_start(
(1 << DSPOR(ppi)->logMinDelayReqInterval) * 1000,
ppi->timers[PP_TIMER_DELAYREQ]);
else
pp_timer_start(1 << DSPOR(ppi)->logMinPdelayReqInterval,
ppi->timers[PP_TIMER_PDELAYREQ]);
pp_timer_start(
(1 << DSPOR(ppi)->logMinPdelayReqInterval) * 1000,
ppi->timers[PP_TIMER_PDELAYREQ]);
}
if (st_com_check_record_update(ppi))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment