Skip to content

Commit 4ae1a7c

Browse files
ghent360marckleinebudde
authored andcommitted
usbd_gs_can: add support for GS_USB_BREQ_DATA_BITTIMING and GS_USB_BREQ_BT_CONST_EXT
1 parent 4dad6a1 commit 4ae1a7c

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

include/can.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ THE SOFTWARE.
2929
#include <stdint.h>
3030
#include <stdbool.h>
3131

32+
#include "config.h"
3233
#include "gs_usb.h"
3334
#include "hal_include.h"
3435
#include "led.h"
@@ -47,9 +48,24 @@ typedef struct {
4748
} can_data_t;
4849

4950
extern const struct gs_device_bt_const CAN_btconst;
51+
extern const struct gs_device_bt_const_extended CAN_btconst_ext;
5052

5153
void can_init(can_data_t *channel, CAN_TypeDef *instance);
5254
void can_set_bittiming(can_data_t *channel, const struct gs_device_bittiming *timing);
55+
56+
#ifdef CONFIG_CANFD
57+
void can_set_data_bittiming(can_data_t *channel, const struct gs_device_bittiming *timing);
58+
#else
59+
static inline bool can_set_data_bittiming(can_data_t *channel,
60+
const struct gs_device_bittiming *timing)
61+
{
62+
(void)channel;
63+
(void)timing;
64+
65+
return false;
66+
}
67+
#endif
68+
5369
void can_enable(can_data_t *channel, uint32_t mode);
5470
void can_disable(can_data_t *channel);
5571
bool can_is_enabled(can_data_t *channel);

src/can_common.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ THE SOFTWARE.
2929
#include "timer.h"
3030
#include "usbd_gs_can.h"
3131

32+
#ifndef CONFIG_CANFD
33+
const struct gs_device_bt_const_extended CAN_btconst_ext;
34+
#endif
35+
3236
bool can_check_bittiming_ok(const struct can_bittiming_const *btc,
3337
const struct gs_device_bittiming *timing)
3438
{

src/usbd_gs_can.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,14 @@ static uint8_t USBD_GS_CAN_Config_Request(USBD_HandleTypeDef *pdev, USBD_SetupRe
323323
}
324324
}
325325

326+
if (!IS_ENABLED(CONFIG_CANFD)) {
327+
switch (req->bRequest) {
328+
case GS_USB_BREQ_DATA_BITTIMING:
329+
case GS_USB_BREQ_BT_CONST_EXT:
330+
goto out_fail;
331+
}
332+
}
333+
326334
switch (req->bRequest) {
327335
// Host -> Device
328336
case GS_USB_BREQ_HOST_FORMAT:
@@ -349,6 +357,13 @@ static uint8_t USBD_GS_CAN_Config_Request(USBD_HandleTypeDef *pdev, USBD_SetupRe
349357
case GS_USB_BREQ_IDENTIFY:
350358
len = sizeof(struct gs_identify_mode);
351359
break;
360+
case GS_USB_BREQ_DATA_BITTIMING:
361+
len = sizeof(struct gs_device_bittiming);
362+
break;
363+
case GS_USB_BREQ_BT_CONST_EXT:
364+
src = &CAN_btconst_ext;
365+
len = sizeof(CAN_btconst_ext);
366+
break;
352367
case GS_USB_BREQ_SET_TERMINATION:
353368
if (get_term(req->wValue) == GS_CAN_TERMINATION_UNSUPPORTED) {
354369
goto out_fail;
@@ -382,6 +397,7 @@ static uint8_t USBD_GS_CAN_Config_Request(USBD_HandleTypeDef *pdev, USBD_SetupRe
382397
case GS_USB_BREQ_BITTIMING:
383398
case GS_USB_BREQ_MODE:
384399
case GS_USB_BREQ_IDENTIFY:
400+
case GS_USB_BREQ_DATA_BITTIMING:
385401
case GS_USB_BREQ_SET_TERMINATION:
386402
if (req->wLength > sizeof(hcan->ep0_buf)) {
387403
goto out_fail;
@@ -395,6 +411,7 @@ static uint8_t USBD_GS_CAN_Config_Request(USBD_HandleTypeDef *pdev, USBD_SetupRe
395411
case GS_USB_BREQ_BT_CONST:
396412
case GS_USB_BREQ_DEVICE_CONFIG:
397413
case GS_USB_BREQ_TIMESTAMP:
414+
case GS_USB_BREQ_BT_CONST_EXT:
398415
case GS_USB_BREQ_GET_TERMINATION:
399416
USBD_CtlSendData(pdev, (uint8_t *)src, len);
400417
break;
@@ -527,6 +544,15 @@ static uint8_t USBD_GS_CAN_EP0_RxReady(USBD_HandleTypeDef *pdev) {
527544
}
528545
break;
529546
}
547+
case GS_USB_BREQ_DATA_BITTIMING: {
548+
const struct gs_device_bittiming *timing = (struct gs_device_bittiming *)hcan->ep0_buf;
549+
550+
if (!can_check_bittiming_ok(&CAN_btconst_ext.dbtc, timing))
551+
goto out_fail;
552+
553+
can_set_data_bittiming(channel, timing);
554+
break;
555+
}
530556
case GS_USB_BREQ_SET_TERMINATION: {
531557
if (get_term(req->wValue) != GS_CAN_TERMINATION_UNSUPPORTED) {
532558
struct gs_device_termination_state *term_state;

0 commit comments

Comments
 (0)