2019-09-28 15:18:22 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2016 Hubert Denkmair
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
2022-10-08 09:00:54 -04:00
|
|
|
#include <stdint.h>
|
2022-11-10 21:50:33 +01:00
|
|
|
|
2022-10-08 09:00:54 -04:00
|
|
|
#include "can.h"
|
2022-12-15 13:29:32 +01:00
|
|
|
#include "compiler.h"
|
2022-11-10 22:32:44 +01:00
|
|
|
#include "config.h"
|
2022-10-08 09:00:54 -04:00
|
|
|
#include "gs_usb.h"
|
2022-11-10 21:50:33 +01:00
|
|
|
#include "led.h"
|
2022-11-11 22:45:52 +01:00
|
|
|
#include "list.h"
|
2022-11-10 21:50:33 +01:00
|
|
|
#include "usbd_def.h"
|
2019-09-28 15:18:22 +02:00
|
|
|
|
|
|
|
/* Define these here so they can be referenced in other files */
|
|
|
|
|
2022-12-15 10:19:53 +01:00
|
|
|
#define GS_CAN_EP0_BUF_SIZE \
|
2024-06-04 15:36:22 +02:00
|
|
|
max5(sizeof(struct gs_host_config), \
|
|
|
|
sizeof(struct gs_device_bittiming), \
|
|
|
|
sizeof(struct gs_device_mode), \
|
|
|
|
sizeof(struct gs_identify_mode), \
|
|
|
|
sizeof(struct gs_device_termination_state))
|
2022-11-21 17:13:39 +01:00
|
|
|
#ifdef CONFIG_CANFD
|
|
|
|
#define CAN_DATA_MAX_PACKET_SIZE 64 /* Endpoint IN & OUT Packet size */
|
|
|
|
#else
|
2022-10-11 19:27:57 -04:00
|
|
|
#define CAN_DATA_MAX_PACKET_SIZE 32 /* Endpoint IN & OUT Packet size */
|
2022-11-21 17:13:39 +01:00
|
|
|
#endif
|
2022-10-11 19:27:57 -04:00
|
|
|
#define USB_CAN_CONFIG_DESC_SIZ 50
|
|
|
|
#define USBD_GS_CAN_VENDOR_CODE 0x20
|
|
|
|
#define DFU_INTERFACE_NUM 1
|
|
|
|
#define DFU_INTERFACE_STR_INDEX 0xE0
|
2019-09-28 15:18:22 +02:00
|
|
|
|
|
|
|
extern USBD_ClassTypeDef USBD_GS_CAN;
|
|
|
|
|
2022-11-21 17:13:39 +01:00
|
|
|
#ifdef CONFIG_CANFD
|
|
|
|
#define GS_HOST_FRAME_SIZE struct_size((struct gs_host_frame *)NULL, canfd_ts, 1)
|
|
|
|
#else
|
2022-11-21 10:20:23 +01:00
|
|
|
#define GS_HOST_FRAME_SIZE struct_size((struct gs_host_frame *)NULL, classic_can_ts, 1)
|
2022-11-21 17:13:39 +01:00
|
|
|
#endif
|
2022-11-21 10:20:23 +01:00
|
|
|
|
2022-11-11 22:45:52 +01:00
|
|
|
struct gs_host_frame_object {
|
|
|
|
struct list_head list;
|
2022-11-21 10:20:23 +01:00
|
|
|
union {
|
|
|
|
uint8_t _buf[GS_HOST_FRAME_SIZE];
|
|
|
|
struct gs_host_frame frame;
|
|
|
|
};
|
2022-11-11 22:45:52 +01:00
|
|
|
};
|
|
|
|
|
2022-11-09 21:46:02 +01:00
|
|
|
typedef struct {
|
2022-11-27 18:03:38 +01:00
|
|
|
uint8_t __aligned(4) ep0_buf[GS_CAN_EP0_BUF_SIZE];
|
2022-11-09 21:46:02 +01:00
|
|
|
|
|
|
|
USBD_SetupReqTypedef last_setup_request;
|
|
|
|
|
2022-11-11 22:45:52 +01:00
|
|
|
struct list_head list_frame_pool;
|
|
|
|
struct list_head list_to_host;
|
2022-11-09 21:46:02 +01:00
|
|
|
|
2022-11-11 22:45:52 +01:00
|
|
|
struct gs_host_frame_object *from_host_buf;
|
2022-11-19 22:59:12 +01:00
|
|
|
struct gs_host_frame_object *to_host_buf;
|
2022-11-09 21:46:02 +01:00
|
|
|
|
2022-11-09 22:55:28 +01:00
|
|
|
can_data_t channels[NUM_CAN_CHANNEL];
|
2022-11-09 21:46:02 +01:00
|
|
|
|
|
|
|
bool dfu_detach_requested;
|
|
|
|
|
|
|
|
bool timestamps_enabled;
|
|
|
|
uint32_t sof_timestamp_us;
|
|
|
|
|
|
|
|
bool pad_pkts_to_max_pkt_size;
|
2022-11-10 22:32:44 +01:00
|
|
|
|
2022-11-11 22:45:52 +01:00
|
|
|
struct gs_host_frame_object msgbuf[CAN_QUEUE_SIZE];
|
2022-11-09 21:46:02 +01:00
|
|
|
} USBD_GS_CAN_HandleTypeDef __attribute__ ((aligned (4)));
|
2022-01-05 14:04:59 -08:00
|
|
|
|
|
|
|
#if defined(STM32F0)
|
2022-10-31 21:51:33 -04:00
|
|
|
# define USB_INTERFACE USB
|
|
|
|
# define USB_INTERRUPT USB_IRQn
|
2022-01-05 14:04:59 -08:00
|
|
|
#elif defined(STM32F4)
|
2022-10-31 21:51:33 -04:00
|
|
|
# define USB_INTERFACE USB_OTG_FS
|
|
|
|
# define USB_INTERRUPT OTG_FS_IRQn
|
2022-01-06 17:42:50 -05:00
|
|
|
|
2022-01-05 14:04:59 -08:00
|
|
|
// RX FIFO is defined in words, so divide bytes by 4
|
|
|
|
// RX FIFO size chosen according to reference manual RM0368 which suggests
|
|
|
|
// using (largest packet size / 4) + 1
|
|
|
|
# define USB_RX_FIFO_SIZE ((256U / 4U) + 1U)
|
|
|
|
#endif
|
|
|
|
|
2022-11-14 15:25:53 +01:00
|
|
|
uint8_t USBD_GS_CAN_Init(USBD_GS_CAN_HandleTypeDef *hcan, USBD_HandleTypeDef *pdev);
|
2022-02-24 18:54:31 +01:00
|
|
|
void USBD_GS_CAN_SuspendCallback(USBD_HandleTypeDef *pdev);
|
|
|
|
void USBD_GS_CAN_ResumeCallback(USBD_HandleTypeDef *pdev);
|
2022-11-19 22:59:12 +01:00
|
|
|
void USBD_GS_CAN_ReceiveFromHost(USBD_HandleTypeDef *pdev);
|
2022-11-18 13:35:55 +01:00
|
|
|
void USBD_GS_CAN_SendToHost(USBD_HandleTypeDef *pdev);
|
2019-09-28 15:18:22 +02:00
|
|
|
bool USBD_GS_CAN_CustomDeviceRequest(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
|
|
|
|
bool USBD_GS_CAN_CustomInterfaceRequest(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
|
|
|
|
|
|
|
|
bool USBD_GS_CAN_DfuDetachRequested(USBD_HandleTypeDef *pdev);
|