libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
st_usbfs_core.c
Go to the documentation of this file.
1/*
2 * This file is part of the libopencm3 project.
3 *
4 * Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
5 * Copyright (C) 2015 Robin Kreis <r.kreis@uni-bremen.de>
6 *
7 * This library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20
25#include <libopencm3/usb/usbd.h>
26#include "../../usb/usb_private.h"
27#include "st_usbfs_core.h"
28
29/* TODO - can't these be inside the impls, not globals from the core? */
31struct _usbd_device st_usbfs_dev;
32
33void st_usbfs_set_address(usbd_device *dev, uint8_t addr)
34{
35 (void)dev;
36 /* Set device address and enable. */
38}
39
40/**
41 * Set the receive buffer size for a given USB endpoint.
42 *
43 * @param dev the usb device handle returned from @ref usbd_init
44 * @param ep Index of endpoint to configure.
45 * @param size Size in bytes of the RX buffer. Legal sizes : {2,4,6...62}; {64,96,128...992}.
46 * @returns (uint16) Actual size set
47 */
48uint16_t st_usbfs_set_ep_rx_bufsize(usbd_device *dev, uint8_t ep, uint32_t size)
49{
50 uint16_t realsize;
51 (void)dev;
52 /*
53 * Writes USB_COUNTn_RX reg fields : bits <14:10> are NUM_BLOCK; bit 15 is BL_SIZE
54 * - When (size <= 62), BL_SIZE is set to 0 and NUM_BLOCK set to (size / 2).
55 * - When (size > 62), BL_SIZE is set to 1 and NUM_BLOCK=((size / 32) - 1).
56 *
57 * This algo rounds to the next largest legal buffer size, except 0. Examples:
58 * size => BL_SIZE, NUM_BLOCK => Actual bufsize
59 * 0 0 0 ??? "Not allowed" according to RM0091, RM0008
60 * 1 0 1 2
61 * 61 0 31 62
62 * 63 1 1 64
63 */
64 if (size > 62) {
65 /* Round up, div by 32 and sub 1 == (size + 31)/32 - 1 == (size-1)/32)*/
66 size = ((size - 1) >> 5) & 0x1F;
67 realsize = (size + 1) << 5;
68 /* Set BL_SIZE bit (no macro for this) */
69 size |= (1<<5);
70 } else {
71 /* round up and div by 2 */
72 size = (size + 1) >> 1;
73 realsize = size << 1;
74 }
75 /* write to the BL_SIZE and NUM_BLOCK fields */
76 USB_SET_EP_RX_COUNT(ep, size << 10);
77 return realsize;
78}
79
80void st_usbfs_ep_setup(usbd_device *dev, uint8_t addr, uint8_t type,
81 uint16_t max_size,
82 void (*callback) (usbd_device *usbd_dev,
83 uint8_t ep))
84{
85 /* Translate USB standard type codes to STM32. */
86 const uint16_t typelookup[] = {
91 };
92 uint8_t dir = addr & 0x80;
93 addr &= 0x7f;
94
95 /* Assign address. */
96 USB_SET_EP_ADDR(addr, addr);
97 USB_SET_EP_TYPE(addr, typelookup[type]);
98
99 if (dir || (addr == 0)) {
100 USB_SET_EP_TX_ADDR(addr, dev->pm_top);
101 if (callback) {
102 dev->user_callback_ctr[addr][USB_TRANSACTION_IN] =
103 (void *)callback;
104 }
105 USB_CLR_EP_TX_DTOG(addr);
107 dev->pm_top += max_size;
108 }
109
110 if (!dir) {
111 uint16_t realsize;
112 USB_SET_EP_RX_ADDR(addr, dev->pm_top);
113 realsize = st_usbfs_set_ep_rx_bufsize(dev, addr, max_size);
114 if (callback) {
115 dev->user_callback_ctr[addr][USB_TRANSACTION_OUT] =
116 (void *)callback;
117 }
118 USB_CLR_EP_RX_DTOG(addr);
120 dev->pm_top += realsize;
121 }
122}
123
125{
126 int i;
127
128 /* Reset all endpoints. */
129 for (i = 1; i < 8; i++) {
132 }
133 dev->pm_top = USBD_PM_TOP + (2 * dev->desc->bMaxPacketSize0);
134}
135
136void st_usbfs_ep_stall_set(usbd_device *dev, uint8_t addr,
137 uint8_t stall)
138{
139 (void)dev;
140 if (addr == 0) {
143 }
144
145 if (addr & 0x80) {
146 addr &= 0x7F;
147
150
151 /* Reset to DATA0 if clearing stall condition. */
152 if (!stall) {
153 USB_CLR_EP_TX_DTOG(addr);
154 }
155 } else {
156 /* Reset to DATA0 if clearing stall condition. */
157 if (!stall) {
158 USB_CLR_EP_RX_DTOG(addr);
159 }
160
163 }
164}
165
166uint8_t st_usbfs_ep_stall_get(usbd_device *dev, uint8_t addr)
167{
168 (void)dev;
169 if (addr & 0x80) {
170 if ((*USB_EP_REG(addr & 0x7F) & USB_EP_TX_STAT) ==
172 return 1;
173 }
174 } else {
175 if ((*USB_EP_REG(addr) & USB_EP_RX_STAT) ==
177 return 1;
178 }
179 }
180 return 0;
181}
182
183void st_usbfs_ep_nak_set(usbd_device *dev, uint8_t addr, uint8_t nak)
184{
185 (void)dev;
186 /* It does not make sense to force NAK on IN endpoints. */
187 if (addr & 0x80) {
188 return;
189 }
190
191 st_usbfs_force_nak[addr] = nak;
192
193 if (nak) {
195 } else {
197 }
198}
199
200uint16_t st_usbfs_ep_write_packet(usbd_device *dev, uint8_t addr,
201 const void *buf, uint16_t len)
202{
203 (void)dev;
204 addr &= 0x7F;
205
207 return 0;
208 }
209
211 USB_SET_EP_TX_COUNT(addr, len);
213
214 return len;
215}
216
217uint16_t st_usbfs_ep_read_packet(usbd_device *dev, uint8_t addr,
218 void *buf, uint16_t len)
219{
220 (void)dev;
222 return 0;
223 }
224
225 len = MIN(USB_GET_EP_RX_COUNT(addr) & 0x3ff, len);
227 USB_CLR_EP_RX_CTR(addr);
228
229 if (!st_usbfs_force_nak[addr]) {
231 }
232
233 return len;
234}
235
237{
238 uint16_t istr = *USB_ISTR_REG;
239
240 if (istr & USB_ISTR_RESET) {
242 dev->pm_top = USBD_PM_TOP;
243 _usbd_reset(dev);
244 return;
245 }
246
247 if (istr & USB_ISTR_CTR) {
248 uint8_t ep = istr & USB_ISTR_EP_ID;
249 uint8_t type;
250
251 if (istr & USB_ISTR_DIR) {
252 /* OUT or SETUP? */
253 if (*USB_EP_REG(ep) & USB_EP_SETUP) {
254 type = USB_TRANSACTION_SETUP;
255 st_usbfs_ep_read_packet(dev, ep, &dev->control_state.req, 8);
256 } else {
257 type = USB_TRANSACTION_OUT;
258 }
259 } else {
260 type = USB_TRANSACTION_IN;
262 }
263
264 if (dev->user_callback_ctr[ep][type]) {
265 dev->user_callback_ctr[ep][type] (dev, ep);
266 } else {
268 }
269 }
270
271 if (istr & USB_ISTR_SUSP) {
273 if (dev->user_callback_suspend) {
274 dev->user_callback_suspend();
275 }
276 }
277
278 if (istr & USB_ISTR_WKUP) {
280 if (dev->user_callback_resume) {
281 dev->user_callback_resume();
282 }
283 }
284
285 if (istr & USB_ISTR_SOF) {
287 if (dev->user_callback_sof) {
288 dev->user_callback_sof();
289 }
290 }
291
292 if (dev->user_callback_sof) {
294 } else {
295 *USB_CNTR_REG &= ~USB_CNTR_SOFM;
296 }
297}
#define USB_CLR_EP_TX_CTR(EP)
#define USB_EP_TX_STAT_DISABLED
#define USB_DADDR_REG
#define USB_EP_RX_STAT
#define USB_EP_TYPE_CONTROL
#define USB_CNTR_REG
#define USB_EP_TX_STAT_NAK
#define USB_EP_TX_STAT_STALL
#define USB_EP_TX_STAT
#define USB_GET_EP_RX_COUNT(EP)
#define USB_SET_EP_RX_STAT(EP, STAT)
#define USB_CLR_ISTR_SUSP()
#define USB_EP_SETUP
#define USB_EP_RX_STAT_VALID
#define USB_DADDR_ADDR
#define USB_EP_RX_STAT_STALL
#define USB_SET_EP_TX_STAT(EP, STAT)
#define USB_SET_EP_RX_ADDR(EP, ADDR)
#define USB_EP_REG(EP)
#define USB_CLR_ISTR_WKUP()
#define USB_ISTR_EP_ID
#define USB_ISTR_DIR
#define USB_ISTR_WKUP
#define USB_SET_EP_ADDR(EP, ADDR)
#define USB_EP_TYPE_BULK
#define USB_EP_TYPE_INTERRUPT
#define USB_ISTR_SOF
#define USB_ISTR_RESET
#define USB_EP_RX_STAT_NAK
#define USB_CLR_EP_TX_DTOG(EP)
#define USB_ISTR_SUSP
#define USB_SET_EP_TX_ADDR(EP, ADDR)
#define USB_CLR_ISTR_RESET()
#define USB_SET_EP_TX_COUNT(EP, COUNT)
#define USB_CLR_EP_RX_DTOG(EP)
#define USB_EP_RX_STAT_DISABLED
#define USB_EP_TYPE_ISO
#define USB_DADDR_EF
#define USB_CNTR_SOFM
#define USB_SET_EP_TYPE(EP, TYPE)
#define USB_ISTR_REG
#define USB_CLR_EP_RX_CTR(EP)
#define USB_CLR_ISTR_SOF()
#define USB_EP_TX_STAT_VALID
#define USB_ISTR_CTR
#define USB_SET_EP_RX_COUNT(EP, COUNT)
struct _usbd_device usbd_device
Definition: usbd.h:53
void _usbd_reset(usbd_device *usbd_dev)
Definition: usb.c:113
#define USB_ENDPOINT_ATTR_BULK
Definition: usbstd.h:234
#define USB_ENDPOINT_ATTR_INTERRUPT
Definition: usbstd.h:235
#define USB_ENDPOINT_ATTR_CONTROL
Definition: usbstd.h:232
#define USB_ENDPOINT_ATTR_ISOCHRONOUS
Definition: usbstd.h:233
void st_usbfs_poll(usbd_device *dev)
void st_usbfs_ep_nak_set(usbd_device *dev, uint8_t addr, uint8_t nak)
uint16_t st_usbfs_ep_write_packet(usbd_device *dev, uint8_t addr, const void *buf, uint16_t len)
struct _usbd_device st_usbfs_dev
Definition: st_usbfs_core.c:31
void st_usbfs_ep_setup(usbd_device *dev, uint8_t addr, uint8_t type, uint16_t max_size, void(*callback)(usbd_device *usbd_dev, uint8_t ep))
Definition: st_usbfs_core.c:80
uint8_t st_usbfs_force_nak[8]
Definition: st_usbfs_core.c:30
uint8_t st_usbfs_ep_stall_get(usbd_device *dev, uint8_t addr)
void st_usbfs_endpoints_reset(usbd_device *dev)
uint16_t st_usbfs_ep_read_packet(usbd_device *dev, uint8_t addr, void *buf, uint16_t len)
uint16_t st_usbfs_set_ep_rx_bufsize(usbd_device *dev, uint8_t ep, uint32_t size)
Set the receive buffer size for a given USB endpoint.
Definition: st_usbfs_core.c:48
void st_usbfs_ep_stall_set(usbd_device *dev, uint8_t addr, uint8_t stall)
void st_usbfs_set_address(usbd_device *dev, uint8_t addr)
Definition: st_usbfs_core.c:33
void st_usbfs_copy_to_pm(volatile void *vPM, const void *buf, uint16_t len)
Definition: st_usbfs_v1.c:57
void st_usbfs_copy_from_pm(void *buf, const volatile void *vPM, uint16_t len)
Copy a data buffer from packet memory.
Definition: st_usbfs_v1.c:73
#define USB_GET_EP_TX_BUFF(EP)
Definition: st_usbfs_v1.h:55
#define USB_GET_EP_RX_BUFF(EP)
Definition: st_usbfs_v1.h:58
#define SET_REG(REG, VAL)
Definition: tools.h:31