tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
15.4k stars 909 forks source link

Custom Board Support #2239

Open bradleypeabody opened 2 years ago

bradleypeabody commented 2 years ago

I've been using TinyGo to develop firmware for a custom board based on the ATSAMD51. Overall it's been an awesome experience.

Some of the changes necessary have required forking TinyGo to get everything working right. I wanted to summarize those points here and see if there is interest in resolving them and get any guidance on the direction for each.

So overall my question is: do these seem like things I should do changes and PRs for and try to get things to the point where custom boards (at least starting with those based on SAM chips) are supported out the box? And also some of the debug tooling mentioned above? If so, I'll see about setting aside some time to clean up the changes I made into merge-able PRs and originate any other discussion about the design of some of these things where needed.

soypat commented 2 years ago

The biggest immediate difference for custom boards is one cannot make assumptions about pin assignments or SERCOMs and the various things that use them (UART, SPI and I2C buses or anything else). This means that UARTs, SPIs, etc. need to be fully definable in user code. This is not terribly hard to do, but there are little things like the interrupt handler func for the UART on SAM microcontrollers is not exported (handleInterrupt).

I'm not quite understanding what you mean. Aren't peripherals consistent for a processor? TinyGo already separates what is machine vs. what is board. machine/machine_* files contain hardware definitions which are constant for the processor, on the other hand machine/board_* files have board specific definitions which may vary for a processor depending on the board it's on. To develop on a custom board one usually sets the environment with the processor as the target, for example, for the Raspberry Pi Pico's processor this would be the rp2040 target.

bradleypeabody commented 2 years ago

@soypat On a SAM microcontroller, for each device (UART, I2C, SPI, etc.) you want to use, you have to pick one of several "SERCOM"s, put it in the appropriate mode and settings and pick which pins it goes on (and internally there are few other things it does like selecting the "pad" for each pin).

As it stands right now (or at least the result of when I tested this earlier): If I specify "atsamd51j19a" as the target, I get compile errors because putchar references a device that isn't present because no UART is defined (since this is done in the "board_" files but there is no board for these raw MCU targets). If this is supposed to work, I can try it again and dig up the exact error message and what I did to get it, I just got the idea from what I was seeing that this approach wasn't supported. (I also didn't see any documentation on this approach, which is part of what led me to that conclusion.)