C8051F32x firmware infrastructure

Sign in or create your account | Project List | Help

C8051F32x firmware infrastructure Git Source Tree

Root/fw/common/usb.h

1/*
2 * common/usb.h - USB hardware setup and standard device requests
3 *
4 * Written 2008, 2009 by Werner Almesberger
5 * Copyright 2008, 2009 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#ifndef USB_H
15#define USB_H
16
17
18#include <stdint.h>
19
20#include "config.h"
21
22
23/*
24 * Descriptor types
25 *
26 * Reuse libusb naming scheme (/usr/include/usb.h)
27 */
28
29#define USB_DT_DEVICE 1
30#define USB_DT_CONFIG 2
31#define USB_DT_STRING 3
32#define USB_DT_INTERFACE 4
33#define USB_DT_ENDPOINT 5
34
35/*
36 * Device classes
37 *
38 * Reuse libusb naming scheme (/usr/include/usb.h)
39 */
40
41#define USB_CLASS_PER_INTERFACE 0xfe
42#define USB_CLASS_VENDOR_SPEC 0xff
43
44/*
45 * Configuration attributes
46 */
47
48#define USB_ATTR_BUS_POWERED 0x80
49#define USB_ATTR_SELF_POWERED 0x40
50#define USB_ATTR_REMOTE_WAKEUP 0x20
51
52/*
53 * Setup request types
54 */
55
56#define TO_DEVICE(req) (0x00 | (req) << 8)
57#define FROM_DEVICE(req) (0x80 | (req) << 8)
58#define TO_INTERFACE(req) (0x01 | (req) << 8)
59#define FROM_INTERFACE(req) (0x81 | (req) << 8)
60#define TO_ENDPOINT(req) (0x02 | (req) << 8)
61#define FROM_ENDPOINT(req) (0x82 | (req) << 8)
62
63/*
64 * Setup requests
65 */
66
67#define GET_STATUS 0x00
68#define CLEAR_FEATURE 0x01
69#define SET_FEATURE 0x03
70#define SET_ADDRESS 0x05
71#define GET_DESCRIPTOR 0x06
72#define SET_DESCRIPTOR 0x07
73#define GET_CONFIGURATION 0x08
74#define SET_CONFIGURATION 0x09
75#define GET_INTERFACE 0x0a
76#define SET_INTERFACE 0x0b
77#define SYNCH_FRAME 0x0c
78
79
80/*
81 * Odd. sdcc seems to think "x" assumes the size of the destination, i.e.,
82 * uint8_t. Hence the cast.
83 */
84
85#define LE(x) ((uint16_t) (x) & 0xff), ((uint16_t) (x) >> 8)
86
87#define LO(x) (((uint8_t *) &(x))[0])
88#define HI(x) (((uint8_t *) &(x))[1])
89
90
91#ifdef LOW_SPEED
92#define EP0_SIZE 8
93#else
94#define EP0_SIZE 64
95#endif
96
97#define EP1_SIZE 64 /* simplify */
98
99
100enum ep_state {
101    EP_IDLE,
102    EP_RX,
103    EP_TX,
104    EP_STALL,
105};
106
107struct ep_descr {
108    enum ep_state state;
109    uint8_t *buf;
110    uint8_t *end;
111    void (*callback)(void *user) __reentrant;
112    void *user;
113};
114
115struct setup_request {
116    uint8_t bmRequestType;
117    uint8_t bRequest;
118    uint16_t wValue;
119    uint16_t wIndex;
120    uint16_t wLength;
121};
122
123
124extern const uint8_t device_descriptor[];
125extern const uint8_t config_descriptor[];
126extern __xdata struct ep_descr ep0;
127#ifdef CONFIG_EP1
128extern __xdata struct ep_descr ep1in, ep1out;
129#endif
130
131extern __bit (*user_setup)(struct setup_request *setup) __reentrant;
132extern __bit (*user_get_descriptor)(uint8_t type, uint8_t index,
133    const uint8_t * const *reply, uint8_t *size) __reentrant;
134extern void (*user_reset)(void) __reentrant;
135
136
137#define usb_left(ep) ((ep)->end-(ep)->buf)
138#define usb_send(ep, buf, size, callback, user) \
139    usb_io(ep, EP_TX, buf, size, callback, user)
140#define usb_recv(ep, buf, size, callback, user) \
141    usb_io(ep, EP_RX, buf, size, callback, user)
142
143void usb_io(struct ep_descr *ep, enum ep_state state, uint8_t *buf,
144    uint8_t size, void (*callback)(void *user), void *user);
145
146
147void usb_init(void);
148void usb_poll(void);
149
150#endif /* !USB_H */
151

Archive Download this file

Branches:
master



interactive