libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
usb.c
Go to the documentation of this file.
1/** @defgroup usb_drivers_file Generic USB Drivers
2
3@ingroup USB
4
5@brief <b>Generic USB Drivers</b>
6
7@version 1.0.0
8
9@author @htmlonly &copy; @endhtmlonly 2010
10Gareth McMullin <gareth@blacksphere.co.nz>
11
12@date 10 March 2013
13
14LGPL License Terms @ref lgpl_license
15*/
16
17/*
18 * This file is part of the libopencm3 project.
19 *
20 * Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
21 *
22 * This library is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU Lesser General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
26 *
27 * This library is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 *
32 * You should have received a copy of the GNU Lesser General Public License
33 * along with this library. If not, see <http://www.gnu.org/licenses/>.
34 */
35
36/**@{*/
37
38#include <string.h>
39#include <libopencm3/usb/usbd.h>
40#include "usb_private.h"
41
43 const struct usb_device_descriptor *dev,
44 const struct usb_config_descriptor *conf,
45 const char * const *strings, int num_strings,
46 uint8_t *control_buffer, uint16_t control_buffer_size)
47{
49
50 usbd_dev = driver->init();
51
52 usbd_dev->driver = driver;
53 usbd_dev->desc = dev;
54 usbd_dev->config = conf;
55 usbd_dev->strings = strings;
56 usbd_dev->num_strings = num_strings;
57 usbd_dev->extra_string_idx = 0;
58 usbd_dev->extra_string = NULL;
59 usbd_dev->ctrl_buf = control_buffer;
60 usbd_dev->ctrl_buf_len = control_buffer_size;
61
62 usbd_dev->user_callback_ctr[0][USB_TRANSACTION_SETUP] =
64 usbd_dev->user_callback_ctr[0][USB_TRANSACTION_OUT] =
66 usbd_dev->user_callback_ctr[0][USB_TRANSACTION_IN] =
68
69 int i;
70 for (i = 0; i < MAX_USER_SET_CONFIG_CALLBACK; i++) {
71 usbd_dev->user_callback_set_config[i] = NULL;
72 }
73
74 return usbd_dev;
75}
76
78{
79 usbd_dev->user_callback_reset = callback;
80}
81
83 void (*callback)(void))
84{
85 usbd_dev->user_callback_suspend = callback;
86}
87
89 void (*callback)(void))
90{
91 usbd_dev->user_callback_resume = callback;
92}
93
94void usbd_register_sof_callback(usbd_device *usbd_dev, void (*callback)(void))
95{
96 usbd_dev->user_callback_sof = callback;
97}
98
99void usbd_register_extra_string(usbd_device *usbd_dev, int index, const char* string)
100{
101 /*
102 * Note: string index 0 is reserved for LANGID requests and cannot
103 * be overwritten using this functionality.
104 */
105 if (string != NULL && index > 0) {
106 usbd_dev->extra_string_idx = index;
107 usbd_dev->extra_string = string;
108 } else {
109 usbd_dev->extra_string_idx = 0;
110 }
111}
112
114{
115 usbd_dev->current_address = 0;
116 usbd_dev->current_config = 0;
117 usbd_ep_setup(usbd_dev, 0, USB_ENDPOINT_ATTR_CONTROL, usbd_dev->desc->bMaxPacketSize0, NULL);
118 usbd_dev->driver->set_address(usbd_dev, 0);
119
120 if (usbd_dev->user_callback_reset) {
121 usbd_dev->user_callback_reset();
122 }
123}
124
125/* Functions to wrap the low-level driver */
127{
128 usbd_dev->driver->poll(usbd_dev);
129}
130
131__attribute__((weak)) void usbd_disconnect(usbd_device *usbd_dev,
132 bool disconnected)
133{
134 /* not all drivers support disconnection */
135 if (usbd_dev->driver->disconnect) {
136 usbd_dev->driver->disconnect(usbd_dev, disconnected);
137 }
138}
139
140void usbd_ep_setup(usbd_device *usbd_dev, uint8_t addr, uint8_t type,
141 uint16_t max_size, usbd_endpoint_callback callback)
142{
143 usbd_dev->driver->ep_setup(usbd_dev, addr, type, max_size, callback);
144}
145
147 const void *buf, uint16_t len)
148{
149 return usbd_dev->driver->ep_write_packet(usbd_dev, addr, buf, len);
150}
151
152uint16_t usbd_ep_read_packet(usbd_device *usbd_dev, uint8_t addr, void *buf,
153 uint16_t len)
154{
155 return usbd_dev->driver->ep_read_packet(usbd_dev, addr, buf, len);
156}
157
158void usbd_ep_stall_set(usbd_device *usbd_dev, uint8_t addr, uint8_t stall)
159{
160 usbd_dev->driver->ep_stall_set(usbd_dev, addr, stall);
161}
162
164{
165 return usbd_dev->driver->ep_stall_get(usbd_dev, addr);
166}
167
168void usbd_ep_nak_set(usbd_device *usbd_dev, uint8_t addr, uint8_t nak)
169{
170 usbd_dev->driver->ep_nak_set(usbd_dev, addr, nak);
171}
172
173/**@}*/
174
struct _usbd_device usbd_device
Definition: usbd.h:53
void(* usbd_endpoint_callback)(usbd_device *usbd_dev, uint8_t ep)
Definition: usbd.h:131
struct _usbd_driver usbd_driver
Definition: usbd.h:52
void usbd_register_resume_callback(usbd_device *usbd_dev, void(*callback)(void))
Registers a resume callback.
Definition: usb.c:88
void usbd_register_suspend_callback(usbd_device *usbd_dev, void(*callback)(void))
Registers a suspend callback.
Definition: usb.c:82
void usbd_register_reset_callback(usbd_device *usbd_dev, void(*callback)(void))
Registers a reset callback.
Definition: usb.c:77
void usbd_ep_nak_set(usbd_device *usbd_dev, uint8_t addr, uint8_t nak)
Set an Out endpoint to NAK.
Definition: usb.c:168
uint8_t usbd_ep_stall_get(usbd_device *usbd_dev, uint8_t addr)
Get STALL status of an endpoint.
Definition: usb.c:163
void _usbd_reset(usbd_device *usbd_dev)
Definition: usb.c:113
usbd_device * usbd_init(const usbd_driver *driver, const struct usb_device_descriptor *dev, const struct usb_config_descriptor *conf, const char *const *strings, int num_strings, uint8_t *control_buffer, uint16_t control_buffer_size)
Main initialization entry point.
Definition: usb.c:42
void usbd_ep_stall_set(usbd_device *usbd_dev, uint8_t addr, uint8_t stall)
Set/clear STALL condition on an endpoint.
Definition: usb.c:158
void usbd_poll(usbd_device *usbd_dev)
Definition: usb.c:126
uint16_t usbd_ep_write_packet(usbd_device *usbd_dev, uint8_t addr, const void *buf, uint16_t len)
Write a packet.
Definition: usb.c:146
void usbd_ep_setup(usbd_device *usbd_dev, uint8_t addr, uint8_t type, uint16_t max_size, usbd_endpoint_callback callback)
Setup an endpoint.
Definition: usb.c:140
uint16_t usbd_ep_read_packet(usbd_device *usbd_dev, uint8_t addr, void *buf, uint16_t len)
Read a packet.
Definition: usb.c:152
void usbd_register_sof_callback(usbd_device *usbd_dev, void(*callback)(void))
Registers a SOF callback.
Definition: usb.c:94
void usbd_register_extra_string(usbd_device *usbd_dev, int index, const char *string)
Registers a non-contiguous string descriptor.
Definition: usb.c:99
void usbd_disconnect(usbd_device *usbd_dev, bool disconnected)
Disconnect, if supported by the driver.
Definition: usb.c:131
#define USB_ENDPOINT_ATTR_CONTROL
Definition: usbstd.h:232
void _usbd_control_out(usbd_device *usbd_dev, uint8_t ea)
Definition: usb_control.c:240
void _usbd_control_in(usbd_device *usbd_dev, uint8_t ea)
Definition: usb_control.c:286
void _usbd_control_setup(usbd_device *usbd_dev, uint8_t ea)
Definition: usb_control.c:222
static struct _usbd_device usbd_dev
Definition: usb_f107.c:34