Skip to content

Commit

Permalink
drivers/dose: make use of checksum module
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Oct 6, 2022
1 parent e313c56 commit 98e18ed
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 17 deletions.
1 change: 1 addition & 0 deletions drivers/dose/Kconfig
Expand Up @@ -28,6 +28,7 @@ menuconfig MODULE_DOSE
depends on HAS_PERIPH_GPIO
depends on HAS_PERIPH_GPIO_IRQ
depends on HAS_PERIPH_UART
select MODULE_CHECKSUM
select MODULE_CHUNKED_RINGBUFFER
select MODULE_EUI_PROVIDER
select MODULE_IOLIST
Expand Down
1 change: 1 addition & 0 deletions drivers/dose/Makefile.dep
Expand Up @@ -7,6 +7,7 @@ ifneq (,$(filter dose_watchdog,$(USEMODULE)))
FEATURES_REQUIRED += periph_timer_periodic
endif

USEMODULE += checksum
USEMODULE += chunked_ringbuffer
USEMODULE += eui_provider
USEMODULE += iolist
Expand Down
20 changes: 3 additions & 17 deletions drivers/dose/dose.c
Expand Up @@ -27,6 +27,7 @@
#include "irq.h"
#include "periph/timer.h"

#include "checksum/crc16_ccitt.h"
#include "net/eui_provider.h"
#include "net/netdev/eth.h"
#include "timex.h"
Expand All @@ -38,7 +39,6 @@
#error "DOSE_TIMER_DEV needs to be set by the board"
#endif

static uint16_t crc16_update(uint16_t crc, uint8_t octet);
static dose_signal_t state_transit_blocked(dose_t *ctx, dose_signal_t signal);
static dose_signal_t state_transit_idle(dose_t *ctx, dose_signal_t signal);
static dose_signal_t state_transit_recv(dose_t *ctx, dose_signal_t signal);
Expand All @@ -59,22 +59,10 @@ static int _init(netdev_t *dev);
static void _poweron(dose_t *dev);
static void _poweroff(dose_t *dev, dose_state_t sleep_state);

static uint16_t crc16_update(uint16_t crc, uint8_t octet)
{
crc = (uint8_t)(crc >> 8) | (crc << 8);
crc ^= octet;
crc ^= (uint8_t)(crc & 0xff) >> 4;
crc ^= (crc << 8) << 4;
crc ^= ((crc & 0xff) << 4) << 1;
return crc;
}

static void _crc_cb(void *ctx, uint8_t *data, size_t len)
{
uint16_t *crc = ctx;
for (uint8_t *end = data + len; data != end; ++data) {
*crc = crc16_update(*crc, *data);
}
*crc = crc16_ccitt_false_update(*crc, data, len);
}

static void _init_standby(dose_t *ctx, const dose_params_t *params)
Expand Down Expand Up @@ -571,15 +559,13 @@ static int _send(netdev_t *dev, const iolist_t *iolist)
size_t n = iol->iol_len;
pktlen += n;
uint8_t *ptr = iol->iol_base;
crc = crc16_ccitt_false_update(crc, ptr, n);
while (n--) {
/* Send data octet */
if (send_data_octet(ctx, *ptr)) {
goto collision;
}

/* Update CRC */
crc = crc16_update(crc, *ptr);

ptr++;
}
}
Expand Down

0 comments on commit 98e18ed

Please sign in to comment.