USB_VBUS is not an alternate function, it is an additionnal function which is always enabled. If configured as an alternate function, it will draw current from VBUS.
127 lines
3.4 KiB
C
127 lines
3.4 KiB
C
/*
|
|
* This file is part of the libopencm3 project.
|
|
*
|
|
* Copyright (C) 2013 Weston Schmidt <weston_schmidt@alumni.purdue.edu>
|
|
* Copyright (C) 2013 Pavol Rusnak <stick@gk2.sk>
|
|
*
|
|
* This library is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <libopencm3/stm32/rcc.h>
|
|
#include <libopencm3/stm32/gpio.h>
|
|
#include <libopencm3/usb/usbd.h>
|
|
#include <libopencm3/usb/msc.h>
|
|
|
|
#include "ramdisk.h"
|
|
|
|
static const struct usb_device_descriptor dev_descr = {
|
|
.bLength = USB_DT_DEVICE_SIZE,
|
|
.bDescriptorType = USB_DT_DEVICE,
|
|
.bcdUSB = 0x0110,
|
|
.bDeviceClass = 0,
|
|
.bDeviceSubClass = 0,
|
|
.bDeviceProtocol = 0,
|
|
.bMaxPacketSize0 = 64,
|
|
.idVendor = 0x0483,
|
|
.idProduct = 0x5741,
|
|
.bcdDevice = 0x0200,
|
|
.iManufacturer = 1,
|
|
.iProduct = 2,
|
|
.iSerialNumber = 3,
|
|
.bNumConfigurations = 1,
|
|
};
|
|
|
|
static const struct usb_endpoint_descriptor msc_endp[] = {{
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
.bEndpointAddress = 0x01,
|
|
.bmAttributes = USB_ENDPOINT_ATTR_BULK,
|
|
.wMaxPacketSize = 64,
|
|
.bInterval = 0,
|
|
}, {
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
.bEndpointAddress = 0x82,
|
|
.bmAttributes = USB_ENDPOINT_ATTR_BULK,
|
|
.wMaxPacketSize = 64,
|
|
.bInterval = 0,
|
|
}};
|
|
|
|
static const struct usb_interface_descriptor msc_iface[] = {{
|
|
.bLength = USB_DT_INTERFACE_SIZE,
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
.bInterfaceNumber = 0,
|
|
.bAlternateSetting = 0,
|
|
.bNumEndpoints = 2,
|
|
.bInterfaceClass = USB_CLASS_MSC,
|
|
.bInterfaceSubClass = USB_MSC_SUBCLASS_SCSI,
|
|
.bInterfaceProtocol = USB_MSC_PROTOCOL_BBB,
|
|
.iInterface = 0,
|
|
.endpoint = msc_endp,
|
|
.extra = NULL,
|
|
.extralen = 0
|
|
}};
|
|
|
|
static const struct usb_interface ifaces[] = {{
|
|
.num_altsetting = 1,
|
|
.altsetting = msc_iface,
|
|
}};
|
|
|
|
static const struct usb_config_descriptor config_descr = {
|
|
.bLength = USB_DT_CONFIGURATION_SIZE,
|
|
.bDescriptorType = USB_DT_CONFIGURATION,
|
|
.wTotalLength = 0,
|
|
.bNumInterfaces = 1,
|
|
.bConfigurationValue = 1,
|
|
.iConfiguration = 0,
|
|
.bmAttributes = 0x80,
|
|
.bMaxPower = 0x32,
|
|
.interface = ifaces,
|
|
};
|
|
|
|
static const char *usb_strings[] = {
|
|
"Black Sphere Technologies",
|
|
"MSC Demo",
|
|
"DEMO",
|
|
};
|
|
|
|
static usbd_device *msc_dev;
|
|
/* Buffer to be used for control requests. */
|
|
static uint8_t usbd_control_buffer[128];
|
|
|
|
int main(void)
|
|
{
|
|
rcc_clock_setup_hse_3v3(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_120MHZ]);
|
|
|
|
rcc_periph_clock_enable(RCC_GPIOA);
|
|
rcc_periph_clock_enable(RCC_OTGFS);
|
|
|
|
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12);
|
|
gpio_set_af(GPIOA, GPIO_AF10, GPIO11 | GPIO12);
|
|
|
|
msc_dev = usbd_init(&otgfs_usb_driver, &dev_descr, &config_descr,
|
|
usb_strings, 3,
|
|
usbd_control_buffer, sizeof(usbd_control_buffer));
|
|
|
|
ramdisk_init();
|
|
usb_msc_init(msc_dev, 0x82, 64, 0x01, 64, "VendorID", "ProductID",
|
|
"0.00", ramdisk_blocks(), ramdisk_read, ramdisk_write);
|
|
|
|
for (;;) {
|
|
usbd_poll(msc_dev);
|
|
}
|
|
}
|