tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

feat: add usb msc support for nrf52840

Open mikesmitty opened this issue 4 months ago • 21 comments

I don't have an nrf52840 on hand to test yet (should have one tomorrow), but I figured I'd push this up in case @aykevl or anyone else wanted to give it a whirl https://github.com/tinygo-org/tinygo/pull/4844#issuecomment-3199301082

mikesmitty avatar Aug 23 '25 16:08 mikesmitty

I tried this, but sadly it doesn't work. I haven't investigated why it doesn't work though.

aykevl avatar Aug 24 '25 10:08 aykevl

No worries, my nrf52840 just showed up so I'll investigate

mikesmitty avatar Aug 24 '25 13:08 mikesmitty

@mikesmitty any update on this? I would really like to have it for something... :smile_cat:

deadprogram avatar Sep 16 '25 16:09 deadprogram

Oh yeah, this fell off my radar. I've got a debugger just about set up with an nrf52840 now so I can dig into this again

mikesmitty avatar Sep 17 '25 03:09 mikesmitty

Ok, at least in my testing it looks like it's just a problem with m.buf being too small. My test device has a write block size of 4 bytes, which I didn't think would ever happen. The m.buf buffer is set, perhaps a bit naively, to use the write block size as a capacity, but it needs to be 64 bytes at a minimum to be able to store a full usb packet and it's panicking. #5017 also modifies these buffer sizes but in a more expansive way so this will likely end up just being a temporary patch. I just saw Ayke commented again on that one so I'll swap back to that one next

mikesmitty avatar Sep 17 '25 13:09 mikesmitty

Pinging Dr. @mikesmitty :smile_cat:

deadprogram avatar Oct 21 '25 11:10 deadprogram

Sorry, been getting distracted a lot recently haha. I'll start working on this again this weekend

mikesmitty avatar Oct 23 '25 13:10 mikesmitty

Thank you!

deadprogram avatar Oct 23 '25 14:10 deadprogram

I gave it a try today on XIAO BLE board.

"usb-storage" example compiles and flashes to the board.

Mac can see the device, but can't really use it. I've tried to "Erase" it with Disk Utility, that just hangs.

See screenshots.

Screenshot 2025-11-24 at 22 45 29 Screenshot 2025-11-24 at 22 48 14 Screenshot 2025-11-24 at 22 48 54

ysoldak avatar Nov 24 '25 21:11 ysoldak

Same happens with xiao-rp2040 target though, which as I understood is supposed to work, so either nrf version already works as expected OR we need a better example to try with OR need an instruction how to initialize the storage device.

ysoldak avatar Nov 24 '25 22:11 ysoldak

Ok, finally getting back into this now. Let me see where I left off

mikesmitty avatar Nov 26 '25 02:11 mikesmitty

Just in case it helps in any way, here is UF2 bootloader for nRF52 chips. Boards with UF2 bootloader show up as mass storage devices.

https://github.com/adafruit/Adafruit_nRF52_Bootloader/blob/master/src/usb/usb.c

ysoldak avatar Nov 28 '25 00:11 ysoldak

Woof, that was an adventure. The usb endpoint handling logic for nrf52840 was unconditionally accepting packets and I ended up having to rework it a little bit in order to allow returning a NAK when needed, but it seems to be working for me now if you guys want to give it a shot

mikesmitty avatar Nov 30 '25 02:11 mikesmitty

@ysoldak please give this PR another try if you get a chance.

deadprogram avatar Nov 30 '25 14:11 deadprogram

Still can't see Mass Storage Device when flashing usb-storage example to XIAO BLE board. tinygo flash -target=xiao-ble -opt=z ./src/examples/usb-storage

There is no popup with Eject, Ignore and Initialize... buttons anymore, but nothing else either.

Is there any other (better) way to test?

ysoldak avatar Dec 01 '25 22:12 ysoldak

Sorry, I've been playing whackamole with bugs in this one a little bit. I started putting together a branch with tests to try and iron those out properly. I'll trim this back and put those in the other branch

mikesmitty avatar Dec 01 '25 23:12 mikesmitty

@mikesmitty #5109 has been merged, perhaps this PR can now be rebased and retested?

deadprogram avatar Dec 06 '25 08:12 deadprogram

I rebased if you want to give it a go, but I haven't tested it yet. I spent a bunch of time arguing with both the jlink and the nrf52 board, but I've finally got a reliable workflow going again

mikesmitty avatar Dec 08 '25 01:12 mikesmitty

Ok, nailed down the bug I was stuck on for the longest time where endpoint stalls were hanging indefinitely and the CSW was not being returned. In order to reset the DATA0/DATA1 in setEPDataPID like is done after an endpoint stall you have to actually write the register twice. Once with just the endpoint address, then again with the address combined the new DATA0/DATA1 value at the appropriate offsets. Getting a new hang now, but making progress

mikesmitty avatar Dec 09 '25 03:12 mikesmitty

Alrighty, I fixed the last bug I was running into and pushed the changes up on #5110 since that's where I was doing my testing. I tried porting the change back over to this branch, but it's running into some fatal bug on this branch and I don't have time to sort it out this morning. I'll sort out/split up the changes a bit later, but for now you can try out the changes on that other PR

Edit: Oh, actually there's another new bug, but it enumerates properly now. I'll take a look into it a bit later

mikesmitty avatar Dec 09 '25 14:12 mikesmitty

Alrighty, I fixed the last bug I was running into and pushed the changes up on #5110 since that's where I was doing my testing. I tried porting the change back over to this branch, but it's running into some fatal bug on this branch and I don't have time to sort it out this morning. I'll sort out/split up the changes a bit later, but for now you can try out the changes on that other PR

Edit: Oh, actually there's another new bug, but it enumerates properly now. I'll take a look into it a bit later

Thanks for working on this @mikesmitty :heart:

deadprogram avatar Dec 10 '25 08:12 deadprogram

I'm still working on this btw. I'm beginning to suspect that easyDMA was named in the same way greenland was. Getting delayed ACKs to work properly with it is anything but easy

mikesmitty avatar Dec 20 '25 16:12 mikesmitty