samd21g18: Simple baremetal USB serial - Page 1
Forum section: electronics / general

author: reneman
author_rank: Contributor
timestamp: August 12, 2021, 12:03:01 pm
content: Hi, I'm looking forward to having a simple USB Serial output (no need for input, just as a way to output some text) with my samd21 board.So far these are the two libraries I've found:https://github.com/kevinmehall/usbhttps://github.com/ataradov/dgw/tree/master/embeddedBut I've had trouble setting them up. Kevinmehall's implementation seems simpler, but it relies on the Arduino framework and does not have examples for the samd21. Ataradov's looks much more complete, but I've had trouble isolating the USB functionality from all the other stuff, so I can export it and use it in my code.Does anyone have an example of what steps should I follow from my main() function to set up USB to work and send some simple string over UART?. I must note that I have already setup a 48MHz clock with an external crystal, and I don't think I need to use the USB source to configure any clock, so that step can be skipped.Thank you.

author: ataradov
author_rank: Super Contributor
timestamp: August 12, 2021, 03:48:00 pm
content: Here is just the VCP part:https://github.com/ataradov/vcp(D11 and D21 have the same USB controller, the code will work without changes).But I don't understand the difficulty isolating the code. My code is as isolated as it gets. Just remove all the stuff that is clearly not USB and whatever is left will be USB. But DGW project alone is not enough for a COM port, since it only implements USB HID. So use that VCP as a starter.

author: reneman
author_rank: Contributor
timestamp: August 13, 2021, 07:40:08 am
content: Quote from: ataradov on August 12, 2021, 03:48:00 pmCould you explain how the VCP code works? It is vastly uncommented. For example in sys_init() I see some USB setting up/calibration. What is it for?Then in main(), this should be the minimal code to make usb work right?Code:int main(void){sys_init();sys_time_init();usb_init();usb_cdc_init();//set_status_state(false);while (1){sys_time_task();usb_task();tx_task();rx_task();//uart_timer_task();//status_task();}return 0;}I still don't understand what the timing tasks are for. I know in the while(1) it is necessary to continuously dispatch USB events so that the USB keeps working. But your app_system_time stuff is necessary for USB or that can be removed too?Can I remove the uart.c & uart.h components as well?

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 13, 2021, 04:00:03 pm
content: I don't think "simple" and "baremetal usb serial" can go in the same line.Doing it by hand is no joke, you will have to fully understand the usb protocol, the hardware interface, glue it together and, yeah, fix all the bugs that *always* appear.What you need is a library that exports simple functions to use, like ataradov's.

author: ataradov
author_rank: Super Contributor
timestamp: August 13, 2021, 04:07:41 pm
content: Yes, with this level of understanding, there is no amount of commenting that will help. I can't help here, sorry. You will have to go trough the code and figure it out. The reason there is no documentation (apart from not having time to write it) is that I consider the code to be self documenting enough. It is not a lot of code to go though and understand, juts spend a bit of time.Timing tasks are to blink LEDs when necessary, and rx/tx tasks are to work with the USB to UART translation.Also, when working with unfamiliar code like this, it is much easier to run it as is, and then just remove the stuff you think you don't need while making sure everything still works.

author: Bassman59
author_rank: Super Contributor
timestamp: August 14, 2021, 05:09:04 pm
content: Quote from: ataradov on August 13, 2021, 04:07:41 pmYou know this, but proper documentation (not doxygen-generated crap) tells the readerwhythe developer implemented something the way it was done.Whatwas done is, as you say, obvious from the code itself.

author: ataradov
author_rank: Super Contributor
timestamp: August 14, 2021, 05:52:56 pm
content: Quote from: Bassman59 on August 14, 2021, 05:09:04 pmSure, I was was getting paid for this code, I would write documentation. For free, you get what you get. I write code for myself and publish it in hope that it will be useful to others, so no extra effort is put into it.