What Are Prerequisites Before Embedded Programming?
Following are the prerequisites need to be covered before jumping into sea of embedded programming.
Binary Numbering System
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.
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.
Starts at 0
Starts at 0
Goes upto 9
Goes upto 1
Reset the digit and increment the next digit.
Reset the current digit and increment the next
Again goes upto 9
What happens next?
What happens with 99, now 2 digits are at optimum
Initial 2 digits got reset and next digit will be incremented
Both will got reset and next digit will be incremented.
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.
What is that?
Hidden language inside Rust
Gives us extra superpowers
Why is it?
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.
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)
Note: Asterisk “ * ” is not a de-referencing operator, it’s part of type name. Let’s see how to create an immutable and mutable raw pointer from reference. let mut num = 5;
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
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.
Static variables always have fixed address in memory.