My struggle with bare-metal programming on the Arduino R4 Minima

Lessons to be learned:
  • Never rely on ChatGPT to generate low-level code;
  • Never flash a bootloader of a different microcontroller onto your board

Recently, I embarked on a journey to explore bare-metal programming. My initial intetion when I approached this kind of programming was to gain a deeper understanding of how a computer really works. I've been curious about the inner workings of operating systems, so I decided to learn a bit more. First, tried to read the Minix book by Andrew S. Tanenbaum, but found out that Minix is almost dead nowadays. Last time I tried to compile it, it was failing due to a cause I couldn't figure out. So I decided to try something else.

I bought an Arduino Uno R3 some years ago, when I was at the college. I used it to develop a small project of a weather data logger, using the DHT11 sensor. It worked fine, but I never really explored the capabilities of the board. R3 is based on the ATmega328P microcontroller, an eight-bit microcontroller from Atmel (now Microchip). It is simple and easy to use, so I took it from my drawer, wiped the dust off it and tried to explore bare-metal programming on it. I found some valuable resources online and eventually managed to develop a small [and unfinished] kernel. Wrote a simple UART driver, a real time clock (RTC) driver, and was trying to discover how to write an ELF loader. Plugged a SD card module to the board, and tried to read files from it, but some problem with the board prevented me from going reading and writing files in the FAT filesystem.

So, I decided to bought a new board, the Arduino R4 Minima, which is based on the ARM Cortex-M4 microcontroller. I was a shoot in the dark, since I had no experience with ARM microcontrollers, and I had no previous idea that the R4 Minima came with a ARM MCU. After receiving the board, I played a little with the Arduino IDE, and then I decided to try bare-metal programming on it.

Arduino R4 Minima

At this moment, my problem started. I tried to find some resources online, but there were very few for this specific board/microcontroller. R4 is based on Renesas RA4M1 microcontroller, a Japanese manufacturer that I've never heard of before. I found some documentation on Renesas website, but it was focused solely on using their own development tools, which I didn't want to use. They offer an IDE called e2studio, based on Eclipse, that I personally dislike.

Spent almost one week trying to figure out how to set up a bare-metal development environment for the R4 Minima. When I had an idea: why not ask ChatGPT to help me with that?

And here I learned my first lesson: never rely on ChatGPT or [PUT ANY OTHER AI HERE] to generate low-level code. I asked ChatGPT to generate a simple program that would send serial data through UART/USB CDC. And what it generated was utterly garbage. It didn't even run.

Finally, I had and idea I should've had from the beginning: just get the bootloader source code, try to understand how it works and modify it to suit my needs. The bootloader is open source, and available on Arduino's GitHub repository. So I downloaded the source code, and started reading it. I managed to understand a little bit how it works. Flashing it onto the board was easy, you should just short the BOOT and GND pins while powering it on, and then use the Renesas Flash Tool to flash the bootloader binary.

But here comes my second lesson: never flash a bootloader of a different microcontroller onto your board. In my case, I tried to run Xinu OS on the R4 Minima. Xinu is an operating system for embedded systems. It was ported to Cortex M3 microcontrollers, so I thought it would work on the Cortex M4 as well. I tried to flash the Xinu bootloader onto the R4 Minima, and the board bricked. It wouldn't boot anymore, and I couldn't flash any code onto it. Now, I'm waiting for a SWD programmer to arrive, so I can try to recover the board.