0
votes

I am trying to understand how a microcontroller is working, basically. At least from a software developers point of view. I have some basic knowledge about "make-files" and the compiling and linking process as well as low-level programming. But what I want to learn is programming even closer to the hardware.

So I was starting to dissect the HardwareSerial code (HardwareSerial.c and HardwareSerial.h) for Arduino. And what I could not figure out is where UBRRH (or UBRR0H) are defined, which means that we have serial0 (the only one for Arduino-Uno). My guess is that the manufacturer of the board needs to provide this somehow. I thought that depending on the target board I use, different code is compiled. So that if I compile the code for an Arduino-Uno the compiled file will somehow include a definition of e.g. UBRRH. Then how does the Arduino IDE know what board I'm compiling?

My goal is to eventually be able to write my own serial protocol. And maybe even design my own board.. Even though the only "board" I have created is an H-bridge but hey, aim high...

2

2 Answers

1
votes

how does the Arduino IDE know what board I'm compiling?

You selected it in the menu Tools -> Board.

There is a lot of information stored for each board.

Just take a look into Arduino\hardware\arduino\avr for example.

My guess is that the manufacturer of the board needs to provide this somehow. I thought that depending on the target board I use, different code is compiled. So that if I compile the code for an Arduino-Uno the compiled file will somehow include a definition of e.g. UBRRH.

More or less yes. Take a look here https://www.nongnu.org/avr-libc/user-manual/group__util__setbaud.html

https://www.nongnu.org/avr-libc/user-manual/index.html

0
votes

It is actually pretty common to be able to build your own compatible board. Checkout the following things on that

Based on the question (given that you referenced "UBRRH") I am guessing you already know what controller is present in the Arduino UNO.

The file /hardware/arduino/avr/cores/arduino/HardwareSerial.h makes use of these registers.

The registers are declared in the libraries which are packed as a part of the compiler toolchain, which is supplied by the company Atmel.

The file hardware/arduino/avr/boards.txt has pre-processor definitions for the compiler, which are selected based on which board you select in tools>board in the Arduino IDE.

Now, these libraries find out what kind of controller is being programmed based on the build.mcu property they get from the Arduino IDE from the boards.txt file.

Depending on which mcu was selected, the libraries define or do not define the registers which the code requires to program the MCU (for example "UBRRH")

I hope this helps you get to where you want to go.