Skip to content

Commit

Permalink
squash! squash candev_stm32: fix wake up
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Dupont committed Jun 14, 2017
1 parent 934be1e commit be8456f
Showing 1 changed file with 47 additions and 108 deletions.
155 changes: 47 additions & 108 deletions cpu/stm32_common/can/candev_stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <cpu_conf.h>

#include "candev_stm32.h"
#include "can/device.h"
Expand All @@ -28,20 +29,17 @@
#include "thread.h"
#include "sched.h"
#include "mutex.h"
#ifdef MODULE_PM_LAYERED
#include "pm_layered.h"
#endif

#define ENABLE_DEBUG (0)
#include "debug.h"

#define CAN_MAX_WAIT_CHANGE 1000
#define CAN_TIxR_SFF_SHIFT 21
#define CAN_TIxR_EFF_SHIFT 3
#define CAN_MAX_WAIT_CHANGE (1000U)
#define CAN_TIxR_SFF_SHIFT (21U)
#define CAN_TIxR_EFF_SHIFT (3U)

#define CAN_MCR_BIT_DONT_TOUCH 0xFFFFFF03
#define CAN_MCR_BIT_DONT_TOUCH (0xFFFFFF03)

typedef enum stm32_can_mode {
typedef enum {
MODE_NORMAL,
MODE_SLEEP,
MODE_INIT,
Expand All @@ -55,8 +53,6 @@ static int _set(candev_t *candev, canopt_t opt, void *value, size_t value_len);
static int _get(candev_t *candev, canopt_t opt, void *value, size_t max_len);
static int _set_filter(candev_t *candev, const struct can_filter *filter);
static int _remove_filter(candev_t *candev, const struct can_filter *filter);
static int _power_up(candev_t *candev);
static int _power_down(candev_t *candev);

static void tx_irq_handler(candev_stm32_t *dev);
static void tx_isr(candev_stm32_t *dev);
Expand All @@ -79,8 +75,6 @@ static const candev_driver_t candev_stm32_driver = {
.abort = _abort,
.set_filter = _set_filter,
.remove_filter = _remove_filter,
.power_down = _power_down,
.power_up = _power_up,
};

static const struct can_bittiming_const bittiming_const = {
Expand Down Expand Up @@ -232,21 +226,6 @@ static inline void unset_filter(CAN_TypeDef *can, uint8_t filter)
can->FA1R &= ~(1 << filter);
}

#ifdef MODULE_PM_LAYERED
static void _ext_wake_up_irq(void* args)
{
candev_t *candev = (candev_t *)args;
candev_stm32_t *dev = (candev_stm32_t *)candev;
DEBUG("_ext_wake_up_irq: device=0x%p\n", (void*)dev);

gpio_irq_disable(dev->rx_pin);

if (dev->candev.event_callback) {
dev->candev.event_callback(candev, CANDEV_EVENT_WAKE_UP, NULL);
}
}
#endif

void candev_stm32_set_pins(candev_stm32_t *dev, gpio_t tx_pin, gpio_t rx_pin,
gpio_af_t af)
{
Expand Down Expand Up @@ -553,40 +532,50 @@ static int _set(candev_t *candev, canopt_t opt, void *value, size_t value_len)
can_mode_t mode;

switch (opt) {
case CANOPT_BITTIMING:
if (value_len < sizeof(dev->candev.bittiming)) {
res = -EOVERFLOW;
}
else {
memcpy(&dev->candev.bittiming, value, sizeof(dev->candev.bittiming));
mode = get_mode(can);
res = set_mode(can, MODE_INIT);
if (res == 0) {
set_bit_timing(dev);
res = sizeof(dev->candev.bittiming);
case CANOPT_BITTIMING:
if (value_len < sizeof(dev->candev.bittiming)) {
res = -EOVERFLOW;
}
if (set_mode(can, mode) < 0) {
res = -EBUSY;
else {
memcpy(&dev->candev.bittiming, value, sizeof(dev->candev.bittiming));
mode = get_mode(can);
res = set_mode(can, MODE_INIT);
if (res == 0) {
set_bit_timing(dev);
res = sizeof(dev->candev.bittiming);
}
if (set_mode(can, mode) < 0) {
res = -EBUSY;
}
}
}
break;
case CANOPT_LISTEN_ONLY:
if (value_len != sizeof(uint8_t)) {
res = -EOVERFLOW;
}
else {
mode = get_mode(can);
res = set_mode(can, MODE_INIT);
can->BTR |= CAN_BTR_SILM;
res += set_mode(can, mode);
if (res == 0) {
res = sizeof(uint8_t);
break;
case CANOPT_STATE:
if (value_len < sizeof(canopt_state_t)) {
res = -EOVERFLOW;
}
}
break;
default:
res = -ENOTSUP;
break;
else {
switch (*((canopt_state_t *)value)) {
case CANOPT_STATE_SLEEP:
case CANOPT_STATE_OFF:
DEBUG("candev_stm32 %p: power down\n", (void*)dev);
res = set_mode(dev->conf->can, MODE_SLEEP);
break;
case CANOPT_STATE_ON:
DEBUG("candev_stm32 %p: power up\n", (void*)dev);
res = set_mode(dev->conf->can, MODE_NORMAL);
break;
case CANOPT_STATE_LISTEN_ONLY:
mode = get_mode(can);
res = set_mode(can, MODE_INIT);
can->BTR |= CAN_BTR_SILM;
res += set_mode(can, mode);
break;
}
}
break;
default:
res = -ENOTSUP;
break;
}

return res;
Expand Down Expand Up @@ -629,15 +618,6 @@ static int _get(candev_t *candev, canopt_t opt, void *value, size_t max_len)
res = sizeof(uint32_t);
}
break;
case CANOPT_LISTEN_ONLY:
if (max_len != sizeof(uint8_t)) {
res = -EOVERFLOW;
}
else {
*((uint8_t*) value) = (can->BTR & CAN_BTR_SILM) == 0 ? 0 : 1;
res = sizeof(uint8_t);
}
break;
case CANOPT_RX_FILTERS:
if (max_len % sizeof(struct can_filter) != 0) {
res = -EOVERFLOW;
Expand Down Expand Up @@ -748,38 +728,6 @@ static int _remove_filter(candev_t *candev, const struct can_filter *filter)
return 0;
}

static int _power_down(candev_t *candev)
{
candev_stm32_t *dev = (candev_stm32_t *)candev;

DEBUG("candev_stm32 %p: power down\n", (void*)dev);

#ifdef MODULE_PM_LAYERED
gpio_init_int(dev->rx_pin, GPIO_IN, GPIO_FALLING, _ext_wake_up_irq, candev);

pm_unblock(1);
#endif

return set_mode(dev->conf->can, MODE_SLEEP);
}

static int _power_up(candev_t *candev)
{
candev_stm32_t *dev = (candev_stm32_t *)candev;

DEBUG("candev_stm32 %p: power up\n", (void*)dev);

#ifdef MODULE_PM_LAYERED
gpio_init(dev->rx_pin, GPIO_IN);
gpio_init_af(dev->rx_pin, dev->conf->af);

pm_block(1);
#endif

return set_mode(dev->conf->can, MODE_NORMAL);

}

static void tx_conf(candev_stm32_t *dev, int mailbox)
{
candev_t *candev = (candev_t *) dev;
Expand Down Expand Up @@ -935,15 +883,6 @@ static void sce_irq_handler(candev_stm32_t *dev)

DEBUG("sce irq\n");

#ifndef MODULE_PM_LAYERED
if ((can->MSR & CAN_MSR_WKUI) == CAN_MSR_WKUI) {
can->MSR = CAN_MSR_WKUI;
if (dev->candev.event_callback) {
dev->candev.event_callback(candev, CANDEV_EVENT_WAKE_UP, NULL);
}
}
#endif

if ((can->MSR & CAN_MSR_ERRI) == CAN_MSR_ERRI) {
can->MSR = CAN_MSR_ERRI;
if ((can->ESR & CAN_ESR_BOFF) == CAN_ESR_BOFF) {
Expand Down

0 comments on commit be8456f

Please sign in to comment.