Skip to content

Commit

Permalink
Merge pull request #17366 from aabadie/pr/can_ztimer
Browse files Browse the repository at this point in the history
sys/can: migrate to ztimer
  • Loading branch information
maribu authored Jan 12, 2022
2 parents a6bfe53 + ce37390 commit 71ed611
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 55 deletions.
6 changes: 4 additions & 2 deletions sys/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,16 @@ ifneq (,$(filter can,$(USEMODULE)))
endif

ifneq (,$(filter can_isotp,$(USEMODULE)))
USEMODULE += xtimer
USEMODULE += ztimer
USEMODULE += ztimer_usec
USEMODULE += gnrc_pktbuf
endif

ifneq (,$(filter conn_can,$(USEMODULE)))
USEMODULE += can
USEMODULE += can_mbox
USEMODULE += xtimer
USEMODULE += ztimer
USEMODULE += ztimer_usec
endif

ifneq (,$(filter entropy_source_%,$(USEMODULE)))
Expand Down
28 changes: 14 additions & 14 deletions sys/can/conn/isotp.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
#define ENABLE_DEBUG 0
#include "debug.h"

#include "xtimer.h"
#include "ztimer.h"

#define _TIMEOUT_TX_MSG_TYPE (0x8000)
#define _TIMEOUT_RX_MSG_TYPE (0x8001)
#define _CLOSE_CONN_MSG_TYPE (0x8002)
#define _TIMEOUT_MSG_VALUE (0xABCDEFAB)

#ifndef CONN_CAN_ISOTP_TIMEOUT_TX_CONF
#define CONN_CAN_ISOTP_TIMEOUT_TX_CONF (10 * US_PER_SEC)
#ifndef CONN_CAN_ISOTP_TIMEOUT_TX_CONF_US
#define CONN_CAN_ISOTP_TIMEOUT_TX_CONF_US (10 * US_PER_SEC)
#endif

static inline int try_put_msg(conn_can_isotp_t *conn, msg_t *msg)
Expand Down Expand Up @@ -169,10 +169,10 @@ int conn_can_isotp_send(conn_can_isotp_t *conn, const void *buf, size_t size, in
return isotp_send(&conn->isotp, buf, size, flags);
}
else {
xtimer_t timer;
ztimer_t timer;
timer.callback = _tx_conf_timeout;
timer.arg = conn;
xtimer_set(&timer, CONN_CAN_ISOTP_TIMEOUT_TX_CONF);
ztimer_set(ZTIMER_USEC, &timer, CONN_CAN_ISOTP_TIMEOUT_TX_CONF_US);

ret = isotp_send(&conn->isotp, buf, size, flags);

Expand All @@ -192,7 +192,7 @@ int conn_can_isotp_send(conn_can_isotp_t *conn, const void *buf, size_t size, in
break;
}
#endif
xtimer_remove(&timer);
ztimer_remove(ZTIMER_USEC, &timer);
return ret;
case _TIMEOUT_TX_MSG_TYPE:
return -ETIMEDOUT;
Expand Down Expand Up @@ -246,11 +246,11 @@ int conn_can_isotp_recv(conn_can_isotp_t *conn, void *buf, size_t size, uint32_t
}
#endif

xtimer_t timer;
ztimer_t timer;
if (timeout != 0) {
timer.callback = _rx_timeout;
timer.arg = conn;
xtimer_set(&timer, timeout);
ztimer_set(ZTIMER_USEC, &timer, timeout);
}

msg_t msg;
Expand All @@ -270,7 +270,7 @@ int conn_can_isotp_recv(conn_can_isotp_t *conn, void *buf, size_t size, uint32_t
}
#endif
if (timeout != 0) {
xtimer_remove(&timer);
ztimer_remove(ZTIMER_USEC, &timer);
}
if (snip->size <= size) {
memcpy(buf, snip->data, snip->size);
Expand All @@ -296,7 +296,7 @@ int conn_can_isotp_recv(conn_can_isotp_t *conn, void *buf, size_t size, uint32_t
if ((msg.content.ptr == conn) || (msg.content.ptr == conn->master)) {
#endif
if (timeout != 0) {
xtimer_remove(&timer);
ztimer_remove(ZTIMER_USEC, &timer);
}
return -ECONNABORTED;
#ifdef MODULE_CONN_CAN_ISOTP_MULTI
Expand All @@ -306,7 +306,7 @@ int conn_can_isotp_recv(conn_can_isotp_t *conn, void *buf, size_t size, uint32_t
default:
DEBUG("conn_can_isotp_recv: unexpected msg %x\n", msg.type);
if (timeout != 0) {
xtimer_remove(&timer);
ztimer_remove(ZTIMER_USEC, &timer);
}
ret = -EINTR;
return ret;
Expand Down Expand Up @@ -383,11 +383,11 @@ int conn_can_isotp_select(conn_can_isotp_slave_t **conn, conn_can_isotp_t *maste

int ret;

xtimer_t timer;
ztimer_t timer;
if (timeout != 0) {
timer.callback = _rx_timeout;
timer.arg = master;
xtimer_set(&timer, timeout);
ztimer_set(ZTIMER_USEC, &timer, timeout);
}

msg_t msg;
Expand All @@ -396,7 +396,7 @@ int conn_can_isotp_select(conn_can_isotp_slave_t **conn, conn_can_isotp_t *maste
mbox_get(&master->mbox, &msg);

if (timeout != 0) {
xtimer_remove(&timer);
ztimer_remove(ZTIMER_USEC, &timer);
}
switch (msg.type) {
case CAN_MSG_RX_INDICATION:
Expand Down
22 changes: 11 additions & 11 deletions sys/can/conn/raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
#define ENABLE_DEBUG 0
#include "debug.h"

#include "xtimer.h"
#include "ztimer.h"

#define _TIMEOUT_TX_MSG_TYPE (0x8000)
#define _TIMEOUT_RX_MSG_TYPE (0x8001)
#define _CLOSE_CONN_MSG_TYPE (0x8002)
#define _TIMEOUT_MSG_VALUE (0xABCDEFAB)

#ifndef CONN_CAN_RAW_TIMEOUT_TX_CONF
#define CONN_CAN_RAW_TIMEOUT_TX_CONF (1 * US_PER_SEC)
#ifndef CONN_CAN_ISOTP_TIMEOUT_TX_CONF_US
#define CONN_CAN_ISOTP_TIMEOUT_TX_CONF_US (1 * US_PER_SEC)
#endif

int conn_can_raw_create(conn_can_raw_t *conn, struct can_filter *filter, size_t count,
Expand Down Expand Up @@ -146,22 +146,22 @@ int conn_can_raw_send(conn_can_raw_t *conn, const struct can_frame *frame, int f
}
}
else {
xtimer_t timer;
ztimer_t timer;
timer.callback = _tx_conf_timeout;
timer.arg = conn;
xtimer_set(&timer, CONN_CAN_RAW_TIMEOUT_TX_CONF);
ztimer_set(ZTIMER_USEC, &timer, CONN_CAN_ISOTP_TIMEOUT_TX_CONF_US);

handle = raw_can_send_mbox(conn->ifnum, frame, &conn->mbox);
if (handle < 0) {
xtimer_remove(&timer);
ztimer_remove(ZTIMER_USEC, &timer);
return handle;
}

msg_t msg;
int timeout = 5;
while (1) {
mbox_get(&conn->mbox, &msg);
xtimer_remove(&timer);
ztimer_remove(ZTIMER_USEC, &timer);
switch (msg.type) {
case CAN_MSG_TX_ERROR:
return -EIO;
Expand All @@ -186,7 +186,7 @@ int conn_can_raw_send(conn_can_raw_t *conn, const struct can_frame *frame, int f
if (!timeout--) {
return -EINTR;
}
xtimer_set(&timer, CONN_CAN_RAW_TIMEOUT_TX_CONF);
ztimer_set(ZTIMER_USEC, &timer, CONN_CAN_ISOTP_TIMEOUT_TX_CONF_US);
break;
}
}
Expand Down Expand Up @@ -216,12 +216,12 @@ int conn_can_raw_recv(conn_can_raw_t *conn, struct can_frame *frame, uint32_t ti

assert(frame != NULL);

xtimer_t timer;
ztimer_t timer;

if (timeout != 0) {
timer.callback = _rx_timeout;
timer.arg = conn;
xtimer_set(&timer, timeout);
ztimer_set(ZTIMER_USEC, &timer, timeout);
}

int ret;
Expand All @@ -230,7 +230,7 @@ int conn_can_raw_recv(conn_can_raw_t *conn, struct can_frame *frame, uint32_t ti

mbox_get(&conn->mbox, &msg);
if (timeout != 0) {
xtimer_remove(&timer);
ztimer_remove(ZTIMER_USEC, &timer);
}
switch (msg.type) {
case CAN_MSG_RX_INDICATION:
Expand Down
11 changes: 6 additions & 5 deletions sys/can/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <errno.h>

#include "thread.h"
#include "ztimer.h"
#include "can/device.h"
#include "can/common.h"
#include "can/pkt.h"
Expand Down Expand Up @@ -146,7 +147,7 @@ static int power_down(candev_dev_t *candev_dev)
}

#ifdef MODULE_CAN_PM
xtimer_remove(&candev_dev->pm_timer);
ztimer_remove(ZTIMER_USEC, &candev_dev->pm_timer);
candev_dev->last_pm_update = 0;
#endif

Expand All @@ -171,15 +172,15 @@ static void pm_reset(candev_dev_t *candev_dev, uint32_t value)

if (value == 0) {
candev_dev->last_pm_value = 0;
xtimer_remove(&candev_dev->pm_timer);
ztimer_remove(ZTIMER_USEC, &candev_dev->pm_timer);
return;
}

if (candev_dev->last_pm_update == 0 ||
value > (candev_dev->last_pm_value - (xtimer_now_usec() - candev_dev->last_pm_update))) {
value > (candev_dev->last_pm_value - (ztimer_now(ZTIMER_USEC) - candev_dev->last_pm_update))) {
candev_dev->last_pm_value = value;
candev_dev->last_pm_update = xtimer_now_usec();
xtimer_set(&candev_dev->pm_timer, value);
candev_dev->last_pm_update = ztimer_now(ZTIMER_USEC);
ztimer_set(ZTIMER_USEC, &candev_dev->pm_timer, value);
}
}
#endif
Expand Down
Loading

0 comments on commit 71ed611

Please sign in to comment.