libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
st_usbfs_v2.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) 2014 Kuldeep Singh Dhaka <kuldeepdhaka9@gmail.com>
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 "common/st_usbfs_core.h"
28
29/** Initialize the USB device controller hardware of the STM32. */
31{
36
37 /* Enable RESET, SUSPEND, RESUME and CTR interrupts. */
41 return &st_usbfs_dev;
42}
43
44void st_usbfs_copy_to_pm(volatile void *vPM, const void *buf, uint16_t len)
45{
46 /*
47 * This is a bytewise copy, so it always works, even on CM0(+)
48 * that don't support unaligned accesses.
49 */
50 const uint8_t *lbuf = buf;
51 volatile uint16_t *PM = vPM;
52 uint32_t i;
53 for (i = 0; i < len; i += 2) {
54 *PM++ = (uint16_t)lbuf[i+1] << 8 | lbuf[i];
55 }
56}
57
58/**
59 * Copy a data buffer from packet memory.
60 *
61 * @param buf Destination pointer for data buffer.
62 * @param vPM Source pointer into packet memory.
63 * @param len Number of bytes to copy.
64 */
65void st_usbfs_copy_from_pm(void *buf, const volatile void *vPM, uint16_t len)
66{
67 const volatile uint16_t *PM = vPM;
68 uint8_t odd = len & 1;
69 len >>= 1;
70
71 if (((uintptr_t) buf) & 0x01) {
72 for (; len; PM++, len--) {
73 uint16_t value = *PM;
74 *(uint8_t *) buf++ = value;
75 *(uint8_t *) buf++ = value >> 8;
76 }
77 } else {
78 for (; len; PM++, buf += 2, len--) {
79 *(uint16_t *) buf = *PM;
80 }
81 }
82
83 if (odd) {
84 *(uint8_t *) buf = *(uint8_t *) PM;
85 }
86}
87
88static void st_usbfs_v2_disconnect(usbd_device *usbd_dev, bool disconnected)
89{
90 (void)usbd_dev;
91 uint16_t reg = GET_REG(USB_BCDR_REG);
92 if (disconnected) {
94 } else {
96 }
97}
98
99const struct _usbd_driver st_usbfs_v2_usb_driver = {
100 .init = st_usbfs_v2_usbd_init,
101 .set_address = st_usbfs_set_address,
102 .ep_setup = st_usbfs_ep_setup,
103 .ep_reset = st_usbfs_endpoints_reset,
104 .ep_stall_set = st_usbfs_ep_stall_set,
105 .ep_stall_get = st_usbfs_ep_stall_get,
106 .ep_nak_set = st_usbfs_ep_nak_set,
107 .ep_write_packet = st_usbfs_ep_write_packet,
108 .ep_read_packet = st_usbfs_ep_read_packet,
109 .disconnect = st_usbfs_v2_disconnect,
110 .poll = st_usbfs_poll,
111};
void rcc_periph_clock_enable(enum rcc_periph_clken clken)
Enable Peripheral Clock in running mode.
@ RCC_USB
Definition: f0/rcc.h:487
#define USB_CNTR_SUSPM
#define USB_CNTR_REG
#define USB_CNTR_RESETM
#define USB_CNTR_WKUPM
#define USB_ISTR_REG
#define USB_CNTR_CTRM
#define USB_BTABLE_REG
const struct _usbd_driver st_usbfs_v2_usb_driver
Definition: st_usbfs_v2.c:99
struct _usbd_device usbd_device
Definition: usbd.h:53
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_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)
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_v2.c:44
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_v2.c:65
static usbd_device * st_usbfs_v2_usbd_init(void)
Initialize the USB device controller hardware of the STM32.
Definition: st_usbfs_v2.c:30
static void st_usbfs_v2_disconnect(usbd_device *usbd_dev, bool disconnected)
Definition: st_usbfs_v2.c:88
#define USB_BCDR_REG
Definition: st_usbfs_v2.h:45
#define USB_BCDR_DPPU
Definition: st_usbfs_v2.h:71
#define GET_REG(REG)
Definition: tools.h:28
#define SET_REG(REG, VAL)
Definition: tools.h:31