What Are Prerequisites Before Embedded Programming?
Following are the prerequisites need to be covered before jumping into sea of embedded programming.
Binary Numbering System
Bitwise operators
Unsafe Rust
Raw Pointers
Mutable Static variables
Binary Number System
Keep the points in mind in sequence.Binary numbers are made up of only 1’s and 0’s. They don’t contain any other number we usually see in decimals (i.e. 2,3,4,...,9). Here are few examples.
Decimal
0
1
2
4
13
21
27
45
Binary
0
1
10
100
1101
10101
11011
101101
Binary numbers have base of 2 in contrast our decimal numbers which have base 10. What base tells us? It tells how many combination can a number give us.
Decimal
Binary
0000
Starts at 0
0000
Starts at 0
0009
Goes upto 9
0001
Goes upto 1
0010
Reset the digit and increment the next digit.
0010
Reset the current digit and increment the next
0019
Again goes upto 9
0011
What happens next?
0099
What happens with 99, now 2 digits are at optimum
0100
Initial 2 digits got reset and next digit will be incremented
0100
Both will got reset and next digit will be incremented.
Bitwise Operator
Wondering! What these are? Now we are familiar with binary numbers. Next we will see concept of Bitwise operators that based on the binary numbers. There are variety of operators in programming languages (i.e. arithmetic, relational, logical and assignment operators). Bitwise operator is one of them.
Bitwise AND ( & )
It do a Boolean AND operation on each bit of its integer arguments.
Bitwise OR ( | )
This perform a Boolean OR operation on each bit of its integer arguments.
Bitwise XOR ( ^ )
It do a Boolean exclusive OR operation on each bit of its integer arguments.
Bitwise NOT ( ! )
This is an unary operator and operates by reversing all the bits in the operand.
Bitwise left shift ( << )
It moves all the bits in its first operand to the left by the number of places specified in the second operand.
Bitwise right shift ( >> )
Binary Right Shift Operator.
Unsafe Rust
What is that?
Hidden language inside Rust
Gives us extra superpowers
It let us do the operations which compiler restricts us from
Without unsafe Rust we can’t do operations on hardware level
De-reference a raw pointer
Call an unsafe function or method
Access or modify a mutable static variable
Implement an unsafe trait
Keep few things in mind while using unsafe Rust :
Unsafe rust doesn’t disable borrow checker or any other safety feature.
It gives you only access to these four features and compiler don’t check for memory safety in them.
Inside unsafe block the code doesn’t need to be necessarily dangerous.
While writing code keep the unsafe block smaller.
For the isolation of unsafe code, it’s better to enclose unsafe code with in a safe abstraction. Wrapper!
When using unsafe rust we as programmer have to ensure that you are accessing memory in a valid way.
Raw pointer
There are two new typs of Unsafe Rus called raw pointers that are similar to references. They can be immutable or mutable and can be written as:
*const T (Immutable)
*mut T (Mutable)
let r1=&num as *const i32;
let r2=&mut num as *mut i32;
Is something wrong! We didn’t include unsafe keyword here? Well, its okay to create raw pointers outside unsafe but it’s must to include while de-referencing them.
How raw pointers different from references!
Are allowed to ignore the borrowing rules by having both immutable and mutable pointers or multiple mutable pointers to the same location
Aren’t guaranteed to point to valid memory
Are allowed to be null
Don’t implement any automatic cleanup
Static variables
Global variables in Rust are called static variables
Static variables are similar to Constants
Naming style of static variables is in SCREAMING_SNAKE_CASE
Type annotation for static variables is must (like constants).
Rust allows access read-only static variables (immutable static variables ), however doesn’t allow access to immutable static variables.