Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
524 lines (456 sloc) 13.9 KB
/*
* This file is part of the multistomp project.
*
* Copyright (C) 2014 Daniel Thompson <daniel@redfelineninja.org.uk>
*
* 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 <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/usb/usbd.h>
#include <libopencm3/usb/audio.h>
#include <libopencm3/usb/midi.h>
#include <libopencm3/cm3/scb.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/systick.h>
#include <librfn/fibre.h>
#include <librfn/time.h>
#include <librfn/util.h>
/*
* All references in this file come from Universal Serial Bus Device Class
* Definition for MIDI Devices, release 1.0.
*/
/*
* Table B-1: MIDI Adapter Device Descriptor
*/
static const struct usb_device_descriptor dev = {
.bLength = USB_DT_DEVICE_SIZE,
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = 0x0200, /* was 0x0110 */
.bDeviceClass = 0, /* device defined at interface level */
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.bMaxPacketSize0 = 64,
.idVendor = 0x6666, /* Prototype product vendor ID */
.idProduct = 0x5119, /* dd if=/dev/random bs=2 count=1 | hexdump */
.bcdDevice = 0x0100,
.iManufacturer = 1, /* index to string desc */
.iProduct = 2, /* index to string desc */
.iSerialNumber = 0,
.bNumConfigurations = 1,
};
/*
* Midi specific endpoint descriptors.
*/
static const struct usb_midi_endpoint_descriptor midi_bulk_endp[] = {{
/* Table B-12: MIDI Adapter Class-specific Bulk OUT Endpoint
* Descriptor
*/
.head = {
.bLength = sizeof(struct usb_midi_endpoint_descriptor),
.bDescriptorType = USB_AUDIO_DT_CS_ENDPOINT,
.bDescriptorSubType = USB_MIDI_SUBTYPE_MS_GENERAL,
.bNumEmbMIDIJack = 1,
},
.jack[0] = {
.baAssocJackID = 0x01,
},
}, {
/* Table B-14: MIDI Adapter Class-specific Bulk IN Endpoint
* Descriptor
*/
.head = {
.bLength = sizeof(struct usb_midi_endpoint_descriptor),
.bDescriptorType = USB_AUDIO_DT_CS_ENDPOINT,
.bDescriptorSubType = USB_MIDI_SUBTYPE_MS_GENERAL,
.bNumEmbMIDIJack = 1,
},
.jack[0] = {
.baAssocJackID = 0x03,
},
} };
/*
* Standard endpoint descriptors
*/
static const struct usb_endpoint_descriptor bulk_endp[] = {{
/* Table B-11: MIDI Adapter Standard Bulk OUT Endpoint Descriptor */
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 0x01,
.bmAttributes = USB_ENDPOINT_ATTR_BULK,
.wMaxPacketSize = 0x40,
.bInterval = 0x00,
.extra = &midi_bulk_endp[0],
.extralen = sizeof(midi_bulk_endp[0])
}, {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 0x81,
.bmAttributes = USB_ENDPOINT_ATTR_BULK,
.wMaxPacketSize = 0x40,
.bInterval = 0x00,
.extra = &midi_bulk_endp[1],
.extralen = sizeof(midi_bulk_endp[1])
} };
/*
* Table B-4: MIDI Adapter Class-specific AC Interface Descriptor
*/
static const struct {
struct usb_audio_header_descriptor_head header_head;
struct usb_audio_header_descriptor_body header_body;
} __attribute__((packed)) audio_control_functional_descriptors = {
.header_head = {
.bLength = sizeof(struct usb_audio_header_descriptor_head) +
1 * sizeof(struct usb_audio_header_descriptor_body),
.bDescriptorType = USB_AUDIO_DT_CS_INTERFACE,
.bDescriptorSubtype = USB_AUDIO_TYPE_HEADER,
.bcdADC = 0x0100,
.wTotalLength =
sizeof(struct usb_audio_header_descriptor_head) +
1 * sizeof(struct usb_audio_header_descriptor_body),
.binCollection = 1,
},
.header_body = {
.baInterfaceNr = 0x01,
},
};
/*
* Table B-3: MIDI Adapter Standard AC Interface Descriptor
*/
static const struct usb_interface_descriptor audio_control_iface[] = {{
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 0,
.bInterfaceClass = USB_CLASS_AUDIO,
.bInterfaceSubClass = USB_AUDIO_SUBCLASS_CONTROL,
.bInterfaceProtocol = 0,
.iInterface = 0,
.extra = &audio_control_functional_descriptors,
.extralen = sizeof(audio_control_functional_descriptors)
} };
/*
* Class-specific MIDI streaming interface descriptor
*/
static const struct {
struct usb_midi_header_descriptor header;
struct usb_midi_in_jack_descriptor in_embedded;
struct usb_midi_in_jack_descriptor in_external;
struct usb_midi_out_jack_descriptor out_embedded;
struct usb_midi_out_jack_descriptor out_external;
} __attribute__((packed)) midi_streaming_functional_descriptors = {
/* Table B-6: Midi Adapter Class-specific MS Interface Descriptor */
.header = {
.bLength = sizeof(struct usb_midi_header_descriptor),
.bDescriptorType = USB_AUDIO_DT_CS_INTERFACE,
.bDescriptorSubtype = USB_MIDI_SUBTYPE_MS_HEADER,
.bcdMSC = 0x0100,
.wTotalLength = sizeof(midi_streaming_functional_descriptors),
},
/* Table B-7: MIDI Adapter MIDI IN Jack Descriptor (Embedded) */
.in_embedded = {
.bLength = sizeof(struct usb_midi_in_jack_descriptor),
.bDescriptorType = USB_AUDIO_DT_CS_INTERFACE,
.bDescriptorSubtype = USB_MIDI_SUBTYPE_MIDI_IN_JACK,
.bJackType = USB_MIDI_JACK_TYPE_EMBEDDED,
.bJackID = 0x01,
.iJack = 0x00,
},
/* Table B-8: MIDI Adapter MIDI IN Jack Descriptor (External) */
.in_external = {
.bLength = sizeof(struct usb_midi_in_jack_descriptor),
.bDescriptorType = USB_AUDIO_DT_CS_INTERFACE,
.bDescriptorSubtype = USB_MIDI_SUBTYPE_MIDI_IN_JACK,
.bJackType = USB_MIDI_JACK_TYPE_EXTERNAL,
.bJackID = 0x02,
.iJack = 0x00,
},
/* Table B-9: MIDI Adapter MIDI OUT Jack Descriptor (Embedded) */
.out_embedded = {
.head = {
.bLength = sizeof(struct usb_midi_out_jack_descriptor),
.bDescriptorType = USB_AUDIO_DT_CS_INTERFACE,
.bDescriptorSubtype = USB_MIDI_SUBTYPE_MIDI_IN_JACK,
.bJackType = USB_MIDI_JACK_TYPE_EMBEDDED,
.bJackID = 0x03,
.bNrInputPins = 1,
},
.source[0] = {
.baSourceID = 0x02,
.baSourcePin = 0x01,
},
.tail = {
.iJack = 0x00,
}
},
/* Table B-10: MIDI Adapter MIDI OUT Jack Descriptor (External) */
.out_external = {
.head = {
.bLength = sizeof(struct usb_midi_out_jack_descriptor),
.bDescriptorType = USB_AUDIO_DT_CS_INTERFACE,
.bDescriptorSubtype = USB_MIDI_SUBTYPE_MIDI_IN_JACK,
.bJackType = USB_MIDI_JACK_TYPE_EXTERNAL,
.bJackID = 0x04,
.bNrInputPins = 1,
},
.source[0] = {
.baSourceID = 0x01,
.baSourcePin = 0x01,
},
.tail = {
.iJack = 0x00,
},
},
};
/*
* Table B-5: MIDI Adapter Standard MS Interface Descriptor
*/
static const struct usb_interface_descriptor midi_streaming_iface[] = {{
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 1,
.bAlternateSetting = 0,
.bNumEndpoints = 2,
.bInterfaceClass = USB_CLASS_AUDIO,
.bInterfaceSubClass = USB_AUDIO_SUBCLASS_MIDISTREAMING,
.bInterfaceProtocol = 0,
.iInterface = 0,
.endpoint = bulk_endp,
.extra = &midi_streaming_functional_descriptors,
.extralen = sizeof(midi_streaming_functional_descriptors)
} };
static const struct usb_interface ifaces[] = {{
.num_altsetting = 1,
.altsetting = audio_control_iface,
}, {
.num_altsetting = 1,
.altsetting = midi_streaming_iface,
} };
/*
* Table B-2: MIDI Adapter Configuration Descriptor
*/
static const struct usb_config_descriptor config = {
.bLength = USB_DT_CONFIGURATION_SIZE,
.bDescriptorType = USB_DT_CONFIGURATION,
.wTotalLength = 0, /* ?automatically calculated? */
.bNumInterfaces = 2, /* control and data */
.bConfigurationValue = 1,
.iConfiguration = 0,
.bmAttributes = 0x80, /* bus powered */
.bMaxPower = 0x32,
.interface = ifaces,
};
static const char * usb_strings[] = {
"redfelineninja.org.uk",
"multistomp",
};
/* Buffer to be used for control requests. */
uint8_t usbd_control_buffer[128];
/* SysEx BOOTLOAD message, preformatted with correct USB framing information */
const uint8_t bootload_message[] = {
0x04, 0xf0, 0x60, 0x07, 0x07, 0x10, 0x4d, 0xf7
};
/* SysEx identity message, preformatted with correct USB framing information */
const uint8_t sysex_identity[] = {
0x04, /* USB Framing (3 byte SysEx) */
0xf0, /* SysEx start */
0x7e, /* non-realtime */
0x00, /* Channel 0 */
0x04, /* USB Framing (3 byte SysEx) */
0x7d, /* Educational/prototype manufacturer ID */
0x66, /* Family code (byte 1) */
0x66, /* Family code (byte 2) */
0x04, /* USB Framing (3 byte SysEx) */
0x51, /* Model number (byte 1) */
0x19, /* Model number (byte 2) */
0x00, /* Version number (byte 1) */
0x04, /* USB Framing (3 byte SysEx) */
0x00, /* Version number (byte 2) */
0x01, /* Version number (byte 3) */
0x00, /* Version number (byte 4) */
0x05, /* USB Framing (1 byte SysEx) */
0xf7, /* SysEx end */
0x00, /* Padding */
0x00, /* Padding */
};
static void jump_to_bootloader(void)
{
char * const marker = (char *)0x20004800; /* RAM@18K */
const char key[] = "remain-in-loader";
memcpy(marker, key, sizeof(key));
scb_reset_system(); /* Will never return. */
}
static void usbmidi_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
{
(void)ep;
char buf[64];
int len = usbd_ep_read_packet(usbd_dev, 0x01, buf, 64);
/* This implementation treats any message from the host as a SysEx
* identity request. This works well enough providing the host
* packs the identify request in a single 8 byte USB message.
*/
if (len) {
if (0 ==
memcmp(buf, bootload_message, sizeof(bootload_message))) {
jump_to_bootloader();
}
while (usbd_ep_write_packet(usbd_dev, 0x81, sysex_identity,
sizeof(sysex_identity)) == 0);
}
}
static void usbmidi_set_config(usbd_device *usbd_dev, uint16_t wValue)
{
(void)wValue;
usbd_ep_setup(usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64,
usbmidi_data_rx_cb);
usbd_ep_setup(usbd_dev, 0x81, USB_ENDPOINT_ATTR_BULK, 64, NULL);
}
static int button_send_event(usbd_device *usbd_dev, int button, bool pressed)
{
char buf[4] = { 0x0b, /* USB framing: virtual cable 0, control change */
0xb0, /* MIDI command: control change, channel 1 */
80, /* General purpose #5 (switch) */
};
// change the CC affect issued
buf[2] += button;
// Set value of CC (pressed or not pressed)
buf[3] = 0x7f * pressed;
uint32_t t = time_now() + 100000;
while (usbd_ep_write_packet(usbd_dev, 0x81, buf, sizeof(buf)) == 0) {
if (cyclecmp32(t, time_now()) < 0) {
return -1;
}
}
return 0;
}
static void button_poll(usbd_device *usbd_dev)
{
static uint16_t button_state[4];
int i;
const uint32_t pin[4] = { GPIO8, GPIO9, GPIO10, GPIO3 };
const uint32_t led[4] = { GPIO4, GPIO5, GPIO6, GPIO7 };
for (i=0; i<4; i++) {
/* This is a simple shift based debounce without any noise
* suppression.
*/
uint16_t old_button_state = button_state[i];
button_state[i] =
(button_state[i] << 1) | !(GPIOA_IDR & pin[i]);
if ((0 == button_state[i]) != (0 == old_button_state)) {
/* blink light until we manage to send data */
do {
gpio_toggle(GPIOA, led[i]);
} while (0 != button_send_event(usbd_dev, i,
!!button_state[i]));
/* ensure the LED has the right state */
if (button_state[i]) {
gpio_set(GPIOA, led[i]);
} else {
gpio_clear(GPIOA, led[i]);
}
}
}
}
static usbd_device *usbd_dev;
static int button_fibre(fibre_t *fibre)
{
static uint32_t time;
PT_BEGIN_FIBRE(fibre);
time = time_now();
while (true) {
button_poll(usbd_dev);
time += 1000;
PT_WAIT_UNTIL(fibre_timeout(time));
}
PT_END();
}
static fibre_t button_task = FIBRE_VAR_INIT(button_fibre);
struct wiggle_data {
fibre_t fibre;
int i;
int32_t time;
};
/*!
* Show a "splash screen" by wiggling the LEDs
*/
static int wiggle_fibre(fibre_t *fibre)
{
const uint32_t sequence[] = { GPIO5, GPIO4, GPIO5, GPIO6,
GPIO7, GPIO6, GPIO5 };
struct wiggle_data *d = containerof(fibre, struct wiggle_data, fibre);
PT_BEGIN_FIBRE(fibre);
d->time = time_now();
for (d->i = 0; d->i < 16; d->i++) {
int p = d->i % 6;
gpio_set(GPIOA, sequence[p+1]);
gpio_clear(GPIOA, sequence[p]);
d->time += 100000;
PT_WAIT_UNTIL(fibre_timeout(d->time));
}
gpio_clear(GPIOA, GPIO4 | GPIO5 | GPIO6 | GPIO7);
fibre_run(&button_task);
PT_END();
}
static struct wiggle_data wiggle_task = {
.fibre = FIBRE_VAR_INIT(wiggle_fibre)
};
static int usb_fibre(fibre_t *fibre)
{
static uint32_t t;
PT_BEGIN_FIBRE(fibre);
/* lower hotplug and leave enough time for the host to notice */
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL,
GPIO11 | GPIO12);
gpio_clear(GPIOA, GPIO11 || GPIO12);
t = time_now() + 10000;
PT_WAIT_UNTIL(fibre_timeout(t));
usbd_dev = usbd_init(&stm32f103_usb_driver, &dev, &config,
usb_strings, 2,
usbd_control_buffer, sizeof(usbd_control_buffer));
usbd_register_set_config_callback(usbd_dev, usbmidi_set_config);
while (true) {
usbd_poll(usbd_dev);
PT_YIELD();
}
PT_END();
}
static fibre_t usb_task = FIBRE_VAR_INIT(usb_fibre);
int main(void)
{
rcc_clock_setup_in_hse_8mhz_out_72mhz();
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_GPIOB);
/* LED init */
gpio_clear(GPIOA, GPIO4 | GPIO5 | GPIO6 | GPIO7);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, GPIO4 | GPIO5 | GPIO6 | GPIO7);
/* Button pin */
gpio_set(GPIOA, GPIO8 | GPIO9 | GPIO10 | GPIO3);
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN,
GPIO8 | GPIO9 | GPIO10 | GPIO3);
time_init();
/* prepare the scheduler */
fibre_run(&wiggle_task.fibre);
fibre_run(&usb_task);
/* assert hotplug */
gpio_set(GPIOB, GPIO8);
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
fibre_scheduler_main_loop();
return 0;
}