Skip to content

Commit

Permalink
MERGECOMMIT: drivers/mcp2515: fixes and cleanups RIOT-OS#17945
Browse files Browse the repository at this point in the history
  • Loading branch information
fjmolinas committed Apr 15, 2022
1 parent efd1cbc commit 26816e2
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 37 deletions.
2 changes: 1 addition & 1 deletion drivers/include/candev_mcp2515.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extern "C" {
* Default CAN bitrate
*/
#ifndef CANDEV_MCP2515_DEFAULT_BITRATE
#define CANDEV_MCP2515_DEFAULT_BITRATE 125000
#define CANDEV_MCP2515_DEFAULT_BITRATE 500000
#endif

/**
Expand Down
2 changes: 1 addition & 1 deletion drivers/mcp2515/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ config MODULE_MCP2515
select MODULE_PERIPH_GPIO
select MODULE_PERIPH_GPIO_IRQ
select MODULE_PERIPH_SPI
select MODULE_XTIMER
select ZTIMER_USEC

config HAVE_MCP2515
bool
Expand Down
2 changes: 1 addition & 1 deletion drivers/mcp2515/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
USEMODULE += xtimer
USEMODULE += ztimer_usec
FEATURES_REQUIRED += periph_gpio periph_spi periph_gpio_irq
29 changes: 3 additions & 26 deletions drivers/mcp2515/candev_mcp2515.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "periph_conf.h"
#include "thread.h"
#include "sched.h"
#include "xtimer.h"
#include "ztimer.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand Down Expand Up @@ -156,11 +156,6 @@ static int _send(candev_t *candev, const struct can_frame *frame)
int ret = 0;
enum mcp2515_mode mode;

if (frame->can_id > 0x1FFFFFFF) {
DEBUG("Illegal CAN-ID!\n");
return -EINVAL;
}

if (mutex_trylock(&_mcp_mutex)) {
mode = mcp2515_get_mode(dev);
mutex_unlock(&_mcp_mutex);
Expand Down Expand Up @@ -245,6 +240,7 @@ static void _isr(candev_t *candev)

if (mutex_trylock(&_mcp_mutex)) {
flag = mcp2515_get_irq(dev);
mcp2515_clear_irq(dev, flag & ~(INT_RX0 | INT_RX1));
mutex_unlock(&_mcp_mutex);
}
else {
Expand Down Expand Up @@ -283,24 +279,13 @@ static void _isr(candev_t *candev)
if (flag & INT_MESSAGE_ERROR) {
_irq_message_error(dev);
}

if (mutex_trylock(&_mcp_mutex)) {
flag = mcp2515_get_irq(dev);
mutex_unlock(&_mcp_mutex);
}
else {
DEBUG("isr2: Failed to lock mutex\n");
_neednewisr = 1;
return;
}

/* clear all flags except for RX flags, which are cleared by receiving */
if (mutex_trylock(&_mcp_mutex)) {
mcp2515_clear_irq(dev, flag & ~(INT_RX0 | INT_RX1));
mutex_unlock(&_mcp_mutex);
}
else {
DEBUG("isr3: failed to lock mutex\n");
DEBUG("isr2: Failed to lock mutex\n");
_neednewisr = 1;
return;
}
Expand Down Expand Up @@ -438,10 +423,6 @@ static int _set_filter(candev_t *dev, const struct can_filter *filter)

candev_mcp2515_t *dev_mcp = container_of(dev, candev_mcp2515_t, candev);

if (f.can_mask == 0) {
return -EINVAL; /* invalid mask */
}

if (mutex_trylock(&_mcp_mutex)) {
mode = mcp2515_get_mode(dev_mcp);
res = mcp2515_set_mode(dev_mcp, MODE_CONFIG);
Expand Down Expand Up @@ -540,10 +521,6 @@ static int _remove_filter(candev_t *dev, const struct can_filter *filter)

candev_mcp2515_t *dev_mcp = container_of(dev, candev_mcp2515_t, candev);

if (f.can_mask == 0) {
return -1; /* invalid mask */
}

if (mutex_trylock(&_mcp_mutex)) {
mode = mcp2515_get_mode(dev_mcp);
res = mcp2515_set_mode(dev_mcp, MODE_CONFIG);
Expand Down
18 changes: 11 additions & 7 deletions drivers/mcp2515/mcp2515.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include <string.h>

#include "xtimer.h"
#include "ztimer.h"
#include "mcp2515.h"
#include "mcp2515_spi.h"
#include "mcp2515_defines.h"
Expand Down Expand Up @@ -99,7 +99,9 @@ int mcp2515_init(candev_mcp2515_t *dev, void (*irq_handler_cb)(void *))
DEBUG("Error setting interrupt pin!\n");
return -1;
}
gpio_init(dev->conf->rst_pin, GPIO_OUT);
if (gpio_is_valid(dev->conf->rst_pin)) {
gpio_init(dev->conf->rst_pin, GPIO_OUT);
}

res = mcp2515_spi_init(dev);
if (res < 0) {
Expand All @@ -118,10 +120,12 @@ int mcp2515_init(candev_mcp2515_t *dev, void (*irq_handler_cb)(void *))

void mcp2515_reset(candev_mcp2515_t *dev)
{
gpio_clear(dev->conf->rst_pin);
xtimer_usleep(RESET_DELAY_US);
gpio_set(dev->conf->rst_pin);
xtimer_usleep(_osc_startup(dev));
if (gpio_is_valid(dev->conf->rst_pin)) {
gpio_clear(dev->conf->rst_pin);
ztimer_sleep(ZTIMER_USEC, RESET_DELAY_US);
gpio_set(dev->conf->rst_pin);
ztimer_sleep(ZTIMER_USEC, _osc_startup(dev));
}
}

static void _fill_standard_id(uint32_t id, uint8_t *bytebuf)
Expand Down Expand Up @@ -273,7 +277,7 @@ void mcp2515_wake_up(candev_mcp2515_t *dev)
dev->wakeup_src = MCP2515_WKUP_SRC_INTERNAL;
mcp2515_spi_bitmod(dev, MCP2515_CANINTF, MCP2515_CANINTF_WAKIF,
MCP2515_CANINTF_WAKIF);
xtimer_usleep(_osc_startup(dev));
ztimer_sleep(ZTIMER_USEC, _osc_startup(dev));

uint8_t flag = mcp2515_get_irq(dev);

Expand Down
1 change: 0 additions & 1 deletion drivers/mcp2515/mcp2515_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "periph/gpio.h"
#include "periph/spi.h"

#include "xtimer.h"
#include "irq.h"

#define ENABLE_DEBUG 0
Expand Down

0 comments on commit 26816e2

Please sign in to comment.