Part 1 - Introduction To ARM and The Basics of Microcontroller Programming
Part 1 - Introduction To ARM and The Basics of Microcontroller Programming
Microcontroller Programming
1
An Overview of the
Microprocessor and
Microcontroller
Systems
2
Microprocessor System
3
Microprocessor Bus System
4
Microcontroller System
5
Introduction to ARM
Based Processors
Advanced RISC Machines = Acorn RISC Machine
6
ARM - Introduction
They are Cortex-M based MCUs: ARM has become a sort of standard in the embedded world; this is especially true for Cortex-A
processors. Investing on this platform is a good choice.
Free ARM based tool-chain: it is possible to work with completely free tool-chains, without investing a lot of money to start
working with this platform.
Know-how reuse: STM32 is a quite extensive portfolio, which is based on a common denominator: their main CPU platform. This
ensures, for example, that know-how acquired working on a given STM32Fx CPU can easily be applied to other devices from the
same family. Moreover, working with Cortex-M processors allows you to reuse much of the acquired skills if you (or your boss)
decide to switch to Cortex-M MCUs from other vendors (assumption).
Pin-to-pin compatibility: most of STM32 MCUs are designed to be pin-to-pin compatible inside the extensive STM32 portfolio.
5V tolerant: Most STM32 pins are 5V tolerant. Level shifter is not required to interface other devices that do not provide 3.3V
I/O . Microcontroller interfacing to target device/s becoming easier.
32 cents for 32 bit: STM32F0 is the right choice if you want to migrate from 8/16-bit MCUs to a powerful and coherent platform,
while keeping a comparable target price. You can use an RTOS to boost your application and write much better code
Integrated bootloader: STM32 MCUs are equipped with an integrated bootloader, which allows the reprogramming of internal
flash memory using several communication peripherals (USART, I²C, etc.).
9
ARM - Disadvantages
Learning curve: STM32’s learning curve can be quite steep, especially for inexperienced users. ST documentation has not been
the best for inexperienced people, too vague and without clear examples.
Lack of official tools: ST does not provide a decent formal development environment (previously) like other manufacturers for
example Microchip. In fact, stm32CubeIDE was launched in 2019.
Fragmented and dispersive documentation: It is indeed a very large database on the ST website, but there is still a lack of good
documentation especially for its HAL. For beginners to the STM32 ecosystem and the Cortex-M world face, it is a very difficult
journey. We are trying to write a book to overcome this abyss.
Buggy HAL: HAL from ST contains some bugs, and some of them are really severe and cause confusion in beginners.
Lack of MCUs for the IoT: The Internet of Things is a current trend in electronics. Just recently (2019), ST launched MCU for IoT
applications.
10
CORTEX M0 – Functional Block Diagram
11
The STM32L0xx Architecture The 32-Bit Multilayer AHB Busmatrix connects the Master device to the Slave device
Allowing several masters on the bus to communicate with slaves simultaneously.
Cortex®-bus connects the DCode/ICode bus of
the Cortex®-M0+ core to the BusMatrix. This
bus is used by the core to fetch instructions, get
data and access the AHB/APB resources.
13
The STM32L053x6/8 Memory Map
• Program memory, data memory, registers and I/O ports are organized
within the same linear 4-Gbyte address space.
• The bytes are coded in memory in Little Endian format.
• The lowest numbered byte in a word is considered the word’s least
significant byte and the highest numbered byte the most significant.
• The addressable memory space is divided into 8 main blocks, of 512
Mbytes each
14
General-purpose inputs/outputs (GPIOs)
Each of the GPIO pins can be configured by software as output (push-pull or open-
drain), as input (with or without pull-up or pull-down) or as peripheral alternate
function. Most of the GPIO pins are shared with digital or analog alternate
functions, and can be individually remapped using dedicated alternate function
registers. All GPIOs are high current capable. Each GPIO output, speed can be
slowed (40 MHz, 10 MHz, 2 Hz, 400 kHz). The alternate function configuration of
I/Os can be locked if needed following a specific sequence in order to avoid
spurious writing to the I/O registers. The I/O controller is connected to a dedicated
IO bus with a toggling speed of up to 32 MHz.
15
General-purpose inputs/outputs (GPIOs) – Block Diagram
16
Basic structure of a 5-Volt tolerant I/O port bit
17
Input floating/pull up/pull down configurations
18
Output configuration
19
Alternate function configuration
20
High impedance analog configuration
21
Some basic circuits
Important for microcontroller connection to another device
22
Logic level – TTL & CMOS STANDARD TTL Typical
𝑉 ( ) 2.4𝑉 3.6𝑉
𝑉 ( ) 0.4𝑉 0.2𝑉
𝑉 ( ) 2.0𝑉
𝑉 ( ) 0.8𝑉
𝑉 𝑉 ( )
( ) STANDARD CMOS
Typical
𝑉 ( ) 𝑉 ( ) 𝑉 − 0.05 ≈𝑉
𝑉 ( )
𝑉 ( ) 𝑉 +0.05 ≈𝑉
𝑉 ( ) 0.7 𝑉
𝑉 ( ) 0.3 𝑉
𝑉 ( )
24
Current sourcing and sinking
25
Some definitions.
Active low switch: Switch produces logic ‘0’ when pressed, other case logic ‘1’
Active high switch: Switch produces logic ‘1’ when pressed, other case logic ‘0’
Active low LED: LED is activated (GLOW) when logic ‘0’ is applied.
Active high LED: LED is activated (GLOW) when logic ‘1’ is applied.
26
Active low & high outputs – TTL & CMOS
𝑉 ( ) −𝑉 ( )
𝑅 ≥
𝐼 ( )
𝑉 −𝑉 ( ) 𝑉 ( )
𝑅 ≥ 27
𝐼 ( )
Active low & high inputs – TTL & CMOS
𝑉 − 𝑉 ( )
𝑅 ≤
𝐼 ( )
𝑉 − 0.9𝑉
𝑅 =
𝐼 ( )
𝑉 ( ) −𝐺
𝑅 ≤
𝐼 ( )
0.5𝑉 ( )
𝑅 =
𝐼 ( )
28
Note: the equation ensures that the logic retains logic ‘1’ and logic ‘0’.
Pull Up
Require Require
No pull up/down pull up
29
Pull Down
Require Require
No pull up/down pull up
30
Open Drain/Collector
Sometimes
Require
No pull up/down
32
Flowcharts, Pseudocode
& State Machines
• Techniques for representing workflows or processes,
algorithms, step -by -step approaches to completing tasks
33
Flowcharts
Definition: A flowchart is a picture of the separate steps of a process in sequential order. It is
a generic tool that can be adapted for a wide variety of purposes, and can be used
to describe various processes, such as a manufacturing process, an administrative or
service process, or a project plan.
https://asq.org/quality-resources/flowchart
A graphical representation of a computer program in relation to its sequence of
functions
34
Flowchart
Block Propose Solution
Direct the codes development
TERMINATOR start/stop/end/return
PREDEFINED
function/ subroutine such delay_ms
PROCESS
35
Example Flowchart - Jackpot 1
36
Example Flowchart - Jackpot 2
SortToken
reg1<=reg1&3
reg2<=reg2&3
reg3<=reg3&3
T<=0
return T
37
Pseudocode
Definition: Pseudocode is an artificial and informal language that helps
programmers develop algorithms. Pseudocode is a "text-based" detail
(algorithmic) design tool. The rules of Pseudocode are reasonably
straightforward. All statements showing "dependency" are to be indented.
Available at: https://www.unf.edu/~broggio/cop2221/2221pseu.htm
38
Example Pseudocode - Jackpot 1
39
State Machines
Definition: A state diagram is a type of diagram used in computer
science and related fields to describe the behavior of systems.
Available at: https://en.wikipedia.org/wiki/State_diagram
40
Example State Machine – Using switch construct
IN
Normally uses switch case construct – allows only 1 sub-task to be processed at a time
Moving across the state is determined by flag/state
Flag/state can be fixed or modified in the program
s0? F Program can be added or removed easily
T s1? F
T
s2? F
Sub-task0
Sub-task1 T s3? F
T
Sub-task2
Sub-task3
Sub-task(n-1)
OUT
41
Example State Machine - Jackpot 1
43
Project Development
Control Validate
Assurance Verify
44
DEFINE
To clearly state accomplishment of the project
WHY? To study feasibility of the project
To ensure the project can, in fact be accomplished
Project proposal
•Summarize the definition phase
•Summarize the research and feasibility testing
•Written list of specifications
•Anticipated budget
•Schedule of completion
Note: This is also known as the Feasibility study •Investment result in a successful outcome 45
DESIGN
FILL in the MACRO BLOCKS with actual circuitry
WHAT? FORM the flowchart/software outline the project software
47
SOFTWARE DEVELOPMENT & SYSTEM INTEGRATION
WHAT? Writing and testing the software against the project specifications
RESULTS? Fully working project using the real or simulated input output
48
SYSTEM TEST
50
NUCLEO-L053R8 – Printed Circuit Board Look
51
NUCLEO-L053R8 – Pin Assignment
52
ST morpho connector on NUCLEO-L053R8
53
Arduino connectors on NUCLEO-L053R8
54
NUCLEO-L053R8 – Circuit Diagram (en.DM00105823)
55
NUCLEO-L053R8 – Blue Push Button & Green LED Circuit Diagram
(en.DM00105823)
56
Arduino Multi-Learning Shield
Features
3 x active low push button
4 x active low LEDs
1 x active buzzer
1 x on-board preset (0 – 5v)
1 x 4-multi 7-segment (common anode)
4 x servo/dc motor terinal (5v)
2 x external analog input (LM35)
57
Arduino Multi Functions Learning Shield – Circuit Diagram
58
Bluepill STM32F103C8T6 – Pin Assignment
59
* Please refer to DocID13587 Rev 17.pdf for detail of STM32F103C8T6
Bluepill STM32F103C8T6 – Schematic Circuit
60
References
[1] STML0x3 Data Sheet, DocID025844 Rev 7, October 2016
[2] STML0x3 Reference Manual, DocID025274 Rev 3, RM0367, May 2015
[3] Cortex®-M0 Programming manual, PM0223 Rev 5, October 2019
[4] STM32™ Nucleo boards, DocID025838 Rev 5, NUCLEO-XXXXRX, September 2014
[5] blue pill STM32 based, available at https://stm32-base.org/boards/STM32F103C8T6-Blue-Pill.html
61