USBD Serial Basics

I'm working on an open source smart watch project https://github.com/Protocentral/healthypi-move-fw.

I'm after serial communication with another device, so I need to re-enable the usbd stack. I have successfully managed to flash new firmware with a new gui that I intend to control, but that's not particularly helpful if I can't get bi-directional communication working. It's a bit slow to work without even printf debugging as a tool. I'd appreciate any help to get me up to speed how this all works.

As for the project, it's presenting a few challenges at the moment since I'm completely new to nRF development. I was hoping to find code related to usb because there is a sensor which attaches to the usb port of this watch. However what I've discovered is that the usb stack has been specifically removed from this project and the sensor that comes with it appears to be controlled via very specific pins like an i2c device (which obviously are linked to the usb port). That's cool to know that's possible, but I'm trying to just work with the basics. I'd like to have bidirectional communication going over serial to simply things.

One immediate problem is that just enabling the usbd stack in the prf.conf actually makes the build fail because the application core is too large. I've managed to find buffers which I could shrink, I've talked with the watch's developers a little bit and they've offered suggestions like shrinking the configured heap and stack sizes, etc... I'd prefer to do things without negatively impacting the watch's existing functionality. I think I've managed to give myself just enough room to add the usbd stack back in.

So the remaining portion is just making sense of what exactly I need to do to get that usb port to act as a serial port. If anyone can offer a more specific translation of how the sample code relates to https://github.com/Protocentral/healthypi-move-fw/blob/582fc5376a4750e1c240f0ac9a189680525b3f45/app/src/hw_module.c#L549 the board configuration https://github.com/Protocentral/healthypi-move-fw/blob/582fc5376a4750e1c240f0ac9a189680525b3f45/boards/protocentral/healthypi_move/nrf5340_cpuapp_common.dtsi#L16 and the prf.conf file https://github.com/Protocentral/healthypi-move-fw/blob/main/app/prj.conf would be great. I've managed to flash the device with some modifications that don't appear to crash with the `usb_init()` function re-enabled, but I'm not getting anything back out over VS Code's serial monitor (the current interrupt handler I believe is setup to echo). So I have no clue if what I've done works at all. I have a sneaking suspicion that the "chosen" "zephyr,console" should be cdc_acm_uart0, however when I do that I get errors that `__device_dts_ord_163` is missing which from the documentation appears to be a problem of selecting a non-existent device.

Then finally, I'm not entirely sure if this is possible with this device, it would be nice if the watch could power the other device over usb. I did see configuration options and power mentioned in here: https://github.com/zephyrproject-rtos/zephyr/blob/main/samples/subsys/usb/common/Kconfig.sample_usbd
If that's related, it'd be nice to know. Part of my not being sure that this works is that the second device clearly isn't being powered by the usb port so until I sort out an alternate power source I can't verify what I've done. However since I'm not getting feedback over the serial monitor I have sneaking suspicion what I've done will not work. Any help is welcome. I have gone through some of the USBD documentation, but clearly I'm missing things in order to get this to work.

I am debugging/flashing firmware using an nRF5340 DK, specifically this one: https://www.amazon.com/dp/B0FCVL5JVT using the nRF Connect Extension in Visual Studio Code over the SWD debug out port.

Related