File usbd_core.h
File List > fw > rbcx-coprocessor > lib > libusb_stm32 > include > usbd_core.h
Go to the documentation of this file.
/* This file is the part of the Lightweight USB device Stack for STM32 microcontrollers
*
* Copyright ©2016 Dmitry Filimonchuk <dmitrystu[at]gmail[dot]com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
.*/
#ifndef _USBD_CORE_H_
#define _USBD_CORE_H_
#if defined(__cplusplus)
extern "C" {
#endif
#if defined(__DOXYGEN__)
#define USBD_SOF_DISABLED
#define USBD_VBUS_DETECT
#define USBD_DP_PORT
#define USBD_DP_PIN
#define USBD_SOF_OUT
#define USBD_PRIMARY_OTGHS
#define USBD_USE_EXT_ULPI
#endif
#define usbd_evt_reset 0
#define usbd_evt_sof 1
#define usbd_evt_susp 2
#define usbd_evt_wkup 3
#define usbd_evt_eptx 4
#define usbd_evt_eprx 5
#define usbd_evt_epsetup 6
#define usbd_evt_error 7
#define usbd_evt_count 8
#define usbd_lane_unk 0
#define usbd_lane_dsc 1
#define usbd_lane_sdp 2
#define usbd_lane_cdp 3
#define usbd_lane_dcp 4
#define USBD_HW_ADDRFST (1 << 0)
#define USBD_HW_BC (1 << 1)
#define USND_HW_HS (1 << 2)
#define USBD_HW_ENABLED (1 << 3)
#define USBD_HW_ENUMSPEED (2 << 4)
#define USBD_HW_SPEED_NC (0 << 4)
#define USBD_HW_SPEED_LS (1 << 4)
#define USBD_HW_SPEED_FS (2 << 4)
#define USBD_HW_SPEED_HS (3 << 4)
#define USB_EPTYPE_DBLBUF 0x04
#define USB_REQ_DIRECTION (1 << 7)
#define USB_REQ_HOSTTODEV (0 << 7)
#define USB_REQ_DEVTOHOST (1 << 7)
#define USB_REQ_TYPE (3 << 5)
#define USB_REQ_STANDARD (0 << 5)
#define USB_REQ_CLASS (1 << 5)
#define USB_REQ_VENDOR (2 << 5)
#define USB_REQ_RECIPIENT (3 << 0)
#define USB_REQ_DEVICE (0 << 0)
#define USB_REQ_INTERFACE (1 << 0)
#define USB_REQ_ENDPOINT (2 << 0)
#define USB_REQ_OTHER (3 << 0)
#if !defined(__ASSEMBLER__)
#include <stdbool.h>
enum usbd_machine_state {
usbd_state_disabled,
usbd_state_disconnected,
usbd_state_default,
usbd_state_addressed,
usbd_state_configured,
};
enum usbd_ctl_state {
usbd_ctl_idle,
usbd_ctl_rxdata,
usbd_ctl_txdata,
usbd_ctl_ztxdata,
usbd_ctl_lastdata,
usbd_ctl_statusin,
usbd_ctl_statusout,
};
typedef enum _usbd_respond {
usbd_fail,
usbd_ack,
usbd_nak,
} usbd_respond;
typedef struct _usbd_device usbd_device;
typedef struct {
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
uint8_t data[];
} usbd_ctlreq;
typedef struct {
void *data_buf;
void *data_ptr;
uint16_t data_count;
uint16_t data_maxsize;
uint8_t ep0size;
uint8_t device_cfg;
uint8_t device_state;
uint8_t control_state;
} usbd_status;
typedef void (*usbd_evt_callback)(usbd_device *dev, uint8_t event, uint8_t ep);
typedef void (*usbd_rqc_callback)(usbd_device *dev, usbd_ctlreq *req);
typedef usbd_respond (*usbd_ctl_callback)(usbd_device *dev, usbd_ctlreq *req, usbd_rqc_callback *callback);
typedef usbd_respond (*usbd_dsc_callback)(usbd_ctlreq *req, void **address, uint16_t *dsize);
typedef usbd_respond (*usbd_cfg_callback)(usbd_device *dev, uint8_t cfg);
typedef uint32_t (*usbd_hw_getinfo)(void);
typedef void (*usbd_hw_enable)(bool enable);
typedef uint8_t (*usbd_hw_connect)(bool connect);
typedef void (*usbd_hw_setaddr)(uint8_t address);
typedef bool (*usbd_hw_ep_config)(uint8_t ep, uint8_t eptype, uint16_t epsize);
typedef void (*usbd_hw_ep_deconfig)(uint8_t ep);
typedef int32_t (*usbd_hw_ep_read)(uint8_t ep, void *buf, uint16_t blen);
typedef int32_t (*usbd_hw_ep_write)(uint8_t ep, void *buf, uint16_t blen);
typedef void (*usbd_hw_ep_setstall)(uint8_t ep, bool stall);
typedef bool (*usbd_hw_ep_isstalled)(uint8_t ep);
typedef void (*usbd_hw_poll)(usbd_device *dev, usbd_evt_callback callback);
typedef uint16_t (*usbd_hw_get_frameno)(void);
typedef uint16_t (*usbd_hw_get_serialno)(void *buffer);
struct usbd_driver {
usbd_hw_getinfo getinfo;
usbd_hw_enable enable;
usbd_hw_connect connect;
usbd_hw_setaddr setaddr;
usbd_hw_ep_config ep_config;
usbd_hw_ep_deconfig ep_deconfig;
usbd_hw_ep_read ep_read;
usbd_hw_ep_write ep_write;
usbd_hw_ep_setstall ep_setstall;
usbd_hw_ep_isstalled ep_isstalled;
usbd_hw_poll poll;
usbd_hw_get_frameno frame_no;
usbd_hw_get_serialno get_serialno_desc;
};
struct _usbd_device {
const struct usbd_driver *driver;
usbd_ctl_callback control_callback;
usbd_rqc_callback complete_callback;
usbd_cfg_callback config_callback;
usbd_dsc_callback descriptor_callback;
usbd_evt_callback events[usbd_evt_count];
usbd_evt_callback endpoint[8];
usbd_status status;
};
inline static void usbd_init(usbd_device *dev, const struct usbd_driver *drv,
const uint8_t ep0size, uint32_t *buffer, const uint16_t bsize) {
dev->driver = drv;
dev->status.ep0size = ep0size;
dev->status.data_ptr = buffer;
dev->status.data_buf = buffer;
dev->status.data_maxsize = bsize - __builtin_offsetof(usbd_ctlreq, data);
}
void usbd_poll(usbd_device *dev);
inline static void usbd_reg_control(usbd_device *dev, usbd_ctl_callback callback) {
dev->control_callback = callback;
}
inline static void usbd_reg_config(usbd_device *dev, usbd_cfg_callback callback) {
dev->config_callback = callback;
}
inline static void usbd_reg_descr(usbd_device *dev, usbd_dsc_callback callback) {
dev->descriptor_callback = callback;
}
inline static bool usbd_ep_config(usbd_device *dev, uint8_t ep, uint8_t eptype, uint16_t epsize) {
return dev->driver->ep_config(ep, eptype, epsize);
}
inline static void usbd_ep_deconfig(usbd_device *dev, uint8_t ep) {
dev->driver->ep_deconfig(ep);
}
inline static void usbd_reg_endpoint(usbd_device *dev, uint8_t ep, usbd_evt_callback callback) {
dev->endpoint[ep & 0x07] = callback;
}
inline static void usbd_reg_event(usbd_device *dev, uint8_t evt, usbd_evt_callback callback) {
dev->events[evt] = callback;
}
inline static int32_t usbd_ep_write(usbd_device *dev, uint8_t ep, void *buf, uint16_t blen) {
return dev->driver->ep_write(ep, buf, blen);
}
inline static int32_t usbd_ep_read(usbd_device *dev, uint8_t ep, void *buf, uint16_t blen) {
return dev->driver->ep_read(ep, buf, blen);
}
inline static void usbd_ep_stall(usbd_device *dev, uint8_t ep) {
dev->driver->ep_setstall(ep, 1);
}
inline static void usbd_ep_unstall(usbd_device *dev, uint8_t ep) {
dev->driver->ep_setstall(ep, 0);
}
inline static void usbd_enable(usbd_device *dev, bool enable) {
dev->driver->enable(enable);
}
inline static uint8_t usbd_connect(usbd_device *dev, bool connect) {
return dev->driver->connect(connect);
}
inline static uint32_t usbd_getinfo(usbd_device *dev) {
return dev->driver->getinfo();
}
#endif //(__ASSEMBLER__)
#if defined(__cplusplus)
}
#endif
#endif //_USBD_STD_H_