References
Just found out about this series of videos. The author is the owner of Quantum Leaps, who has a nice tool for designing state machines. Their tutorials are also excellent.
In fact, one of his documents is already in the GDrive docs
folder:
AppNote - A Crash Course in UML State Machines
Go to Resources -> Application Notes & Articles
Timers
-
A Timer is a specific type of Peripheral — a piece of logic+hardware that is configured by writing bits to various Registers.
One instruction at a time
So far, if you were the CPU in a MCU (microcontroller unit) merrily executing code, this is how you would see if your WhizBang5000 order has arrived:
That works, but is not that great … unless you have nothing better to do.
What you really should do is:
-
Send a request to the delivery company to have the driver ring the doorbell when dropping off the package.
-
Engage in some other task on your TODO list.
-
When the doorbell rings, put down what you are working on in such a way that you can get back to it after returning from the door, and service the doorbell’s interruption
(figures from https://www.renesas.com/us/en/support/engineer-school/mcu-05)
These two strategies for checking if some condition or state has changed are called:
-
polling
-
interrupts
Study these two timelines to see the difference between these two techniques:
(figures from https://www.renesas.com/us/en/support/engineer-school/mcu-programming-peripherals-04-interrupts)
interrupt service routine (ISR)
A section of code (a function void handle_ISR_XYZ(void)
) whose execution is triggered only upon an interrupt event.
The CPU can only execute one instruction at a time, so how does this work?
-
CPU is executing some other code (perhaps in
main()
in some function called from there) -
An Interrupt event occurs. There are lots of sources for these events. Most are programmable and have something to do with a peripheral.
-
The CPU saves the value of the Program Counter to a safe place (the "stack"). It also saves the values of other important CPU registers (but not always all of them. The MSP430 has 16 registers, for reference).
-
The memory address of the first instruction of the interrupt service routine corresponding to the event is loaded into the program counter.
-
The CPU then continues, but now is running the ISR’s code.
-
When it reaches the end of the ISR function, the last instruction is (automatically added by the compiler) a reti or "return from interrupt".
-
The CPU restores the saved value of the PC and other registers that it saved.
-
Then it continues executing the code that it was running before the interrupt event, as if nothing happened. (People are terrible at this!)