02 Io
02 Io
02 Io
1
Embedded System
Ø Embedded computing system: any
device that includes a processing
system but is NOT a general-purpose
computer.
Ø Often application specific: takes
advantage of application characteristics output analog
to optimize the design
Ø Might have real-time requirements input analog
CPU
Ø Might be power constrained
mem
embedded
computer 2
Connecting Analog and Digital Worlds
ØCyber ØPhysical
§Digital §Continuum
§Discrete in Time §Continuous in time
§Sequential §Concurrent
3
Practical Issues
Ø Analog vs. digital
Ø Wired vs. wireless
Ø Serial vs. parallel
Ø Sampled or event triggered
Ø Bit rates
Ø Access control, security, authentication
Ø Physical connectors
Ø Electrical requirements (voltages and currents)
4
History of ARM Processor
5
ARM Cortex Processors
ARM Cortex-A family:
Applications processors
Support OS and high-performance
applications, such as smartphones, Smart TV
ARM Cortex-R family:
Real-time processors with high performance
and high reliability
Support real-time processing and mission-
critical control
ARM Cortex-M family:
Microcontroller, energy-efficient,
cost-sensitive, support SoC
6
Raspberry Pi – Know your board
Ø The Raspberry Pi 3 Model B+
§ Broadcom BCM2837B0, Cortex-
A53 (ARMv8) 64-bit SoC @
1.4GHz
§ 1GB LPDDR2 SDRAM
§ 2.4GHz and 5GHz IEEE
802.11.b/g/n/ac wireless LAN,
Bluetooth 4.2, BLE
§ Gigabit Ethernet over USB 2.0
(maximum throughput 300 Mbps)
§ Extended 40-pin GPIO header
§ Full-size HDMI
7
Raspberry Pi – Know your board
Ø The Raspberry Pi 3 Model B+
§ CSI camera port for connecting a
Raspberry Pi camera
§ DSI display port for connecting a
Raspberry Pi touchscreen display
§ 4-pole stereo output and composite
video port
§ Micro SD port for loading your
operating system and storing data
§ 5V/2.5A DC power input
§ Power-over-Ethernet (PoE) support
(requires separate PoE HAT)
8
ARM Peripherals BCM2837 Manual
9
Address Mapping
Ø Addresses in ARM Linux are:
• issued as virtual addresses by the ARM core,
• mapped into a physical address by the ARM/MMU,
• mapped into a bus address by the ARM mapping
MMU,
• used to select the appropriate peripheral or location in
RAM.
10
GPIO Pins
Ø https://pinout.xyz
11
Resistors and LEDs
12
Breadboard Connections
13
Dual In-Line Package or DIP
14
GPIO
Ø GPIO to
Breadboard
Interface Board
Ø GPIO Ribbon
Cable
Ø Breadboard
15
Convention
16
Circuit to Breadboard
Ø Use 3V
17
Circuit to Breadboard
Ø Use GPIO pin
18
sysfs – pseudo-filesystem
Ø The sysfs filesystem is a
pseudo-filesystem which
provides an interface to
kernel data structures.
Ø The files under sysfs
provide information about
devices, kernel modules,
filesystems, and other kernel
components.
19
Linux Kernel vs User Space
Ø The Linux kernel runs in an area of system memory called
the kernel space
Ø Regular user applications run in an area of system memory
called user space
Ø A hard boundary between these two spaces prevents
§ User applications from accessing memory and resources required by the
Linux kernel
§ Linux kernel from crashing due to badly written user code
§ Interfering one user’s applications with another
§ Provides a degree of security.
20
sysfs
Ø Paths in sysfs (/sys/class/gpio)
§ Control interfaces used to get userspace control over GPIOs
o export
o unexport
§ GPIOs themselves
§ GPIO controllers ("gpiochip" instances)
Ø GPIO signals have paths like /sys/class/gpio/gpioN/
§ "direction" - reads as either "in" or "out”
§ "value" - reads as either 0 (low) or 1 (high)
§ "edge" - reads as either "none", "rising", "falling", or "both”
§ "active_low" - reads as either 0 (false) or 1 (true)
21
Steps to perform I/O using sysfs
Ø Export the pin.
Ø Set the pin direction (input or output).
Ø If an output pin, set the level to low or high.
Ø If an input pin, read the pin's level (low or high).
Ø When done, unexport the pin.
22
Exporting GPIO control to userspace
Ø "export"
§ Userspace may ask the kernel to export control of a GPIO to
userspace by writing its number to this file.
§ Example: "echo 19 > export" will create a "gpio19" node for
GPIO #19, if that's not requested by kernel code.
Ø "unexport"
§ Reverses the effect of exporting to userspace.
§ Example: "echo 19 > unexport" will remove a "gpio19" node
exported using the "export" file.
23
Control GPIO with Linux
Ø Become the sudo user
§ dsaha@sahaPi:~ $ sudo su
Ø Go to the GPIO folder and list the contents
§ root@sahaPi:/home/dsaha# cd /sys/class/gpio/
§ root@sahaPi:/sys/class/gpio# ls
§ export gpiochip0 gpiochip128 unexport
Ø Export gpio 4
§ root@sahaPi:/sys/class/gpio# echo 4 > export
§ root@sahaPi:/sys/class/gpio# ls
§ export gpio4 gpiochip0 gpiochip128 unexport
24
Control GPIO with Linux
Ø Go to the gpio4 folder and list contents
§ root@sahaPi:/sys/class/gpio# cd gpio4/
§ root@sahaPi:/sys/class/gpio/gpio4# ls
§ active_low device direction edge power subsystem uevent value
Ø Set direction (in or out) of pin
§ root@sahaPi:/sys/class/gpio/gpio4# echo out > direction
Ø Set value to be 1 to turn on the LED
§ root@sahaPi:/sys/class/gpio/gpio4# echo 1 > value
25
Control GPIO with Linux
Ø Set value to be 0 to turn off the LED
§ root@sahaPi:/sys/class/gpio/gpio4# echo 0 > value
Ø Check the status (direction and value) of the pin
§ root@sahaPi:/sys/class/gpio/gpio4# cat direction
§ out
§ root@sahaPi:/sys/class/gpio/gpio4# cat value
§ 0
26
Control GPIO with Linux
Ø Ready to give up the control? Get out of gpio4 folder and list
contents, which shows gpio4 folder
§ root@sahaPi:/sys/class/gpio/gpio4# cd ../
§ root@sahaPi:/sys/class/gpio# ls
§ export gpio4 gpiochip0 gpiochip128 unexport
Ø Unexport gpio 4 and list contents showing removal of gpio4
folder
§ root@sahaPi:/sys/class/gpio# echo 4 > unexport
§ root@sahaPi:/sys/class/gpio# ls
§ export gpiochip0 gpiochip128 unexport
27
Program
Ø Bash Script
§ exploringrpi/chp05/bashLED/bashLED
Ø Python Code
§ exploringrpi/chp05/pythonLED/python2LED.py
Ø C code
§ exploringrpi/chp05/makeLED/makeLED.c
28
C/C++
ADVANTAGES DISADVANTAGES
You can build code directly on the RPi or you can cross-compile code
Compiled code is not portable. Code compiled for your x86 desktop will
using professional toolchains. Runtime environments do not need to be
not run on the RPi ARM processor.
installed.
C++ has full support for procedural programming, OOP, and support for Many consider the languages to be complex to master. There is a tendency
generics through the use of STL (Standard Template Library). to need to know everything before you can do anything.
It gives the best computational performance, especially if optimized. The use of pointers and the low-level control available makes code prone to
However, optimization can be difficult and can reduce the portability of memory leaks. With careful coding these can be avoided and can lead to
your code. efficiencies over dynamic memory management schemes.
Can be used for high-performance user-interface application development
By default, C and C++ do not support graphical user interfaces, network
on the RPi using third-party libraries. Libraries such as Qt and Boost
sockets, etc. Third-party libraries are required.
provide extensive additional libraries for components, networking, etc.
Offers low-level access to glibc for integrating with the Linux system. Not suitable for scripting (there is a C shell, csh, that does have syntax like
Programs can be setuid to root. C). You can integrate with Lua. Not ideal for web development either.
The Linux kernel is written in C and having knowledge of C/C++ can
C++ attempts to span from low-level to high-level programming tasks, but
help if you ever have to write device drivers or contribute to Linux kernel
it can be difficult to write very scalable enterprise or web applications.
development.
The C/C++ languages are ISO standards, not owned by a single company.
29
Building C/C++ Applications
30
Bash and Python Script
31
C Program
32
Use Rpi Library
Ø https://sourceforge.net/projects/raspberry-gpio-python/
Ø Note: Current release does not support SPI, I2C, 1-wire or
serial functionality on the RPi yet
33
Use gpiozero Library
Ø https://gpiozero.readthedocs.io/en/stable/
34
GPIO as Input
Ø Push-button Switch
35
Reading GPIO
36
Block Diagram
Ø Pg 89 of BCM2837
Ø Alternate functions
Ø Dedicated interrupt
Ø Pull-up/pull-down state
37
TTL & CMOS
Ø TTL (Transistor-transistor logic) & CMOS (complementary metal-oxide-
semiconductor)
Margin for 5V
Ø Noise Margin: absolute difference between the output voltage levels and
the input voltage levels.
Ø Floating Input: leave unused logic gate inputs “floating,” or disconnected
§ TTL: Float high, should tie to ground
§ CMOS
o inputs are sensitive to the high voltages
o from static electricity & electrical noise
o should never be left floating
38
Pull-down and Pull-up Resistors
Ø Used to ensure that the switches do not create floating inputs
Ø Pull-down resistors:
§ used to guarantee that the inputs to the gate are low when the switches are open
Ø Pull-up resistors:
§ used to guarantee that the inputs are high when the switches are open.
Ø The RPi has weak internal pull-up and pull-down resistors that
can be used for this purpose.
39
Voltage Divider Circuit
Vin = I(R1 + R2 )
Vout = IR2
R2
Vout = .Vin
R 1 + R2
40
Calculate Internal Resistor Value
Ø Voltage Divider
Address Mapped
42
/dev/mem
Ø /dev/mem is a character device file that is an
image of the main memory of the computer.
Ø Byte addresses in /dev/mem are interpreted as
physical memory addresses.
Ø References to nonexistent locations cause errors
to be returned.
43
Use /dev/mem directly
Ø wget http://www.lartmaker.nl/lartware/port/devmem2.c
Ø gcc devmem2.c -o devmem2
Ø ./devmem2
Usage: ./devmem2 { address } [ type [ data ] ]
address : memory address to act upon
type : access operation type : [b]yte, [h]alfword, [w]ord
data : data to be written
44
GPIO Pull-up/down Register Control
Ø The GPIO Pull-up/down Register controls the actuation of
the internal pull-up/down control line to ALL the GPIO pins.
This register must be used in conjunction with the 2
GPPUDCLKn registers.
Ø Note that it is not possible to read back the current Pull-
up/down settings and so it is the users’ responsibility to
‘remember’ which pull-up/downs are active. The reason for
this is that GPIO pull-ups are maintained even in power-down
mode when the core is off, when all register contents is lost.
45
Default Configuration
46
Alternate Functions of GPIO Pins
47
BCM 2837 Manual
Ø Table 6-1
Ø Table 6-28
48
BCM 2837 Manual
49
Control Pull-up/down (from BCM2837)
1. Write to GPPUD to set the required control signal (i.e. Pull-up or Pull-
Down or neither to remove the current Pull-up/down)
2. Wait 150 cycles – this provides the required set-up time for the control
signal
3. Write to GPPUDCLK0/1 to clock the control signal into the GPIO pads
you wish to modify – NOTE only the pads which receive a clock will be
modified, all others will retain their previous state.
4. Wait 150 cycles – this provides the required hold time for the control
signal
5. Write to GPPUD to remove the control signal
6. Write to GPPUDCLK0/1 to remove the clock
50
Pull Down Resistor is enabled
Ø Set bit 4 on the GPPUDCLK0 register, clear the GPPUD
register, and then remove the clock control signal from
GPPUDCLK0
§ GPIO4 is bit 4, which is 100002 (0x1016)
Ø Get the Value in GPIO 4
§ sudo su
§ cd /sys/class/gpio/
§ echo 4 > export
§ cd gpio4
§ cat value
51
Pull Down Resistor is enabled
Ø GPPUD Enable Pull-down
§ sudo /home/dsaha/myCode/devmem2 0x3F200094 w 0x01
Ø GPPUDCLK0 enable GPIO 4
§ sudo /home/dsaha/myCode/devmem2 0x3F200098 w 0x10
Ø GPPUD Disable Pull-down
§ sudo /home/dsaha/myCode/devmem2 0x3F200094 w 0x00
Ø GPPUDCLK0 disable Clk signal
§ sudo /home/dsaha/myCode/devmem2 0x3F200098 w 0x00
Ø cat value
§ 0
52
Pull up Configuration
Ø GPPUD Enable Pull-up
§ sudo /home/dsaha/myCode/devmem2 0x3F200094 w 0x02
Ø GPPUDCLK0 enable GPIO 4
§ sudo /home/dsaha/myCode/devmem2 0x3F200098 w 0x10
Ø GPPUD Disable Pull-up
§ sudo /home/dsaha/myCode/devmem2 0x3F200094 w 0x00
Ø GPPUDCLK0 disable Clk signal
§ sudo /home/dsaha/myCode/devmem2 0x3F200098 w 0x00
Ø cat value
§ 1
53
GPIO Function Select
Ø The function select registers are used to define the operation of the general-purpose
I/O pins. Each of the 54 GPIO pins has at least two alternative functions as defined
in section 16.2. The FSEL{n} field determines the functionality of the nth GPIO
pin. All unused alternative function lines are tied to ground and will output a “0” if
selected. All pins reset to normal GPIO input operation.
54
GPIO Function Select Register
55
GPIO Pin Output Set Registers
Address
Values
56
GPIO Pin Output Clear
57
GPIO Pin Level
Ø The pin level registers return the actual value of the pin. The
LEV{n} field gives the value of the respective GPIO pin.
58
WiringPi
59
The gpio Command (WiringPi)
60
wiringPi
Ø Functions
61
wiringPi Blink LED
http://wiringpi.com/examples/blink/ 62
Analog Output
Ø Pulse Width Modulation (PWM)
§ Technique that conforms a signal width, generally pulses
§ The general purpose is to control power delivery
§ The on-off behavior changes the average power of signal
§ Output signal alternates between on and off within a specified
period.
§ If signal toggles between on and off quicker than the load,
then the load is not affected by the toggling
63
PWM – Duty Cycle
Ø A measure of the time the modulated signal is in
its “high” state
Ø Generally recorded as the percentage of the signal
period where the signal is considered on
On Off
VH
Duty
VL Cycle (D)
Period (T)
64
Duty Cycle Formulation
On Off
Duty Cycle is determined by:
VH On Time
Duty Cycle = ´100%
Duty Period
Cycle (D)
VL *Average value of a signal
Period (T) can be found as:
1 T
y = ò f (t )dt
T 0
Vavg = D × VH + (1 - D ) × VL
*In general analysis, VL is taken as zero volts for simplicity.
65
PWM Duty Cycle
66
PWM Mode
Ø Counter counts
up to the range
provided
Ø When the
counter value is
higher than set
value, output is
high
67
PWM Duty Cycle Calculation
Ø The PWM device on the RPi is clocked at a fixed
base-clock frequency of 19.2 MHz
Ø Integer divisor and range values are used to tailor the
PWM frequency according to application
requirements
Ø 𝑓!"# = 19.2𝑀𝐻𝑧/(𝑑𝑖𝑣𝑖𝑠𝑜𝑟×𝑟𝑎𝑛𝑔𝑒)
Ø If 𝑓!"# is 10KHz (0.01MHz), and range is 128,
!".$%&'
§ 𝑑𝑖𝑣𝑖𝑠𝑜𝑟 = = 15
(!"# ×*+,-.
68
PWM0 and PWM1 Map
69
exploringPi/chp06/wiringPi/pwm.cpp
70