can: can_enable: move the mode flags evaluation into the CAN drivers

...having so many arguments doesn't scale.
This commit is contained in:
Marc Kleine-Budde 2023-01-19 23:58:41 +01:00 committed by Marc Kleine-Budde
parent 45424cf541
commit 206de0067a
3 changed files with 16 additions and 13 deletions

View File

@ -48,7 +48,7 @@ typedef struct {
void can_init(can_data_t *channel, CAN_TypeDef *instance);
bool can_set_bittiming(can_data_t *channel, uint16_t brp, uint8_t phase_seg1, uint8_t phase_seg2, uint8_t sjw);
void can_enable(can_data_t *channel, bool loop_back, bool listen_only, bool one_shot);
void can_enable(can_data_t *channel, uint32_t mode);
void can_disable(can_data_t *channel);
bool can_is_enabled(can_data_t *channel);

View File

@ -78,22 +78,30 @@ bool can_set_bittiming(can_data_t *channel, uint16_t brp, uint8_t phase_seg1, ui
}
}
void can_enable(can_data_t *channel, bool loop_back, bool listen_only, bool one_shot)
void can_enable(can_data_t *channel, uint32_t mode)
{
CAN_TypeDef *can = channel->instance;
uint32_t mcr = CAN_MCR_INRQ
| CAN_MCR_ABOM
| CAN_MCR_TXFP
| (one_shot ? CAN_MCR_NART : 0);
| CAN_MCR_TXFP;
if (mode & GS_CAN_MODE_ONE_SHOT) {
mcr |= CAN_MCR_NART;
}
uint32_t btr = ((uint32_t)(channel->sjw-1)) << 24
| ((uint32_t)(channel->phase_seg1-1)) << 16
| ((uint32_t)(channel->phase_seg2-1)) << 20
| (channel->brp - 1)
| (loop_back ? CAN_MODE_LOOPBACK : 0)
| (listen_only ? CAN_MODE_SILENT : 0);
| (channel->brp - 1);
if (mode & GS_CAN_MODE_LISTEN_ONLY) {
btr |= CAN_MODE_SILENT;
}
if (mode & GS_CAN_MODE_LOOP_BACK) {
btr |= CAN_MODE_LOOPBACK;
}
// Reset CAN peripheral
can->MCR |= CAN_MCR_RESET;

View File

@ -532,12 +532,7 @@ static uint8_t USBD_GS_CAN_EP0_RxReady(USBD_HandleTypeDef *pdev) {
hcan->timestamps_enabled = (mode->flags & GS_CAN_MODE_HW_TIMESTAMP) != 0;
hcan->pad_pkts_to_max_pkt_size = (mode->flags & GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE) != 0;
can_enable(channel,
(mode->flags & GS_CAN_MODE_LOOP_BACK) != 0,
(mode->flags & GS_CAN_MODE_LISTEN_ONLY) != 0,
(mode->flags & GS_CAN_MODE_ONE_SHOT) != 0
// triple sampling not supported on bxCAN
);
can_enable(channel, mode->flags);
led_set_mode(&channel->leds, LED_MODE_NORMAL);
}