mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge pull request #17945 from fjmolinas/pr_mcp2515_cleanups
drivers/mcp2515: fixes and cleanups
This commit is contained in:
commit
df95c6ca83
@ -32,7 +32,6 @@
|
||||
#include "periph/gpio.h"
|
||||
#include "periph/spi.h"
|
||||
#include "mutex.h"
|
||||
#include "xtimer.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -42,7 +41,7 @@ extern "C" {
|
||||
* Default CAN bitrate
|
||||
*/
|
||||
#ifndef CANDEV_MCP2515_DEFAULT_BITRATE
|
||||
#define CANDEV_MCP2515_DEFAULT_BITRATE 125000
|
||||
#define CANDEV_MCP2515_DEFAULT_BITRATE 500000
|
||||
#endif
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
|
@ -1,2 +1,2 @@
|
||||
USEMODULE += xtimer
|
||||
USEMODULE += ztimer_usec
|
||||
FEATURES_REQUIRED += periph_gpio periph_spi periph_gpio_irq
|
||||
|
@ -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"
|
||||
@ -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);
|
||||
@ -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 {
|
||||
@ -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;
|
||||
}
|
||||
@ -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);
|
||||
@ -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);
|
||||
|
@ -20,7 +20,7 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "xtimer.h"
|
||||
#include "ztimer.h"
|
||||
#include "mcp2515.h"
|
||||
#include "mcp2515_spi.h"
|
||||
#include "mcp2515_defines.h"
|
||||
@ -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) {
|
||||
@ -118,10 +120,15 @@ 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);
|
||||
}
|
||||
else {
|
||||
mcp2515_spi_reset(dev);
|
||||
}
|
||||
ztimer_sleep(ZTIMER_USEC, _osc_startup(dev));
|
||||
}
|
||||
|
||||
static void _fill_standard_id(uint32_t id, uint8_t *bytebuf)
|
||||
@ -273,7 +280,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);
|
||||
|
||||
|
@ -25,7 +25,6 @@
|
||||
#include "periph/gpio.h"
|
||||
#include "periph/spi.h"
|
||||
|
||||
#include "xtimer.h"
|
||||
#include "irq.h"
|
||||
|
||||
#define ENABLE_DEBUG 0
|
||||
|
@ -16,11 +16,10 @@
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef MODULE_MCP2515
|
||||
#include "can/device.h"
|
||||
#include "mcp2515_params.h"
|
||||
|
||||
#define CANDEV_MCP2515_NUMOF ((ARRAY_SIZE(candev_mcp2515_params) / ARRAY_SIZE(candev_params_t)))
|
||||
#define CANDEV_MCP2515_NUMOF (ARRAY_SIZE(candev_mcp2515_params))
|
||||
|
||||
#ifndef CANDEV_MCP2515_STACKSIZE
|
||||
#define CANDEV_MCP2515_STACKSIZE (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF)
|
||||
@ -55,6 +54,3 @@ void auto_init_can_mcp2515(void) {
|
||||
&candev_dev_mcp2515[i]);
|
||||
}
|
||||
}
|
||||
#else
|
||||
typedef int dont_be_pedantic;
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user