bsiever / microbit-webusb

WebUSB Utils for Micro:bit
MIT License
30 stars 8 forks source link

Need Help : how to communicate stably using microbit and webUSB #3

Closed mommocmoc closed 4 years ago

mommocmoc commented 4 years ago

Hi, I'm making educational program using microbit, webusb api. In Mac OS, I can access interface No.2 that has transfer in,out and I can use it to communicate with microbit easily. However, I can't access interface No2 in window and Chrome OS("Net Work Error" occurred), so I can't use transferOut to Endpoint 4 for communication.

In that situation, I found your repository and I can connect my microbit. I tested your microbit makecode sample and live Example, but it work sometime and doesn't sometimes.

I don't know why the reason, but I found that test code and live Example work well when the pairing and downloading is successful on Makecode.(When I can see "Show Console Device" Button on Makecode)

Do you know why it is happening?

Please help me.

Thank you.

bsiever commented 4 years ago

I wanted to use the USB UART interface, which, if I remember correctly, is interface 2. Based on a feedback from @mmoskal in the Micro:bit discord, USB UART is often used at the OS layer, so the browser may not be able to access it. That may explain the problems you've had with Interface 2 on some platforms.

So far I've only tested my work on macOS and haven't experienced the problems you've described. That being said, I have a suggestion:

In my current version I don't do much to configure the micro:bit when it connects. It currently just does one DAP command (the controlTransferOutFN) to set the Baud):

  device.open()
        .then(() => device.selectConfiguration(1))
        .then(() => device.claimInterface(4))
        .then(controlTransferOutFN(Uint8Array.from([0x82, 0x00, 0xc2, 0x01, 0x00]))) // Vendor Specific command 2 (ID_DAP_Vendor2): https://github.com/ARMmbed/DAPLink/blob/0711f11391de54b13dc8a628c80617ca5d25f070/source/daplink/cmsis-dap/DAP_vendor.c ;  0x0001c200 = 115,200kBps
        .then( () => { callback("connected", device, null); return Promise.resolve()}) 
        .then(transferLoop)
        .catch(error => callback("error", device, error))

In an earlier version (here) I was using most of the same connection sequence used by Makecode:

    device.open()
        .then(() => device.selectConfiguration(1))
        .then(() => device.claimInterface(4))
        .then(controlTransferOutFN(Uint8Array.from([2, 0])))  // Connect in default mode: https://arm-software.github.io/CMSIS_5/DAP/html/group__DAP__Connect.html
        .then(controlTransferOutFN(Uint8Array.from([0x11, 0x80, 0x96, 0x98, 0]))) // Set Clock: 0x989680 = 10MHz : https://arm-software.github.io/CMSIS_5/DAP/html/group__DAP__SWJ__Clock.html
        .then(controlTransferOutFN(Uint8Array.from([0x13, 0]))) // SWD Configure (1 clock turn around; no wait/fault): https://arm-software.github.io/CMSIS_5/DAP/html/group__DAP__SWD__Configure.html
        .then(controlTransferOutFN(Uint8Array.from([0x82, 0x00, 0xc2, 0x01, 0x00]))) // Vendor Specific command 2 (ID_DAP_Vendor2): https://github.com/ARMmbed/DAPLink/blob/0711f11391de54b13dc8a628c80617ca5d25f070/source/daplink/cmsis-dap/DAP_vendor.c ;  0x0001c200 = 115,200kBps
        .then( () => { callback("connected", device, null); return Promise.resolve()}) 
        .then(transferLoop)
        .catch(error => callback("error", device, error))

It sends several configuration commands that aren't included in the most recent version. Adding them back in may solve the problem. (They were various DAP commands)

Please let me know if it fixes the problem for you! I'll consider adding them back to my code.

mommocmoc commented 4 years ago

I tried your code and it works well in Chrome OS! I'll try on Window soon.

Most stable environment is mac os I think.

Thanks. 😄

bsiever commented 4 years ago

Great! Thanks for checking it! Please let me know about windows too. If it seems stable too, I'll just add it back to my repo as well (and close this "issue")

I developed this partly for an app that will probably be used primarily on Chrome OS, but I hadn't tested there yet.

mommocmoc commented 4 years ago

I tested on Windows also and it seems working well. 👍

bsiever commented 4 years ago

Great --- Thanks again! I'll update my version soon.

bsiever commented 4 years ago

Updated!