From 7acc6fe474766788687a5257be21ac549bed77f3 Mon Sep 17 00:00:00 2001 From: Amir Hammad Date: Wed, 1 Apr 2015 16:22:05 +0200 Subject: libusbhost: Open source USB host stack for embedded devices First public version, date: 1.4.2015 Signed-off-by: Amir Hammad --- include/driver/usbh_device_driver.h | 131 ++++++++++++++++++++++++++++++++++++ include/usbh_config.h | 56 +++++++++++++++ include/usbh_driver_gp_xbox.h | 73 ++++++++++++++++++++ include/usbh_driver_hid_mouse.h | 43 ++++++++++++ include/usbh_driver_hub.h | 36 ++++++++++ include/usbh_hubbed.h | 76 +++++++++++++++++++++ include/usbh_lld_stm32f4.h | 43 ++++++++++++ 7 files changed, 458 insertions(+) create mode 100644 include/driver/usbh_device_driver.h create mode 100644 include/usbh_config.h create mode 100644 include/usbh_driver_gp_xbox.h create mode 100644 include/usbh_driver_hid_mouse.h create mode 100644 include/usbh_driver_hub.h create mode 100644 include/usbh_hubbed.h create mode 100644 include/usbh_lld_stm32f4.h (limited to 'include') diff --git a/include/driver/usbh_device_driver.h b/include/driver/usbh_device_driver.h new file mode 100644 index 0000000..f8837e0 --- /dev/null +++ b/include/driver/usbh_device_driver.h @@ -0,0 +1,131 @@ +/* + * This file is part of the libusbhost library + * hosted at http://github.com/libusbhost/libusbhost + * + * Copyright (C) 2015 Amir Hammad + * + * + * libusbhost is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + * + */ + +#ifndef USBH_DEVICE_DRIVER_ +#define USBH_DEVICE_DRIVER_ + +#include "usbh_config.h" +#include "usbh_hubbed.h" + +#include + +BEGIN_DECLS + +#define USBH_EPTYP_CONTROL (0) +#define USBH_EPTYP_ISOCHRONOUS (1) +#define USBH_EPTYP_BULK (2) +#define USBH_EPTYP_INTERRUPT (3) + +#define USBH_SPEED_FULL (0) +#define USBH_SPEED_LOW (1) +#define USBH_SPEED_HIGH (2) + + +enum USBH_PACKET_CALLBACK_STATUS { + USBH_PACKET_CALLBACK_STATUS_OK = 0, + USBH_PACKET_CALLBACK_STATUS_ERRSIZ = 1, + USBH_PACKET_CALLBACK_STATUS_EAGAIN = 2, // -- TODO: automatic handling of transmit errors 3xTXERR->FATAL + USBH_PACKET_CALLBACK_STATUS_EFATAL = 3 +}; + +enum USBH_POLL_STATUS { + USBH_POLL_STATUS_NONE, + USBH_POLL_STATUS_DEVICE_CONNECTED, + USBH_POLL_STATUS_DEVICE_DISCONNECTED +}; + +struct _usbh_device { + uint16_t packet_size_max0; + int8_t address; + uint8_t speed; // (USBH_SPEED_*) + uint8_t state; // for enumeration purposes + uint8_t toggle0; + const usbh_dev_driver_t *drv; + void *drvdata; + const void *lld; +}; +typedef struct _usbh_device usbh_device_t; + +struct _usbh_packet_callback_data { + enum USBH_PACKET_CALLBACK_STATUS status; + uint32_t transferred_length; +}; +typedef struct _usbh_packet_callback_data usbh_packet_callback_data_t; + +typedef void (*usbh_packet_callback_t)(usbh_device_t *dev, usbh_packet_callback_data_t status); + +struct _usbh_packet { + void *data; // Pointer to data + uint16_t datalen; // Data length 0..1023 + int8_t address; // Device address + uint8_t endpoint_type; // Endpoint type (see USBH_EPTYP_*) + uint8_t endpoint_address; // Endpoint number 0..15 + uint16_t endpoint_size_max; // Max packet size for an endpoint + uint8_t speed; // (USBH_SPEED_*) + uint8_t *toggle; + usbh_packet_callback_t callback; + void *callback_arg; +}; +typedef struct _usbh_packet usbh_packet_t; + +struct _usbh_driver { + void (*init)(void *drvdata); + void (*write)(void *drvdata, const usbh_packet_t *packet); + void (*read)(void *drvdata, usbh_packet_t *packet); + enum USBH_POLL_STATUS (*poll)(void *drvdata, uint32_t t_us); + uint8_t (*root_speed)(void *drvdata); + + // Pointer to Low-level driver data + void *driver_data; +}; +typedef struct _usbh_driver usbh_driver_t; + +struct _usbh_generic_data { + usbh_device_t usbh_device[USBH_MAX_DEVICES]; + uint8_t usbh_buffer[BUFFER_ONE_BYTES]; +}; +typedef struct _usbh_generic_data usbh_generic_data_t; + + +#define ERROR(arg) LOG_PRINTF("UNHANDLED_ERROR %d: file: %s, line: %d",\ + arg, __FILE__, __LINE__) + + +/// Hub related functions + +usbh_device_t *usbh_get_free_device(const usbh_device_t *dev); +bool usbh_enum_available(void); +void device_enumeration_start(usbh_device_t *dev); + +/// All devices functions + +/// +void usbh_read(usbh_device_t *dev, usbh_packet_t *packet); +void usbh_write(usbh_device_t *dev, const usbh_packet_t *packet); +/// Helper functions +void device_xfer_control_read(void *data, uint16_t datalen, usbh_packet_callback_t callback, usbh_device_t *dev); +void device_xfer_control_write(void *data, uint16_t datalen, usbh_packet_callback_t callback, usbh_device_t *dev); + + +END_DECLS + +#endif diff --git a/include/usbh_config.h b/include/usbh_config.h new file mode 100644 index 0000000..1b60954 --- /dev/null +++ b/include/usbh_config.h @@ -0,0 +1,56 @@ +/* + * This file is part of the libusbhost library + * hosted at http://github.com/libusbhost/libusbhost + * + * Copyright (C) 2015 Amir Hammad + * + * + * libusbhost is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + * + */ + +#ifndef USBH_CONFIG_ +#define USBH_CONFIG_ + + + +// Max devices per hub +#define USBH_HUB_MAX_DEVICES (8) + +// Max number of hub instancies +#define USBH_MAX_HUBS (2) + +// Max devices +#define USBH_MAX_DEVICES (15) + +// Min: 128 +// Set this wisely +#define BUFFER_ONE_BYTES (2048) + +// MOUSE +#define USBH_HID_MOUSE_MAX_DEVICES (2) + +#define USBH_HID_MOUSE_BUFFER (32) + +// Gamepad XBOX +#define USBH_GP_XBOX_MAX_DEVICES (2) + +#define USBH_GP_XBOX_BUFFER (32) + +/* Sanity checks */ +#if (USBH_MAX_DEVICES > 127) +#error USBH_MAX_DEVICES > 127 +#endif + +#endif diff --git a/include/usbh_driver_gp_xbox.h b/include/usbh_driver_gp_xbox.h new file mode 100644 index 0000000..8080f30 --- /dev/null +++ b/include/usbh_driver_gp_xbox.h @@ -0,0 +1,73 @@ +/* + * This file is part of the libusbhost library + * hosted at http://github.com/libusbhost/libusbhost + * + * Copyright (C) 2015 Amir Hammad + * + * + * libusbhost is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + * + */ + +#ifndef USBH_DRIVER_GP_XBOX_ +#define USBH_DRIVER_GP_XBOX_ + +#include "usbh_hubbed.h" + +#include + +BEGIN_DECLS + +#define GP_XBOX_DPAD_TOP (1 << 0) +#define GP_XBOX_DPAD_LEFT (1 << 1) +#define GP_XBOX_DPAD_BOTTOM (1 << 2) +#define GP_XBOX_DPAD_RIGHT (1 << 3) +#define GP_XBOX_BUTTON_X (1 << 4) +#define GP_XBOX_BUTTON_Y (1 << 5) +#define GP_XBOX_BUTTON_A (1 << 6) +#define GP_XBOX_BUTTON_B (1 << 7) +#define GP_XBOX_BUTTON_SELECT (1 << 8) +#define GP_XBOX_BUTTON_START (1 << 9) +#define GP_XBOX_BUTTON_LT (1 << 10) +#define GP_XBOX_BUTTON_RT (1 << 11) +#define GP_XBOX_BUTTON_XBOX (1 << 12) +#define GP_XBOX_BUTTON_AXIS_LEFT (1 << 13) +#define GP_XBOX_BUTTON_AXIS_RIGHT (1 << 14) + +struct _gp_xbox_packet { + uint32_t buttons; + int16_t axis_left_x; + int16_t axis_left_y; + int16_t axis_right_x; + int16_t axis_right_y; + uint8_t axis_rear_left; + uint8_t axis_rear_right; +}; +typedef struct _gp_xbox_packet gp_xbox_packet_t; + + +struct _gp_xbox_config { + void (*update)(uint8_t device_id, gp_xbox_packet_t data); + void (*notify_connected)(uint8_t device_id); + void (*notify_disconnected)(uint8_t device_id); +}; +typedef struct _gp_xbox_config gp_xbox_config_t; + +void gp_xbox_driver_init(const gp_xbox_config_t *config); + +extern const usbh_dev_driver_t usbh_gp_xbox_driver; + +END_DECLS + +#endif diff --git a/include/usbh_driver_hid_mouse.h b/include/usbh_driver_hid_mouse.h new file mode 100644 index 0000000..c12fed4 --- /dev/null +++ b/include/usbh_driver_hid_mouse.h @@ -0,0 +1,43 @@ +/* + * This file is part of the libusbhost library + * hosted at http://github.com/libusbhost/libusbhost + * + * Copyright (C) 2015 Amir Hammad + * + * + * libusbhost is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + * + */ + +#ifndef USBH_DRIVER_HID_MOUSE_ +#define USBH_DRIVER_HID_MOUSE_ + +#include "usbh_hubbed.h" + +#include + +BEGIN_DECLS + +struct _hid_mouse_config { + void (*mouse_in_message_handler)(uint8_t device_id, const uint8_t *data); +}; +typedef struct _hid_mouse_config hid_mouse_config_t; + +void hid_mouse_driver_init(const hid_mouse_config_t *config); + +extern const usbh_dev_driver_t usbh_hid_mouse_driver; + +END_DECLS + +#endif diff --git a/include/usbh_driver_hub.h b/include/usbh_driver_hub.h new file mode 100644 index 0000000..cda1463 --- /dev/null +++ b/include/usbh_driver_hub.h @@ -0,0 +1,36 @@ +/* + * This file is part of the libusbhost library + * hosted at http://github.com/libusbhost/libusbhost + * + * Copyright (C) 2015 Amir Hammad + * + * + * libusbhost is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + * + */ + +#ifndef USBH_DRIVER_HUB_ +#define USBH_DRIVER_HUB_ + +#include "usbh_hubbed.h" + +BEGIN_DECLS + +void hub_driver_init(void); + +extern const usbh_dev_driver_t usbh_hub_driver; + +END_DECLS + +#endif diff --git a/include/usbh_hubbed.h b/include/usbh_hubbed.h new file mode 100644 index 0000000..00d3b99 --- /dev/null +++ b/include/usbh_hubbed.h @@ -0,0 +1,76 @@ +/* + * This file is part of the libusbhost library + * hosted at http://github.com/libusbhost/libusbhost + * + * Copyright (C) 2015 Amir Hammad + * + * + * libusbhost is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + * + */ + +#ifndef USBH_HUBBED_ +#define USBH_HUBBED_ + +#include "usbh_config.h" + +#include + +/* This must be placed around external function declaration for C++ + * support. */ +#ifdef __cplusplus +# define BEGIN_DECLS extern "C" { +# define END_DECLS } +#else +# define BEGIN_DECLS +# define END_DECLS +#endif + +BEGIN_DECLS + +#ifndef bool +#define bool _Bool +#define false 0 +#define true 1 +#endif + + +// set to -1 to unused items +struct _usbh_dev_driver_info { + int32_t deviceClass; + int32_t deviceSubClass; + int32_t deviceProtocol; + int32_t idVendor; + int32_t idProduct; + int32_t ifaceClass; + int32_t ifaceSubClass; + int32_t ifaceProtocol; +}; +typedef struct _usbh_dev_driver_info usbh_dev_driver_info_t; + +struct _usbh_dev_driver { + bool (*analyze_descriptor)(void *drv, void *descriptor); + void *(*init)(void *usbh_dev); + void (*poll)(void *drvdata, uint32_t t_us); + void (*remove)(void *drvdata); + const usbh_dev_driver_info_t * const info; +}; +typedef struct _usbh_dev_driver usbh_dev_driver_t; + +void usbh_init(const void *drivers[], const usbh_dev_driver_t * const device_drivers[]); +void usbh_poll(uint32_t t_us); + +END_DECLS + +#endif diff --git a/include/usbh_lld_stm32f4.h b/include/usbh_lld_stm32f4.h new file mode 100644 index 0000000..c55c615 --- /dev/null +++ b/include/usbh_lld_stm32f4.h @@ -0,0 +1,43 @@ +/* + * This file is part of the libusbhost library + * hosted at http://github.com/libusbhost/libusbhost + * + * Copyright (C) 2015 Amir Hammad + * + * + * libusbhost is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + * + */ + +#ifndef USBH_LLD_STM32F4_H_ +#define USBH_LLD_STM32F4_H_ + +#include "usbh_hubbed.h" + +#include + +BEGIN_DECLS + +// pass this to usbh init +extern const void *usbh_lld_stm32f4_drivers[]; + +#ifdef USART_DEBUG +void print_channels(const void *drvdata); +#else +#define print_channels(arg) ((void)arg) +#endif + +END_DECLS + +#endif -- cgit