Loading...
Searching...
No Matches
usbdev.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018 Koen Zandberg <koen@bergzand.net>
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser General
5 * Public License v2.1. See the file LICENSE in the top level directory for
6 * more details.
7 */
75#ifndef PERIPH_USBDEV_H
76#define PERIPH_USBDEV_H
77
78#include <stdbool.h>
79#include <stdint.h>
80#include <stddef.h>
81
82#include "assert.h"
83#include "periph_conf.h"
84#include "usb.h"
85#include "usb/usbopt.h"
86
87#ifdef __cplusplus
88extern "C" {
89#endif
90
94typedef struct usbdev usbdev_t;
95
99typedef struct usbdev_ep usbdev_ep_t;
100
106#ifndef USBDEV_CPU_DMA_REQUIREMENTS
107#define USBDEV_CPU_DMA_REQUIREMENTS
108#endif
109
126#define usbdev_ep_buf_t USBDEV_CPU_DMA_REQUIREMENTS uint8_t
127
138#ifndef USBDEV_CPU_SET_ADDR_AFTER_STATUS
139#define USBDEV_CPU_SET_ADDR_AFTER_STATUS 1
140#endif
141
148#ifndef USBDEV_NUM_ENDPOINTS
149#define USBDEV_NUM_ENDPOINTS 8
150#endif
151
226
234
243
255
266
273typedef struct usbdev_driver {
274
283
298 usbdev_ep_t *(*new_ep)(usbdev_t *dev, usb_ep_type_t type, usb_ep_dir_t dir, size_t size);
299
312 void *value, size_t max_len);
313
326 const void *value, size_t value_len);
327
336 void (*esr)(usbdev_t *dev);
337
346
355 void (*ep_init)(usbdev_ep_t *ep);
356
371 void (*ep_stall)(usbdev_ep_t *ep, bool enable);
372
385 void *value, size_t max_len);
386
399 const void *value, size_t value_len);
400
409 void (*ep_esr)(usbdev_ep_t *ep);
410
427 int (*xmit)(usbdev_ep_t *ep, uint8_t *buf, size_t len);
429
436
445
455static inline void usbdev_init(usbdev_t *dev)
456{
457 assert(dev);
458 dev->driver->init(dev);
459}
460
477 usb_ep_dir_t dir, size_t size)
478{
479 assert(dev);
480 return dev->driver->new_ep(dev, type, dir, size);
481}
482
498static inline int usbdev_get(usbdev_t *dev, usbopt_t opt,
499 void *value, size_t max_len)
500{
501 assert(dev);
502 return dev->driver->get(dev, opt, value, max_len);
503}
504
520static inline int usbdev_set(usbdev_t *dev, usbopt_t opt,
521 const void *value, size_t value_len)
522{
523 assert(dev);
524 return dev->driver->set(dev, opt, value, value_len);
525}
526
536static inline void usbdev_esr(usbdev_t *dev)
537{
538 assert(dev);
539 dev->driver->esr(dev);
540}
541
555static inline void usbdev_ep0_stall(usbdev_t *dev)
556{
557 assert(dev);
558 dev->driver->ep0_stall(dev);
559}
560
571static inline void usbdev_ep_init(usbdev_ep_t *ep)
572{
573 assert(ep);
574 assert(ep->dev);
575 ep->dev->driver->ep_init(ep);
576}
577
591static inline void usbdev_ep_stall(usbdev_ep_t *ep, bool enable)
592{
593 assert(ep);
594 assert(ep->dev);
595 ep->dev->driver->ep_stall(ep, enable);
596}
597
614static inline int usbdev_ep_get(usbdev_ep_t *ep, usbopt_ep_t opt,
615 void *value, size_t max_len)
616{
617 assert(ep);
618 assert(ep->dev);
619 return ep->dev->driver->ep_get(ep, opt, value, max_len);
620}
621
638static inline int usbdev_ep_set(usbdev_ep_t *ep, usbopt_ep_t opt,
639 const void *value, size_t value_len)
640{
641 assert(ep);
642 assert(ep->dev);
643 return ep->dev->driver->ep_set(ep, opt, value, value_len);
644}
645
656static inline void usbdev_ep_esr(usbdev_ep_t *ep)
657{
658 assert(ep);
659 assert(ep->dev);
660 ep->dev->driver->ep_esr(ep);
661}
662
679static inline int usbdev_ep_xmit(usbdev_ep_t *ep, uint8_t *buf, size_t len)
680{
681 assert(ep);
682 assert(ep->dev);
683 return ep->dev->driver->xmit(ep, buf, len);
684}
685
686#ifdef __cplusplus
687}
688#endif
689
690#endif /* PERIPH_USBDEV_H */
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:136
void usbdev_init_lowlevel(void)
Low level USB peripheral driver initialization.
static int usbdev_ep_set(usbdev_ep_t *ep, usbopt_ep_t opt, const void *value, size_t value_len)
Set an option value for a given usb device endpoint.
Definition usbdev.h:638
static usbdev_ep_t * usbdev_new_ep(usbdev_t *dev, usb_ep_type_t type, usb_ep_dir_t dir, size_t size)
Retrieve an USB endpoint of the specified type.
Definition usbdev.h:476
static void usbdev_init(usbdev_t *dev)
Initialize the USB peripheral device.
Definition usbdev.h:455
usbdev_event_t
List of event types that can be send from the device driver to the upper layer.
Definition usbdev.h:156
static int usbdev_ep_get(usbdev_ep_t *ep, usbopt_ep_t opt, void *value, size_t max_len)
Get an option value from a given usb device endpoint.
Definition usbdev.h:614
static void usbdev_ep_init(usbdev_ep_t *ep)
Initialize the USB endpoint.
Definition usbdev.h:571
static int usbdev_set(usbdev_t *dev, usbopt_t opt, const void *value, size_t value_len)
Set an option value for a given usb device.
Definition usbdev.h:520
static int usbdev_get(usbdev_t *dev, usbopt_t opt, void *value, size_t max_len)
Get an option value from a given usb device.
Definition usbdev.h:498
static void usbdev_ep0_stall(usbdev_t *dev)
Stall both OUT and IN packets on endpoint 0 until a setup packet is received.
Definition usbdev.h:555
usbdev_t * usbdev_get_ctx(unsigned num)
Retrieve usbdev context from the peripheral.
static void usbdev_ep_stall(usbdev_ep_t *ep, bool enable)
Enable or disable the stall condition on the USB endpoint.
Definition usbdev.h:591
void(* usbdev_ep_event_cb_t)(usbdev_ep_t *ep, usbdev_event_t event)
Event callback for signaling endpoint events to upper layers.
Definition usbdev.h:241
void(* usbdev_event_cb_t)(usbdev_t *usbdev, usbdev_event_t event)
Event callback for signaling usbdev event to upper layers.
Definition usbdev.h:233
static void usbdev_esr(usbdev_t *dev)
a driver's user-space event service handler
Definition usbdev.h:536
static int usbdev_ep_xmit(usbdev_ep_t *ep, uint8_t *buf, size_t len)
Submit a buffer for a USB endpoint transmission.
Definition usbdev.h:679
struct usbdev_driver usbdev_driver_t
usbdev driver functions
static void usbdev_ep_esr(usbdev_ep_t *ep)
an endpoint's user-space event handler
Definition usbdev.h:656
@ USBDEV_EVENT_SUSPEND
USB suspend condition active.
Definition usbdev.h:200
@ USBDEV_EVENT_TR_STALL
Transaction stall event.
Definition usbdev.h:222
@ USBDEV_EVENT_ESR
Driver needs it's ESR (event service routine) handled.
Definition usbdev.h:160
@ USBDEV_EVENT_HOST_DISCONNECT
Host disconnected from the device.
Definition usbdev.h:176
@ USBDEV_EVENT_RESUME
USB suspend condition no longer active.
Definition usbdev.h:205
@ USBDEV_EVENT_HOST_CONNECT
Host connection detected.
Definition usbdev.h:168
@ USBDEV_EVENT_TR_COMPLETE
Transaction completed event.
Definition usbdev.h:214
@ USBDEV_EVENT_RESET
Line reset occurred.
Definition usbdev.h:188
@ USBDEV_EVENT_SOF
Start of Frame received.
Definition usbdev.h:195
usbopt_t
List of configuration settings for USB peripherals.
Definition usbopt.h:37
usbopt_ep_t
List of configuration settings for USB peripheral endpoints.
Definition usbopt.h:91
usb_ep_dir_t
USB endpoint directions.
Definition usb.h:245
usb_ep_type_t
USB endpoint types.
Definition usb.h:234
event structure
Definition event.h:148
usbdev driver functions
Definition usbdev.h:273
int(* set)(usbdev_t *usbdev, usbopt_t opt, const void *value, size_t value_len)
Set an option value for a given usb device.
Definition usbdev.h:325
int(* ep_set)(usbdev_ep_t *ep, usbopt_ep_t opt, const void *value, size_t value_len)
Set an option value for a given usb device endpoint.
Definition usbdev.h:398
void(* ep_init)(usbdev_ep_t *ep)
Initialize the USB endpoint.
Definition usbdev.h:355
void(* ep_stall)(usbdev_ep_t *ep, bool enable)
Enable or disable the stall condition on the USB endpoint.
Definition usbdev.h:371
int(* xmit)(usbdev_ep_t *ep, uint8_t *buf, size_t len)
Transmit a data buffer.
Definition usbdev.h:427
void(* init)(usbdev_t *usbdev)
Initialize the USB peripheral device.
Definition usbdev.h:282
void(* esr)(usbdev_t *dev)
a driver's user-space event service handler
Definition usbdev.h:336
void(* ep_esr)(usbdev_ep_t *ep)
an endpoint's user-space event handler
Definition usbdev.h:409
void(* ep0_stall)(usbdev_t *usbdev)
Stall both OUT and IN packets on endpoint 0 until a setup packet is received.
Definition usbdev.h:345
usbdev_ep_t *(* new_ep)(usbdev_t *dev, usb_ep_type_t type, usb_ep_dir_t dir, size_t size)
Retrieve an USB endpoint of the specified type.
Definition usbdev.h:298
int(* ep_get)(usbdev_ep_t *ep, usbopt_ep_t opt, void *value, size_t max_len)
Get an option value from a given usb device endpoint.
Definition usbdev.h:384
int(* get)(usbdev_t *usbdev, usbopt_t opt, void *value, size_t max_len)
Get an option value from a given usb device.
Definition usbdev.h:311
usbdev endpoint descriptor
Definition usbdev.h:259
usbdev_t * dev
USB device this endpoint belongs to
Definition usbdev.h:260
uint8_t num
Endpoint number
Definition usbdev.h:264
size_t len
Endpoint configured max transfer size in bytes.
Definition usbdev.h:261
usb_ep_dir_t dir
Endpoint direction
Definition usbdev.h:262
usb_ep_type_t type
Endpoint type
Definition usbdev.h:263
usbdev device descriptor
Definition usbdev.h:247
usbdev_ep_event_cb_t epcb
Endpoint event callback for upper layer
Definition usbdev.h:251
void * context
Ptr to the thread context
Definition usbdev.h:253
usbdev_event_cb_t cb
Event callback supplied by upper layer
Definition usbdev.h:249
const struct usbdev_driver * driver
usbdev driver struct
Definition usbdev.h:248
Definition of global compile time configuration options.
Definition of global USB peripheral and USB peripheral endpoint configuration options.