Skip to content

Commit

Permalink
bxcan: can_set_bittiming(): evaluate gs_device_bittiming in CAN driver
Browse files Browse the repository at this point in the history
  • Loading branch information
marckleinebudde committed Jan 30, 2024
1 parent ad72111 commit b1e557b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
2 changes: 1 addition & 1 deletion include/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ typedef struct {
extern const struct gs_device_bt_const CAN_btconst;

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);
bool can_set_bittiming(can_data_t *channel, const struct gs_device_bittiming *timing);
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);
Expand Down
20 changes: 11 additions & 9 deletions src/can/bxcan.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,19 @@ void can_init(can_data_t *channel, CAN_TypeDef *instance)
device_can_init(channel, instance);
}

bool can_set_bittiming(can_data_t *channel, uint16_t brp, uint8_t phase_seg1, uint8_t phase_seg2, uint8_t sjw)
bool can_set_bittiming(can_data_t *channel, const struct gs_device_bittiming *timing)
{
if ( (brp>0) && (brp<=1024)
&& (phase_seg1>0) && (phase_seg1<=16)
&& (phase_seg2>0) && (phase_seg2<=8)
&& (sjw>0) && (sjw<=4)
const uint8_t tseg1 = timing->prop_seg + timing->phase_seg1;

if ( (timing->brp>0) && (timing->brp<=1024)
&& (tseg1>0) && (tseg1<=16)
&& (timing->phase_seg2>0) && (timing->phase_seg2<=8)
&& (timing->sjw>0) && (timing->sjw<=4)
) {
channel->brp = brp & 0x3FF;
channel->phase_seg1 = phase_seg1;
channel->phase_seg2 = phase_seg2;
channel->sjw = sjw;
channel->brp = timing->brp & 0x3FF;
channel->phase_seg1 = tseg1;
channel->phase_seg2 = timing->phase_seg2;
channel->sjw = timing->sjw;
return true;
} else {
return false;
Expand Down
8 changes: 2 additions & 6 deletions src/usbd_gs_can.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,9 @@ static uint8_t USBD_GS_CAN_EP0_RxReady(USBD_HandleTypeDef *pdev) {
break;

case GS_USB_BREQ_BITTIMING: {
struct gs_device_bittiming *timing;
const struct gs_device_bittiming *timing = (struct gs_device_bittiming *)hcan->ep0_buf;

timing = (struct gs_device_bittiming*)hcan->ep0_buf;
can_set_bittiming(channel, timing->brp,
timing->prop_seg + timing->phase_seg1,
timing->phase_seg2,
timing->sjw);
can_set_bittiming(channel, timing);
break;
}
case GS_USB_BREQ_MODE: {
Expand Down

0 comments on commit b1e557b

Please sign in to comment.