These are some notes for this topic based on these references:
- Bare Metal C by Stephen Oualline published by No Starch Press.
Basics#
Arrays and Pointers#
They are very simmilar but differ in terms of how they are declared:
int array[5] = {1,2,3,4,5};
int* arrayPtr = array; // note we used array and not &array
// array is already a ptr
int i = array[1];
// is same as
int i = *(arrayPtr + 1);
Linker Models#
Ideal C Model#
There are 3 standard sections: text, data, bss. text is where read-only data go to. data is the place for initialized data. bss is for uninitialized data. Use size to view each sections.
$ size example.o
text data bss dec hex filename
481 4 4 489 1e9 example.o
dec is the total number and hex is the total in hexadecimal. Those sections are allocated by the compiler. However there are two more sections stack and heap which is allocated by the linker.
GCC flags#
-
-Wall -Wextra * : To print warning messages for code that is correct but questionable.
-
-Wa, <ASM_FLAGS> * : Pass flags that follow to assembler
-
-Wl, <LD_FLAGS> * : Pass flags that follow to the linker
Bit Twidling#
PORTD = (1 << 3) // sets the 3rd bit to 1
PORTD |= (1 << 3) // sets the 3rd bit to 1 keeping rest of them same
PORTD |= (1 << 3) | (1 << 5) // set multiple bits;
// keeping the rest of them same
PORTD &= ~(1 << 3) // unset a bit; keeping others same
PORTD &= ~((1 << 3) | (1 << 5)) // unset multiple bits;
// keeps rest of them same
Select Problems#
Chapter 4 : Extracting bits 2 and 3 from an 8 bit register
uint8_t parity = (IO_REG >> 2) & 0x03