Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mass_storage/debug.h
275 changes: 249 additions & 26 deletions demos/mass_storage_OLIMEX_STM32E407/main.c
Original file line number Diff line number Diff line change
@@ -1,40 +1,263 @@
#include "usb_msd.h"
#include "ch.h"
#include "hal.h"
#include <stdio.h>
#include <string.h>

#include "ch.h"
#include "hal.h"
/* endpoint index */
#define USB_MS_DATA_EP 1

#include "usb_msd.h"
/* USB device descriptor */
static const uint8_t deviceDescriptorData[] =
{
USB_DESC_DEVICE
(
0x0200, /* supported USB version (2.0) */
0x00, /* device class (none, specified in interface) */
0x00, /* device sub-class (none, specified in interface) */
0x00, /* device protocol (none, specified in interface) */
64, /* max packet size of control end-point */
0x0483, /* vendor ID (STMicroelectronics!) */
0x5740, /* product ID (STM32F407) */
0x0100, /* device release number */
1, /* index of manufacturer string descriptor */
2, /* index of product string descriptor */
3, /* index of serial number string descriptor */
1 /* number of possible configurations */
)
};
static const USBDescriptor deviceDescriptor =
{
sizeof(deviceDescriptorData),
deviceDescriptorData
};

/* configuration descriptor */
static const uint8_t configurationDescriptorData[] =
{
/* configuration descriptor */
USB_DESC_CONFIGURATION
(
32, /* total length */
1, /* number of interfaces */
1, /* value that selects this configuration */
0, /* index of string descriptor describing this configuration */
0xC0, /* attributes (self-powered) */
50 /* max power (100 mA) */
),

/* interface descriptor */
USB_DESC_INTERFACE
(
0, /* interface number */
0, /* value used to select alternative setting */
2, /* number of end-points used by this interface */
0x08, /* interface class (Mass Storage) */
0x06, /* interface sub-class (SCSI Transparent Storage) */
0x50, /* interface protocol (Bulk Only) */
0 /* index of string descriptor describing this interface */
),

/* end-point descriptor */
USB_DESC_ENDPOINT
(
USB_MS_DATA_EP | 0x80, /* address (end point index | OUT direction) */
USB_EP_MODE_TYPE_BULK, /* attributes (bulk) */
64, /* max packet size */
0x05 /* polling interval (ignored for bulk end-points) */
),

/* end-point descriptor */
USB_DESC_ENDPOINT
(
USB_MS_DATA_EP | 0x00, /* address (end point index | IN direction) */
USB_EP_MODE_TYPE_BULK, /* attributes (bulk) */
64, /* max packet size */
0x05 /* polling interval (ignored for bulk end-points) */
)
};
static const USBDescriptor configurationDescriptor =
{
sizeof(configurationDescriptorData),
configurationDescriptorData
};

/* Language descriptor */
static const uint8_t languageDescriptorData[] =
{
USB_DESC_BYTE(4),
USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
USB_DESC_WORD(0x0409) /* U.S. english */
};
static const USBDescriptor languageDescriptor =
{
sizeof(languageDescriptorData),
languageDescriptorData
};

/* Vendor descriptor */
static const uint8_t vendorDescriptorData[] =
{
USB_DESC_BYTE(22),
USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
'D', 0, 'e', 0, 'm', 0, 'o', 0, 'V', 0, 'e', 0, 'n', 0, 'd', 0, 'o', 0, 'r', 0
};
static const USBDescriptor vendorDescriptor =
{
sizeof(vendorDescriptorData),
vendorDescriptorData
};

/* Product descriptor */
static const uint8_t productDescriptorData[] =
{
USB_DESC_BYTE(24),
USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
'D', 0, 'e', 0, 'm', 0, 'o', 0, 'P', 0, 'r', 0, 'o', 0, 'd', 0, 'u', 0, 'c', 0, 't', 0
};
static const USBDescriptor productDescriptor =
{
sizeof(productDescriptorData),
productDescriptorData
};

/* Serial number descriptor */
static const uint8_t serialNumberDescriptorData[] =
{
USB_DESC_BYTE(26),
USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
'0', 0, '0', 0, '0', 0, '0', 0, '0', 0, '0', 0, '0', 0, '0', 0, '0', 0, '0', 0, '0', 0, '1', 0
};
static const USBDescriptor serialNumberDescriptor =
{
sizeof(serialNumberDescriptorData),
serialNumberDescriptorData
};

/* Handles GET_DESCRIPTOR requests from the USB host */
static const USBDescriptor* getDescriptor(USBDriver* usbp, uint8_t type, uint8_t index, uint16_t lang)
{
(void)usbp;
(void)lang;

/*
* Green LED blinker thread, times are in milliseconds.
*/
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {

(void)arg;
chRegSetThreadName("blinker");
while (TRUE) {
palTogglePad(GPIOC, GPIOC_LED);
chThdSleepMilliseconds(500);
}
switch (type)
{
case USB_DESCRIPTOR_DEVICE:
return &deviceDescriptor;

case USB_DESCRIPTOR_CONFIGURATION:
return &configurationDescriptor;

case USB_DESCRIPTOR_STRING:
switch (index)
{
case 0: return &languageDescriptor;
case 1: return &vendorDescriptor;
case 2: return &productDescriptor;
case 3: return &serialNumberDescriptor;
}
}

return 0;
}

/* Handles global events of the USB driver */
static void usbEvent(USBDriver* usbp, usbevent_t event)
{
switch (event)
{
case USB_EVENT_CONFIGURED:
chSysLockFromIsr();
msdConfigureHookI(usbp);
chSysUnlockFromIsr();
break;

case USB_EVENT_RESET:
case USB_EVENT_ADDRESS:
case USB_EVENT_SUSPEND:
case USB_EVENT_WAKEUP:
case USB_EVENT_STALLED:
default:
break;
}
}

/* Configuration of the USB driver */
const USBConfig usbConfig =
{
usbEvent,
getDescriptor,
msdRequestsHook,
0
};

/* Turns on a LED when there is I/O activity on the USB port */
static void usbActivity(bool_t active)
{
if (active)
palSetPad(GPIOC, GPIOC_LED);
else
palClearPad(GPIOC, GPIOC_LED);
}

int main(void) {
halInit();
chSysInit();
/* USB mass storage configuration */
static USBMassStorageConfig msdConfig =
{
&USBD2,
(BaseBlockDevice*)&SDCD1,
USB_MS_DATA_EP,
&usbActivity,
"DVendor",
"DProduct",
"0.1"
};

/* USB mass storage driver */
USBMassStorageDriver UMSD1;

int main(void)
{
/* system & hardware initialization */
halInit();
chSysInit();

sdcStart(&SDCD1, NULL);
sdcConnect(&SDCD1);
/* initialize the SD card */
sdcStart(&SDCD1, NULL);
sdcConnect(&SDCD1);

//palSetPad(GPIOC, GPIOC_LED);
/* turn off the test LED */
palClearPad(GPIOC, GPIOC_LED);

USBMassStorageDriver UMSD1;
msdInit(&USBD1, &SDCD1, &UMSD1);
/* initialize the USB mass storage driver */
msdInit(&UMSD1);

chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
/* start the USB mass storage service */
msdStart(&UMSD1, &msdConfig);

while (TRUE) {
/* start the USB driver */
usbDisconnectBus(&USBD2);
chThdSleepMilliseconds(1000);
}
usbStart(&USBD2, &usbConfig);
usbConnectBus(&USBD2);

/* watch the mass storage events */
EventListener connected;
EventListener ejected;
chEvtRegisterMask(&UMSD1.evt_connected, &connected, EVENT_MASK(1));
chEvtRegisterMask(&UMSD1.evt_ejected, &ejected, EVENT_MASK(2));

while (TRUE)
{
eventmask_t event = chEvtWaitOne(EVENT_MASK(1) | EVENT_MASK(2));
if (event == EVENT_MASK(1))
{
/* media connected */
}
else if (event == EVENT_MASK(2))
{
/* media ejected */
}
}

return 0;
}
2 changes: 0 additions & 2 deletions demos/mass_storage_OLIMEX_STM32E407/readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Mass Storage Demo for Olimex STM32E407

Uses SDC card + HS USB

Note: not yet functional
Loading