Skip to content

Commit

Permalink
Merge Microsoft upstream as of 2018-02-05; turn off USE_WEBUSB, which…
Browse files Browse the repository at this point in the history
… gives a notification on Linux
  • Loading branch information
dhalbert committed Feb 6, 2018
2 parents ffbf98a + 99bdc3d commit 38cb858
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 55 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ WFLAGS = \
CFLAGS = $(COMMON_FLAGS) \
-x c -c -pipe -nostdlib \
--param max-inline-insns-single=500 \
-fno-strict-aliasing -fdata-sections -ffunction-sections -mlong-calls \
-fno-strict-aliasing -fdata-sections -ffunction-sections \
-D__$(CHIP_VARIANT)__ \
$(WFLAGS)

Expand All @@ -43,7 +43,7 @@ endif
LDFLAGS= $(COMMON_FLAGS) \
-Wall -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common \
-Wl,--warn-section-align -Wl,--warn-unresolved-symbols \
-save-temps \
-save-temps -nostartfiles \
--specs=nano.specs --specs=nosys.specs
BUILD_PATH=build/$(BOARD)
INCLUDES = -I. -I./inc -I./inc/preprocessor
Expand Down
2 changes: 0 additions & 2 deletions boards/metro_m0/board_config.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef BOARD_CONFIG_H
#define BOARD_CONFIG_H

#define __SAMD21G18A__ 1

#define VENDOR_NAME "Adafruit Industries"
#define PRODUCT_NAME "Metro M0"
#define VOLUME_LABEL "METROBOOT"
Expand Down
14 changes: 10 additions & 4 deletions inc/neopixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ static void neopixel_send_buffer_core(volatile uint32_t *clraddr, uint32_t pinMa
const uint8_t *ptr, int numBytes) {
asm volatile(" push {r4, r5, r6, lr};"
" add r3, r2, r3;"
" cpsid i;"
"loopLoad:"
" ldrb r5, [r2, #0];" // r5 := *ptr
" add r2, #1;" // ptr++
Expand Down Expand Up @@ -56,7 +55,6 @@ static void neopixel_send_buffer_core(volatile uint32_t *clraddr, uint32_t pinMa
" bcs stop;"
" b loopLoad;"
"stop:"
" cpsie i;"
" pop {r4, r5, r6, pc};"
"");
}
Expand All @@ -69,14 +67,22 @@ static inline void neopixel_send_buffer(const uint8_t *ptr, int numBytes) {
PINOP(BOARD_NEOPIXEL_PIN, DIRSET);

// turn off mux too, needed for metro m0 but no harm done!
PORT->Group[BOARD_NEOPIXEL_PIN / 32].PINCFG[BOARD_NEOPIXEL_PIN % 32].reg=(uint8_t)(PORT_PINCFG_INEN) ;
PORT->Group[BOARD_NEOPIXEL_PIN / 32].PINCFG[BOARD_NEOPIXEL_PIN % 32].reg =
(uint8_t)(PORT_PINCFG_INEN);

PINOP(BOARD_NEOPIXEL_PIN, OUTCLR);
delay(1);

volatile uint32_t *clraddr = &PORT->Group[portNum].OUTCLR.reg;

neopixel_send_buffer_core(clraddr, pinMask, ptr, numBytes);
// equivalent to cpu_irq_is_enabled()
if (__get_PRIMASK() == 0) {
__disable_irq();
neopixel_send_buffer_core(clraddr, pinMask, ptr, numBytes);
__enable_irq();
} else {
neopixel_send_buffer_core(clraddr, pinMask, ptr, numBytes);
}
}

#endif
Expand Down
16 changes: 14 additions & 2 deletions inc/uf2.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#define INDEX_URL "https://www.pxt.io/"
#endif

#ifndef WEBUSB_URL
#define WEBUSB_URL "pxt.io"
#endif

#include "uf2_version.h"

// needs to be more than ~4200 (to force FAT16)
Expand All @@ -51,7 +55,9 @@
// Support Human Interface Device (HID) - serial, flashing and debug
#define USE_HID 1 // 788 bytes
// Expose HID via WebUSB
#define USE_WEBUSB 0 // 400 bytes
#define USE_WEBUSB 0
// Return allowed WebUSB URLs; it seems to have been removed from the standard
#define USE_WEBUSB_ORIGINS 0
// Doesn't yet disable code, just enumeration
#define USE_MSC 1

Expand Down Expand Up @@ -112,9 +118,15 @@
#define WEB_VERSION ""
#endif

#if USE_MSC_HANDOVER
#define MSC_HANDOVER_VERSION "O"
#else
#define MSC_HANDOVER_VERSION ""
#endif

#define UF2_VERSION \
UF2_VERSION_BASE " " CDC_VERSION LOGS_VERSION FAT_VERSION ASSERT_VERSION HID_VERSION \
WEB_VERSION RESET_VERSION
WEB_VERSION RESET_VERSION MSC_HANDOVER_VERSION

// End of config

Expand Down
2 changes: 2 additions & 0 deletions inc/uf2format.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ static inline void hf2_handover(uint8_t ep) {
}
}

// the ep_in/ep_out are without the 0x80 mask
// cbw_tag is in the same bit format as it came
static inline void check_uf2_handover(uint8_t *buffer, uint32_t blocks_remaining, uint8_t ep_in,
uint8_t ep_out, uint32_t cbw_tag) {
if (!is_uf2_block(buffer))
Expand Down
4 changes: 2 additions & 2 deletions scripts/dbgtool.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ function main() {

if (!pkgDir) fatal("cannot find Arduino packages directory")

let openocdPath = pkgDir + "tools/openocd/0.9.0-arduino/"
let openocdPath = pkgDir + "tools/openocd/0.10.0-arduino1-static/"
if (!fs.existsSync(openocdPath)) fatal("openocd not installed in Arduino")

let openocdBin = openocdPath + "bin/openocd"

if (process.platform == "win32")
openocdBin += ".exe"

let zeroCfg = pkgDir + "hardware/samd/1.6.8/variants/arduino_zero/openocd_scripts/arduino_zero.cfg"
let zeroCfg = pkgDir + "hardware/samd/1.6.17/variants/arduino_zero/openocd_scripts/arduino_zero.cfg"

let cmd = `telnet_port disabled; init; halt; `
if (mode == "map")
Expand Down
4 changes: 3 additions & 1 deletion scripts/gendata.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ infostr = infostr.split(/\r?\n/).map(l => "// " + l + "\n").join("")

let size = buf.length
let s = infostr + "\n"
s += "const uint8_t bootloader[" + size + "] __attribute__ ((aligned (4))) = {"
s += "const uint8_t bootloader[" + size + "] "
s += "__attribute__ ((section(\".vectors.needs.to.go.first\"))) "
s += "__attribute__ ((aligned (4))) = {"
function tohex(v) {
return "0x" + ("00" + v.toString(16)).slice(-2) + ", "
}
Expand Down
107 changes: 78 additions & 29 deletions src/cdc_enumerate.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ __attribute__((__aligned__(4))) UsbDeviceDescriptor usb_endpoint_table[MAX_EP];
__attribute__((__aligned__(4)))
const char devDescriptor[] = {
/* Device descriptor */
0x12, // bLength
0x01, // bDescriptorType
0x00, // bcdUSBL - v2.00; v2.10 is needed for WebUSB, but it breaks newer laptops running Win10
0x12, // bLength
0x01, // bDescriptorType
// bcdUSBL - v2.00; v2.10 is needed for WebUSB; there were issues with newer laptops running Win10
// but it seems to be resolved
#if USE_WEBUSB
0x10,
#else
0x00,
#endif
0x02, //
0xEF, // bDeviceClass: Misc
0x02, // bDeviceSubclass:
Expand All @@ -55,7 +61,7 @@ const char devDescriptor[] = {
0x42, //
0x01, // iManufacturer // 0x01
0x02, // iProduct
0x00, // SerialNumber
0x03, // SerialNumber (required (!) for WebUSB)
0x01 // bNumConfigs
};

Expand Down Expand Up @@ -111,7 +117,7 @@ char cfgDescriptor[] = {
0x01, // CbConfigurationValue
0x00, // CiConfiguration
0x80, // CbmAttributes 0x80 - bus-powered
250, // 500mA
250, // 500mA

#if USE_CDC
// IAD for CDC
Expand Down Expand Up @@ -272,6 +278,8 @@ char cfgDescriptor[] = {
#endif
};

#define WINUSB_SIZE 170

__attribute__((__aligned__(4)))
static char bosDescriptor[] = {
0x05, // Length
Expand All @@ -294,7 +302,7 @@ static char bosDescriptor[] = {
0x8B, 0xFD, 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65, // WebUSB GUID
0x00, 0x01, // Version 1.0
0x01, // Vendor request code
0x02, // landing page
0x01, // landing page

0x1C, // Length
0x10, // Device Capability descriptor
Expand All @@ -303,7 +311,7 @@ static char bosDescriptor[] = {
0xDF, 0x60, 0xDD, 0xD8, 0x89, 0x45, 0xC7, 0x4C, // MS OS 2.0 GUID
0x9C, 0xD2, 0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F, // MS OS 2.0 GUID
0x00, 0x00, 0x03, 0x06, // Windows version
0x2e, 0x00, // Descriptor set length
WINUSB_SIZE, 0x00, // Descriptor set length
0x02, // Vendor request code
0x00 // Alternate enumeration code
#endif
Expand All @@ -316,50 +324,71 @@ static char msOS20Descriptor[] = {
0x0A, 0x00, // Descriptor size (10 bytes)
0x00, 0x00, // MS OS 2.0 descriptor set header
0x00, 0x00, 0x03, 0x06, // Windows version (8.1) (0x06030000)
0x2e, 0x00, // Size, MS OS 2.0 descriptor set

// Microsoft OS 2.0 configuration subset header
0x08, 0x00, // Descriptor size (8 bytes)
0x01, 0x00, // MS OS 2.0 configuration subset header
0x00, // bConfigurationValue
0x00, // Reserved
0x24, 0x00, // Size, MS OS 2.0 configuration subset
WINUSB_SIZE, 0x00, // Size, MS OS 2.0 descriptor set

// Microsoft OS 2.0 function subset header
0x08, 0x00, // Descriptor size (8 bytes)
0x02, 0x00, // MS OS 2.0 function subset header
WEB_IF_NUM, // first interface no
0x00, // Reserved
0x1c, 0x00, // Size, MS OS 2.0 function subset
0x08, 0x00, // Descriptor size (8 bytes)
0x02, 0x00, // MS OS 2.0 function subset header
WEB_IF_NUM, // first interface no
0x00, // Reserved
WINUSB_SIZE - 10, 0x00, // Size, MS OS 2.0 function subset

// Microsoft OS 2.0 compatible ID descriptor (table 13)
0x14, 0x00, // wLength
20, 0x00, // wLength
0x03, 0x00, // MS_OS_20_FEATURE_COMPATIBLE_ID
'W', 'I', 'N', 'U', 'S', 'B', //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

// interface guids
132, 0, 4, 0, 7, 0,
//
42, 0,
//
'D', 0, 'e', 0, 'v', 0, 'i', 0, 'c', 0, 'e', 0, 'I', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0,
'a', 0, 'c', 0, 'e', 0, 'G', 0, 'U', 0, 'I', 0, 'D', 0, 's', 0, 0, 0,
//
80, 0,
//
'{', 0, '9', 0, '2', 0, 'C', 0, 'E', 0, '6', 0, '4', 0, '6', 0, '2', 0, '-', 0, '9', 0, 'C', 0,
'7', 0, '7', 0, '-', 0, '4', 0, '6', 0, 'F', 0, 'E', 0, '-', 0, '9', 0, '3', 0, '3', 0, 'B', 0,
'-', 0, '3', 0, '1', 0, 'C', 0, 'B', 0, '9', 0, 'C', 0, '5', 0, 'A', 0, 'A', 0, '3', 0, 'B', 0,
'9', 0, '}', 0, 0, 0, 0, 0};

STATIC_ASSERT(sizeof(msOS20Descriptor) == WINUSB_SIZE);

#if USE_WEBUSB_ORIGINS
#define ALLOWED_ORIGINS_SEQ 1, 2
static const char *allowed_origins[] = {"localhost:3232", "samd.pxt.io"};

#define NUM_ALLOWED_ORIGINS 2
static const uint8_t allowedOriginsDesc[] = {
// Allowed Origins Header, bNumConfigurations = 1
0x05, 0x00, 0x0c + NUM_ALLOWED_ORIGINS, 0x00, 0x01,
0x05,
0x00,
0x0c + NUM_ALLOWED_ORIGINS,
0x00,
0x01,
// Configuration Subset Header, bNumFunctions = 1
0x04, 0x01, 0x01, 0x01,
0x04,
0x01,
0x01,
0x01,
// Function Subset Header, bFirstInterface = pluggedInterface
0x03 + NUM_ALLOWED_ORIGINS, 0x02, WEB_IF_NUM, ALLOWED_ORIGINS_SEQ,
0x03 + NUM_ALLOWED_ORIGINS,
0x02,
WEB_IF_NUM,
ALLOWED_ORIGINS_SEQ,
};

STATIC_ASSERT((sizeof(allowed_origins) / sizeof(allowed_origins[0])) == NUM_ALLOWED_ORIGINS);
STATIC_ASSERT(sizeof(allowedOriginsDesc) == 0x0c + NUM_ALLOWED_ORIGINS);
#endif

typedef struct {
uint8_t len, tp, scheme;
uint8_t buf[64];
} __attribute__((packed)) WebUSB_URL;

STATIC_ASSERT((sizeof(allowed_origins) / sizeof(allowed_origins[0])) == NUM_ALLOWED_ORIGINS);
STATIC_ASSERT(sizeof(allowedOriginsDesc) == 0x0c + NUM_ALLOWED_ORIGINS);

#endif

static uint8_t currentConfiguration;
Expand All @@ -370,7 +399,7 @@ typedef struct {
uint8_t data[70];
} StringDescriptor;

static const char *string_descriptors[] = {0, VENDOR_NAME, PRODUCT_NAME, };
static const char *string_descriptors[] = {0, VENDOR_NAME, PRODUCT_NAME, "4242"};

#define STRING_DESCRIPTOR_COUNT (sizeof(string_descriptors) / sizeof(string_descriptors[0]))

Expand Down Expand Up @@ -604,6 +633,12 @@ uint32_t USB_ReadCore(void *pData, uint32_t length, uint32_t ep, PacketBuffer *c
if (USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRCPT0) {
/* Set packet size */
cache->size = epdesc->DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT;

// this is when processing a hand-over
if (cache->read_job == 2) {
memcpy(&cache->buf, (void *)epdesc->DeviceDescBank[0].ADDR.reg, cache->size);
}

packetSize = MIN(cache->size, length);
if (pData) {
cache->ptr = packetSize;
Expand Down Expand Up @@ -800,6 +835,7 @@ void AT91F_CDC_Enumerate() {
break;
#if USE_WEBUSB
case STD_VENDOR_CTRL1:
#if USE_WEBUSB_ORIGINS
if (wIndex == 0x01)
sendCtrl(allowedOriginsDesc, sizeof(allowedOriginsDesc));
else if (wIndex == 0x02) {
Expand All @@ -816,6 +852,19 @@ void AT91F_CDC_Enumerate() {
} else {
stall_ep(0);
}
#else
if (wIndex == 0x02 && ctrlOutCache.buf[2] == 0x01) {
WebUSB_URL url;
url.len = strlen(WEBUSB_URL) + 3;
url.tp = 3;
// first URL is http, rest is https
url.scheme = 1;
memcpy(url.buf, WEBUSB_URL, url.len - 3);
sendCtrl(&url, url.len);
} else {
stall_ep(0);
}
#endif
break;
case STD_VENDOR_CTRL2:
if (wIndex == 0x07)
Expand Down
6 changes: 4 additions & 2 deletions src/fat.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,13 @@ void write_block(uint32_t block_no, uint8_t *data, bool quiet, WriteState *state
if (state->numWritten >= state->numBlocks) {
// wait a little bit before resetting, to avoid Windows transmit error
// https://github.com/Microsoft/uf2-samd21/issues/11
resetHorizon = timerHigh + 30;
if (!quiet)
resetHorizon = timerHigh + 30;
// resetIntoApp();
}
}
} else {
resetHorizon = timerHigh + 300;
if (!quiet)
resetHorizon = timerHigh + 300;
}
}
3 changes: 3 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ int main(void) {
while (1) {
}

#if USB_PID == 0x0013 // Metro m0
delay(100);
#endif
led_init();

logmsg("Start");
Expand Down
Loading

0 comments on commit 38cb858

Please sign in to comment.