Lecture 6 - Resource constrained programming Flashcards
What is a microcontroller?
A small computer
CPU, RAM, program storage, peripherals (ports, clocks, DAC/ADC, GPIO, other)
All components are on a single chip
What is the difference between a PC and a micro controller?
Micro controller has weaker performance, but uses far less power.
Low transistor count and because of this really cheap.
Limited pluggability (keyboard, …) and peripherals
What are the constraints of micro controllers that you need to have in mind when programming them?
Memory, performance, Code size (storage, limits on OS functionality, support libraries), power
When looking at performance constraints, what are some things we need to look at?
Algorithmic space-time tradeoff:
- More CPU time - less memory
- Less CPUtime - more memory
Most gains on algorithmic level - avoid doing unecessary work
Every function call adds overhead - use inlining, static functions, macros
Reduce Load/stores
Use a profiler to identify hotspots where optimization is needed
What do we need to think about when it comes to power constraints?
How you MCU operates most of the time: mostly sleep or mostly work
How can we optimize power use for a MCU that is mostly sleeping?
Most of the time sleeping:
- Leakage is the main power eater
- circuit is mostly acting as resistor
- What hardware can we turn off on sleep
- What is the cost of waking up
Maximize sleep time: One wakeup is better than many smaller ones. Keep frequency high
How can we optimize power use for a MCU that is mostly awake?
Most of the time working:
- Transistor toggling the main power eater
- Can we reduce CPU frequency
- Can we reduce voltage
- reduce expensive operations (load/store)
Reduce workloads:
- frequency gridlocked with voltage
- CPU power scaling per V is cubic
- clocking down is better than sleeping
- reduce workload to allow cpu to do that
P_dynamic = CV²f
What is the CPU power formula?
P_cpu = P_dyn + P_sc + P_leak
When looking at storage constraints, what are factors we need to think about?
Optimizations:
- Store data efficiently
- generate data, don’t load them?
- Use the libraries we have available
- remove unused libraries
When looking at memory constraints (RAM), what are things we need to think about?
Algorithmic tradeoff:
- More CPU time - less memory
Code is either in RAM or ROM, if RAM
- Smaller code size -> less memory footprint
- keep track of what compiler puts in code
Stack cost - avoid recursion
use minimal datatypes, clever/simpler datastructures
structures alignes to the worst member. If one variable is 8 bytes(double), the structure must be 8-byte aligned (if power point was understood correctly?)
How can you set bits using bitmasks?
Use | operator (or)
How can you clear bits using bitmasks?
Use & operator (and)
How can you toggle bits using bitmasks?
Use ^ operator (xor)
How should you do conditional statements using bitmasks
if(KEY_A & KEY_DOWN)
Check if A has been pushed
What does the volatile keyword do?
Guarantees that the compiler won’t reorder volatile operations
Compiler won’t optimize away volatile operations