Skip to content

Commit

Permalink
usbd_gs_can: add support for GS_USB_BREQ_DATA_BITTIMING and GS_USB_BR…
Browse files Browse the repository at this point in the history
…EQ_BT_CONST_EXT
  • Loading branch information
ghent360 authored and marckleinebudde committed Jun 6, 2024
1 parent 4dad6a1 commit 4ae1a7c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ THE SOFTWARE.
#include <stdint.h>
#include <stdbool.h>

#include "config.h"
#include "gs_usb.h"
#include "hal_include.h"
#include "led.h"
Expand All @@ -47,9 +48,24 @@ typedef struct {
} can_data_t;

extern const struct gs_device_bt_const CAN_btconst;
extern const struct gs_device_bt_const_extended CAN_btconst_ext;

void can_init(can_data_t *channel, CAN_TypeDef *instance);
void can_set_bittiming(can_data_t *channel, const struct gs_device_bittiming *timing);

#ifdef CONFIG_CANFD
void can_set_data_bittiming(can_data_t *channel, const struct gs_device_bittiming *timing);
#else
static inline bool can_set_data_bittiming(can_data_t *channel,
const struct gs_device_bittiming *timing)
{
(void)channel;
(void)timing;

return false;
}
#endif

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
4 changes: 4 additions & 0 deletions src/can_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ THE SOFTWARE.
#include "timer.h"
#include "usbd_gs_can.h"

#ifndef CONFIG_CANFD
const struct gs_device_bt_const_extended CAN_btconst_ext;
#endif

bool can_check_bittiming_ok(const struct can_bittiming_const *btc,
const struct gs_device_bittiming *timing)
{
Expand Down
26 changes: 26 additions & 0 deletions src/usbd_gs_can.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,14 @@ static uint8_t USBD_GS_CAN_Config_Request(USBD_HandleTypeDef *pdev, USBD_SetupRe
}
}

if (!IS_ENABLED(CONFIG_CANFD)) {
switch (req->bRequest) {
case GS_USB_BREQ_DATA_BITTIMING:
case GS_USB_BREQ_BT_CONST_EXT:
goto out_fail;
}
}

switch (req->bRequest) {
// Host -> Device
case GS_USB_BREQ_HOST_FORMAT:
Expand All @@ -349,6 +357,13 @@ static uint8_t USBD_GS_CAN_Config_Request(USBD_HandleTypeDef *pdev, USBD_SetupRe
case GS_USB_BREQ_IDENTIFY:
len = sizeof(struct gs_identify_mode);
break;
case GS_USB_BREQ_DATA_BITTIMING:
len = sizeof(struct gs_device_bittiming);
break;
case GS_USB_BREQ_BT_CONST_EXT:
src = &CAN_btconst_ext;
len = sizeof(CAN_btconst_ext);
break;
case GS_USB_BREQ_SET_TERMINATION:
if (get_term(req->wValue) == GS_CAN_TERMINATION_UNSUPPORTED) {
goto out_fail;
Expand Down Expand Up @@ -382,6 +397,7 @@ static uint8_t USBD_GS_CAN_Config_Request(USBD_HandleTypeDef *pdev, USBD_SetupRe
case GS_USB_BREQ_BITTIMING:
case GS_USB_BREQ_MODE:
case GS_USB_BREQ_IDENTIFY:
case GS_USB_BREQ_DATA_BITTIMING:
case GS_USB_BREQ_SET_TERMINATION:
if (req->wLength > sizeof(hcan->ep0_buf)) {
goto out_fail;
Expand All @@ -395,6 +411,7 @@ static uint8_t USBD_GS_CAN_Config_Request(USBD_HandleTypeDef *pdev, USBD_SetupRe
case GS_USB_BREQ_BT_CONST:
case GS_USB_BREQ_DEVICE_CONFIG:
case GS_USB_BREQ_TIMESTAMP:
case GS_USB_BREQ_BT_CONST_EXT:
case GS_USB_BREQ_GET_TERMINATION:
USBD_CtlSendData(pdev, (uint8_t *)src, len);
break;
Expand Down Expand Up @@ -527,6 +544,15 @@ static uint8_t USBD_GS_CAN_EP0_RxReady(USBD_HandleTypeDef *pdev) {
}
break;
}
case GS_USB_BREQ_DATA_BITTIMING: {
const struct gs_device_bittiming *timing = (struct gs_device_bittiming *)hcan->ep0_buf;

if (!can_check_bittiming_ok(&CAN_btconst_ext.dbtc, timing))
goto out_fail;

can_set_data_bittiming(channel, timing);
break;
}
case GS_USB_BREQ_SET_TERMINATION: {
if (get_term(req->wValue) != GS_CAN_TERMINATION_UNSUPPORTED) {
struct gs_device_termination_state *term_state;
Expand Down

0 comments on commit 4ae1a7c

Please sign in to comment.