0% found this document useful (0 votes)
14 views571 pages

Emb Sys Design

The document outlines the course EECE216P: Embedded Systems Design and Programming, detailing learning outcomes such as understanding embedded systems, programming advanced robots, and analyzing hardware interfaces. It provides examples of various embedded systems across different applications, emphasizing the importance and prevalence of embedded devices in modern technology. Additionally, it discusses real-time systems, multitasking, and the necessity of operating systems in embedded devices for efficient task management and performance.

Uploaded by

e22mecu0009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views571 pages

Emb Sys Design

The document outlines the course EECE216P: Embedded Systems Design and Programming, detailing learning outcomes such as understanding embedded systems, programming advanced robots, and analyzing hardware interfaces. It provides examples of various embedded systems across different applications, emphasizing the importance and prevalence of embedded devices in modern technology. Additionally, it discusses real-time systems, multitasking, and the necessity of operating systems in embedded devices for efficient task management and performance.

Uploaded by

e22mecu0009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 571

Course Name: Embedded Systems Design

and Programming
Course Code: EECE216P

Module 1: Introduction to Embedded


System Design
Course Learning Outcomes
On completing the course, the students are expected to
CLO1: Understand and apply the fundamental concepts in
designing and programming the embedded systems
CLO2: Understand, design and programme advanced robots
using embedded boards.
CLO3: Analyse and write algorithms to interface with required
embedded hardware and develop Software for Embedded
Systems and its application areas
CLO4: Understand memory management and task scheduling
COL5: Understand Embedded Operating System, and real-
time operating systems for IoT uses and applications
End term project

 Proposed Project Design Review and


Proposal (1 week),
 Design Project Implementation (3 weeks),
 Project Demo and Presentation (1 week)
What is an Embedded System?

• Any electronic device that incorporates a


computer in its implementation.

• The user of the device is often not even


aware that a computer is present.

• The computer is used primarily to provide


flexibility and to simplify the system design.
Some Interesting Examples of
Embedded Systems
Product: Sonicare Elite
toothbrush.
Microprocessor: 8-bit
Has a programmable
speed control, timer,
and charge gauge
Product: Any PC
Mouse, Keyboard,
or USB Device

Microprocessor:
8-bit
Microcontroller
Inside view of a Microsoft Mouse
Product: Any Disk
Drive

Microprocessor: Dual
32-bit Marvel ARM
SOC & mixed signal
DSP
Product: Any
Printer

Microprocessor:
Intel, Motorola, or
ARM 32-bit RISC
Product:Vendo Vue
40 vending
machine.

Microprocessor:
Two 16-bit Hitachi
H8/300H
Processors
A robot hand
dispenses items
Product: Cannon EOS
30D Digital Camera
Microprocessor: DIGIC II
Image Processor
Product: NASA's Twin Mars
Rovers.

Microprocessor:
Radiation Hardened
20Mhz PowerPC

Commercial Real-time OS

Software and OS was


Photograph courtesy of NASA/JPL CALTECH
developed during multi-year
flight to Mars and
downloaded using a
deep space radio link
Product: Sony Aibo
ERS-7 Robotic Dog.

Microprocessor:
64-bit MIPS R7000.

OS: Aperios -
Sony’s Real Time
OS

Used in Robocup
Soccer Teams
Video: http://www.pleoworld.com/
Product: Pleo
robotic toy
dinosaur

Processors:
two 32-bit
(main is ARM)
and four 8-bit
microcontrollers
Pleo Autopsy Photos

From: http://www.ifixit.com/Guide/First-Look/Pleo
Pleo Processor Boards

From: http://www.ifixit.com/Guide/First-Look/Pleo
Product: Amazon’s Kindle DX eBook reader

Processor: Freescale
532 MHz, ARM-11
90 nm processor,
32MB main memory,
4 GB moviNAND
flash storage, 3G
modem, and a 3.7 V
1530 mAh lithium
polymer battery

OS: Linux
Product: Radiant
Systems Point-of-Sale
(POS) Terminal

Microprocessor:
Intel X86 Celeron

OS: Windows XP
Embedded

Look for one next time


you order fast food!
Product: High End
Systems DL2 Digital Media
Server
Microprocessor: X86
OS: Windows Embedded
Used for lighting effects in
shows and rock concerts
Product: New Apple Watch
Dual-core
ARM
Cortex-A9
CPU
Product: Agilent
Oscilloscope

Microprocessor:
X86

OS: Windows XP
Product: Tektronix
Logic Analyzer

Microprocessor:
X86

OS: Windows XP
Product: Kuka
Industrial Robot
Arm

Microprocessor:
X86

OS: Windows &


Others
Kuka robot arms welding a Mercedes
Product: National
Instruments
CompactRIO Industrial
Control and Data
Acquisition system

Processor: X86 & large


user programmable
FPGA

OS: Windows CE
(CE is now called IoT)

Applications can be developed using LabVIEW for several CE target


systems and PDAs. Different Analog and Digital I/O modules can be
selected and plugged into the CompactRIO chassis.
Product: IBM Self
Checkout Station

Microprocessor: X86

OS: Windows
Product: LagoTek
Home Automation
Controller

Microprocessor:
ARM + DSP

OS: Windows IoT


Product:
SmartPhones
ARM Processors in Smartphones
• ARM Cortex-A
family:
– Applications
processors
• ARM Cortex-R
family:
– Embedded
processors for real-
time signal
processing, control
applications
• ARM Cortex-M
family:
– Microcontroller-
oriented
processors for
MCU, ASSP, and
SoC applications
Product: Atronic Slot
Machine

Microprocessor: X86

OS: Windows IoT

Slots are networked to


a casino’s back office
computer system. State
Gaming agencies audit
code and payoff odds.
Product: Phantom 2 Vision Plus Drone
Product: Dresser Wayne
Ovation iX Gas Pump

Microprocessor:
Marvel Xscale (ARM)

OS: Windows IoT

Displays video ads &


is networked to a gas
station’s back office
computer system. Also has
remote maintenance features.
Product: Bernina
Artista 730 Sewing
Machine

Microprocessor:
Marvel StrongARM

OS: Windows IoT

Can download new


images from the
Internet and sew them
Product: Trimble
Total Station

Microprocessor:
Marvel Xscale
(ARM)

OS: Windows IoT


Product: Ford Sync

Microprocessor:
ARM

OS: Windows
Mobile now QNX
Product: MediaCart

OS: Windows IoT


Product: Slingbox
OS: Windows IoT
Product: ESPN
Universal Remote

OS: Windows IoT


• Product:
Momento WiFi
Picture Frame

OS: Windows IoT


Product: Nest Home
Thermostat

ARM processor with


color LCD,
temperature,
humidity, motion
sensors and Wi Fi
Product: Nest Home
Thermostat

ARM processor with RGB


LED, speaker, smoke,
CO, motion sensors,
Zigbee, and
Wi Fi
Product: Internet of Things -
Crockpot
Product: Next
Generation of
Washers and Dryers

Whirlpool, HP, and


Microsoft recently
tested WiFi-enabled
appliances that
send alerts
Product: Toto
Intelligence Toilet

It measures urine
sugar, blood
pressure, body fat
and weight on a daily
basis. This data can
be transferred over
the network.
Product: S class
Mercedes

Microprocessors:
around 100
embedded
processors and over
a hundred million
lines of code!
Product: Zoll
Automated
External
Defibrillator
(AED)

Commercial OS
Coca-Cola’s new
Freestyle is the
Most Advanced
Soda Fountain
Ever

OS: Windows IoT

http://www.youtube.com/watch?v=9V-yphI7JPk
iRobot AVG Telepresence Robot
More Examples of Embedded Systems
Aircraft & Aircraft autopilots, avionics and navigation
Military Systems systems, automatic landing systems,
guidance systems, engine controls.
Biomedical XRAY, MRI, and Ultrasound imaging
systems, patient monitors, heart pacers.
Cars Engine control, anti-lock braking systems,
traction control systems, air bag controls,
heating and air conditioning controls, GPS
mapping, Satellite Radio, On-board
Diagnostics.
Communications Communication Satellites, network routers,
switches, hubs.
Examples of Embedded Systems - Continued

Computer I/O Keyboards, mice, printers, scanners, displays,


devices modems, hard disk drives, DVD drives,
graphics cards, USB devices.
Electronic Data acquisition systems, oscilloscopes,
Instrumentation voltmeters, signal generators, logic analyzers.

Home Microwave ovens, dishwashers, DVD players,


Electronics televisions, stereos, security systems, lawn
sprinkler controls, thermostats, cameras, TVs,
clock radios, answering machines, satellite or
cable box, appliances.
Industrial Elevator controls, surveillance systems, robots,
Equipment CNC machines, Programmable Logic
Controllers, industrial automation and control
systems.
Examples of Embedded Systems - Continued
Office FAX machines, copiers, telephones,
Machines calculators, cash registers.
Personal Smart phones, portable MP3 players, Video
Devices players, electronic wrist watches, handheld
video games, digital cameras, GPS systems.

Robots Industrial robots, autonomous vehicles,


space exploration robots (i.e. Mars robots)
Toys Video Game systems, “Aibo”, "Furby“,
“Elmo”, and “Pleo” type robotic toys.
Importance of Embedded Systems
• Ratio of Embedded Devices / Desktop PCs is greater than
100
• The typical house may contain over 50 embedded
processors
• A high-end car can have over 100 embedded processors
• Embedded systems account for the most of the world’s
production of microprocessors!
Challenging System Design Goals
• Reliability
– Can’t Crash, may not be able to reboot
– Can’t update many embedded devices
• Performance & Power
– Real-time issues in many applications
– May need to run on battery power
– Limited memory and processing power
• Cost
– Fast time to market on new products
– Consumer Products are very cost competitive
Real-Time System
• A real-time system responds to events.

External Process External


Input Event New Data Output Event

Example: An Automobile airbag system.


When the airbag’s motion sensors detect a
collision, the system needs to respond by
deploying the airbag within 10ms or less.
– or the system fails!
Real-Time System
• A real-time system must respond to external inputs
and produce new outputs in a limited amount of time.
• The response time needs to be bounded. Response
times that are too long cause real-time systems to
fail.
• General purpose desktop Operating Systems are not
designed for real-time use
• Real-time Operating Systems need to be initially
designed for real-time use in order to provide fast
bounded response times
Hard & Soft Real-Time Systems
• Soft Real-Time System
– Critical tasks get priority. Normally meets the real-time response
constraint.
– Example: A Multimedia Player could skip a video frame or audio
sample occasionally and a user might not notice.
• Hard Real-Time System
– A new output response must be computed by the specified time
bound or the system will fail.
– Examples: In an aircraft flight control system when the pilot moves
the control yoke, the flight control surfaces need to move quickly
in response or the aircraft could crash.
– Virtual memory page swapping and garbage collection routines
needed for object oriented languages can cause problems in Hard
Real-Time systems
Bare metal, RTOS or OS?
• Fast Hard real-time tasks may require a
dedicated microcontroller without an
RTOS/OS (Bare metal) and even special
hardware such as FPGAs may be needed in
some cases
• An RTOS can provide a real-time response
in the range of a few milliseconds or less
• A General Purpose OS can take 10-100
milliseconds or even longer to respond!
Real-Time Performance of an OS
100 ms

Soft Real-Time
Response Cycle Time

20 ms

10 ms Windows?
Hard Hard Real-Time
Linux?
5 ms
Real-Time

Windows IoT
1 ms Mbed RTOS

500 us Bare Metal

0 100 µs 1,000 µs 5,000 µs 10,000 µs

Cycle Variation or Jitter (µs)

Real-Time Performance OS Measurements showing Response Cycle Time . Soft and Hard Real-Time
categories are based on OMAC study and OS times are without any third party real-time extensions to
the OS.
Using Additional Microcontrollers

• If a device has hard-real time constraints,


but still needs a general purpose OS to
run applications and networking, additional
microcontrollers are often used for hard
real-time tasks
• Take another look at these examples
– Smart Phone’s I/O microcontrollers
– Quad Copter’s electronic motor speed control
microcontrollers
ARM Processors in Smartphones
• ARM Cortex-A
family:
– Applications
processors
• ARM Cortex-R
family:
– Embedded
processors for real-
time signal
processing, control
applications
• ARM Cortex-M
family:
– Microcontroller-
oriented
processors for
MCU, ASSP, and
SoC applications
Product: Phantom 2 Vision Plus Drone
Using Additional Microcontrollers
• Examples of devices with internal
microcontrollers with firmware used in lab parts
kit
– Mbed’s USB interface
– uLCD module
– Wi Fi module
– Bluetooth module
– uSD card
– Sonar and Lidar sensor
– Some complex I2C devices such as IMUs
Multi-Tasking and Scheduling
• Many embedded systems are real-time systems with
several inputs and outputs. Multiple events are occurring
independently.
• Programming is simplified by separating the tasks, but this
requires the CPU to switch back and forth among the
different tasks
• An operating system that supports multitasking has
features to time slice the CPU among multiple tasks.
• OS provides Synchronization Primitives such as Mutex
locks and Semaphores
Product: TMIO
ConnectIO Oven

OS: Windows IoT

Also refrigerates food


before cooking and is
controlled over the
internet or via phone
Internet Oven: Example Tasks
•Control Oven Temperature (when cooking)
•Control Refrigeration Temperature (when cooling)
•Check for Keypad input and handle input
•Check cooking time to turn oven on/off (timed cooking)
•Update time and temperature display on front panel
•Check for Internet communications and handle messages
•Check for Phone communications and handle messages
Internet Oven: Tasks

• How would all of these tasks be


prioritized and scheduled?
• Is synchronization needed anywhere?
• What networking and communications
support is needed?
• Would an Operating System help?
Why have an OS in an embedded device?

• Support for multitasking, scheduling, and synchronization


• Support for a wide range of I/O devices
• Support for file systems
• Scheduling and buffering of I/O operations
• Support for networking
• Memory management
• Support for graphics displays
• Security and Power Management
Why have an OS in an embedded device?

Example:
A recent cell phone design contained over
five million lines of code!
• Few, if any projects will have the time and funding
needed to develop all of this code on their own!
• Typical Embedded OS license fees are free or only a
few dollars per device – less than a desktop OS
• Some very simple low-end devices might not need an
RTOS or OS – but new devices are getting more
complex
Typical Software Development Tools
Used for Embedded Applications
•Compiler - compile C/C++ and in-line assembly language
•Linker – links compiled application code, OS, and runtime
libraries
•Memory Image tools – tools to place code in non-volatile
memory at a given physical memory address
•Debugger – tool to debug OS and applications programs
•Loader – load OS at power on and applications. Also a tool
to download new code from the development system is
typically provided.
What is actually being used in
New Embedded Designs?
• What Types of Processors are used?
• What Operating Systems are used?
• What Programming Languages are used?
• Will examine data from a 2006 Market
Survey of design engineers by EETimes
and Embedded Systems Design Magazine
Processor Bit Size Used in
New Embedded Designs

Data was derived from EETimes and Embedded Systems Design Magazine 2006 Embedded Market Survey
Processor Architectures Widely
Used in New Embedded Designs

• ARM Cortex M microcontrollers


• ARM Cortex A with MMU (virtual
memory needed for Linux & Windows)
• X86 Processors from Intel and AMD
Processors Considered for
Next Design
Processor Selection Issues
• Software Support
– OS, Compilers, Debug Tools, Applications
• Price
• Performance
• Power
– Battery Life (MIPS/Watt), Cooling (Fan?)
• Desktop PC 100 W vs. Battery power 200 mw
• Availability
– Long term availability, Multiple Vendors?
Why not develop a Full Custom VLSI Processor?

• Too costly for the vast majority of designs


• Can take over a year and require
hundreds of design and test engineers
• Development of chip design and mask
setup costs run tens of millions of dollars
for a state of the art processor
• Use an off-the-shelf processor design and
existing support ICs whenever possible!
ARM Processors

• 32-bit RISC low-power design from an English IP


company, ARM ltd (Advanced RISC Machines)
http://www.arm.com/
• ARM’s processor designs are licensed to over 125 chip
manufacturers. ARM does not make chips.
• Used in many devices such as Cellphones, iPod Nano,
Cameras, Handheld Games, HDTVs, and Set-Top boxes.
80% of ARM processors are in phones
• Good performance/power makes it a very popular choice
in low power and battery operated devices.
ARM Processors
• ARM’s thumb instruction subset is coded into 16-bits and
decompressed on-the-fly to full 32-bit instructions. Can
switch from 16-bit to 32-bit instructions on the sub-routine
level.
• ARM processors range from small microcontrollers
(Cortex M) to processors with virtual memory support
(MMU – Memory Management Unit) and multiple cores
(Cortex A) that can run Linux or Windows. Some have
GPUs and DSP features.
X86 (IA-32) Processors
• Based on the Intel X86 CISC instruction set used in
processors in PCs since the mid 1980s
• Low cost due to widespread use in PC technology
• Processors, support chips and board level designs are
available from multiple vendors
• A wide processor performance range is available
• Most X86 processors for desktop PCs have been
optimized for performance and not low power
• Two PC processor vendors (Intel, AMD). Intel’s has some
new low power X86 processors being used in embedded
devices.
Number of Processors Used in
New Embedded Designs

Data was derived from EETimes and Embedded Systems Design Magazine 2006 Embedded Market Survey
Use of RTOS/OS Kernels
in New Embedded Designs
69% of new designs use an RTOS/OS, 31% do not
Open Source OS?
• “Free” OS might cost more for product development
• More time to develop kernel, device drivers & product can
increase labor cost more than the commercial OS license
fees saved in some cases
• Some other licenses still required for many devices in
addition to free OS (real-time kernel, browser, encoders &
decoders, encryption, media player)
• Open source license model may require that you publish
your device’s source code
• Internal ongoing support needed for device’s version of OS
can be significant. Linux has around 2K patches a year!
Who writes Linux?
Red Hat 12.4%
Novell 7.0%
IBM 6.9%
Unknown 6.4%
Intel 5.8%
Consultants 2.6%
Oracle 2.3%
Renesas Technology 1.4%
The Linux Foundation 1.3%
Academics 1.3%
Programming Languages Used in
New Embedded Designs

Data was derived from EETimes and Embedded Systems Design Magazine 2006 Embedded Market Survey
Conclusions
• Embedded devices can be found everywhere
in large numbers!
• Most new devices are using 32-bit processors
• New devices split almost evenly between, no
OS, commercial OS, or Open Source OS
• The C family (C, C++, C#,Java) is the most
widely used language for embedded systems
Additional Information
• See
– Embedded Software: Facts, Figures, and Futu
re
, IEEE Computer April 2009
– EE Times Embedded Markey Surveys
Product Life Cycle

Design Development Production


Phase Phase Phase

 Design Concept  Design Review  Pilot Production


 Product Planning  Build Prototype  Mass Production
 Product Testing  Technical Support

Figure 1.10 The Embedded device product life cycle phases, Design, Development, and Production.
Embedded System Design Cycle
• Short Product Development Cycle – 6 to
12 months (and getting shorter!)
• Software and Hardware must be designed
in parallel
• Software run on emulator or similar board
until final hardware becomes available
• Mass Production often moves to another
location or country
33%

Per-Unit Production Cost


42%
Direct Costs

Gross Margin

Average Discount
10%
15%

Elements that contribute to the list price of a product.


Data Flash
Processor Memory Memory
(RAM) (non-volatile
storage)

Most Embedded systems use two types of memory. RAM for main memory and Flash
memory instead of a hard disk to store the OS and application programs. Flash may be on
processor chip and/or a uSD card in small devices. RAM may be on the processor chip.
Memory in an embedded device
• Need both volatile and non-volatile memory
• Flash memory typically used instead of Hard
Disks
– Less power, smaller, and more reliable
• Store OS and applications in Flash memory
• Boot from Flash Memory at power up
• Flash is very slow compared to RAM
– Copy code to RAM to execute?
– Add a special Cache for Flash?
• Need software development tools that support
code storage and execution in ROM
Reliability in Embedded Devices
• Moving parts tend to wear out faster than
electronics!
• Would also like to avoid any cooling fans
whenever possible for both reliability and
power concerns
• Hard disk drives have a higher failure rate
than flash since they have moving parts
Flash and Solid State Drives (SSD)

•The reliability of traditional magnetic hard disk


drives can be problematic in many embedded
devices! – moving parts wear out
•Use new generation Flash memory chips
•Larger, Faster, and more write cycles than
earlier generation Flash devices
•Algorithm to distribute write wear means a
lifetime of several years (64G 5yrs min)
Samsung 1TB SSD for PCs

• Up to 5x more reliability
• Up to 10x faster than hard disk
• Up to 90x less power and heat
• Smaller and less weight
• But 4x cost of hard disk per bit
Power Management
• To minimize power consumption and
prolong battery life
– Slow down clock
– Turn off processor clock, wake on timer or I/O
interrupt – called sleep
– Power off unused hardware units – but will lose
values in registers so reinit is needed
– Read wiki example for mbed at:
https://developer.mbed.org/cookbook/Power-Ma
nagement
Brown Out Detection
• Circuit Detects Power Loss on Processor
– Generates interrupt on low supply voltage
– Could be Loss of AC or low battery
– A few ms. of power is left in capacitors
– Put I/O devices in fail safe mode
– Perhaps time to save a few critical values
– Stop processor before crash
– Read wiki example for mbed at:
https://developer.mbed.org/cookbook/WatchDog-T
imer#brown-out-detection
Introduction to Common
Embedded System User
I/O Devices
User Input Devices
• Study the examples on the Wiki page at:
https://developer.mbed.org/users/4180_1/no
tebook/user-input-devices/

• It contains an overview with links to


detailed code and wiring examples for
many common user input devices
• Many of these devices will be used in
laboratory projects later
User Output Devices
• Study the examples on the Wiki page at:
https://developer.mbed.org/users/4180_1/no
tebook/user-output-devices/

• It contains an overview with links to


detailed code and wiring examples for
many common user output devices
• Many of these devices will be used in
laboratory projects later
I/O Interfacing Standards
for External Devices
Common Digital I/O Interfacing
Standards for External Devices

•Parallel Port (GPIO)


•PWM
•Serial Port
•SPI & I2C serial bus to connect ICs
•Analog I/O
•USB
•CAN and LIN bus
•Driving Motors and Relays
Parallel Port transfer of an 8-bit
data value to a printer

Data Lines Data Valid

Busy

nStrobe

nAck

1 2 3 4
I/O Device Handshake Lines
• I/O Devices are a lot slower than the processor!
• In addition to data lines, need digital lines from
an I/O device that it is ready for another data
transfer (both for input and output data transfer)
• Computer must wait for these lines to be in the
correct state before another data transfer can
start!
• If this is not done – data values will be lost!
Parallel Printer I/O Operation
• 1. Computer waits for port’s output ready
bit (i.e. busy=0)
• 2. Computer outputs a new 8-bit data
value
• 3. Computer toggles strobe control line
• 4. Computer must wait for ack to pulse low
before any data or strobe line changes
• Printer can be busy for long time periods
(out of paper, offline, page feed…etc)
Legacy Parallel Ports
• Similar Parallel port/register style
interfaces with ready signals are still used
inside chips for I/O transfers and a few
logic bits (GPIO ports)
• Between chips and devices, it is now
cheaper to use high speed serial interfaces
– Pins can cost as much as 50,000+ gates!
– Cables and connectors with more wires cost a
lot more!
Hardware PWM
• PWM – Pulse Width Modulation
• Special hardware output pin(s) with a digital signal that
automatically turns on and off at a rapid rate
• On time (i.e., duty cycle of square wave) can be changed
in software by writing to an I/O register
• Clock time can also be changed in software – typically
only done once during initialization
• PWM hardware automatically pulses the output pin
• Software only needs to set the new duty cycle whenever
a change is needed
Pulse Width Modulation (PWM)

With fast PWM (ON/OFF square wave outputs) LEDs, motors, and speakers will
respond to the average DC voltage level. With transistors, PWM is significantly
more energy efficient than using analog outputs.
PWM Applications
• Most new microcontrollers have hardware PWM controller(s). A
driver circuit may be needed for high current and/or higher voltage
devices.
• More energy efficient than analog outputs
• Relays are too slow for PWM – must use transistor drivers
• Used for dimming LEDs, motor speed control and audio output
(Class D)
• Similar PWM idea used in higher efficiency switching power
supplies – a big power saver vs. linear supplies
• Can implement software PWM using a timer with interrupts when
PWM hardware is not available
RS-232 Serial Interface
• Transmit and Receive data lines
• No clock signal exchanged – Sender and
receiver need to have same baud rate
• Baud rate is the clock rate of the bits
• Normal Bits: Start, 8 Data, Stop
• Voltage levels: a 1 is >3V and a 0 is <-3V
• Special RS232 voltage level converter
chips are typically used
RS-232 Signals Used on Cables
RS-232 Serial Cable
Tx Rx
U U
8-bit 8-bit
A Rx Tx A
Parallel Parallel
Data R R Data
T Ground T

A minimum of three Signal wires are needed for RS232 Serial


Data Transfers over a cable. A UART converts from parallel to
serial and serial to parallel at each end. With optional hardware
handshaking more than three wires will be required. Note that Tx
(Transmit) connects to Rx (Receive) at other end. A “null modem”
cable does this swap in the cable and a regular cable does not.
RS-232 Serial Interface
• UART is the name for the hardware used
for a RS-232 Serial Interface
• UART – Universal Asynchronous Receiver
Transmitter
• Early PCs had a UART chip, but this
functionality is now found inside a larger
chip that also contains other I/O features
• Most microcontrollers have one or more
serial ports
RS-232C Serial interface
transmission of an 8-bit data value

0x50 = ASCII “P”

0 0 0 0 1 0 1 0
Mark (1)

Space (0)
Start 0 1 2 3 4 5 6 7 Stop
Bit Data Bit number Bit
LSB MSB

RS-232C Serial interface transmission of an 8-bit data value. Voltage levels


are >3 for a mark and <-3 for a space in the RS232 standard. Older
hardware can have voltage levels as high as 12V. This is not compatible
with standard logic levels and a voltage converter circuit is needed.
Serial with TTL levels
• On the same PCB, serial connections
often use TTL logic levels (not RS232
levels) with the same serial data bit pattern
and protocol seen in the previous RS232
slide.
• The same hardware and software is used,
but no RS232 voltage level conversion
circuits are used.
Serial Peripheral Interface (SPI)
• Developed by Motorola in the 1980s
• Works like a shift register for input and output
• 4 signals: clock, shift-in, shift-out, and chip
enable
• Chip enable turns on a tri-state driver on each
slave devices shift-out pin
• Two setup modes: one large shift register with
chip enables all connected or independent shift
registers each with a chip enable
• Common in A/D and D/A chips
The two SPI slave device
configuration options.
Slave 0
Slave
SDI SDO SDI SDO
Master Master
CS0 CS0
CS1 CS1
CS2 SDI CS2 Slave 1
Slave
SCLK SDO SCLK SDI SDO
SDO SDO

SDI SDI SDI Slave 2


Slave
SDO SDI SDO
Inter IC (I C) bus
2

• Developed by Philips in 1980s


• Low pin count serial interface used to connect chips
on a circuit board
• Also used by Intel in the PC motherboard System
Management Bus (SMB)
• 7 bit address
• 1 bit Read/Write
• 8 Data bits
• Probably have one in your TV
• Newer I2C chips have faster clocks
Basic I2C interface serial
transmission of an 8-bit data
value

SDA
MSB ACK Signal ACK Signal
from Slave from Receiver
Byte Complete Clock Line Held
Low while serviced

SCL 1 2 3-6 7 8 9 1 2 3-7 8 9


S R/W ACK ACK P
STA RT Address Data STOP
Many newer I2C devices support
multi-byte R/W Cycles
I2S
• Serial bus to send digital audio signals
• 2 clock lines, one stereo data line
• Mono or Stereo
• High sample rates possible
• Available in many ICs
• Mbed LPC1768 has I2S, but no APIs
• Sending digital audio can reduce noise
I2S Applications
• Microphone A/D IC to processor
• Processor to I2S speaker amp IC
– Pi Zero W add-on audio output board below
uses two I2S Class D amp ICs
CSI
• Camera Serial Interface
• Includes an I2C bus to send signals for
camera control and status
• Plus a high speed parallel data bus to
transfer digital image pixel data – number
of bits used varies, but six on Pi.
CSI
CSI Applications
• Used in most phone camera interfaces and the
Pi camera

The FFC cable has the CSI pins and power for the camera module
Analog I/O
• Analog inputs – convert to digital using an Analog to
Digital converter (A/D or ADC)
• Analog output – convert digital output to analog using a
Digital to Analog converter (D/A or DAC)
• A/D outputs and D/A inputs can be attached to digital
I/O ports
• Design issues to consider – number of bits of accuracy,
conversion time delay, and sample rate needed by
application. Carefully examine noise levels and the
non-linear response of sensors, amplifiers, low pass
filters, and A/D or D/A devices
Analog I/O
• Analog sensors will pick up noise from power
supplies along with other circuits used in the
analog portion of a design
• Many power supplies have enough noise to impact
the accuracy of analog sensor readings.
• Add a filter or decoupling capacitor near the
analog devices and/or consider a separate analog
power supply running to analog devices only.
Digital devices will add noise to a DC power
supply.
The MCP3008 10-bit 200Ksps A/D chip is used in USB modules and has an SPI interface.
Signal Conditioning
on Analog Inputs
Analog Signal Microprocessor
Sensor Conditioning A/D input pin
Voltage Circuit

The voltage output from an analog sensor often needs to be scaled in a


signal conditioning circuit so that the sensor’s voltage output range
matches the A/D input voltage range (i.e., 0 to 3.3V on mbed) for
maximum accuracy (i.e., all A/D bits can change)
This is done using a high input impedance low-noise amplifier (or
attenuator) and perhaps a low pass filter (recall DSP sampling theory).
Op Amps are often used. In some sensors, it is more complex than just
scaling the sensor voltage.
Signal Conditioning
References
• Free NI reference on signal conditioning:
https://lumen.ni.com/nicif/US/GB_EKITGUIDESI
GCON/content.xhtml

• Free TI reference on signal conditioning:


https://store.ti.com/ANALOGPRGBOOK.aspx
Signal Conditioning Example
• Most microphones will need a circuit with a gain near
100 and an audio low pass filter prior to the
microprocessor’s A/D input pin.
• One Solution: Rail to rail op amps with a pot to adjust
gain (0..100) on this breakout board along with an 16Khz
audio low pass filter with a fixed DC offset of 1.65V (i.e.
3.3V/2). There are even special microphone pre amp
ICs.
Universal Serial Bus (USB)
• 1-bit serial sent over twisted pair wire
• USB Packets contain address and data
• Up to 127 devices on bus
• Special purpose low-cost USB single chip
microcontrollers are used.
• Length is limited to a few feet – a bus and
not a network
VGA – 15-pin D-sub Connector

USB connector pins


5 1 6
Pin # Signal Name Pin # Signal Name Pin # Signal Name
1 MRed 6 GND 11 NC
2 MGreen 7 GND 12 Vcc
3 MBlue 8 GND 13 HSYNC
15 11
4 NC 9 NC 14 VSYNC
5 GND 10 GND 15 Vcc

USB - For connection to external USB


1 4 Pin # Signal Name Pin # Signal Name Pin # Signal Name
1 Vcc 3 USB+ 5 NC
2 USB– 4 GND 6 NC

COM – 9-pin D-sub Connector

1 5 Pin # Signal Name Pin # Signal Name Pin # Signal Name


1 DCD 4 DTR 7 RTS
2 RXD 5 GND 8 CTS
6 9 3 TXD 6 DSR 9 RI

PS/2 - Keyboard and Mouse – 6-pin Mini-Din

2 1 Pin # Signal Name Pin # Signal Name Pin # Signal Name


6 3 1 KBCLK 3 GND 5 PMDATA
5 4 2 PMCLK 4 KBDATA 6 SB5V
A low-cost USB Microcontroller
is typically used in USB Devices

+5V
bytes
D+ bytes USB Program & Data
Serial I/O Ports
D- Interface (R AM & ROM)
Interface
Engine
(SIE)
General
GND Purpose
Microprocessor
USB USB
Connector Transceiver
USB Data Rates
• USB 2.0 – Up to 480 Mbps
• USB 1.0 – 12 Mbps and 1.5 Mbps
• USB 2.0 supports USB 1.0 devices
• Needed higher USB 2.0 data rates for
external drives and video
• USB 3.0 is around 10x USB 2.0
• New USB C – 20X faster and cables cannot
be plugged in upside down – but the 24-pin
cables cost more
USB Enumeration
• Hot pluggable – longer power and ground
(outer) pins on USB connector
• At power up, software (OS) can read
product and vendor ID codes (PID/VID)
• Codes are used to locate device driver
• The required Device Driver is loaded
• The new device is assigned its USB
address (0..127) with a command
The USB Serial Interface Engine
(SIE) handles USB packets

D C D C
A E C A E C
O A Payload R A O A Payload R A
D N R D N R
U T Data C C U T Data C C
D D C D D C
T A 1 K T A 1 K
R P 5 R P 5
1 6 0 6
Token Packet Data Packet H/S Pkt Token Packet Data Packet H/S Pkt

Payload
D ata

D+ Serial Payload
Interface D ata
Engine
D- (SIE) A
C
K
USB
Transceiver
USB Packets
• Contain a packet type
• USB Address and Endpoint fields
• Each device can have several endpoints
• Endpoint is a hardware buffer between the
SIE and the firmware running on the
microcontroller (typically 8-64 bytes)
• CRC error code checked and Ack sent
back
New 24-pin USB C connector

Don’t need to flip connector or cable and


provides up to 20A at 5V for device power
USB C pin functions
Serial Interface Engine (SIE)
• Special hardware unit that handles
sending and receiving high speed serial
USB packets
• Serial data rates needed are too high to
handle using only software running on the
microcontroller
• Microcontroller only transfers bytes to/from
endpoints in the SIE
USB Software
• OS typically has a USB driver that handles
enumeration and loads driver for device
• Some common USB device drivers may
be provided with OS (storage, serial,
keyboard, mouse)
• Most other devices need a device driver
from the manufacturer
OS USB Software Support
• OS handles USB enumeration
• Typically Includes USB storage and serial
driver
• Some common USB drivers included
(Flash, WiFi, Mouse, Keyboard)
Figure 3.9 A USB protocol analyzer captures and displays the USB packets exchanged with a USB
Flash Drive (www.usbdeveloper.com) .
ARM
• Processor IP company in Cambridge England, now owned by
Softbank(Japan)
• The most widely used processor family, 45 million ARM processors made
every day by over 450 companies. 100 billion to date.
• ARM processors are a pipelined RISC design, similar to the now defunct
MIPS (Microprocessor without Interlocked Pipelined Stages) processors
used in many courses and textbooks
• ARM does not make ICs, they license the processor design (IP-Intellectual
Property) to other companies that fab ICs
• Targeted for low-power battery-operated mobile devices years earlier than
X86 processors. Found in almost all cell phones.
ARM’s mbed Rapid Prototyping
Technology
• Originally an internal research project at ARM to make it
easier for students to develop prototypes for embedded
devices – now also used in industry
• ARM/Keil C++ Cloud Compiler runs in any web browser – no
software to install, industry standard compiler
• Easy to use C++ class I/O APIs
• Supported on over 100 boards from over twenty vendors
• Microcontroller boards make it easy to build prototypes with a
student breadboard
mbed Rapid Prototyping Platform
• Web based Hardware and Software
Solution

Dedicated Developer Website Online C++ Compiler

High-level I/O APIs Easy Prototyping Form-Factor


mbed Cloud Compiler
• Online Compiler
– Web 2.0 browser-based IDE with personal workspace “in the cloud”
– Nothing to install or configure, login from anywhere
– Runs industry standard ARM/Keil full C++ compiler
mbed Cloud Compiler
• Can also export mbed projects and use offline compilers
with hardware breakpoints using the USB cable
• Some offline C++ compilers will require a license fee and
are more complex to install and run
• With the Cloud compiler, students can develop and run
code on mbed anywhere with only a PC and an Internet
connection
• Still easier and faster for student projects to use the cloud
compiler!
mbed USB Flash drive
• Mbed boards show up just like a USB flash drive
(PCs or Macs)
• Run cloud compiler in web browser, it returns a new
code file in browser
• To download, save code file to mbed’s flash drive
• Hit reset pushbutton on mbed, and newest code file
runs
• USB cable also powers a student breadboard with
3.3 and 5V DC. USB limit is <500MA on power.
Mbed USB Virtual Com Port
• USB cable also is a virtual com port
• Can R/W characters from a terminal application
running on a PC running Windows or on a MAC
• In Windows, a USB driver install is needed and
• Download a free terminal application for PC
• Connect to new COM port on PC in terminal
application
• Mbed code can then printf output to PC
mbed C++ I/O APIs
• ARM’s Mbed library provides easy to use C++ classes
to control I/O hardware (just add #include mbed.h)
• ARM/Keil Compiler supports full C++ standard
• C++ Constructor automatically selects I/O pin functions
and initializes I/O hardware units to default values
• Don’t need to use ugly 32-bit I/O register
addresses in code
• Don’t need to know bit number and location of
control & status bits
• Higher level C++ I/O APIs save a lot of
development time
mbed I/O APIs
• Member functions for hardware setup and options
• C++ Operator overloading makes I/O hardware work a
bit like the standard C assignment statements.
led = 1; //writes to led with overload of ‘=‘
foo = pushbutton; //reads pushbutton with overload of int(..)
• Same APIs will work on any of over 100 mbed boards,
but initial pin number assignments will need to change
mbed I/O APIs
• The mbed handbook contains a detailed reference
Wiki page for each mbed I/O API -
developer.mbed.org/handbook
• Each of the example I/O devices seen in these slides
has example mbed wiring and “hello world” code in the
mbed component pages or user notebook pages
• Searching the mbed site using Google tends to work
better than the search engine at mbed.org
Widely Used mbed I/O APIs
• wait - Wait for a specified time (in sec)
• DigitalIn - Configure and control a digital input pin.
• DigitalOut - Configure and control a digital output pin.
• PwmOut - Pulse-width modulated (PWM) output
• InterruptIn - Trigger an event when an input pin changes.
• AnalogIn - Read the voltage applied to an analog input pin
• AnalogOut - Set the voltage of an analog output pin
• Serial - Serial/UART bus
• SPI - SPI bus master
• I2C - I²C bus master
mbed Pin functions
• On most modern microprocessors, I/O pins can
be used for several different I/O functions. The
function selected depends on each application’s
I/O requirements.
• An I/O pin function register selects the pin’s I/O
function
• Pin I/O functions are automatically programmed
by the mbed APIs
• Illegal pin functions generate a run-time error.
On LPC1768, all LEDs flash and program stops
mbed Pin functions
• LPC1768 pins operate using 0.0 to 3.3V
logic levels
• All pins are 5V tolerant
• 3.3V or 5V devices can be connected
without the need for any logic level
conversion ICs or resistors. Some other
microprocessor’s require these circuits!
• Use 3.3V devices when possible to save
power on new designs
mbed LPC1768 Pin functions

The colors above indicate the different possible pin I/O functions.
Light blue indicates basic DigitalIn/Out pin use is also possible
mbed wait API
• wait(x); waits for time period x (in seconds).
• Used to add small time delays to code
• Some I/O applications will require small time
delays to work correctly
• Note: When using the RTOS in thread code later,
will need to use Thread::wait(mS); instead of wait
DigitalOut Applications
• For digital devices that need a few control bits
• Also called GPIO (General Purpose IO)
• Used to turn on/off LEDs
• Driver circuit will be needed for higher current or
higher voltage devices (>15-40mA, and/or >5V)
including motors, solenoids, relays, and high
power LEDs
DigitalOut Applications
LEDs used for basic ON/OFF indicators
Most small LEDs need a 80-220 ohm series resistor!
– should have only 2-3V across LED terminals
Longer LED wire lead is +, shorter one – or ground
DigitalOut Applications
• Chip select, enable, or reset lines on some I/O
ICs
• Some older and/or high speed devices use a
parallel data interface (several DigitalOut bits) –
BusOut or PortOut are faster for this use).
• Most new I/O devices use a serial interface (i.e.,
Serial, SPI, I2C, USB) to save IC pins, PCB area,
and cost. A pin on an IC package can cost as
much as 50,000 transistors inside the chip.
DigitalOut Applications
• Turn a transistor driver circuit on/off which
then turns on/off a higher current and/or
voltage I/O device
• Relays will even need a transistor driver
circuit for the required coil current!
DigitalOut Applications
• Basic on/off control of a motor or solenoid
• Requires a transistor driver circuit to boost current and
also perhaps voltage (12-24VDC at a few amps is not
uncommon)
• Higher current/voltage devices might also use a relay or
a high power MOSFET
DigitalOut Applications
• Control a driver circuit for a stepper motor with 4-bits
• Stepper motors provide low cost position control
DigitalOut Applications
• For on/off control of AC devices such as small home
appliances, a relay, solid state relay, or TRIAC is
typically used that is controlled by the digital out logic bit
• Circuit is available in low-cost small plug-in AC modules that are
a bit safer for students to use – just add 2 wires for digital signal
and gnd – Keeps high AC voltages off of breadboard!
mbed DigitalOut API

#include "mbed.h"

DigitalOut myled(LED1);

int main() {
while(1) {
myled = 1;
wait(0.2);
myled = 0;
wait(0.2);
}
}
mbed DigitalOut API

• LED1..4 can be used for built-in LEDs on mbed LPC1768

• Outputs can drive 3.3V or 5V logic levels. Older


microcontroller chips might need special logic level
conversion chips for this to convert to/from 3.3 and 5V.

• BusOut and PortOut APIs are faster for multiple bits

• Drivers are needed for higher current loads (>20-40 MA)

• C++ operator overloading makes a statement like myled =


1; work in mbed APIs!
DigitalIn Applications
• Used to read switch inputs and other
simple digital signals
• Often used for switches and pushbuttons,
but switch debouncing may be required!
• Other digital devices that need to read a
few status bits
• InterruptIn may be a faster alternative for
some signals (i.e., does not require
constant software polling of input)
DigitalIn Applications

Read Pushbuttons, DIP switches, limit switch…


SPST switches need a pullup or pulldown resistor
mbed DigitalIn API
#include "mbed.h“

DigitalIn enable(p5);
DigitalOut led(LED1);

int main() {
while(1) {
if(enable) {
led = !led; //Blinks if enable input high
}
wait(0.25);
}
}
mbed DigitalIn API
• .mode() controls internal pullups use PullUp, PullDown,
PullNone as arguments. Default is PullDown. SPST
switches need a Pullup or PullDown!

• Inputs can be 3.3V or 5V logic levels

• Pushbutton inputs probably need to be debounced!

• BusIn and PortIn are faster alternatives for multiple bits


Interrupt Applications
• In hardware, a digital signal change forces the processor to
jump to a C++ function (called an ISR Interrupt Service
Routine) after finishing the current instruction and saving the
current machine state on stack
• The C++ ISR function runs, machine state is popped off the
stack and execution resumes at the next instruction in the
function that was interrupted.
• Interrupt routines need to be short and fast!
• Interrupts occurring too frequently will overflow stack!
InterruptIn Applications
• Used to detect an external asynchronous event
as fast as possible. Polling constantly in
software is significantly slower and takes more
processor time and power.
• Often used for pushbuttons, but switch
debounce in software is likely to be required
• Some SPI and I2C sensors and other I/O
devices will generate an interrupt signal for a pin
whenever new data is ready.
mbed InterruptIn API
#include "mbed.h“
InterruptIn button(p5);
DigitalOut led(LED1);
DigitalOut flash(LED4);

void flip() {
led = !led;
}

int main() {
button.rise(&flip); //jump to flip function on the rising edge
while(1) { // wait around, interrupts will interrupt this!
flash = !flash;
wait(0.25);
}
}
mbed InterruptIn API

• Works on most, but not all pins

• .rise() interrupts on rising edge

• .fall() interrupts on falling edge

• Can do both .rise() and .fall() with different


functions

• Keep interrupt routines as short and fast as


possible. Do not use printf() or wait() or other
slow or non re-entrant functions in interrupt
routines!
PWM Applications
Dimming single color LEDs
Can dim built-in LEDs1..4 on LPC1768 using PWM
Color and Dimming in RGB LEDs with three PWM
bits
PWM Applications
Class D Audio Output uses a PWM clock around 10X
highest audio frequency. Speaker will respond to average
DC value. Used in Class D Audio Amp ICs.
PWM Applications
RC Servos use a PWM control signal with a 1-2 ms wide
pulse sent every 20 ms. DC motor, gears, driver and
control circuit is inside the servo
PWM Applications
• Other RC devices use servo PWM signals
such as ESCs (
Electronic Speed Control Modules) for Bru
shless DC motors
)
PWM Applications
• Micro stepping on newest stepper motor
driver ICs
• Stepper motor coils turned on partially with PWM
allow some intermediate movement control
between steps (magnetic poles built into motor’s
armature)
• Strength control of pull on solenoids
• A few solenoid applications use PWM to vary the
speed and pull strength of solenoids
• Variable Frequency Drive systems used on
large AC motors and Brushless DC motors
mbed PwmOut API Hello World
#include "mbed.h"

PwmOut led(LED1);

int main() {
while(1) {
for(float p = 0.0f; p < 1.0f; p += 0.1f) {
led = p; //dim led using PWM
wait(0.1);
}
}
}
mbed PwmOut API Hello World

• .write(float) 0.0 is 0% duty cycle, 1.0 is 100%


duty cycle (i.e. high all of the time)

• .period() is the period of the output. 50Hz is


default. 50Hz is used for RC Servos and
dimming LEDs. Motors can hum at 50Hz and
may need a frequency of several thousand Hz.
For Audio output, want 10x highest audio
frequency.

• Even with multiple PWM outputs some devices


still only have one PWM clock circuit
PWM Applications
DC Motor Speed Control using a transistor driver circuit
Reversible DC motors need an H-bridge driver
with four power transistors. Dual H-bridge driver ICs
are common.
The Need for Additional Power
• Motors, Servos, Relays, loud Speakers, Wireless modules, or
using several IO devices on a breadboard at the same time will
require more power!
• USB 2.0 cables can provide only 5V 500ma power
• mbed power/activity LED blinks off when too much current is
drawn. A MOSFET on the board turns off power to protect the
PC’s USB power circuits at around 460ma.
• The mbed board uses around 200ma, leaving <260ma for all
other external IO devices on the breadboard.
Adding Additional Power
• Use 5V 2A AC adapter and jack for more breadboard power or another
5V DC regulated power supply – put high current device on this new
second supply
• Connect grounds only (not + outputs!) when multiple power supplies are
used!
• Double check +- power leads!!!!!! – a mistake can blow up ICs
• Can also add a 3.3V low dropout voltage regulator for more 3.3V power
on breadboard, if needed.
Running mbed LPC1768 on Battery Power

• Hook up a 5-12V battery pack (such as one in


robot kit) to Vin. Voltage can be 4.5 to 14.0, but
recall that battery voltage drops under load and
jumps higher with no load
Running mbed LPC1768 on Battery Power

• USB 5V Rechargeable Battery Packs are


another low cost option
LPC1768 Battery Power
• Vbat is for real time clock circuit battery backup only!
• On board voltage regulator provides 3.3V power for
processor and Vout pin.
• USB cable should not also be connected at the same time
as a battery. Download code and then unplug USB cable
• No 5V output on Vu when USB cable not plugged in
• Add other low dropout switching voltage regulators to battery
output when more current or other voltages are needed
LPC1768 Battery Power
• Motors, servos, relays, solenoids, and other high current devices
should sometimes be connected directly to battery
– Check datasheet for voltage tolerance levels of device
– More regulated supply output current available for other devices
– Motors and servos have a large initial inrush current spike (perhaps 3X or
more the normal value)
– Reduces noise during switching on processor’s power supply
• In some power setups, switching on a high current load or instantly reversing a motor can
briefly drop the power supply voltage low enough to reset the processor!
– Larger gauge and shorter wires can help reduce noise a bit along with large
decoupling capacitors near the high current device
Serial Applications
• Most microcontrollers have at least one serial UART port
• Used for buffered bidirectional asynchronous
communication link between two microprocessors on same
PCB (Using Serial with digital logic levels and not RS232
+/- voltage levels)
• RS232 Serial works on long cables, but needs a voltage
converter chip. Max cable length decreases with increasing
baud rate
• Max baud rate only a few MHz or less and 115K for RS232
Serial. UART needs a clock 8-16x baud rate and buffers
only 4-8 characters in hardware
• Serial data is typically ASCII character strings, but can be
binary
Serial Applications
• RS232 Com ports on PCs – more common on older PCs
and devices
• USB virtual com ports on PC and mbed simulate a serial
port and work the same in software, but use USB
hardware and cables
• The Serial driver may be interrupt driven with a large
software buffer with perhaps 0.25-2K characters in each
direction. Often needed at high baud rates to avoid
character loss.
• A few devices require extra serial hardware handshake
signals. This stops data flow and avoids character loss.
Only common on high baud rate devices. Not used on
most devices.
Serial Applications

Smart Color LCD module


Contains a microcontroller with firmware and a serial port
Serial Applications
PC Com ports use RS232 Serial signal levels (<-3 and>3 volts)
Serial Applications

• “RS232” serial ports (like PC COM Port) will need a voltage level
conversion circuit. If not “RS232” , normal (TTL) logic levels are
used for serial interfaces.
• Special RS232 voltage level conversion ICs (i.e., MAX232) are
available just for RS232 signals
Serial Applications
Mbed and other USB Virtual Com Ports such as USB to RS232 cables
simulate and/or convert to serial signals in software using a USB
microcontroller and a USB cable. Normal serial APIs can be used at
both ends.
Serial Applications
Many mbed boards have a second small ARM USB microcontroller used
to download code to the main processor just like a flash drive. This
processor on the back of the LPC1768 board, has firmware that also
provides a USB virtual com port. In software, it works just like a serial
port on both mbed and the PC (but signal is on USB cable!).
Serial Applications

• Most GPS receiver modules use serial I/O


pins. 4800 baud is common.
• GPS Data sent as ASCII strings in
NMEA format
Serial Applications
• Most Cell Phone radio modules use serial
I/O pins
• Commands sent as ASCII strings in AT
command format
Serial Applications
• Many small thermal printers use a serial
interface
• Common in Cash registers and ATM
machines
mbed Serial API Hello World

#include "mbed.h"
Serial pc(USBTX, USBRX); // USB tx, rx
int main()
{
pc.printf("Hello World!\n");
while(1) {
pc.putc(pc.getc() + 1);
}
}
mbed Serial API

• Printf() defaults to USB virtual com port (serial)

• .baud (int baudrate) sets baud rate. Default is 9600


baud. If garbage characters are seen, baud rate is
likely incorrect

• .putc() and .getc() to send and receive an ASCII


character. Printf() and scanf() calls them using
virtual functions.

• .readable() and .writeable() is often handy to check


if device is ready prior to .putc() or .getc() (which
will block and not return until ready)
mbed Serial API
• C++ cin and cout will also work by adding the
required include file.

• Most embedded programmers tend to avoid using


cin and cout since they increase code size by 20-
40KB which can be a huge amount of memory on a
small microcontroller – but it is not an issue on
processors with GBs of RAM. Most of this added
code also never gets called (sometimes called
“Dead code”). This is why mbed examples always
show the use of printf() instead. It does mean a bit
of extra work dealing with those ugly printf format
strings! Even printf increases code size by up to
20KB
AnalogIn Applications
• Reads voltage from external analog
sensors
• Used for audio input from microphones
• Used for analog joysticks and
potentiometers
• Many SPI and I2C devices also contain
analog sensor(s) but they convert it to a
digital signal internally with an A/D. This
approach also helps a bit to reduce analog
signal noise levels.
AnalogIn Applications
• Read analog voltage from a potentiometer
• Used in many devices such as a volume
control knobs
• Analog joysticks have two potentiometers
(X and Y directions)
AnalogIn Applications
• Analog voltage input from other analog
sensors such as the Sharp IR distance
and TMP36 temperature sensor below.
Max analog input level is 3.3V.
AnalogIn Applications
• Analog input from microphones
• A preamp circuit is needed for
microphones to scale to correct analog
input range for max A/D accuracy
mbed AnalogIn API Hello World

#include "mbed.h"
//Visual volt meter using LEDs
AnalogIn my_pot(A0);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

int main() {
while (1){
led1 = (my_pot > 0.2f) ? 1 : 0;
led2 = (my_pot > 0.4f) ? 1 : 0;
led3 = (my_pot > 0.6f) ? 1 : 0;
led4 = (my_pot > 0.8f) ? 1 : 0;
}
}
mbed AnalogIn API

• API float return value of 0.0 is 0.0 volts


external (min), 1.0 is 3.3V (max)
• Signal conditioning circuits may be needed
to scale analog input from 0 to 3.3V range
for more A/D accuracy
• Low pass filter may also be required
• Sample rate around 40Khz max using API.
A/D conversion time and software overhead
• Analog noise is always an issue!
mbed AnalogOut API

• API float arg value 0.0 is 0.0 volts external, 1.0


is 3.3V (max)

• Sample rate around 40Khz max using API

• Low pass filter may also be required

• Driver needed for high current load (>20MA)


AnalogOut Applications
• Used for audio output
• Driver circuit needed to drive a speaker
• A few other external I/O devices may need an
analog signal
• PWM is more energy efficient than AnalogOut in
most cases (i.e., Audio, LEDs, Motors)
• Using all unit16_t for output and avoiding any
floating point ops is a bit faster – but it needs
careful scaling by the user!
AnalogOut Applications

• Audio output to drive a speaker


• Speakers will need a driver/amp circuit
AnalogOut Applications
• Audio output signal - Line in signal for another
audio device
• Can add a resistor divider circuit to drop voltage to
<1V (needed for typical audio line in levels)
• Use a breakout board with an audio jack for
other audio devices such as headphones, PC
speakers, or PC audio input cables
mbed AnalogOut API – float Arg
#include "mbed.h"
// The sinewave is created on this pin
AnalogOut aout(p18);

int main()
{
const double pi = 3.141592653589793238462;
const double amplitude = 0.5f;
double rads = 0.0;
float sample = 0;
while(1) {
// sinewave output
for (int i = 0; i < 360; i++) {
rads = (pi * i) / 180.0f;
sample = amplitude * (cos(rads + pi) + 1.0) ;
aout = sample;
}
}
}
mbed AnalogOut API – Int Arg

#include "mbed.h"
// The sinewave is created on this pin
AnalogOut aout(p18);

int main()
{
const double pi = 3.141592653589793238462;
const double amplitude = 0.5f;
const double offset = 65535/2; //scaled for 16-bits unsigned
double rads = 0.0;
uint16_t sample = 0;
while(1) {
// sinewave output
for (int i = 0; i < 360; i++) {
rads = (pi * i) / 180.0f;
sample = (uint16_t)(amplitude * (offset * (cos(rads + pi))) +
offset);
aout.write_u16(sample);
}
}
}
SPI Applications
• Works like a big shift register with a clock line and data
• Can hook up multiple SPI devices in a chain to form one
long shift register
• More bandwidth than serial or I2C (driver circuit supports
a faster clock & fewer address bits are needed than I2C)
• Used for SD cards
• Used for high speed A/D and D/A chips
• Newer sensors often support both SPI and I2C
• Hardware SPI controllers on most new microcontrollers,
but can be done in software
SPI Applications

Simple I/O Port Expander ICs


Used to provide extra digital I/O pins
SPI Applications

SD and uSD cards


SPI Applications
• Chainable RGB LED driver ICs
• Driver IC has SPI control interface, but uses 3
PWM bits on LED
SPI Pins
• Sck – clock used for SPI shift registers
• MOSI – Master Output Slave Input, used for
writes
• MIS0 – Master Input Slave Output, used to read
• CE or CS – Chip enable pin on each SPI IC.
Turns off the tri-state driver on SPI IC’s output
pin. Allows SPI IC’s to be connected in parallel
on an SPI bus and activated one at a time.
Requires a GPIO digital output pin to use or can
be always enabled in long serial chains of SPI
ICs.
mbed SPI API
#include "mbed.h"

SPI spi(p5, p6, p7); // mosi, miso, sclk


DigitalOut cs(p8);

int main() {
cs = 1;
// Setup spi for 8 bit data, high steady state clock,
// second edge capture, with a 1MHz clock rate
spi.format(8,3);
spi.frequency(1000000);
// Select the device by seting chip select low
cs = 0;
// Send 0x8f, the command to read the WHOAMI register
spi.write(0x8F);
// Must write to read it – send dummy byte
int whoami = spi.write(0x00);
printf("WHOAMI register = 0x%X\n", whoami);
}
mbed SPI API

• .format(# bits, mode(clock edge to use)) up to


16 bits, but default is 8 bits

• .frequency() is the clock frequency. 1Mhz


default. Want a faster rate for new SD cards
perhaps up to 25 to 50Mhz.

• Must use a write to read (like a shift register)

• Some new SPI chips send commands hidden in


data bits
SPI Modes
• Mode 0..3 controls clock inversion and using
the rising or falling clock edge to trigger
input and output
• SPI APIs default mode value is 0, 3 also
common
Clock Polarity Clock Phase Clock Edge
SPI Mode
(CPOL/CKP) (CPHA) (CKE/NCPHA)
0 0 0 1
1 0 1 0
2 1 0 1
3 1 1 0
I2C Applications
• Common in many Sensor ICs
• I2C addressing supports multiple devices
on one I2C bus
• Newer complex sensors often support
both SPI and I2C using different I/O pins –
need to pick one for design
• Hardware I2C controllers on most new
microcontrollers, but can be done in
software (called bit-banging)
I2C Applications
Basic I2C 8-bit Digital Port Expander
I2C address only - Just need to write/read
data bits
I2C Applications
I2C Touch keypad controller with multiple registers
In addition to the I2C address, need to write a
register address before each I2C data R/W
I2C Applications
• Time of Flight (ToF) sensor with I2C
address and registers
• IC contains a laser and a single photon
avalanche diode array. Delay in detecting
laser dot’s reflection measures distance
I2C Applications
This 9DOF IMU chip contains three I2C
devices with three different addresses and
each device has multiple registers
mbed I2C API Basic Hello World

//I2C driver class for PCF8574


class PCF8574 {
public:
// Create an instance of the PCF8574
// connected to specfied I2C pins & address
PCF8574(PinName sda, PinName scl, int address);

int read(); //I2C read


void write(int data);//I2C write
private:
I2C _i2c;
int _address;
};
mbed I2C API Basic Hello World
PCF8574::PCF8574(PinName sda, PinName scl, int address)
: _i2c(sda, scl) {
_address = address;
}

int PCF8574::read() {
char foo[1];
_i2c.read(_address, foo, 1);
return foo[0];
}

void PCF8574::write(int data) {


char foo[1];
foo[0] = data;
_i2c.write(_address, foo, 1);
}
mbed I2C API Basic Hello World

#include "mbed.h"
#include "PCF8574.h"

PCF8574 my_io(p9,p10,0x70);//I2C address from datasheet is 0x70


int main()
{//reads in one bit and writes to another
while(1) {
//my_io.write(my_io.read() >> 7) needed without overloads
my_io = my_io>>7; //operator overloads like other mbed APIs
//for auto read and write instead of function calls
}
}
mbed I2C API Hello World
#include "mbed.h"
// Read temperature from LM75B
I2C my_i2c(p28, p27);
const int addr = 0x90; //LM75B I2C Address
int main() {
char cmd[2]; //Holds I2C r/w data values
while (1) {
cmd[0] = 0x01;
cmd[1] = 0x00;
my_i2c.write(addr, cmd, 2); //Write 0 to config
register (1)
wait(0.5);
cmd[0] = 0x00;
my_i2c.write(addr, cmd, 1);//select temperature
register (0)
my_i2c.read(addr, cmd, 2);//Read two bytes (16-
bits)
float tmp = (float((cmd[0]<<8)|cmd[1]) / 256.0);
printf("Temp = %.2f\n", tmp);
}
mbed I2C API

• Pullups are required on I2C SCL and SDA open


collector lines. 2.2K to 4.7K ohm typical value. Internal
pullups not correct value for this purpose. Use only one
set of pullups when multiple devices on one I2C bus.

• I2C API may hang without pullups and/or with incorrect


I2C address

• Check schematic for pullups on new breakout boards –


some have them and some do not!

• Cookbook has a handy I2C address test program which


scans and returns all of the valid I2C device addresses
mbed I2C API

• mbed API uses 8 bit I2C addresses, so make sure to take


any 7 bit address from datasheets and left shift by 1

• .frequency() Sets I2C clock frequency. Older devices may


need a slower I2C clock near 100 or 400Khz. Default is
1Mhz.

• May need to set a R/W register address in addition to I2C


address on newer complex devices such as IMUs

• Some Devices will auto increment register address


Many newer I2C devices and mbed APIs
support faster multi-byte R/W Cycles
mbed Timer APIs

• Timer - Create, start, stop and read a timer


• Timeout - Call a function after a specified delay
• Ticker - Repeatedly call a function
• wait - Wait for a specified time
• time - Get and set the realtime clock
Mbed Timer APIs

• All use one hardware timer (timer 3 on LPC1768)

• Use hardware timers for short time delays (<30


min)

• Use Real Time Clock (RTC) time for longer delays

• Must set RTC before it starts running

• External interrupts can also start and stop timers


mbed Timer Applications
•Sonar – time delay*speed of sound is distance
measurement
•Sonar driver uses a timer with interrupts
mbed Timer Applications
•Most processors have multiple timers
– Several General Purpose
– Watchdog
– OS time slice
– Real Time Clock

•A Timer is needed in an Operating System for Time


Slice Interrupts (1-100ms typical)
Mbed Timer Tutorial
• There are several additional types of
hardware timers found on most
microcontrollers.
• The following tutorial describes which
applications that they are typically used
for:
– Using Hardware Timers
mbed USB Examples
• On LPC7168 mbed board, a USB Virtual Com
Port is included and the on-board flash appears
as a USB flash drive using firmware in the USB
microcontroller on back. Not all mbed boards
support additional USB host/slave interfaces
• USB drivers are more complex than other I/O
interfaces. Most USB and network drivers have
threads and need the RTOS
• Flash drive, mouse, keyboard and HID device ex
amples
mbed USB Examples
• LPC1768 chip has a USB interface that
can support using mbed as a USB Slave
or Host device, but it also needs a USB
connector breakout board and a USB
software driver
mbed Filesystem
• Filesystem drivers are available for SD cards or a
USB Flash drive – just add breakout board and plug in
media
• PC can also R/W the files when media is mounted on PC
• Filesystem calls several C++ virtual functions for R/W–
so can be ported to other digital storage media with
additional work
mbed CAN bus Examples
• Used in cars to send data between
microcontrollers and read OBD (On-Board
Diagnostic) II error codes
• Needs a CAN driver board and OBD II cable for
LPC1768
mbed CAN bus Examples
• Only a few very basic OBD II & CAN codes are
public domain – limits student projects
• Codes used to help repair cars by plugging in
OBD II cable under dash and using a OBD II
code reader
I/O Device Interfacing Case Studies

• Several examples of different I/O devices


with code and wiring information for parts
used in laboratory projects are available
on wiki pages
• These will now be examined in depth
• They will use the standard hardware I/O
interfaces introduced earlier
• Code examples use the mbed APIs
I/O Device Interfacing Case Studies

• Digital Input, Output and Interrupts


– Using Pushbuttons and Debouncing
– Relays
– Solenoids
– Stepper Motors
– Navigation Switch (Digital Joystick)
I/O Device Interfacing Case Studies

• PWM
– Dimming LEDs
– Using RBG LEDs
– Audio Output with a Speaker
– DC Motor Speed Control
– RC Servos
– Electronic Speed Control modules (ESC)
I/O Device Interfacing Case Studies

• Analog Input
– Potentiometer
– Temperature Sensor
– Light Sensor
– MEMs Microphone
– Analog Joystick
I/O Device Interfacing Case Studies

• Analog Output
– Audio output using a speaker
I/O Device Interfacing Case Studies

• I2C
– I/O Expander
– Touch keypad
– LIDAR distance sensor
– 9 DOF IMU
I/O Device Interfacing Case Studies

• SPI
– I/O Expander
– Using a uSD card for file I/O
I/O Device Interfacing Case Studies

• USB Host Device


– Using a USB Flash Drive
– Using a USB Keyboard
– Using a USB Mouse
• USB Slave Device
– Simulating USB Mouse input
– Simulating USB Keyboard input
I/O Device Interfacing Case Studies

• Serial
– Using a TTL serial LCD module
– Using an RS232 serial device
– Using a GPS module
– Interfacing to other microprocessors
• Wi Fi module
• Bluetooth module
• iRobot Roomba or Create Robot
Driving Motors and Relays
• High current devices like motors, relays,
solenoids, buzzers, speakers, and high output
LEDs can require more than 500MA of current
• Even though voltage levels may be the same,
digital outputs from a GPIO (parallel) port typically
drive only 5-20MA of current
• They cannot drive high current devices directly
and trying to do so will likely blow out the output
circuit
• In some cases, driver circuits will also need to
boost voltage levels in addition to current
Driver Circuits
• A higher current driver circuit must be added
after the digital output pin and before the device
• A driver circuit typically uses a discrete power
transistor to amplify current levels
• For DC motors, consider using an H-bridge
circuit module. It contains four power transistors
than can also reverse the motor.
• Diodes are often used for additional protection
across the load on motors and relays. When you
turn off the current in an inductive load it
generates a reverse voltage spike that might
damage the transistor (back EMF). The diode
shorts it out.
Driving a high current Load

Vcc
Discrete Power
Digital Transistor

Logic LOAD
Control
Signal Optional Voltage
Suppression Diode
Use Decoupling Capacitors
on Power Supply lines!

• Decoupling or bypass capacitors should be placed across the


power supply lines (Vcc and GND) on each board or near the high
current device.
• These capacitors help filter out the noise spikes on the power
supply that are generated by switching high current loads.
• Processor chips also need them as shown in the manufacturer’s
data sheets!
Decoupling Capacitors
Vcc

10-1000uf .01-.1uf
Electrolytic Ceramic
GND
•Two capacitors (made of different dielectric materials) are
typically connected in parallel from Vcc to GND.
• A larger electrolytic capacitor (i.e., 100+ uf) and a smaller one
(i.e., .01 to .1 uf) in parallel are needed to filter noise
This Phidgets relay board contains two electromechanical
relays that can switch loads up to 5 A at 240 VAC or 10 A
at 100 VDC. The maximum operation speed is 20 times
per minute and lifetime is 105 to 107 cycles. The control
input is a digital logic signal. The control input feeds into a
discrete power transistor that provides the higher current
levels needed to drive the relay’s coil (5V at 75 mA).
Limitations of Mechanical Relays
Easy way to switch high current and/or high
voltage loads and provides electrical
isolation, but…
• Number of lifetime ON/OFF cycles is limited by
mechanical wear (typically 106 to 107 cycles)
• Slow ON/OFF times – around 20 per minute
• Contacts can wear out due to arcing on inductive loads
(perhaps as few as 105 cycles) even on rated loads
• Oxidation on relay contacts can be a problem on low
voltage analog signals (around 2V needed to initially
punch through oxidation layer between metal contacts)
• Worn out relays will sometimes stick
H-Bridge - DC Motor Driver Circuit
H-Bridge Control Functions
Input Function Operation
10 Forward DC Motor runs in the
forward direction
01 Reverse DC Motor runs in the
reverse direction
00 Stop Motor is not connected
– Coasts
11 Brake* or Motor Terminals
Shorted or
Short Power Supply Power Supply
(not allowed!) Shorted!
*The Brake function requires a more complex decoder circuit to control the power
transistors. Check the H-Bridge data sheet to make sure it is supported before using
it. In some old very simple H-Bridge circuits using only one type of power transistor
(i.e., N or P), the fourth state must be avoided (i.e., illegal state) and it will short out
the power supply! Most new H-bridge ICs now support the brake feature, but in
some cases it might still overheat the IC when used too frequently.
H-Bridge Example - Forward

HIGH LOW
H-Bridge Example - Reverse

LOW HIGH
Figure 3.10 Fairchild FAN8100N Low Voltage Dual H-Bridge DC Motor Driver IC.
Images courtesy of Fairchild Semiconductor.
Higher current H-Bridge modules often use discrete power
transistors assembled on a board. This dual H-Bridge module
switches up to 10 amps at 24V DC. The eight power
transistors can be see arranged on the right side of the board.
Photograph courtesy of RoboticsConnection.
This ST VNH2SP30 30Amp DC MOSFET motor driver IC
contains an H-bridge circuit. It supports 5V PWM speed
control up to 20Khz. Image courtesy of www.pololu.com.
Electromechanical Relays

This electromechanical relay contains an electromagnetic coil (right side of


image) that moves a metal arm to make and break an electrical connection.
Photograph courtesy of Omron.
Electromechanical Relay Limitations -
why other approach may be needed

•The number of lifetime ON/OFF cycles is limited by mechanical wear (typically


106 to 107 cycles)

•They have slow ON/OFF times – around 20 per minute. Too slow for motor
speed control or dimming lights.

•Relay contacts can wear out due to arcing on inductive loads (perhaps as few
as 105 cycles) even on rated loads.

•Oxidation on relay contacts can be a problem on low voltage analog signals.


(around 2 volts is needed to initially punch through the oxidation layer that
occurs between any two metal contacts)

•Worn out relays will sometimes stick due to mechanical wear and an audible
click is typically heard during switching.
Solid State Relays
• Optical isolation between input and output load
• Unlike electromechanical relays, No moving parts
• May be fast enough for motor speed control and dimming lights
• Resistant to shock and vibration
• 150X more reliable than mechanical relays

The NEC SSR chip family seen above can switch


AC or DC from .2 to 2 Amps at 60-600 Volts
This optically isolated solid state relay module can control a
120V AC 10 Amp circuit using a digital logic input signal.
Photograph courtesy of Opto22.
NEC SSR Device Schematic
INPUT Photovoltaic OUTPUT
GaAs LED Diode Array FET Switch

Diode Stack Thyristor/Charge Control


This Phidgets SSR board uses an NEC SSR IC and
can switch 2 Amps at 40 VDC or 28 VAC. A digital logic
output bit controls it. Typically, this digital control signal
would be a bit coming from a parallel I/O (GPIO) output
port on a computer.
Mbed Driver Tutorial and Code
Examples
• See the wiki page at:
– https://developer.mbed.org/users/4180_1/
notebook/relays1/
• It contains links to mbed code and wiring
examples of driver ICs, breakout boards,
relays, H-bridge modules, AC dimmers,
and other devices used to drive high
power and high voltage devices using a
microprocessor.
I/O Device Interfacing Case Studies

• More complex systems with several I/O


devices and drivers:
– Model Trains
– ESC Motor drivers for Quadcopters
– Shadow Robot Base
– IoT Clock using uLCD and Ethernet
Introduction to Embedded
System I/O Architectures
and Buses
A single bus architecture was used on
early computers.

Processor Memory I/O


Hardware

System Bus

But Modern systems are more complex and


actually contain a hierarchy of different busses!
MBED board

96MHz 32-bit ARM Cortex


M3, 32KB RAM, 512KB flash
C++ libraries
Free web-based software
tools
Programmed via USB
Ethernet, USB slave
2xSPI, 2xI2C, 3xUART,
1xCAN
GPIO, 6xPWM, 6xADCs,
1xDAC
40-pin DIP, 0.1" pitch
www.mbed.org
Mbed ARM Cortex M3
processor on NXP 1768
SoC microcontroller
chip

•512K FLASH and 32K


SRAM

•Note all of the I/O


features (bottom half of
figure) are attached to
the processor using an
internal bus, ARM’s
AHB

NXP LPC 1768 Block Diagram


A First Generation Bus Example: ISA

• ISA bus used in early PCs for Plug-in Cards


• Address Bus (SAx)
– Originally 16 bits then expanded to 24
• Data Bus (SDx)
– 8 bits then expanded to16 bits
– EISA later expanded it to 32 bits
• Bus Status Signals
– MEMR, MEMW, IOR, IOW
• 5V TTL signals
Digital Logic Review:
A simple address decoder circuit for
4-bit address (A3..A0) = 0xA = 1010B

A3
Address
A2 Decoder
A1 Output
A0

Would need to decode more address bits in an actual system


Tri-state logic gate outputs
are used to drive most bus signals
tri-state
control input output control
0 0 High Z input
0 1 High Z
1 0 0
1 1 1 input output
(bus)

New third High Z state means “not connected”

Hardware allows only one tri-state gate at a time to drive a bus signal!

Works just like a large multiplexer:


One of several inputs connects to the output
Open Collector (OC) Output
• Older and slower alternative to tri-state outputs –
can connect multiple OC outputs together
• OC gate outputs only have a circuit to drive output
low (nothing in circuit to pull high)
• 1 external resistor (1-10Kohm?) is added to pull it
high (when nothing drives it low)
• Multiple OC outputs can be tied together and any
one could pull it low
• OC is still used on I2C SDA and SCL pins
• An OC bus will not work without the resistor!
Legacy PC I/O address assignments

I/0 address range I/O device


000h – 200h Reserved for Internal Devices:
Interrupt & DMA controllers, timers
278h - 27Fh Parallel Printer (LPTx:)
2E8h - 2EFh Serial Port 4 (COM4:)
2F8h - 2FFh Serial Port 2 (COM2:)
378h - 37Fh Parallel Printer (LPT1:)
3B0h - 3BBh MDA Adapter
3BCh - 3BFh Parallel Printer (LPTx:)
3C0h - 3CFh VGA/EGA Adapter
3D0h - 3DFh CGA Adapter
3E8h - 3EFh Serial Port 3 (COM3:)
3F0h - 3F7h Floppy Controller
3F8h - 3FFh Serial Port 1 (COM1:)

Original PC design only decoded low 10 I/O address bits to save hardware.
Each I/O device must have a unique address on a bus!
An Example ISA Bus I/O Write Operation

Bus Clock

Address Valid Address

I/O Write

Data Valid Data

Clock data into a


register on this edge!
An Example ISA Bus I/O Read Operation

Bus Clock

Valid Address
Address

I/O Read

Data Valid Data


Typical I/O Input Port
Hardware Operation

Bus I/O Read Command


Device’s I/O Address Decoded
Address Tri-state
Address Bus Decoder Control
Circuit Data bit x
Data Bus bit x in from
I/O Device

One tri-state
gate is needed
for each bit on
the data bus
Typical I/O Output Port Hardware
Operation

R
Address e
Address Bus Decoder g
Circuit Data Bus i Data out to
s I/O Device
t
e
r

Device’s I/O Address Decoded


Bus I/O Write Command Clock
Options for Building Custom I/O ICs

• Full custom VLSI devices and Application


Specific Integrated Circuits (ASICs) have very
high setup costs – a very large volume is
required to recover development cost. Typically
can be made only by the major chip vendors.
• Field Programmable Gate Arrays (FPGAs) are
programmed by the end user. Low development
costs, but higher unit costs, slower clock rates,
and higher power levels. – only a moderate
volume needed to recover development costs
Using an HDL to design I/O hardware
-- VHDL-based Address Decoder for 0x3E0
PORT_IO_DECODE <= '1' WHEN ADDRESS = X"3E0" AND
AEN='0' ELSE '0';

-- VHDL-based I/O Input Port - use tri state buffers


DATA <= PORT_DATA_IN WHEN PORT_IO_DECODE = '1' AND
IOR = '0' ELSE "ZZZZZZZZ";

-- VHDL-based I/O Output Port – use a register (with DFFs)


PROCESS
BEGIN
-- clock on positive edge of ISA IOW
WAIT UNTIL IOW'EVENT AND IOW='1';
-- use address decoder output for the DFF clock enable
IF PORT_IO_DECODE = '1' THEN
-- save data on ISA data bus in register
PORT_DATA_OUT <= DATA;
END IF;
END PROCESS;
Software for I/O port transfers
• Can use in-line assembly language in C/C+
+
• Most C/C++ compilers have built-in function
calls for I/O port input and output
• Other languages may have support for I/O
and/or typically can call C/C++ routines, but
details vary
• On RISC processors (i.e., ARM), pointers
can be used to directly access I/O ports
In-line Assembly Example for X86

// I/O Input Routine // I/O Output Routine


__asm{ __asm{
mov dx, IO_address mov dx,IO_address
in al, dx mov al,IO_data
mov IO_data,al out dx, al
} }

Problems: Does not port to other processors and many people do not
understand assembly language!
ARM Assembly Language
• See mbed wiki page on ARM assembly
language:
– https://developer.mbed.org/cookbook/
Assembly-Language
• I/O registers (ports) are memory mapped
on RISC processors, so Load and Store
instructions can be used directly for I/O
(unlike “in” and “out” special case on X86)
Example X86 I/O Port R/W Functions

• READ_PORT_UCHAR(I/O_Address)
– Returns 8-bit input value from input port
• WRITE_PORT_UCHAR(I/O_Address,I/O_Data)
– Sends 8-bit data value to output port
• Used in some versions of Windows
• Typically used in code for low-level device
drivers
ARM I/O Registers
• On RISC processors, I/O registers are memory
mapped, but not on X86 processors.
• C pointers set to the correct address are the
fastest way to access these I/O registers (faster
than function calls). Pointers are used in mbed’s
I/O API library code.
• Wiki page with details and examples at:
– https://developer.mbed.org/users/4180_1/notebook/
cc-io-register-names/
A Second Generation Bus - PCI

• 32-bit Multiplexed Address and Data Bus (AD)


• Address sent on first clock cycle
• Bus command sent on first clock cycle (C/BE)
• Data on subsequent clock cycles
• Bus Clock rates 33 to 512Mhz
• One Data transfer per clock is possible
• Supports Data Bursts (example to follow)
• ARM processors have a similar bus, AMBA
PCI Bus Commands (C/BE)
PCI Bus Command C/BE
Interrupt Acknowledge 0000
Special Cycle 0001
I/O Read 0010
I/O Write 0011
Reserved 0100
Reserved 0101
Memory Read 0110
Memory Write 0111
Reserved 1000
Reserved 1001
Configuration Read 1010
Configuration Write 1011
Memory Read Multiple 1100
Dual Address Cycle 1101
Memory Read Line 1110
Memory Write and Invalidate 1111
PCI Read Burst Cycle
0 1 2 3 4 5 6 7 8

Bus Clock

F rame #

AD Address Data1 Data2 Data3

C/BE# Bus-Cmd BE#’s

IRDY#

TRDY#

DEVSEL#

Address Data Data Data


Phase Phase Phase Phase

Bus Transaction
PCI Read Burst Cycle
Clock Cycle Description of PCI operation
0 Bus is idle
1 The initiator sets FRAME low, places the address on the Address/Data (ADx) lines,
and the bus command (read) on the Command/Byte Enable (C/BE) lines (address
phase).
2 The initiator tri-states the address and waits for the target to return a data value by
turning on its tri-state drivers. Device Select (DEVSEL) low indicates a target
device has decoded its address range and it is responding to the command. The
target drives TRDY high to indicate the target needs another clock cycle to respond
with the data.(data phase)
3 The target drives the data value and sets target ready (TRDY) low to indicate that
data is valid. When both IRDY and TRDY are low a data transfer occurs.
4 The target sets TRDY high to indicate it need an additional clock cycle for the next
data transfer.
5 The second data transfer occurs when both TRDY and IRDY are low. The initiator
saves the target data.
6 The target drives the data value, but the initiator requests an additional clock cycle
by set IRDY high.
7 The initiator sets IRDY low to complete the third data transfer. The initiator saves
the target data value, The initiator drives FRAME high to end the data phase.
8 All bus signals are tri-stated or driven to the inactive state.
PCI Write Burst Cycle
0 1 2 3 4 5 6 7 8

Bus Clock

F rame #

AD Address Data1 Data2 Data3

C/BE# Bus-Cmd BE-1 BE-2 BE-3

IRDY#

TRDY#

DEVSEL#

Address Data Data Data


Phase Phase Phase Phase

Bus Transaction
PCI Write Burst Cycle
Clock Cycle Description of PCI operation
0 Bus is idle
1 The initiator sets FRAME low, places the address on the Address/Data (ADx) lines, and
the bus command (write) on the Command/Byte Enable (C/BE) lines (address phase).
2 The initiator places the data on the ADx lines and byte enables on C/BE lines, Device
Select (DEVSEL) low indicates a target device has decoded it’s address range and it is
responding to the command. When both IRDY and TRDY are low the target saves the
data. (data phase)
3 The initiator drives new data and byte enables. When both initiator ready IRDY and
TRDY are low a data transfer occurs and the target saves the data.
4 The initiator sets IRDY high and the target sets TRDY requesting an additional clock
cycle.
5 The initiator drives new data and byte enables and sets IRDY low. The initiator sets
FRAME high indicating the final data transfer.
6 The target drives the data value, but the initiator requests an additional clock cycle by
set IRDY high.
7 The initiator sets IRDY low to complete the third data transfer. The target saves the data
value.
8 All bus signals are tri-stated or driven to the inactive state.
PCI IP Cores
Intel 82541 Ethernet Controller IC

Note the internal PCI core, DMA


controller, and packet buffer
Software for PCI devices
• Each PCI device has a 256 byte
configuration area
• At power up each device can respond with
manufacturer and device type information
• Allows system to locate and load device
drivers at power up
• Memory and I/O base addresses are
configured with software (no jumpers)
Third Generation Bus – PCI Express
• High-Speed Serial line(s) are used to transfer
PCI signals
• Fewer signal lines are used, but with much
higher bandwidth on each signal line
– More stringent design restrictions on drivers, length,
loading, crosstalk, and terminations
• Clock rates from 2.5 Gbps to 10 Gbps
• Can combine serial lines into groups called
lanes to provide more bandwidth to a device
• No changes needed in software – works the
same as PCI
I/O Transfers
• I/O devices are orders of magnitude slower than a
Processor. Handshake lines are used to synchronize each
transfer.
• I/O device sets a hardware handshake line when it is ready
to transfer data. (input ready, output ready)
• Before any data transfer, the Processor must check that the
I/O device is ready by reading the device’s handshake line.
• Handshake line bits are connected to another I/O port
(status port). Typically the next I/O address after the I/O
data port.
• When the Processor reads or writes the data port, the
handshake line is usually reset in hardware.
I/O Transfer Methods

• Programmed I/O (Software Polling)

• Interrupt Driven I/O

• Direct Memory Access (DMA)


Programmed I/O
• Processor must read and check I/O ready
bits for proper value before a transfer.
Requires looping, reading status port, and
constantly waiting for correct value.
• Processor then inputs or outputs data
value to/from I/O device.
• This approach is limited to systems with
just a few I/O devices
– Processor overhead to loop and check in software
Programmed I/O
Pseudo Code for Ouput
// loop until output ready=1
do { // read status and mask off ready bit
status = read_port(status_port_address);
output_ready = status & 0x01;
}
while (output_ready == 0);
// output new data value
write_port(data_port_address, data_value);
Programmed I/O
Pseudo Code for Input
// loop until input ready=1
do { // read status and mask off ready bit
status = read_port(status_port_address);
input_ready = status & 0x02;
}
while (input_ready == 0);
// input new data value
data_value = read_port(data_port_address);
Interrupts
• I/O ready signals generate a hardware Interrupt
signal. Eliminates Processor I/O ready wait
loops.
• An interrupt signal stops the Processor at the
next instruction and the Processor calls the
Interrupt service routine (ISR)
• Interrupt service routine transfers the data
• Interrupt routines must save all registers on the
stack for the interrupt return to work (ISRs like
subroutines - but “called” by a hardware signal)
Interrupts
• Interrupt control hardware is needed to support
and enable/disable multiple interrupt signals
• Lower number interrupts typically have a higher
priority (can even interrupt a higher number
interrupt’s ISR) Gives fast I/O devices priority.
• Most modern processors have a vectored
interrupt system. Each interrupt jumps to a
different ISR address (X86 uses a table lookup
of addresses in low memory to jump to ISR)
Servicing an Interrupt
1. Currently Running
Process is Interrupted
2. ISR 3. IST
……. Code Launched
Mov… executes (Optional)
Add…
…… …… ……….
….. Mov.. …………
……. Out.. …..
…… ……. ……..
……. Reti ……..
……
…….
………
……..
….. 4. ISR
.
……. Returns
…..
…..
….
…….
Interrupts – Other Issues
• Interrupt latency time – set by longest instruction
or longest block of code that disables interrupts.
Determines the max time to respond to an
interrupt. ISR may launch an interrupt service
thread (IST) to complete operations so that it
can return in less time.
• Some critical code (non reentrant) might require
that interrupts be disabled. Most machines have
an enable and disable interrupt instruction. But
doing this impacts interrupt latency time.
Interrupts
• Interrupt controller hardware is needed to set
priority and enable/disable each interrupt signal

• Additional bus signals needed


– PCI: INTx lines

• For security, the OS must control all interrupt


hardware, the vector table, and the ISRs.
Interrupt Timing Example
•Assume you want to sample an
input at 1000Hz using interrupts.
•A hardware timer could be used to
generate the interrupt request signal
at a 1000Hz rate.
•1/1000Hz is 1ms, so the ISR would
need to execute in less than 1ms.
•But this would not leave any time for
the OS and other applications to run!
Interrupt Timing Example
• The ISR should consume only a small
percentage of the available CPU time so
that it does not impact the OS,applications
and other ISRs that are also running.
• Lets assume now that the ISR should use
only 5% of the CPU cycles, so we have
0.05 x 1ms = 50 us for the maximum ISR
execution time (us = 10-6 second)
Interrupt Service Time
• For an RTOS, one would expect an interrupt
response time (time between interrupt hardware
signal and ISR start) to be 0 – 100 us. (i.e., CE
is around 10-20us)
• Service time = Worst case time to respond to
interrupt + ISR execution time
• A General Purpose Desktop OS will be a lot
slower (i.e., Windows XP can be >5000us)
• The OS interrupt response time will limit the
maximum interrupt rate possible
Ways to Measure Interrupt Times
• Add up ISR instruction execution times
• Measure time on device with test equipment
(scope or logic analyzer)
• Use code profiling tools

Why Worry about Interrupt Times?


• Interrupts occurring too fast (i.e., faster than they
are being serviced) can eventually crash the OS
when stacks overflow or it may also miss
interrupts
Direct Memory Access

• A DMA controller transfers blocks of data


directly to/from memory and the I/O device.
• DMA controller is a state machine with an
address pointer and a counter. Counts down
number of memory locations for transfer and
drives bus address and control lines (is a Bus
Master)
• Processor not involved in transfer once it starts
DMA Bus Cycle
Processor I/O
Memory
With Hardware
Cache using DMA

System Bus

• Processor does not drive the bus during a


DMA bus cycle (i.e.,tri-states drivers)
• Bus Arbitration hardware is needed to
support multiple bus masters
Direct Memory Access
• DMA controller and Processor must both share
the bus. Need bus arbitration hardware to control
bus access (DMAs or Processor).
• DMA controller interrupts Processor when it’s
block transfer is complete.
• Processor programs DMA controller’s address
register and counter to start a new transfer.
• Need hardware for each DMA controller and an
interrupt system
Direct Memory Access
• Need new bus signals for DMA. DMA controller must
request a bus cycle and wait until it is granted by bus
arbitration hardware
– PCI: REQx and GNTx
• For security, the OS must control all DMA hardware.
• DMA normally used for high-speed block transfer devices
like disk drives (disks transfer sectors)
• Processor and DMA controllers can work in parallel on
different tasks (overlap of computation and I/O)
Tradeoffs
Transfer Technique Hardware CPU Overhead

Programmed I/O

Interrupt

DMA
Mbed I/O Transfer Examples
• Most mbed APIs use software polling
• Basic GPIO with DigitalIn and DigitalOut
do not have or use any ready bits
• Serial API uses interrupts with a character
buffer – getc and putc check buffer status
• RawSerial does not use a character buffer
Mbed I/O Transfer Examples
• Most mbed user I/O pins and devices can
be setup to generate an interrupt
– Pindetect switch debounce code uses
interrupts
• Mbed has DMA controllers, but they are
currently not used in standard APIs. Some
DMA examples are available but are not
easy to use
General Purpose OS Transfers
• Most use interrupt driven I/O with buffering
• DMA typically used for disk I/O transfers
• Hardware protection forces user programs
to use OS APIs for I/O operations
• May need to write a custom I/O device
driver for new hardware
Device Independent I/O
• Operating systems provide support for the
underlying I/O hardware.
• Many of the I/O devices can be supported in
applications programs by using the basic file
system API calls. (i.e. Parallel and Serial Ports)
• One important feature of an operating system is
the support it provides for file system I/O.
• It provides a uniform logical view of information
storage.
OS File I/O Operations
• Open (or Create): For a read operation on an existing file, the open
operation searches for the file, for a write operation it searches for
available space on the storage device and typically allocates buffer
space to handle file transfers.
• Read: The next block of data is read from the open file. A file identifier
or handle and a pointer to a buffer area in which to return the data read
are arguments.
• Write: The next block of data to write to the open file. A file identifier or
handle and a pointer to a buffer area from which to copy the data to
write are arguments.
• Seek: Move to a specific location within a file. The argument is
normally the record number to seek to. Seek is much faster than
reading multiple times to move to a specific record in a file.
• Close: Close the open file. For a write operation, close flushes all write
buffers and updates the directory entries for the new file.
WindowsFile I/O API Calls

• Open with CreateFile


• Read with ReadFile
• Write with WriteFile
• Seek with SetFilePointer
• Close with CloseHandle
Common OS APIs
API Category MS Windows OS Unix/Linux OS
Process Control CreatProcess(...) fork(...)
ExitProcess(...) exit(...)
WaitForSingleObject(…) wait(...)
File Manipulation CreateFile(...) open(...)
ReadFile(...) read(...)
WriteFile(...) write(...)
CloseHandle(...) close(...)
Device Manipulation SetConsoleMode(...) ioctl(...)
ReadConsole(...) read(...)
WriteConsole(...) write(...)
Information GetCurrentProcessID(...) getpid(...)
SetTimer(...) alarm(...)
Sleep(...) sleep(...)
Communication CreatePipe(...) pipe(...)
CreateFileMapping(...) shmget(...)
MapViewOfFile(...) mmap(0
Protection SetFileSecurity(...) chmod(...)
InitializeSecurityDescriptor(...) umask(...)
SetSecurityDescriptorGroup(...) chown(...)
Threads CreateThread(…) pthread_create or clone(…)
Unicode and ASCII
Character Encoding
• For international market will need to
support different languages
– www.unicode.org/charts/
• Unicode is a 16-bit character code that
supports scripts for different languages
• ASCII is an 8-bit character code for
English (Basic Latin Script)
• Have to work with both and convert!
ASCII Character to Hexadecimal Conversion Table
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI
1 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US
2 SP ! " # $ % & ' ( ) * + , - . /
3 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
4 @ A B C D E F G H I J K L M N O
5 P Q R S T U V W X Y Z [ \ ] ^ _
6 ` a b c d e f g h i j k l m n o
7 p q r s t u v w x y z { | } ~ DEL

•Example: Find “A” in the table above. It is in row 4


and column 1, so it’s ASCII value is 0x41.
•In Unicode, the first 128 characters are the same as
the 8-bit ASCII character with a high byte of all zeros
to pad it to 16-bits.
Important ASCII Control
Characters (<0x20)
• ASCII characters below 0x20 are control codes, they do
not print a character - they perform control operations
• LF (line feed) - Moves the cursor down to a new line,
but not to the left. “\n” in C/C++/. Some systems
automatically add CR to a LF.
• CR (carriage return) - Moves the cursor all the way to
the left, but does not advance to the next line. “\r” in
C/C++/. For a new line, both CR and LF are needed.
• SP (space) prints a blank space and cursor moves to the
next character position.
• FF (form feed) - Advances paper to the top of the next
page on printers or clears the display window in
terminals. “\f” in C/C++/.
Windows
• Unicode by default (but sometimes ASCII)
• New C type TCHAR for Unicode strings
• _T(“hi”) makes a Unicode string
• Functions are provided to convert between
ASCII and Unicode
– WideCharToMultiByte and
MultiByteToWideChar
Figure 2.15 A Tektronix logic analyzer setup to capture and decode PCI bus signals. This logic analyzer is actually an embedded system that runs Windows XP. A special PCI
interposer adapter card is used to quickly connect probes along with special fi rmware for the logic analyzer. Images courtesy of Nexus Technology.
Figure 2.16 A Tektronix logic analyzer with optional software setup to capture and disassemble an
ARM processor’s instruction execution sequence. This logic analyzer is actually an embedded system
that runs Windows XP. Images courtesy of Nexus Technology.
A Programmable Automation Controller (PAC)
https://in.video.search.yahoo.com/search/video?fr=mcafee&p=Programmable+automation+controller#id=5&vid=b673334b61800b230a640b3009e30f9a&action=click
CAN (Controller Area Network) and LIN (Local Interconnect Network) Bus
• Serial Buses Developed for Automotive Industry
in mid 1980s – More advanced than RS232
Serial
• CAN is used to connect car subsystem
processors together, has multiple bus masters,
& uses 2 wires
• LIN is used to connect a processor with it’s
smart sensors, a single master, & uses only 1
wire
• LIN used at lower levels along with CAN at
higher levels in the system
• Both are now found in other embedded devices
https://www.csselectronics.com/screen/page/simple-intro-to-can-bus/language/en
https://in.video.search.yahoo.com/search/video?fr=mcafee&p=LIN+bus#id=1&vid=b3a2029212810fc39ecaa4dc5434b392&action=click
Car Body Network with CAN & LIN
Mirror

Lock
Lock
Window Lift
Universal Light
CAN Light

Seat
Htng
Instruments
Htng Wiper
Power Train Central WHtg
Body Ctrl Roof Interior
ITS Light
Trunk
Htng
Climate
x6
Seat
Light Seat
Htng
St-Wheel Panel CAN

Universal Motor
Lock Lock
1 backbone, 13 nodes
8 subnets, 1-8 local nodes Sub-Bus
Universal Panel
52 nodes total
Mirror
Controller Area Network (CAN)
• Messages contain a message number and
not an address
• Lower number messages have priority
• Each devices checks the message
number to see if it needs to handle the
message
• Data rates up to 1M Baud
• Special CAN chips are available
Message Frame

Bus Idle Arbitration Field Control Data Field CRC Field ACK EOF Int Bus Idle

SOF 11 Bit Identifier D LC Data (0-8 Bytes) 15 Bits Slot

RTR r0
r1 D elimiter

Figure 3.15 A CAN 2.0A Message Frame


Local Interconnect Network (LIN)

• Can be implemented with a UART and


microcontroller firmware
• Data rates up to 20K Baud
• Only one bus master
• Lower cost, lower data rates, and 1 less
wire than CAN
Message Frame

Header Response

Sync Sync Ident Data Data Data Data Check-


Break Field Field Field Field Field Field Sum
Field Interframe
Space or
Interbyte Space Break
In_Frame Response Space

Figure 3.16 A Typical LIN Frame Consisting of Synch Break, Synch Field, Identifier, Data Field and
Checksum.
Figure 3.13 The Tektronix DPO7000 1-4 Channel Oscilloscope is an embedded device that runs
Windows XP. The display above is from an optional software package for the oscilloscope that
decodes and displays SPI bus signals. Images courtesy of Prodigy Test Solutions.
Figure 3.14 The Tektronix DPO7000 1-4 Channel Oscilloscope is an embedded device that runs
Windows XP. The display above is from an optional software package for the oscilloscope that
decodes and displays I 2C bus signals. Images courtesy of Prodigy Test Solutions.
Network Interface
Standards
Networking
• Ethernet Frames
• Ethernet LAN
• The TCP/IP Protocol Suite
• Software APIs for networking
Network Interfaces
• Ethernet is the most widely used network
interface
• Details found in IEEE Standard 802.3
• Data is transferred in Ethernet Packets (Frames)
• Data rates: 10Mbps, 100Mbps, and now 1Gbps
• Low-cost Ethernet controller chips are used
– Often attaches directly to a PCI bus
• Original Idea: each network device is assigned a
unique 48-bit address by the manufacturer (MAC
address)
An Ethernet Frame
Preamble 56 bits of alternating Is and Os.
SFD Start field delimiter, flag (10101011)

Destination Source Length


Preamble SFD Data and Padding CRC
Address Address PDU

7 bytes 1 byte 6 bytes 6 bytes 2 bytes 46 to 1500 bytes 4 bytes

• Preamble - 56-bit start region, denotes the beginning of a Ethernet frame


• SFD - Start Frame Delimiter, 8-bit value marking the end of the preamble
• Destination MAC Address – 48-bit address denoting where data is headed
• Source MAC Address – 48-bit address denoting were data came from
• Length/Type (PDU) - 16-bit sub-protocol label field with length or type
• Data Payload – The data being sent
• CRC (or Frame Check Sequence), a CRC value computed on the entire
frame. Used to detect bit errors occurring during transmission.
Intel 82541 Ethernet Controller IC

Note the internal PCI core, DMA


controller, and packet buffer
A Small Ethernet Network

P ri nter
C
Computer
A

Ethernet Medium

Compu ter Computer


B D
CSMA/CD
• Carrier sense multiple access with
collision detection (CSMA/CD)
• Device waits for medium to be idle
• Device starts transmitting data
• Device Listens to data to detect a collision
• When collision detected, stop and wait for
a random amount of time before starting
over
• Works a bit different in 1 Gbps Ethernet
An Ethernet Bridge Connecting
Two Segments

Statio n S tati on
A D
Bridge
Segment 1 Segment 2

S tati on S tati on
B C
TCP/IP Protocol
• Application Protocols – HTTP, SMTP,
SSH, etc.
• Transport Protocols – TCP, UDP
• Network Communication Protocols – IP
• Network Topology – Ethernet, Wi-Fi,
Token Ring, etc.
Network Protocol Layers

Data Application Protocols

TCP TCP
Header Data Transport Protocols

IP
Header IP Data Internet Protocols

Frame Frame
Frame Data Network Topology
Header Trailer
Network Software Support
• Ethernet controller hardware filters, buffers,
sends, and receives Ethernet packets
• Software is still needed to implement protocols
• OS typically provides software to implement the
network protocols (TCP/IP, HTTP, FTP, etc.)
• OS may also support networked file systems
• Common network applications (i.e. browser,
telnet, ftp) may be provided with OS or available
from third parties
OS Networking Software

• Includes a TCP/IP Protocol Suite


• Supports sockets model programming
interface in Windows Sockets (Winsock)
• Also has some higher level API interfaces
such as WinInet & WinHTTP in Windows
• OS typically has browser & ping
applications.
Wireless Networking Standards

• WiFi - IEEE Standard 802.11


• Bluetooth - IEEE Standard
802.15.1
• ZigBee - IEEE Standard 802.15.4
• Features of each on the next slide
Feature 802.11b/WiFi 802.15.1/Bluetooth 802.15.4/ZigBee
Application Web, Email, Video Cable Replacement Control &
Area Monitoring
Target Cost $25 $15 $5
Memory 1MB+ 250KB 4KB - 32KB+
Requirement
Battery Life .5-5 Days 1-7 Days 100-1000 Days
Network Size 11,000+ 7 20-250
Peak Data 1, 2, 5.5, 11, 1Mbps 250 Kbps @ 2.4
Rate (54-802.11g) Mbps GHz
40 Kbps @ 915
MHz
20 Kbps @ 868
MHz
Power 1.5 W active 80 mV active @ 0 60 mW active @ 0
Consumption @ 20 dBm dBm dBm
(targeted) 45 mW sleep 5–2000 mW sleep
100 mW sleep (mode dependent)
Adjacent >35 dB 0 dB 0 dB
Channel
Rejection
Receiver –80 dBm –70 dBm –85 dBm
Sensitivity –92 dBm @
868/915 MHz
Range Line ~200 m @ 11 Mbps ~30 m ~50 m @ 0 dBm
of Sight ~500 m @ 1 Mbps ~100 m, Class 1
Range— ~40 m @ 11 Mbps ~10 m ~10 m @ 0 dBm
Indoor ~100 m @ 1 Mbps ~30 m, Class 1
(approx.)
Number of 11 - DSSS 79 16 @ 2.4 GHz
Channels 79 - FHSS 10 @ 915 MHz
1 @ 868 MHz
Modulation GFSK—FHSS GFSK O-QPSK @ 2.4
Technique BPSK/QPSK—DSSS GHz
BPSK @ 868/915
MHz
Maximum 20 dBm—FHSS 20 dBm 30 dBm
Transmit 30 dBm—DSSS
Power
Figure 4.5 The free Ethereal Network Protocol Analyzer capturing and decoding network
traffic on a desktop PC. The PC just loaded a short web page from a remote server (see
HTTP packets). (originally www.ethereal.com and now newer versions are found at
www.wireshark.org )
mbed Ethernet Examples

• A wired Ethernet connection can provide more bandwidth and


is more reliable than Wi Fi
• Not all mbed boards support Ethernet interfaces
• LPC1768 has an Ethernet interface, but needs a Magjack
connector breakout board
• Newest Ethernet networking code uses the RTOS
• Networking Examples for mbed
• Can also add a Wi Fi device such as ESP8266, a few mbed
boards include Wi Fi, but not LPC1768
mbed Ethernet Examples
• LPC1768 has an Ethernet interface and a physical layer
driver IC, but still needs a Magjack connector breakout
board with four jumper wires and cable to connect to the
Internet
Ethernet and Wi Fi Security Issues

• Most networks are locked down for security, other steps


are likely needed to get an IP address for Internet Access
using DHCP
• May need to enable DHCP using the MAC address
(mbed Ethernet function can print it out). Additional logon
steps also may be needed in a browser (hard on mbed!)
• Sharing the Internet connection or setting up a Wi Fi Hot
Spot on a PC or phone is another solution (sometimes
easier)
Adding Wireless

• For the LPC1768, can add a low-cost SOC processor module


with firmware and RF circuits for wireless such as the Bluetooth
or Wi Fi modules seen below. Both devices use a serial
interface to mbed.
Networking Case Studies
• Ethernet
• Huzzah ESP8266 Wi Fi module
• Adafruit Bluetooth UART module
Mbed Networking Libraries

• There are two versions of the mbed networking


libraries. The newer one uses the RTOS and
works a bit better.
• The old and new networking libraries have the
same names (EthernetInterface, RTOS, mbed…)
• If you mix old and new library versions, a lot of
compiler warnings will appear – so check
versions!
TCP IP Protocols and APIs

• There are several network protocols,


clients and sever setups using Ethernet:
– https://developer.mbed.org/handbook/TCP-IP-
protocols-and-APIs
• Watch RAM usage, with networking can run
out of RAM space using more than one
protocol at a time on LPC1768. Click “Build”
tab on right in compiler to check.
mbed RTOS
• Real-Time Operating System – RTOS
• Faster worst case response time and time slicing than a general
purpose desktop OS such as Windows or Linux
• RTOS switches between multiple threads (i.e. C++ functions)
automatically
• 1ms hardware timer interrupt used to switch threads in RTOS
scheduler. Called OS time slice.
• Also includes several synchronization primitives (mutex locks,
semaphore…)
Adding the mbed RTOS
• Add mbed-rtos library code to the project

• Want mbed RTOS ver 2.0 (not 5.0!) from

https://developer.mbed.org/handbook/RTOS

• Click on “Import Library”

• Add #include “rtos.h” after “mbed.h” include in main


mbed RTOS

• A thread is just a series of instructions to execute (in C+


+ it is just a function)

• Threads can share global variables with other threads

• Main is always the first thread

• Make a new thread object to start running a new threads

• Will want to change any wait(sec) to thread::wait(ms)


mbed RTOS

• Synchronization primitives needed on R/W global


variables and I/O devices to insure mutual exclusion
(strange and random bugs if not done properly!)

• Make simple C predefined type R/W global variables


“volatile” to provide an automatic mutex lock (only in
ARM/Keil compiler!)

• More complex global variables, objects, or arrays need a


mutex or semaphore for synchronization
mbed RTOS
• Can add interrupt routines when using the RTOS, but
they still need to be fast!

• RTOS uses more flash memory, but the critical memory


is likely to be RAM

• Each thread and the RTOS needs its own stack area in
RAM. Around 8 threads max or so on LPC1768 mbed
(limited by 32K RAM)
mbed RTOS

•Minimize Global variables and minimize the scope of


mutex locks (Global variables are evil!)

•Avoid recursion whenever possible – might use up too


much stack space and crash or use too much processor
time

•Difficult to determine max stack sizes needed


mbed RTOS Scheduling

• A thread gets a 1ms max time slice in running state


• If it does not go into a wait state first (i.e., a synchronization
lock, thread::wait(), or terminates), it is interrupted after
running for 1ms by the OS time slice timer interrupt
• OS Scheduler then runs next thread waiting in ready queue
• Scheduler checks thread priority first, and then (round
robin) queue order for threads with the same priority to
select next thread to run
Thread states in OS
Ways to use threads
• Different thread for each task
• Divide a complex computation between threads.
Speeds up computations on multi-core processors and
can even speedup a single core processor
• On bidirectional I/O devices (i.e. networking, serial…)
can use two threads, one to send and one to receive
• It is faster to activate a waiting thread than creating a
new one
Rate Monotonic Analysis/Scheduling
(RMA/RMS)
• Prioritize tasks (threads) based on period (shortest period =
highest priority)
• Ensures each real-time threads execution deadlines are not
missed under a complex set of assumptions
• Need to know worst case execution time of each task
• Can be very hard to get these numbers accurately!
Rate Monotonic Analysis/Scheduling
(RMA/RMS)
• In typical operation, safer to only use about 60% of
processor time running real-time threads and interrupt
routines. Anomalous conditions might miss thread real-time
deadlines and cause a failure, when bad things happen (i.e.,
worst case asynchronous thread arrivals to scheduler ready
queue)
• Watchdog timers should detect a lock up of any thread
• Should also use Safety Critical coding rules
Other RTOS Scheduling Algorithms

See https://en.wikipedia.org/wiki/Real-
time_operating_system#Algorithms
Other Synchronization Issues

• Starvation – A low priority thread never get the resources


it needs to run and winds up waiting forever. Resources
include CPU, memory, I/O devices
• Deadlock – A thread holds a resource while waiting on a
second resource to be released by another thread that is
waiting on the resource held by the first thread (i.e., two
or more threads can get stuck in a circular resource lock
wait condition forever, if they can hold resource(s) while
waiting for others)
Other Synchronization Issues

Priority Inversion – a lower priority thread holds a


resource needed by a higher priority thread to run. Could
run it first to release resource (but this messes up priority
scheduling). Could need to run through several lower
priority threads first to fix everthing! – Bad for a real-time
system!
Priority Inversion – often handled automatically by OS in
a general purpose OS, but up to programmer to avoid in
RTOS
Basic Mbed RTOS APIs
• Thread – starts a new thread
• wait – waits for time period (in ms) before running
thread again
• yield – exits thread and runs other threads of higher
or equal priority first
• Mutex – simple mutex lock/unlock on a resource
• Semaphore – used to allocate and lock
resource(s)
RTOS example

• A case study showing how to use the RTOS for


LED lighting effects with sounds is available at:
https://developer.mbed.org/users/4180_1/notebook/
led-lighting-effects-for-modelers/
• For a more complex mbed RTOS case study with 8
threads and a mutex lock on an I/O device, see
Application Board Hands on Demo4 at:
https://developer.mbed.org/users/4180_1/notebook/
mbed-application-board-hands-on-demos/
Communication between mbed and a PC
• Use the mbed’s built-in USB virtual com port
– Mbed code can talk to USB serial port using mbed’s serial APIs
– In Windows C++, use file system APIs for mbed COM port
– In Windows C#, use serialport method for mbed COM port
– Will see more code examples later for Windows
– In Linux, find serial port device name and use OS file APIs
• Other options, use hardwired Ethernet or add-on a Wi Fi or
Bluetooth module
mbed RPC
• Remote Procedure Call (RPC)
• A standard way to call procedures on another remote
computer.
• Any communication channel that sends ASCII
characters (bytes) can be used for RPC.
• Can use with Wi Fi, Bluetooth, Ethernet, Serial, or
USB
• All code needed for mbed side is provided.
• Examples provided for Matlab, Labview, Python,
Javascript, and C#
mbed RPC
• Human readable ASCII text strings are used to call function
and pass and return arguments
• RPC can also be used to read/write RPC variables
• Procedures and variables must be setup to enable RPC
using special templates at compile time (in mbed RPC code)
• RPC code on mbed maintains a table of RPC procedures
and variables along with the arguments.
• RPC code on mbed has a command line interpreter (CLI)
and passes the commands and arguments
Adding RPC Procedures and Variables

• https://developer.mbed.org/cookbook/RPC-Interface-
Library has examples on adding new RPC procedures
and variables using the library code
• RPCFunction - A class which can be used to call user
defined functions over RPC (in mbed RPC code)
• RPCVariable - A class which allows you to read and write
the value of a variable on mbed using RPC (in mbed RPC
code)
• SerialRPCInterface- A class which will set up RPC for
serial by registering all the base classes and setting up
the serial port on an interrupt
mbed RPC
• RPC Commands are in the format: "/<Object
name>/<Method name> <Arguments separated by
spaces>
• If you send just CRLF mbed will return a list of objects
that can be used
• If you send "/<object name>/" then mbed will return the
methods which can be used on this object.
• Command is not echoed back. Must send both ASCII
CR and LF at end of line!
Mbed RPC Demo using USB Serial

• Running RPC over (USB) Serial demo code from


cookbook
• In terminal window, type PC “/DigitalOut/new LED1
myled” and myled object is created
• In PC terminal window, type “/myled/write 1” and
led1 on mbed turns on
• Must set up PC’s terminal application to display
data sent out and send a CRLF at end of line!
Other RPC examples

• Cookbook demos for MATLAB, Labview, Java, Python,


and C# (.NET)
• Caution: There are old and new versions of the mbed
RPC library. The old version returned an object number
and the newer version returns object name over the serial
channel – make sure you have the correct version!
RPC using C# on a PC

• https://developer.mbed.org/users/kennyainny/notebook/
mbed-net-rpc-library---revised/ uses new version of RPC
library
• Demo sets up a C# GUI on PC and controls mbed using
RPC over USB virtual com port.
PC C# GUI controlling mbed via RPC
Serial RPC using Interrupts

• Drop in the interrupt driven serial RPC library,


“SerialRPCInterface” to add RPC features to any mbed
application with a serial interface.
• Just add RPC procedures and variables needed for
remote use.
RPC over the Internet

• RPC using HTTP code example in cookbook


• RPC Commands over HTTP are in the format: http://<url
of mbed>/rpc/<Object name>/<Method name>
<Arguments separated by spaces>
• Uses standard web page GET and POST APIs to
communicate
RPC HTTP demo

The USB serial port displays status and debug messages on the PC. The mbed IP address is
displayed along with connection messages. The GET request is coming from the PCs web browser
using mbed’s IP address for RPC. The debug messages can use a lot of code space, slow things
down, and probably should not be used in a final product. Often times a “Debug” switch feature is
available in code or a compiler to not include debug messages.
RPC HTTP Demo

A web browser connects to mbed’s IP and the RPC arguments are added at the end of the
URL (“http://<mbed IP>/rpc/”). The image above shows the PC’s RPC request to list RPC
objects and mbed returns the object names as an ASCII string that appear as the text for the
web page in the browser. Since this is a local IP address for mbed, the PC needs to be on the
same subnet. Application programs running on the PC can do HTTP GET requests for RPC
(without running a web browser).
IoT Devices
275 Billion IoT devices by 2035
Early Consumer IoT Devices

• Nest IoT Home Thermostat


Early Consumer IoT Devices

• Nest IoT Protect Fire, Smoke & CO Alarm


Early Consumer IoT Devices

• Nest IoT Camera


Early Consumer IoT Devices

• Phillips Hue IoT Lighting


Early Consumer IoT Devices

• IoT Crockpot Slow Cooker


Early Consumer IoT Devices

• Skydrop IoT Lawn Sprinkler Controller


Early Consumer IoT Devices

• Kwikset Kevo IoT Door Lock


IoT Devices

• Internet of Things (IoT) embedded devices connect to the Internet


to control and monitor devices.
• The IoT device has a Wi Fi or hardwired Ethernet network
connection to the Internet
• Another device with networking such as a phone, tablet, web
browser, or PC can support remote control and monitoring of the
IoT device
• Some low-cost IoT devices use a local RF link (such as Bluetooth)
and talk to a special local hub that has the Internet connection (Wi
Fi or hardwired Ethernet)
IoT Devices
IoT Devices

• Most networks assign a local IP address to IoT devices


• In a local IP address the first number is 192., 172., or 10.
• Only devices on the same local subnet can connect directly when
local IP addresses are used
• A device cannot be contacted externally from the Internet using a
local IP address
• So the IoT device contacts a cloud server using a registered domain
name for external communications. Other devices can communicate
to the IoT device using the server (requires account info)
IoT Devices

• Most IoT devices require the use of a Cloud server for


external communication on the Internet
• Each IoT device has an account on the Cloud server
and/or a special key access code.
• Typically the HTTP protocol is used with GETs and
POSTs to transfer all data via the Cloud server
• The Cloud server can provide extra computational
processing power, cloud storage, and data from other
servers
IoT Devices

• Typically, the IoT device contacts a cloud server using a


registered domain name for all external communications.
• HTTP GET reads data from cloud server
• HTTP POST sends data to cloud server
• Other devices can communicate to the IoT device using the
cloud server (but it requires account info for the device)
IoT Devices

• Several standards exist for web transactions using


servers
• Representational state transfer (REST) or RESTful APIs
are one common way of handling transactions with the
cloud server
• Other standards include WDSL and SOAP
IoT Devices

• Cloud servers for IoT devices are provided by Amazon,


Google, Microsoft, IBM, ARM mbed cloud, IFTT and
many others
• Many have free limited use developer accounts that can
be used to develop new IoT devices
• Once the device is mass produced, these accounts are
no longer free and there is a charge for such services
IoT Example

• Amazon Dash IoT Pushbutton


• Wi Fi link to Amazon Server
• Select product to order and setup account info using a
another device with a web page interface
• After setup, just click button to order a product
• Broadcom Processor, Battery powered
• $4.99, but full refund after first use
• Dash Teardown with photos
IoT Example

• Second Generation Dash Wand


• Adds Barcode Scanner & Voice Recognition
• Scan barcode to order and setup account info using a
another device with a web page interface
• After setup, just scan & click button to order a product
• ARM Cortex M4 Processor, Battery powered
• $10 with refund after first use
• Dash Wand Teardown with photos
IoT Example

• Amazon Echo and Dot IoT devices


• Voice controlled
• Wi Fi link to Amazon AWS Cloud Server
• Cloud Server performs speech recognition task on
uploaded audio file and sends back audio files in
response
• Other devices can communicate to IoT device using
Cloud Server
IoT Example

• Quad core ARM + GPU


• 256M RAM, 4G Flash
• Array of seven microphones
• RGB LED light ring
• Wi Fi + Bluetooth to talk to speakers
• Small speaker and audio out jack
• Teardown with photos
IoT Example

• Can download new applications for free (called skills)


• Free development tools from Amazon for new Echo/Dot
applications
• Users can write a custom NodeJS Lambda function to
add voice control features to other IoT devices
• Can get free Amazon AWS server account for
development
IoT Example

• Amazon Echo Show


• Add Amazon video display and features to Echo
IoT Example

• IBM Watson TJBot


• Raspberry Pi 3 with microphone and speaker
• Wi Fi connection to IBM Watson cloud server
• Designer for Makers to try Watson cloud services
• Uses NodeJS
IoT Examples

• Some devices use a lower cost RF connection to another


device or hub with internet connectivity
• The Witti Dotti contains 64 RGB Leds with a Bluetooth
link to a phone to display emojis and notifications
mbed IoT Examples using Dot

•https://developer.mbed.org/users/hbindra3/notebook/
alexa-dot/
•https://developer.mbed.org/users/omabogunje3/
notebook/dot-bot-two-way-communication-between-aws-
and-mbed/
•https://developer.mbed.org/users/RohanIyengar/
notebook/amazon-dot-alexa-controlled-game-playing/
Other mbed IoT Examples

•IFTTT WiFi ESP8266 and IFTTT Ethernet


•Mbed LPC168 using IBM Watson
•Mbed LPC1768 with Ethernet or ESP8266 using Mbed
Device connector (Recent addition needs OS 5?)
IoT Examples

See many more new IoT devices including locks,


appliances, trackers, lighting, cameras, thermostats,
monitors, and controllers at:

http://www.postscapes.com/internet-of-things-award/
winners/
Next Step Up in Performance

• ARM Processor with MMU (virtual memory), a lot more RAM


(hundreds of meg), and perhaps multiple cores
• Floating point hardware and basic GPU
• Similar to ARM processors found in smart phones
• Performance, power, size, and cost increases
• This type of embedded device, typically runs Linux
• Will examine two popular board options for students
• Keep in mind that real products would have their own board
design!
Embedded Devices with Linux

• GUI IDE often available for code development


• Compilers can run on device or PCs
• Developers will need to know and use Linux terminal
commands for initial device setup and configuration
• Can be a bit slow to boot – no instant on!
• OS on flash or uSD card
• Need OS device drivers for new hardware
• Developing a new custom Linux OS for a device takes
significant effort to develop and support
Embedded Devices with Linux

• If a serial console connection is needed to get a Linux


device configured, mbed can emulate an FTDI USB to
serial cable – no need to buy another $15 cable
• For more details on this setup, see
https://developer.mbed.org/users/4180_1/notebook/using
-mbed-for-a-pi-console-cable/
Linux/Windows Real Time Issues

• A General Purpose OS time slice interrupt can occur and OS


will run other processes before returning
• Time period can be10-100ms. or longer and will occur
randomly in any I/O code. Many processes are running.
• Around 10X longer than with an RTOS
• This can be too long for some hard real-time tasks
• Since processes need to change MMU values (virtual memory)
there is more overhead to a context switch, so a GP OS
typically also has a slower time slice than an RTOS
Addressing OS Real Time Issues

• Multiple cores and using priorities in processes and


threads can help
• Add microcontroller(s) for hard real-time I/O tasks
• Some SoCs now have microcontrollers in addition to the
main ARM core that runs the OS and application code
• Most Smart Phones and Tablets also have several
microcontrollers used in I/O devices
ARM Processors in Smartphones
• ARM Cortex-A
family:
– Applications
processors
• ARM Cortex-R
family:
– Embedded
processors for real-
time signal
processing, control
applications
• ARM Cortex-M
family:
– Microcontroller-
oriented
processors for
MCU, ASSP, and
SoC applications
Raspberry PI boards

Rasberry Pi Zero W with Adafruit Pi Cobbler Setup for Breadboard Use


Raspberry Pi

• Non-profit organization makes ARM boards at a low cost for


educational use (as low as $5US)
• Broadcom donates (or sells at low cost) the processor
• Can’t buy the chip for your own board, but can use Pi boards
in products (but supply can be limited)
• Probably has the most stable version of Linux on an
embedded board with over 11 million boards in use
• Newest boards include Wi Fi and Bluetooth
• See https://www.raspberrypi.org/
Raspberry Pi

• Built-in Camera Interface for $26 Pi camera


• External I/O is a bit limited for some projects (i.e., only 1
hardware PWM, serial port, SPI, and I2C)
• No cables, I/O header, 8G SD card, USB hub, or power
supply comes with the board
• Boards need around $20 to $50 of additional cables, an
uSD card, and parts for use and development
• I/O Header pins must be soldered on for breadboard use
Raspberry Pi

• Broadcom will not release all of the processor data


manuals to users – only to large customers under NDA
• Need examples doing similar I/O operations since full
data is not available on all processor I/O hardware
• Broadcom recently purchased by Qualcomm – Pi future
might be impacted at some point
• Need 8G+ uSD card to boot OS. SD OS setup can take
several hours initially from NOOBS OS release image
and with added update and upgrade steps
Raspberry Pi Software

• GUI for code development with HDMI monitor and USB


mouse and keyboard – some boards need USB hub
• GUI can be viewed on a PC using VNC – no monitor or
cables needed (after getting IP address)
• Geany IDE for C/C++, PHP, Python, and Javascript
• C/C++ I/O libraries available that must be run as
superuser – not a real OS I/O device driver
• Can also run Windows IoT, but not as many code
examples are available
BeagleBone Boards

BeagleBone Black Wireless and BeagleBone Blue Boards have


both Wi Fi and Bluetooth. OS can boot from on-board Flash or uSD.
BeagleBone Boards

• TI ARM processor with two microcontrollers (called


PRUs) for Real-Time tasks
• Newest boards include Wi Fi and Bluetooth
• More I/O pins available than Pi boards
• Can buy TI ARM chips for real products ($30 vs $5 mbed
LPC1768)
• See https://beagleboard.org/
BeagleBone Software

• Uses Cloud9 IDE for Python, NodeJS, and C/C++


development – works over USB cable
• USB interface responds with a web page interface in a
web browser using a local IP address 192.168.7.2
• Python, NodeJS, C/C++ I/O libraries and quite a few I/O
examples provided
• Can also run Android, but not as many code examples
are available
BeagleBone Black Wireless
BeagleBone Black Wireless

• Adds Wi Fi and Bluetooth to earlier BeagleBone Black


board – drops wired Ethernet
• Uses two standard .1 inch I/O header socket
• A nice choice for small IoT devices that also need a bit of
I/O and perhaps need real-time response using PRUs
Using BeagleBone for Robots
BeagleBone Blue
BeagleBone Blue
• BeagleBone board intended for robotics applications
• Board includes 9DOF IMU and Barometer
• 4 DC motor H-bridge drivers and quadrature encoders
• 8 RC Servo connections
• Wi Fi, Bluetooth, & DSM2 Remote control radio interface
• Plug in LiPo battery setup
• No HDMI connector or Ethernet
• Small JST connectors for each device
Introduction to Linux C++ I/O and
Applications Programming

• On PI, install and use Geany IDE with VNCviewer or


hook up HDMI monitor with USB keyboard and mouse
• On BeagleBone, use Cloud9 IDE in a web browser
• Can also edit, compile, link and run in a Linux command
line terminal window
• On BeagleBone, the two PRUs can also be used for
faster low level I/O – but will not cover that topic in this
class
Using Linux Device Driver

• This example uses the Linux file system device driver


• Opens USB virtual com port used by mbed
• Mbed is running demo test program
• Pi Writes a message to mbed
• Pi Sends a character to mbed
• Pi Waits for mbed to echo back character
• Blinks LED each time mbed gets the command
• Closes USB virtual com port
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

int main(int argc, char ** argv) {


int fd;
// Open the Port. We want read/write, no "controlling tty" status,
// and open it no matter what state DCD is in
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
//open mbed's USB virtual com port
if (fd == -1) {
perror("open_port: Unable to open /dev/ttyACM0 - ");
return(-1);
}
// Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that
fcntl(fd, F_SETFL, 0);
// Write to the port
int n = write(fd,"Hello mbed\n",11);
if (n < 0) {
perror("Write failed - ");
return -1;
}
// Read up to 255 characters from the port if they are there
char buf[256];
n = read(fd, (void*)buf, 255);
if (n < 0) {
perror("Read failed - ");
return -1;
} else if (n == 0) printf("No data on port\n");
else {
buf[n] = '\0';
printf("%i bytes read back: %s", n, buf);
}
//Send command to blink mbed led 10 times at one second rate
//mbed code turns on led2 with a '1' and off with a '0'
//mbed echoes back each character
buf[1] = '\n'; // end of line
buf[2] = '\0'; // end of string
for(int i=0;i<10;i++) {
write(fd,"1",1); //led on
sleep(1); //one second delay
read(fd,(void*)buf,1); //read echo character
printf("%s",buf); //print in terminal window
write(fd,"0",1); //led off
sleep(1);
read(fd,(void*)buf,1);
printf("%s",buf);
}
// Don't forget to clean up and close the port
close(fd);
return 0;
}
Linux GPIO in C
• Can use OS file system APIs to control GPIO pins, but it is
slow
• Can add special kernel drivers, but it is difficult
• Examples in Python or Java Script run orders of magnitude
slower than C for low-level I/O operations!
– These languages run in an interpreter and are not compiled like
C/C++
– Just toggling a bit might be in Khz range and in C Mhz range!
• Phython or Java are appropriate in many cases for higher
level operations such as somewhat slow client server
interactions
Pi GPIO Blinking LED Example

• Uses an C++ I/O library for Raspberry Pi that must be


installed and linked
• Library used here is BCM2835 for Pi
• Library function calls must be used to initialize, configure
pin functions, read, and write.
• There are several C I/O libraries for BeagleBone with very
similar functions used
• Newer PIGPIO library has more functions
• Must run code as superuser for these I/O operations to be
permitted in Linux (i.e.,sudo….)
#include <bcm2835.h>
//IO Library – must also link it and run using sudo
#define PIN18 RPI_GPIO_P1_12
//header pin # used for LED
int main(int argc, char **argv){
if(!bcm2835_init())return 1; /* Initialize */
bcm2835_gpio_fsel(PIN18, BCM2835_GPIO_FSEL_OUTP);
/*Sets PIN18 to output*/
while(1){ /* Forever loop */
bcm2835_gpio_set(PIN18); /* PIN18 = high */
bcm2835_gpio_clr(PIN18); /* PIN18 = low */
}
}
Using a PC for Embedded Devices
• PCs are often a cost effective solution, since this hardware is
mass produced and low cost.
• PCs have fast Graphics for GUI user interfaces
• GUI interfaces with a mouse allow fast human interactions and
increase productivity
• Custom I/O hardware can be added to a PC for the embedded
device
• I/O hardware can use a USB interface
• For very fast I/O hardware, a new PCI card can also be
designed, but is typically not required
• Need software to communicate with new I/O hardware
• Several I/O examples using C++ and C# will be examined
Windows OS
• Standard Desktop Windows OS can be used for
embedded devices
• Another OS, Windows IoT uses a subset of the same OS
APIs, but is an RTOS intended for embedded products.
OS Kernel code is totally different. Application
development looks the same in VS.
• Examples here use the subset of APIs found in Windows
IoT, so they work with both
• Windows has well over 20,000 APIs and Windows IoT is
around 2K, so IoT kernel is a lot smaller!
• Can run Windows IoT on Pis
Introduction to Windows
I/O and Applications
Programming
Console Applications
• Simplest programming model in Windows
• Outputs plain text to console window
• Console window appears after first printf
• Can read command line parameter
• Uses stdio.h and not fstream.h
• Will have Windows GUI examples later
FileIO Example Program
• Demo of File system API calls
• File name is in Unicode
• Uses CreateFile to create a new file
• Writes to File with WriteFile
• Closes file with CloseHandle
• Opens file again for read with CreateFile
• Reads file with ReadFile
• Closes file with CloseHandle
• Uses Sleep(ms) for time delay
// FileIO.cpp : Defines the entry point for the console application.
//
// FileIO demo program
// Shows how to create, write, and read a file using CE file APIs
//
// Remember CE uses 16-bit Unicode for character strings!
// _T("....") generates a Unicode string
// TCHAR is a Unicode string type
//
#include "stdafx.h"

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[]){


HANDLE hFile;
DWORD cBytes;
char cBuffer[] = "Hello File World\n";
printf("\nCreating CE_Demo.txt file\n");
// Open File for Write
hFile = CreateFile(_T("\\Temp\\CE_Demo.TXT"), GENERIC_WRITE,
FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
NULL);

// Check for file open errors


if (hFile == INVALID_HANDLE_VALUE){
printf("file write open errors\n");
Sleep(1000);
return 0;
}
if (!WriteFile(hFile, cBuffer, strlen(cBuffer), &cBytes, NULL)){
printf("file write errors\n");
Sleep(1000);
return 0;
}
// Close File
CloseHandle(hFile);

// Open File again for read


hFile = CreateFile(TEXT("\\Temp\\CE_Demo.TXT "),
GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// Check for file open errors
if (hFile == INVALID_HANDLE_VALUE){
printf("file read open errors\n");
Sleep(1000);
return 0;
}
// Read back data just written to new file
if (!ReadFile(hFile, cBuffer, 79, &cBytes, NULL)){
printf("file read errors\n");
Sleep(1000);
return 0;
}
// Display Data read back and delay 4 seconds before exiting
printf("%s\n",cBuffer,cBytes);
printf("the file data was written and read back OK\n");
Sleep(4000);
// Close File
CloseHandle(hFile);
return 1;
}
Serial IO Example
• Uses same File System APIs to read and write
data from serial port.
• GetCommStatus and SetCommStatus APIs are
used to setup the baud rate, number of bits, and
handshake options
• SetCommEvent and WaitCommEvent APIs are
used to notify program that new serial data is
available
Serial IO Demo
• Serial port is setup for 9600 baud, 8 data
bits, no parity, 1 stop bit, and no
handshake
• To run demo attach from device COM port
to an unused COM port on the desktop PC
using a null modem cable
• Start HyperTerminal or RealTerm on the
desktop PC and set COM settings
Figure 8.1 HyperTerminal running on development system and communicating with eBox COM Port
// SerialIO.cpp : Defines the entry point for the console application.
//
// Serial Port File I/O Demo Program
//
// FOR DEMO: Connect Ebox COM2: to PC with null modem cable
// Run HyperTerminal with 9600 Baud 8 data bits 1 stop bit
// no parity and no flow control

#include "stdafx.h"
BOOL ModifyCommSettings (HANDLE hComPort);
HANDLE hSerial;

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])


{
DWORD cBytes_out, cBytes_in;
DWORD dwMask;
char cBuffer_out[] = "\f\n Hello Serial World! \n\r Type
something and watch it echo back \n\r Ctrl C to exit \n\r";
TCHAR cBuffer_in[80];
// Display message on console
printf("\nOpening COM2: Serial Port - Type ctrl C on other device
to exit\n\r");
// Open Serial Port COM2: for Read and Write
// Note: COM1: can be setup to send out CE Serial Debug info
// In this case COM2: becomes COM1: in the OS – turned off in eBox 4300
hSerial = CreateFile(_T("COM2:"), GENERIC_READ | GENERIC_WRITE, 0,
NULL,
OPEN_EXISTING, 0, NULL);
// Check for file open errors
if (hSerial == INVALID_HANDLE_VALUE){
printf("file open errors\n");
Sleep(4000);
return 0;
}
// Modify Com Port settings (i.e. Baud Rate, #bits, parity etc)
if(!ModifyCommSettings (hSerial)){
printf("com port settings errors\n");
Sleep(4000);
return 0;
}
// Write to title out to serial port.
if (!WriteFile(hSerial, cBuffer_out, strlen(cBuffer_out),
&cBytes_out, NULL)) {
printf("file write errors\n");
Sleep(4000);
return 0;
}
// Set Communication event mask for WaitCommEvent for rxchar (recieve
character) in buffer
SetCommMask(hSerial, EV_RXCHAR | EV_ERR);

cBuffer_in[0] = 0;
// Read in characters, copy to console display and Echo
// Loop until ctrl C is typed
while (cBuffer_in[0] != 0x03){
// Wait for character in input buffer - events are more efficient than
looping
WaitCommEvent(hSerial, &dwMask, 0);
cBytes_in = 1;
// Loop just in case more than one character is in UARTs input buffer
while (cBytes_in != 0){
// Read back data any serial data and display
if (ReadFile(hSerial, cBuffer_in, 64, &cBytes_in, NULL)){
if (cBytes_in == 0) break;
// Display Data read back
printf("%s",cBuffer_in, cBytes_in);
// Echo characters back to sender
if (!WriteFile(hSerial, cBuffer_in, cBytes_in,
&cBytes_out, NULL)){
printf("\rfile write errors\n");
Sleep(4000);
return 0;
}
}
}
}
// Close File
CloseHandle(hSerial);
return 1;
}
// Function to set COM port options
BOOL ModifyCommSettings (HANDLE hComPort)
{
COMMTIMEOUTS ctos;
DCB PortDCB;
// Initialize the DCBlength member.
PortDCB.DCBlength = sizeof (DCB);
// Get the default serial port settings DCB information.
GetCommState (hSerial, &PortDCB);
// Change the common DCB structure settings to modify serial port
settings.
PortDCB.BaudRate = 9600; // Current baud
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = TRUE; // Enable parity checking
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
PortDCB.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement
PortDCB.fNull = FALSE; // Disable null stripping
PortDCB.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
error
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// Configure the port settings according to the new specifications
// of the DCB structure.
if (!SetCommState (hSerial, &PortDCB)){
printf("Unable to configure the serial port");
Sleep(4000);
return false;
}
// Set read time outs
ctos.ReadIntervalTimeout = MAXDWORD;
ctos.ReadTotalTimeoutMultiplier = MAXDWORD;
ctos.ReadTotalTimeoutConstant = 1;
ctos.WriteTotalTimeoutMultiplier = 0;
ctos.WriteTotalTimeoutConstant = 0;
if(!SetCommTimeouts(hSerial, &ctos)){
printf("Unable to configure the serial port");
Sleep(4000);
return false;
}
return true;
}
Port IO Example
• Operates the same as the previous serial port
example
• Instead of File Systems APIs this code talks
directly to the serial port I/O hardware (A 16550
UART)
• Uses the CEDDK library functions
WRITE_PORT_UCHAR & READ_PORT_UCHAR
to read and write I/O ports
• Intended to demo the use of CEDDK functions
(not replace device driver!)
Table 8.1 16550 UART I/O Ports
Register Function I/O Port Address
Data (DLB=1, Baud rate divisor LSB) Base + 0
Interrupt Enable (DLB=1, Baud rate divisor MSB) Base + 1
Interrupt ID Base + 2
Data Format Base + 3
Modem control Base + 4
Line Status Base + 5
Modem status Base + 6
Scratch-Pad Base + 7
// PortIO.cpp : Defines the entry point for the console application.
// Educational example intended to illustrate how programmed I/O works
// using the serial I/O port hardware on the target system
// and show the use of READ_PORT_UCHAR and WRITE_PORT_UCHAR
// from the CE Device Driver Kit (CEDDK)
//
// Setup for X86 PC (CEPC)
// using 16550 UART compatiable serial port hardware
//
// Not intended to replace a good serial port device driver!
// Does not use interrupts, have any timeouts, or provide
// support for all of the serial port's features
// Would normally use OS API calls for this operation!
//
// FOR DEMO: Connect Ebox COM2: to PC with null modem cable
// Run HyperTerminal with 9600 Baud 8 data bits 1 stop bit
// no parity and no flow control

#include "stdafx.h"

// For WRITE_PORT_UCHAR & READ_PORT_UCHAR functions


#include "..\WINCE600\CEPC_x86\cesysgen\ddk\inc\ceddk.h"
// Also need to include CEDDK.lib in link (see sources file)
// add $(_SYSGENOAKROOT)\lib\$(_CPUINDPATH)\ceddk.lib
// to TARGETLIBS entries

void Setup_UART (PUCHAR Data_Port_Address);


void Write_Serial_Character (PUCHAR Data_Port_Address, UCHAR
Serial_Data);
UCHAR Read_Serial_Character (PUCHAR Data_Port_Address);

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])


{
PUCHAR Data_Port_Address;
UCHAR Serial_Input_Data = 0;
char Title[]= "\f\n Hello Serial World!\n\rType something and
watch it echo back\n\rCtrl C to exit\n\r";
int i;
// Force Console to Display
printf("I/O Port READ/WRITE Demo Program - Echos data on
COM2:\n\r Type Ctrl C on other device to exit\n\r");
// Data Port Address for COM2:
Data_Port_Address = (PUCHAR)0x2F8;
// Sets up UART for 9600 Baud & No Interrupts with 8D NP 1S
Setup_UART(Data_Port_Address);
// Print title out on serial port
for (i=0; i<strlen(Title); i++)
Write_Serial_Character(Data_Port_Address, (UCHAR)Title[i]);
// Start Echo Loop - Loop until Ctrl C is hit
while (Serial_Input_Data != 0x03){
// Read in Data
Serial_Input_Data = Read_Serial_Character
(Data_Port_Address);
// Copy Data to Console
printf("%c", Serial_Input_Data);
// Write Data Back out (Echo)
Write_Serial_Character(Data_Port_Address,
Serial_Input_Data);
}
return 0;
}

void Write_Serial_Character (PUCHAR Data_Port_Address, UCHAR


Serial_Data)
// Write out a character to the serial port
{
UCHAR Status;
// Wait for TX output ready bit=1
// Status I/O Port Address is Data I/O Port Address + 5
do{
Status = READ_PORT_UCHAR(Data_Port_Address + 5);
} while ((Status & 0x40) == 0);
// Write (Echo) new data back out on COM2:
WRITE_PORT_UCHAR(Data_Port_Address, Serial_Data);
return;
}
UCHAR Read_Serial_Character (PUCHAR Data_Port_Address)
{
// Read in a character from the serial port
UCHAR Serial_Data, Status;
// Wait for RX input ready bit=1
// Status I/O Port Address is Data I/O Port Address + 5
do{
Status = READ_PORT_UCHAR(Data_Port_Address + 5);
// If not ready release remainder of time slice
if ((Status & 0x01) == 0) Sleep(0);
} while ((Status & 0x01) == 0);
// Read in new serial data
Serial_Data = READ_PORT_UCHAR(Data_Port_Address);
return Serial_Data;
}
void Setup_UART (PUCHAR Data_Port_Address)
{
UCHAR Temp;
// Will need a good PC Hardware Reference Text and/or
// the 16550 UART data sheet to fully understand this!
// Disable COMx: Interrupts (use Programmed I/O)
WRITE_PORT_UCHAR(Data_Port_Address + 1, 0);
// Set Baud Rate to 9600 with clock Divisor settings
// Put in set divisor mode
Temp = READ_PORT_UCHAR(Data_Port_Address + 3);
WRITE_PORT_UCHAR(Data_Port_Address + 3, Temp | 0x83);
// Set Divisor LSB (note: 12 = 115200/9600)
WRITE_PORT_UCHAR(Data_Port_Address , 12);
// Set Divisor MSB
WRITE_PORT_UCHAR(Data_Port_Address + 1, 0);
// Back to normal operation mode (and set for 8D NP 1S)
Temp = READ_PORT_UCHAR(Data_Port_Address + 3);
WRITE_PORT_UCHAR(Data_Port_Address + 3, Temp & 0x03);
return;
}
ILASMIO Example Program
• Operates the same as the previous serial port
examples
• X86 in-line assembly language is used for I/O
port read and write operations (instead of
CEDDK functions)
• Will only work on X86 Processors
• Example of how low-level code can use
assembly language to talk to hardware
• Should only use assembly language when
absolutely necessary – in just a few of the
lowest level OS routines
void Write_Port (short int Data_Port_Address, UCHAR Data)
{
// X86 in-line assembly language
// use only when you have to!
_asm {
mov dx,Data_Port_Address
mov al,Data
out dx,al
}
return;
}
UCHAR Read_Port (short int Data_Port_Address)
{
UCHAR Data;
// X86 in-line assembly language
// use only when you have to!
_asm {
mov dx,Data_Port_Address
in al,dx
mov Data,al
}
return Data;
}
Thread and Synchronization
Example Program
• Uses CreateThread API to create a thread
• Sleep used for time delays
• CreateEvent sets up an event
• SetEvent signals an event
• WaitForSingleObject waits until the event
is signaled
Figure 8.2 A thread running without synchronization can run several times before the main program
Figure 8.3 Thread running using WaitForSingleObject to wait for a synchronization event
// Thread_Demo.cpp : Defines the entry point for the console
application.
//
// Demo that shows how to create and synchronize a thread using an
event
//
#include "stdafx.h"
//Thread Function
DWORD WINAPI WorkerThread (LPVOID lpArg);
HANDLE g_hMainEvent = 0;

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])


{
HANDLE hThread1;
DWORD dwThread1ID = 0;
INT nParameter = 1;
int count1 = 0;
printf(" \n");
_tprintf(_T(" Hello Thread World!\n"));
g_hMainEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
Sleep(1000);
hThread1 = CreateThread (NULL, 0, WorkerThread,
(LPVOID)nParameter, 0, &dwThread1ID);
while (count1 < 100){
printf(" main thread computes value: %d\n",count1);
count1++;
SetEvent(g_hMainEvent);
Sleep(370);
}
CloseHandle(hThread1);
CloseHandle(g_hMainEvent);
return 0;
}
DWORD WINAPI WorkerThread (LPVOID lpArg) {
INT threadnumber = (INT) lpArg;
INT count = 0;
INT RunCode;
while (1){
// RunCode = WaitForSingleObject(g_hMainEvent, INFINITE);
// if (RunCode == WAIT_OBJECT_0){
printf(" worker thread %d computes value %d\n",
threadnumber, count);
count++;
Sleep(50);
// }
}
return 0;
}
Producer Consumer Problem
• Producer Thread put items in shared
buffer
• Consumer Thread takes items out of
shared buffer.
• A circular buffer is used (wraps around)
with an in and out pointer.
• Critical Sections used on access to global
data in first try (need mutual exclusion)
// ProCom.cpp : Defines the entry point for the console application.
// Producer Consumer problem with a shared circular buffer
//
// Demo that shows how to create and use critical sections
//
#include "stdafx.h"
//Thread Function
DWORD WINAPI ConsumerThread (LPVOID lpArg);
// Critical Section
CRITICAL_SECTION CriticalSection;
// Shared Circular Buffer and pointers
int count = 0;
int in = 0;
int out = 0;
DWORD Buffer[3];

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])


{
HANDLE hThread1;
DWORD dwThread1ID = 0;
INT nParameter = 1;
int i;

printf(" \n");
_tprintf(_T(" Producer Consumer example\n"));
for (i=0; i<4; i++) Buffer[i] = 0;
InitializeCriticalSection(&CriticalSection);
hThread1 = CreateThread (NULL, 0, ConsumerThread,
(LPVOID)nParameter, 0, &dwThread1ID);
// Producer
while (1){
// Check for Buffer Full
while (count == 4)
{
printf("Buffer Full - Producer Waiting\n");
Sleep(0);
};
// Insert new item into Buffer
// Shared Global Variables - use Critical Sections
EnterCriticalSection (&CriticalSection);
printf(" producer thread produces new item at
Buffer[%d] \n",in);
++count;
Buffer[in] = GetTickCount();
in = (in + 1) % 4;
LeaveCriticalSection (&CriticalSection);
// Random delay to simulate process producing new item for Buffer
Sleep(Random()>>21);
}
CloseHandle(hThread1);
DeleteCriticalSection(&CriticalSection);
return 0;
}
DWORD WINAPI ConsumerThread (LPVOID lpArg) {
INT threadnumber = (INT) lpArg;
// Consumer
while (1){
// Check for Buffer Empty
while (count == 0)
{
printf("Buffer Empty - Consumer Waiting\n");
Sleep(0);
};
// Remove item from Buffer
// Shared Global Variables - use Critical Sections
EnterCriticalSection (&CriticalSection);
--count;
printf(" consumer thread consumes item from
Buffer[%d] with time stamp %d\n",out,Buffer[out]);
out = (out + 1) % 4;
LeaveCriticalSection (&CriticalSection);
// Random delay to simulate process consuming Buffer item
Sleep(Random()>>21);
}
return 0;
}
Figure 8.7 The Producer Consumer Problem.
Producer Consumer Problem
• While loops consume CPU time and
power
• Count violates mutual exclusion rule
• Use three semaphores
– One for mutual exclusion lock
– One to detect buffer full and block
– One to detect buffer empty and block
• Different initial values used for
semaphores
// ProCom.cpp : Defines the entry point for the console application.
// Producer Consumer problem with a shared circular buffer
//
// Demo that shows how to create and use semaphores
//
#include "stdafx.h"
// Semaphores
static HANDLE mutex_semaphore; // mutual exclusion lock
static HANDLE full_semaphore; // something is in buffer
static HANDLE empty_semaphore; // buffer has an empty space
// Shared Circular Buffer and pointers
static int in = 0;
static int out = 0;
static int count = 0;
// Shared Buffer Area
DWORD Buffer[4];
//Thread Function
DWORD WINAPI ConsumerThread (LPVOID lpArg);

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])


{
HANDLE hThread1;
DWORD dwThread1ID = 0;
INT nParameter = 1;
int i;
printf(" \n");
// setup semaphores and their initial and max values
mutex_semaphore = CreateSemaphore(NULL, 1, 1, TEXT("mutex"));
// 1 for mutex lock
full_semaphore = CreateSemaphore(NULL, 0, 4, TEXT("full"));
// 0 items in buffer
empty_semaphore = CreateSemaphore(NULL, 4, 4, TEXT("empty"));
// 4 max items in buffer
_tprintf(_T(" Producer Consumer example\n"));
for (i=0; i<4; i++) Buffer[i] = 0;
hThread1 = CreateThread (NULL, 0, ConsumerThread,
(LPVOID)nParameter, 0, &dwThread1ID);
// Producer
while (1){
// Wait for empty space in buffer
WaitForSingleObject(empty_semaphore, INFINITE);
// Shared Global Variables - use mutex
WaitForSingleObject(mutex_semaphore, INFINITE);
// Insert new item into Buffer
Buffer[in] = GetTickCount();
count++;
// Check for buffer full message
if (count >= 4)
printf("producer thread produces new item at
Buffer[%d] %d Buffer now Full \n",in, Buffer[in]);
else
printf("producer thread produces new item at
Buffer[%d] %d \n",in, Buffer[in]);
in = (in + 1) % 4;
ReleaseSemaphore(mutex_semaphore, 1, NULL);
ReleaseSemaphore(full_semaphore, 1, NULL);
// Random delay to simulate process producing new item for Buffer
Sleep(Random()>>21);
}
CloseHandle(hThread1);
CloseHandle(mutex_semaphore);
CloseHandle(full_semaphore);
CloseHandle(empty_semaphore);
return 0;
}
DWORD WINAPI ConsumerThread (LPVOID lpArg) {
INT threadnumber = (INT) lpArg;
// Consumer
while (1){
// Wait for item in buffer
WaitForSingleObject(full_semaphore, INFINITE);
// Shared Global Variables - use mutex
WaitForSingleObject(mutex_semaphore, INFINITE);
count--;
// Check for Buffer Empty message
if (count == 0)
printf("consumer thread consumes item from
Buffer[%d] %d Buffer now Empty\n",out,Buffer[out]);
else
printf("consumer thread consumes item from
Buffer[%d] %d\n",out,Buffer[out]);
// Remove item from Buffer
out = (out + 1) % 4;
ReleaseSemaphore(mutex_semaphore, 1, NULL);
ReleaseSemaphore(empty_semaphore, 1, NULL);
// Random delay to simulate process consuming Buffer item
Sleep(Random()>>21);
}
return 0;
}
Figure 8.8 Sample Output from the second Producer Consumer Problem using Semaphores.
Audio Example Program
• Uses PlaySound to play a *.wav file
• Uses eBox AC97 Audio Device
• Need a speaker or headphone connected
to audio output jack on front of eBox
• OS also has waveIn and waveOut API
calls for audio
// Playsound.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])


{
int i;
_tprintf(_T("Hello Audio World!\n"));
Sleep(1000);
// Play WAV file seven times on audio device
for (i=0; i<7; i++)
{
// Uses registry wave file entry for EXCLAM
// Can replace with path to your wav file
// if you add | SND_FILENAME to third arg
PlaySound (TEXT("EXCLAM"), NULL, SND_SYNC);
Sleep(500);
}
Sleep(1500);
return 0;
}
Windows Application
• Windows Applications do not directly
request I/O read operations from the
mouse or keyboard
• The OS sends messages (mouse and
keyboard events) to a Windows
Application and it then responds to them
• Push vs. Pull Technology
Windows Application WinMain
• All Windows Applications have the
program entry point WinMain
• WinMain registers the Window class
• WinMain then creates, displays, and
updates the new Window
• WinMain always ends with a Windows
Messaging (While) Loop
Windows Messaging Loop
• Translates and Dispatches messages to
the Window
• Stays in Windows Messaging While Loop
as long as application is running
• Blocks waiting for each new message
• Messages that are Dispatched cause the
OS to activate the Windows callback
function
Windows Callback Function
• The function that handles the applications
messages
• Called by OS when a message is generated for
the Window
• A case (switch) statement is used to decode the
message
• The action for each message is coded into each
case
• After the message is processed, the callback
function returns
Table 8.3 Common Hungarian Notation Prefixes
Used for Variables in Windows Programs

Variable Type Hungarian Prefix


Integer i or n
Word (16-bit) w or s
Double Word (32-bit) dw
Long (32-bit signed) l
Char c
String (zero terminated) sz
Pointer p
Long Pointer lp
Handle h
Window Handle hwnd
Structure Size cb
// WinAPP.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

// Forward declarations of functions included in this code module:


ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,


HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings


LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WinAPP, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:


if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WinAPP);


// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;


wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = 0;
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;

return RegisterClass(&wc);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global
// variable and create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,


0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM
lParam)
{
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any additional drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, _tcslen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
C# Applications
• C# has a syntax similar to Java
• Also called managed code
• Compiles to the Common Intermediate
Language (CIL) – (like Java Byte Codes)
• One version of code for all processors
• .NET Compact Framework executes CIL
code (like Java Virtual Machine)
C# Advantages
• Middleware provided to make
programming of GUI and networking
easier (programmers are more productive)
• Built in threads and synchronization
• More run-time checking on array bounds
and data types (safe code)
• No direct pointer manipulations (like Java)
C# Disadvantages
• .NET Compact Framework increases
kernel size by several MBs.
• C# runs a bit slower and uses more
memory than C/C++
• Periodic Garbage Collection routines have
a negative effect on real-time performance
• Items above are bigger issues on a small
embedded device than on a desktop PC
with lots of memory and a fast processor
C# Hello World Console Application
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Threading;

namespace Hello_CS_World
{
class Program
{
static void Main(string[] args)
{
bool _continue = true ;
string message;
StringComparer stringComparer=StringComparer.OrdinalIgnoreCase
// Display title and prompt line on console
Console.WriteLine("Hello C# World");
Console.WriteLine ("Type QUIT to Exit");

// Loop reading in lines from keyboard until user types "quit"


while (_continue)
{
message = Console.ReadLine();
if (stringComparer.Equals("quit", message))
{
_continue = false;
}
}
}
}
}
C# Serial Port Hello World
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
namespace Hello_CS_Serial_World
{
class Program
{
static void Main(string[] args)
{
SerialPort _serialPort;
// Create a new SerialPort object with desired settings.
_serialPort = new SerialPort();
//Defaults to ("COM1", 9600, Parity.None, 8, StopBits.One)
_serialPort.Open();
_serialPort.WriteLine("Hello CS Serial World");
_serialPort.Close();
}
}
}
C# Window Applications
• GUI designed using graphical tools (Visual
Studio Application Designer)
• Tool automatically generates C# code
• Can generate Windows Applications
quickly
• Most time is spent writing code manually
to handle events (hitting buttons etc.)
• Example earlier in C# Tutorial
Figure 8.9 Calling C/C++ functions from C# using Platform Invoke (PInvoke).
Calling C/C++ functions from C#

• Special Interface is used called “P/Invoke”


• C# can call C/C++ Functions with:
[DllImport("Users.dll")]
private static extern uint Userfunction( . . .);

• Must expose C/C++ Functions in a DLL with:


extern “C”{
_declspec(dllexport) DWORD Userfunction(. . .);
}
Winsock Networking Example
• A simple C/C++ application using networking and sockets
• Sends “Hello World” message between two computers
• IP address is entered for destination computer
• Uses Port 7 (echo service must be enabled)
• Receiving computer sends back (echoes) message
• Uses the widely supported Winsock API library
• Must include and link using Winsock library (ws2.lib)
• Can also be used as a network loopback test on a single
computer by using it’s own IP address
• Newer higher-level APIs such as web services in C# require
less code
Table 8.3 WinSock APIs used in the Winsock_Echo example program

WSAStartup Initiates use of WS2_32.DLL by a process. Required once before other WinSock API
calls can be used.
connect Establishes a connection to a specified socket.
socket Creates a socket that is bound to a specific service provider.
bind Associates a local address with a socket.
listen Places a socket in a state where it is listening for an incoming connection.
accept Permits an incoming connection attempt on a socket.
sendto Sends data to a specific destination using a socket.
recvfrom Receives a datagram using a socket and stores the source address.
getaddrinfo Provides protocol-independent translation from an ANSI host name to an address.
freeaddrinfo Frees address information that the getaddrinfo function dynamically allocates in
addrinfo structures.
getnameinfo Provides name resolution from an IPv4 or IPv6 address to an ANSI host name and from
a port number to the ANSI service name.
shutdown Disables sends or receives on a socket.
closesocket Closes an existing socket
Figure 8.10 The Winsock_echo network loopback test program running on the eBox.
Web Services

Client Internet Server

Client Web
Application Service
HTTP
on Device Object
Web Service Code Example
• C# Server Code on remote system:
[WebMethod (namespace="http://myserver/xmlwebservices/")]
public int Add(int a, int b)
{
return a + b;
}

• C# Client Code on Device


using WebServiceClient.mywebreferenceclass
mywebreferenceclass ws = new mywebreferenceclass();
Sum = ws.Add(x,y);
Create Remote Control GUI
• The Create robot is an educational version of the iRobot
Roomba Robotic Vacuum Cleaner
• It has an internal microcontroller and can be controlled
using serial port commands with this C# example code.
This C# GUI was designed to remotely control the iRobot Create robot using the PCs serial port.
// Create Remote Control GUI for eBox attached to a iRobot Create Robot
// can drive robot, play song, return to charger, and view sensors
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.IO.Ports;
using System.Windows.Forms;
using System.Threading;

namespace CreateRemote
{
namespace CreateRemote
{
#region Enumerations
// Definitions of iRobot Create OpenInterface Command Numbers
// See the Create OpenInterface manual for a complete list
public enum Commands
{
// Create Command // Arguments
Start = 128,
SafeMode = 131,
FullMode = 132,
Drive = 137, // 4: [Vel. Hi][Vel Low][Rad. Hi][Rad. Low]
DriveDirect = 145, // 4: [Right Hi][Right Low][Left Hi][Left Low]
Sensors = 142, // 1: Sensor Packet ID
CoverandDock = 143,// 1: Return to Charger
Demo = 136, // 2: Run Demo x
Song = 140, // Download Song Notes - up to 16 notes
PlaySong = 141, // 2: Play Song number x
LED = 139, // 3: LED output control
Stream = 148, // x+1: [# of packets]IDs of packets to stream
QueryList = 149, // x+1: [# of packets] IDs of requested packets
StreamPause = 150, // 1: 0 = stop stream, 1 = start stream
}
/* iRobot Create Sensor Packet IDs */
public enum Sensors
{
BumpsandDrops = 7,
Distance = 19,
Angle = 20,
}
#endregion Enumerations
public partial class Form1 : Form
{
public SerialPort port;
public Int16 left = 50;
public Int16 right = 50;
public byte Color = 0;
private bool ProcessingSensorDataPacket = false;
public bool raw_sensor_debug = false;
public volatile short bumpandrop_data = 0;
public Form1()
{
InitializeComponent();
// Serial Port Open Code - used to communicate with iRobot Create robot
port = new SerialPort("COM1",57600,Parity.None,8,StopBits.One);
port.Handshake = Handshake.None;
port.ReadTimeout = 60000;
port.WriteTimeout = 60000;
try // Just in case .NET CF not updated
{
port.Open();
}
catch (Exception com_open_except) { }
}
// Start Button - send start and safe mode, start streaming sensor data
private void button5_Click(object sender, EventArgs e)
{
port.Write(new byte[] { (byte)Commands.Start,
(byte)Commands.SafeMode }, 0, 2);
Thread.Sleep(100);
port.Write(new byte[] { (byte)Commands.Stream, 1,
(byte)Sensors.BumpsandDrops}, 0, 3);
Thread.Sleep(200);
port.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);
}
// Stop Button - turn off drive motors
private void button6_Click(object sender, EventArgs e)
{
port.Write(new byte[] { (byte)Commands.DriveDirect,
(byte)0, (byte)0,
(byte)0, (byte)0 }, 0, 5);
}
// Forward Button - turn on drive motors
private void button1_Click(object sender, EventArgs e)
{
port.Write(new byte[] { (byte)Commands.DriveDirect,
(byte)(right >> 8), (byte)right,
(byte)(left >> 8), (byte)left }, 0, 5);
// Reverse Button - reverse drive motors
private void button4_Click(object sender, EventArgs e)
{
port.Write(new byte[] { (byte)Commands.DriveDirect,
(byte)(-right >> 8), (byte)-right,
(byte)(-left >> 8), (byte)-left }, 0, 5);
}
// Left Button - drive motors set to rotate to left
private void button3_Click(object sender, EventArgs e)
{
port.Write(new byte[] { (byte)Commands.DriveDirect,
(byte)(right >> 8), (byte)right,
(byte)(-left >> 8), (byte)-left }, 0, 5);
}
// Right Button - drive motors set to rotate to right
private void button2_Click(object sender, EventArgs e)
{
port.Write(new byte[] { (byte)Commands.DriveDirect,
(byte)(-right >> 8), (byte)-right,
(byte)(left >> 8), (byte)left }, 0, 5);
}
// Charger Button - return to charger using IR beacons
private void button7_Click(object sender, EventArgs e)
{
port.Write(new byte[] { (byte)Commands.Demo, (byte) 1}, 0, 2);
}
// Play Song Button - define and play a song on robot
private void button8_Click(object sender, EventArgs e)
{ // Send out notes & duration to define song and play song
port.Write(new byte[] { (byte)Commands.Song, 0, 16,
91, 24, 89, 12, 87, 36, 87, 24, 89, 12,
91, 24, 91, 12, 91, 12, 89, 12, 87, 12, 89, 12,
91, 12, 89, 12, 87, 24, 86, 12, 87, 48 }, 0, 35);
Thread.Sleep(100);
port.Write(new byte[]{(byte)Commands.PlaySong,
(byte)0 },0,2);
}
// Slider trackbar to set motor speed
private void Speed(object sender, EventArgs e)
{
left = (Int16) trackBar1.Value;
right = (Int16) trackBar1.Value;
}
// Checks for Proper Checksum at end of Sensor Data Packet
bool validData(byte[] buf, byte chksum)
{
foreach (byte b in buf) chksum += b;
if (chksum != 0)
{
return false;
}
else return true;
}
// Event activated whenever new serial data is recieved
// packets are sent every 15 MS
void port_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
if (ProcessingSensorDataPacket) return;
{
ProcessingSensorDataPacket = true;
try
{
// align with sensor packet header value and read in data
while (port.ReadByte() != 19) { }
byte[] buf = new byte[port.ReadByte()];
port.Read(buf, 0, buf.Length);
// grab final checksum, validate checksum, and update sensor data
if (validData(buf, (byte)(port.ReadByte()+
(buf.Length + 19))))
{ // Display raw sensor data on console for debug?
if (raw_sensor_debug) {
foreach (byte b in buf) Console.Write(b + " ");
Console.WriteLine();
};
// Checksum OK - so save new sensor data in packet
bumpandrop_data = buf[1];
}
}
catch (TimeoutException)
{
Console.WriteLine("Serial port Read timed out?");
}
catch
{
}
ProcessingSensorDataPacket = false;
}
}
// Update Sensor Display - timer executes this every 300 MS
private void timer1_Tick(object sender, EventArgs e)
{
// Flash through different colors on LED to indicate program is running
Color += 64;
port.Write(new byte[] { (byte)Commands.LED, 0, Color,
200 }, 0, 4);
// mask off sensor bits & Update Sensor Check Boxes on GUI
checkBox1.Checked = (bumpandrop_data & 1) == 1;
checkBox1.Update();
checkBox3.Checked = (bumpandrop_data & 2) == 2;
checkBox3.Update();
checkBox4.Checked = (bumpandrop_data & 4) == 4;
checkBox4.Update();
checkBox5.Checked = (bumpandrop_data & 8) == 8;
checkBox5.Update();
checkBox6.Checked = (bumpandrop_data & 16) == 16;
checkBox6.Update();
}
// Checkbox to display raw sensor data on console for debug
// delays response a bit - data backs up in buffers when on
private void checkBox2_CheckStateChanged(object sender,
EventArgs e)
{
raw_sensor_debug = checkBox2.Checked;
}
}
}
For debug purposes, checking “Display Raw Sensor Packets” in the CreateRemote GUI will display
incoming robot sensor data packets on the PC’s console window.
PC with mbed custom I/O
• Use mbed to add custom I/O hardware to a PC
• Use the USB virtual com port interface for
communication between mbed and the PC.
• C++/C# Code from earlier serial port examples
will work
• RPC can be used for complex applications
• PC can use a GUI for user interface
Serial IO Example

• Uses Linux File System APIs to read and write


data from a serial port (i.e.open,write,read,close).
• Examples use mbed’s USB virtual com port to
communicate with mbed
• Virtual com port device name is /dev/tyyACM0 on
both Pi and Beaglbone boards
• Code example runs on either board when mbed
is plugged into the boards USB port
• Mbed runs code to echo characters back and
toggles led1 whenever it gets a ‘1’ or ‘0’
RPC using C# on a PC

• https://developer.mbed.org/users/kennyainny/notebook/
mbed-net-rpc-library---revised/ uses new version of RPC
library
• Demo sets up a C# GUI on PC and controls mbed using
RPC over USB virtual com port.
PC C# GUI controlling mbed via RPC
Introduction to OS I/O
Device Drivers
Device Drivers
• Device drivers help provide a layer of
abstraction between the hardware and the
user’s application programs.
• A user developing an application does not need
to understand the low-level hardware details of
each interface.
• Applications programmers can be more
productive working at a higher level of
abstraction using device drivers.
• Numerous Drivers come with the OS.
• New or unique hardware may require that the
developer write a new device driver.
Device Drivers
• Depending on OS, a device driver that is part of
the kernel code may be required to communicate
with any I/O hardware
• RISC I/O registers may be in a protected memory
region that user code cannot access directly (but
OS Kernel drivers can)
• On X86 processors, I/O instructions are privileged
and protected by the mode bit, so I/O instructions
need to be executed in kernel mode (not in a user
mode program – so need driver)
• Some Linux examples run I/O code as superuser
to avoid this issue – but it is not as secure.
Stream Interface Driver
• Best choice for a simple driver for a device that
produces and/or consumes streams of data
• Applications use standard file system APIs to
call the driver
• Driver must have several required Standard
Entry Points for OS
• These entry points are used by the OS file
system API calls from user application code
– (i.e., open, read, write, seek, close…)
Windows Stream Interface Driver Functions.

Programming Description
element
XXX_Close (Device This function closes the device context identified by
Manager) hOpenContext.. This function is required to access the device
with CreateFile. If you implement XXX_Close, you must
implement XXX_Open.
XXX_Deinit (Device This function de-initializes a device. It is called by Device
Manager) Manager.This function is required by drivers loaded by
ActivateDeviceEx, ActivateDevice, or RegisterDevice.
XXX_Init (Device This function initializes a device. It is called by Device
Manager) Manager.This function is required by drivers loaded by
ActivateDeviceEx, ActivateDevice, or RegisterDevice.
XXX_IOControl This function sends a command to a device.This function might
(Device Manager) or might not be required, depending on the device capabilities
that the driver exposes. This function requires an
implementation of XXX_Open and XXX_Close.
XXX_Open (Device This function opens a device for reading, writing, or both. An
Manager) application indirectly invokes this function when it calls
CreateFile to obtain a handle to a device. This function is
required to access the device with CreateFile.
XXX_PowerDown Optional. This function ends power to the device. It is useful
(Device Manager) only with devices that can be shut off under software control.
XXX_PowerUp Optional. This function restores power to a device.
(Device Manager)
XXX_PreClose Optional. This function marks the closing handle as invalid and
(Device Manager) wakes any sleeping threads.
XXX_PreDeinit This function marks the device instance as invalid and wakes
(Device Manager) sleeping threads. This function is required if the XXX_PreClose
function is implemented.
XXX_Read (Device This function reads data from the device identified by the open
Manager) context. This function might or might not be required,
depending on the device capabilities that the driver exposes.
This function requires an implementation of XXX_Open and
XXX_Close.
XXX_Seek (Device This function moves the data pointer in the device. This function
Manager) might or might not be required, depending on the device
capabilities that the driver exposes. This function requires an
implementation of XXX_Open and XXX_Close.
XXX_Write (Device This function writes data to the device. This function might or
Manager) might not be required, depending on the device capabilities that
the driver exposes.This function requires an implementation of
XXX_Open and XXX_Close.
KOM Driver Example
• Educational example of a simple stream
interface device driver – not a production quality
driver!
• Used instead of standard COM port driver
provided with Windows CE (now called Windows
IoT)
• Based on earlier serial port IO example
• Uses CEDDK functions to communicate with
16550 UART compatible hardware on PC serial
port
• Must be setup as a DLL in Visual Studio
// This is a Sample Stream Interface Device Driver
// Educational example intended to illustrate how stream drivers work
// using the serial I/O port hardware on the target system
// and show the use of READ_PORT_UCHAR and WRITE_PORT_UCHAR
// from the CE Device Driver Kit (CEDDK)
//
// Setup for X86 PC (CEPC)
// using 16550 UART compatiable serial port hardware
//
// Not intended to replace a good serial port device driver!
// Does not use interrupts, have any timeouts, or provide
// support for all of the serial port's features
// Would normally use OS API calls for this operation!
//
// FOR DEMO: Connect Ebox COM2: to PC with null modem cable
// Run HyperTerminal with 9600 Baud 8 data bits 1 stop bit
// no parity and no flow control

#include "stdafx.h"
#include <windows.h>

// For WRITE_PORT_UCHAR & READ_PORT_UCHAR functions


// need to add to sources file include section:
// ..\Wince600\ICOP_Vortex86_60A_x86\cesysgen\ddk\inc; \
// ..\Wince600\ICOP_Vortex86_60A_x86\cesysgen\oak\inc; \

#include "ceddk.h"

// Also need to include CEDDK.lib in link (see sources file)


// add $(_SYSGENOAKROOT)\lib\$(_CPUINDPATH)\ceddk.lib
// to TARGETLIBS entries

// Declare the Standard External Stream Driver Functions


__declspec(dllexport) extern DWORD KOM_Init(LPCTSTR pContext, LPCVOID
lpvBusContext);
__declspec(dllexport) extern BOOL KOM_Deinit( DWORD hDeviceContext );
__declspec(dllexport) extern DWORD KOM_Open( DWORD hDeviceContext, DWORD
AccessCode, DWORD ShareMode );
__declspec(dllexport) extern BOOL KOM_Close( DWORD hOpenContext );
__declspec(dllexport) extern BOOL KOM_IOControl( DWORD hOpenContext,
DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD
dwLenOut, PDWORD pdwActualOut );
__declspec(dllexport) extern void KOM_PowerUp( DWORD hDeviceContext );
__declspec(dllexport) extern void KOM_PowerDown( DWORD hDeviceContext );
__declspec(dllexport) extern DWORD KOM_Read( DWORD hOpenContext, PUCHAR
pBuffer, ULONG Count );
__declspec(dllexport) extern DWORD KOM_Write( DWORD hOpenContext, PUCHAR
pBuffer, ULONG Count );
__declspec(dllexport) extern DWORD KOM_Seek( DWORD hOpenContext, long
Amount, WORD Type );

void DBGOut(DWORD dwValue);


void Setup_UART (PUCHAR Data_Port_Address);
void Write_Serial_Character (PUCHAR Data_Port_Address, UCHAR
UCHAR Read_Serial_Character (PUCHAR Data_Port_Address);

PUCHAR Data_Port_Address;
UCHAR Serial_Input_Data = 0;

// ----------------------------------------------------

BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,


LPVOID lpReserved )
{
switch(ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
OutputDebugString(L"KOM_DRIVER - DLL_PROCESS_ATTACH\n");
break;
case DLL_PROCESS_DETACH:
OutputDebugString(L"KOM_DRIVER - DLL_PROCESS_DETACH\n");
break;
case DLL_THREAD_ATTACH:
OutputDebugString(L"KOM_DRIVER - DLL_THREAD_ATTACH\n");
break;
case DLL_THREAD_DETACH:
OutputDebugString(L"KOM_DRIVER - DLL_THREAD_DETACH\n");
break;
default:
break;
}
return TRUE;
}

// Stream Driver Init...


DWORD KOM_Init( LPCTSTR pContext, LPCVOID lpvBusContext)
{
OutputDebugString(L"KOM_DRIVER - KOM_Init - Context: ");
OutputDebugString(pContext);
OutputDebugString(L"\n");
OutputDebugString(L"DemoDriver - Exit KOM_Init\n");
return 0x1234;
}

BOOL KOM_Deinit( DWORD hDeviceContext )


{
OutputDebugString(L"KOM_DRIVER - KOM_Deinit\n");

OutputDebugString(L"KOM_DRIVER - Exit KOM_Deinit\n");


return TRUE;
}
// Stream Driver Open
DWORD KOM_Open( DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode
)
{
OutputDebugString(L"DemoDriver - KOM_Open\n");
OutputDebugString(L"hDeviceContext - ");
DBGOut(hDeviceContext);
OutputDebugString(L"\n");

// Data Port Address for COM2:


Data_Port_Address = (PUCHAR)0x2F8;
// Sets up UART for 9600 Baud & No Interrupts with 8D NP 1S
Setup_UART(Data_Port_Address);

OutputDebugString(L"DemoDriver - Exit KOM_Open\n");


return 0x5678;
}
// Stream Driver Close
BOOL KOM_Close( DWORD hOpenContext )
{
OutputDebugString(L"KOM_DRIVER - KOM_Close\n");
OutputDebugString(L"hOpenContext - ");
DBGOut(hOpenContext);
OutputDebugString(L"\n");
// Add Close Function Code Here
OutputDebugString(L"KOM_DRIVER - Exit KOM_Close\n");
return TRUE;
}
// Stream Driver IOCTL
BOOL KOM_IOControl( DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn,
DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut )
{
OutputDebugString(L"KOM_DRIVER - KOM_IOControl\n");
OutputDebugString(L"hOpenContext - ");
DBGOut(hOpenContext);
OutputDebugString(L"\n");
// Add IOCTL Functon Code Here
OutputDebugString(L"KOM_DRIVER - Exit KOM_IOControl\n");
return TRUE;
}
// Stream Driver PowerUP
void KOM_PowerUp( DWORD hDeviceContext )
{
OutputDebugString(L"KOM_DRIVER - KOM_PowerUp\n");
OutputDebugString(L"hDeviceContext - ");
DBGOut(hDeviceContext);
OutputDebugString(L"\n");
// Add PowerUP Function Code Here
OutputDebugString(L"KOM_DRIVER - Exit KOM_PowerUp\n");
}
// Stream Driver PowerDown
void KOM_PowerDown( DWORD hDeviceContext )
{
OutputDebugString(L"KOM_DRIVER - KOM_PowerDown\n");
OutputDebugString(L"hDeviceContext - ");
DBGOut(hDeviceContext);
OutputDebugString(L"\n");
// Add PowerDown Function Code Here
OutputDebugString(L"KOM_DRIVER - Exit KOM_PowerDown\n");
}
}
// Stream Driver Read
DWORD KOM_Read( DWORD hOpenContext, PUCHAR pBuffer, ULONG Count )
{
ULONG i;
OutputDebugString(L"KOM_DRIVER - KOM_Read\n");
OutputDebugString(L"hOpenContext - ");
DBGOut(hOpenContext);
OutputDebugString(L"\n");

// Data Port Address for COM2:


Data_Port_Address = (PUCHAR)0x2F8;
// Read in Serial Data
for (i=0; i<Count; i++)
pBuffer[i] = Read_Serial_Character(Data_Port_Address);

OutputDebugString(L"KOM_DRIVER - Exit KOM_Read\n");

return Count;
}
// Stream Driver Write
DWORD KOM_Write( DWORD hOpenContext, PUCHAR pBuffer, ULONG Count )
{
ULONG i;
OutputDebugString(L"KOM_DRIVER - KOM_Write\n");
OutputDebugString(L"hOpenContext - ");
DBGOut(hOpenContext);
OutputDebugString(L"\n");

// Data Port Address for COM2:


Data_Port_Address = (PUCHAR)0x2F8;
// Write out Serial Data
for (i=0; i<Count; i++)
{
Write_Serial_Character(Data_Port_Address, pBuffer[i]);
// DBGOut((DWORD)pBuffer[i]);
}

OutputDebugString(L"KOM_DRIVER - Exit KOM_Write\n");

return Count;
}
// Stream Driver Seek
DWORD KOM_Seek( DWORD hOpenContext, long Amount, WORD Type )
{
OutputDebugString(L"KOM_DRIVER - KOM_Seek\n");
OutputDebugString(L"hOpenContext - ");
DBGOut(hOpenContext);
OutputDebugString(L"\n");
// Add Seek Function Code Here
OutputDebugString(L"KOM_DRIVER - Exit KOM_Seek\n");

return 0;
}

void DBGOut(DWORD dwValue)


{
TCHAR tcTemp[10];
wsprintf(tcTemp,L"%ld",dwValue);
OutputDebugString(tcTemp);
}

void Write_Serial_Character (PUCHAR Data_Port_Address, UCHAR


Serial_Data)
// Write out a character to the serial port
{
UCHAR Status;
// Wait for TX output ready bit=1
// Status I/O Port Address is Data I/O Port Address + 5
do{
Status = READ_PORT_UCHAR(Data_Port_Address + 5);
// if UART transmit buffer full release remainder of time slice
if ((Status & 0x40) == 0) Sleep(0);
} while ((Status & 0x40) == 0);
// Write data out on COM2:
WRITE_PORT_UCHAR(Data_Port_Address, Serial_Data);
return;
}
UCHAR Read_Serial_Character (PUCHAR Data_Port_Address)
// Read in a character from the serial port
{
UCHAR Serial_Data, Status;
// Wait for RX input ready bit=1
// Status I/O Port Address is Data I/O Port Address + 5
do{
Status = READ_PORT_UCHAR(Data_Port_Address + 5);
// if UART recieve buffer empty release remainder of time slice
if ((Status & 0x01) == 0) Sleep(0);
} while ((Status & 0x01) == 0);
// Read in new serial data
Serial_Data = READ_PORT_UCHAR(Data_Port_Address);
return Serial_Data;
}

void Setup_UART (PUCHAR Data_Port_Address)


{
UCHAR Temp;
// Setup UART to 9600 Baud 8D,NP,1S no interrupts
// Will need a good PC Hardware Reference Text and/or
// the 16550 UART data sheet to fully understand this!
// Disable COMx: Interrupts (use Programmed I/O)
WRITE_PORT_UCHAR(Data_Port_Address + 1, 0);
// Set Baud Rate to 9600 with clock Divisor settings
// Put in set divisor mode
Temp = READ_PORT_UCHAR(Data_Port_Address + 3);
WRITE_PORT_UCHAR(Data_Port_Address + 3, Temp | 0x83);
// Set Divisor LSB (note: 12 = 115200/9600)
WRITE_PORT_UCHAR(Data_Port_Address , 12);
// Set Divisor MSB
WRITE_PORT_UCHAR(Data_Port_Address + 1, 0);
// Back to normal operation mode (and set for 8D NP 1S)
Temp = READ_PORT_UCHAR(Data_Port_Address + 3);
WRITE_PORT_UCHAR(Data_Port_Address + 3, Temp & 0x03);

return;
}
Windows Registry & Drivers
• On an CreateFile (Open) API call the
Device Manager searches the registry for
an entry for the device name (i.e. KOM)
• The registry entry tells the device manager
which driver to use for the device
• So a new registry entry is needed for the
KOM driver to function correctly
• Can use Regedit program to add
Registry Entry for KOM driver

[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\KOM_DRIVER]
"Dll" = "KOM_Port.Dll"
"Prefix" ="KOM"
"Index"= dword:1
"Order"= dword:0
"FriendlyName" = "KOM Port Demo Driver"
"Ioctl" = dword:0
KOM Tester Example
• Need test program for new KOM driver
• Attach serial port to HyperTerminal on PC
• Open KOM1: Device
• Read & Write characters from serial port
• Close Device when CTL C sent
• Driver code outputs debug messages that
will show what is happening
// KOM_Tester.cpp : Defines the entry point for the console
application.
//
// Serial Port File I/O Test Program for KOM_Port Driver
//
// FOR DEMO: Connect Ebox COM2: to PC with null modem cable
// Run HyperTerminal with 9600 Baud 8 data bits 1 stop bit
// no parity and no flow control

#include "stdafx.h"

HANDLE hSerial;

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])


{
DWORD cBytes_out, cBytes_in;

char cBuffer_out[] = "\f\n Hello KOM Serial World!\n\rType


something and watch it echo back\n\rCtrl C to exit\n\r";
TCHAR cBuffer_in[80];
// Display message on console
printf("\nOpening KOM2: Serial Port to test new KOM Driver - Type
ctrl C on other device to exit\n\r");
// Open Serial Port COM2: for Read and Write
// Note: COM1: is setup to send out Debug info
// so COM2 becomes COM1
hSerial = CreateFile(_T("KOM1:"), GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL);
// Check for file open errors
if (hSerial == INVALID_HANDLE_VALUE){
printf("file open errors\n","%X", hSerial);
Sleep(4000);
return 0;
}
// Write to title out to serial port.
if (!WriteFile(hSerial, cBuffer_out, strlen(cBuffer_out),
&cBytes_out, NULL)) {
printf("file write errors\n");
Sleep(4000);
return 0;
}
cBuffer_in[0] = 0;

// Read in characters, copy to console display and Echo (write) back


// Loop until ctrl C (0x03) is typed
while (cBuffer_in[0] != 0x03){
// Read back data any serial data and display
if (ReadFile(hSerial, cBuffer_in, 1, &cBytes_in, NULL)){
if (cBytes_in == 0) break;
// Display Data read back
printf("%s",cBuffer_in, cBytes_in);
// Echo characters back to sender
if (!WriteFile(hSerial, cBuffer_in, cBytes_in,
&cBytes_out, NULL)){
printf("\rfile write errors\n");
Sleep(4000);
return 0;
}
}

}
// Close File
CloseHandle(hSerial);
return 1;
}
Running KOM Driver Test Program
Run Programs s KOM_Tester
PB Debugger Loaded symbols for 'C:\WINCE600\OSDESIGNS\OSDESIGN7\OSDESIGN7\
RELDIR\ ICOP_VORTEX86_60A_X86_RELEASE\KOM_TESTER.EXE'
s KOM_Tester 22:33:23 11/25/2006 Eastern Standard Time
End s KOM_Tester 22:33:23 11/25/2006 Eastern Standard Time

PB Debugger Loaded symbols for 'C:\WINCE600\OSDESIGNS\OSDESIGN7\OSDESIGN7\


RELDIR\ ICOP_VORTEX86_60A_X86_RELEASE\CONSOLE.DLL'
61200 PID:400002 TID:4880012 DemoDriver - KOM_Open
61200 PID:400002 TID:4880012 hDeviceContext -
61200 PID:400002 TID:4880012 4660
61201 PID:400002 TID:4880012
61202 PID:400002 TID:4880012 DemoDriver - Exit KOM_Open
61203 PID:400002 TID:4880012 KOM_DRIVER - KOM_Write
61203 PID:400002 TID:4880012 hOpenContext -
61204 PID:400002 TID:4880012 22136
61204 PID:400002 TID:4880012
61317 PID:400002 TID:4880012 KOM_DRIVER - Exit KOM_Write
61318 PID:400002 TID:4880012 KOM_DRIVER - KOM_Read
61318 PID:400002 TID:4880012 hOpenContext -
61319 PID:400002 TID:4880012 22136
61319 PID:400002 TID:4880012
69052 PID:400002 TID:4880012 KOM_DRIVER - Exit KOM_Read
69066 PID:400002 TID:4880012 KOM_DRIVER - KOM_Write
69067 PID:400002 TID:4880012 hOpenContext -
69067 PID:400002 TID:4880012 22136
69067 PID:400002 TID:4880012
69067 PID:400002 TID:4880012 KOM_DRIVER - Exit KOM_Write
69068 PID:400002 TID:4880012 KOM_DRIVER - KOM_Read
69068 PID:400002 TID:4880012 hOpenContext -
69069 PID:400002 TID:4880012 22136
69069 PID:400002 TID:4880012
69221 PID:400002 TID:4e80012 KOM_DRIVER - DLL_THREAD_DETACH
.
.
.
185145 PID:400002 TID:4880012 KOM_DRIVER - Exit KOM_Read
185159 PID:400002 TID:4880012 KOM_DRIVER - KOM_Close
185160 PID:400002 TID:4880012 hOpenContext -
185160 PID:400002 TID:4880012 22136
185162 PID:400002 TID:4880012
185162 PID:400002 TID:4880012 KOM_DRIVER - Exit KOM_Close
PB Debugger Unloaded symbols for 'C:\WINCE600\OSDESIGNS\OSDESIGN7\
OSDESIGN7\RELDIR\ICOP_VORTEX86_60A_X86_RELEASE\CONSOLE.DLL'
PB Debugger Unloaded symbols for 'C:\WINCE600\OSDESIGNS\OSDESIGN7\
OSDESIGN7\ RELDIR\ICOP_VORTEX86_60A_X86_RELEASE\KOM_TESTER.EXE'
206855 PID:400002 TID:40b0002 KOM_DRIVER - DLL_THREAD_DETACH
KOM Driver Works!
• Can see characters echoed on serial
port
• Debug messages show that the driver
is being called correctly by the test
program
• But, this is not a “production quality”
driver and it would need a lot of
improvements!
Linux Driver Examples
• Linux also supports a similar stream
interface device driver interface
• Details will vary with each OS, but have
many things in common
• Short Linux Device Driver Tutorial
Device Driver Resources

•Linux Device Drivers, 3rd Edition

•Programming the Microsoft Windows Driver


Model (2nd Edition)
Porting the OS to a new design
• Need to develop a Board Support
Package (BSP)
• BSP includes the following:
– Boot Loader
– OEM Adaptation Layer (OAL)
• Sometimes called HAL Hardware Abstraction Layer
– Device Drivers for Board
• Will be easier if you have some of these
items from a similar board
Bootloader
• Need a boot loader to bring up the OS on
a new device
• The boot loader places the run-time image
into memory for execution and then jumps
to the OS startup routine
• OS startup routine uses OAL routines
• Several example boot loaders are
provided with the OS
Modifying the OAL
• OEM Adaptation Layer (OAL) is the lowest
level of routines in the OS that
communicate directly with hardware
• Includes the basic hardware units needed
for the OS such as the real time clock,
timers, interrupts, and the cache
• Needs to be modified for each new
hardware design
Hardware Debug Tools
• Use special debug/trace pins on processor
• Can follow instruction execution and set
hardware breakpoints
• Useful when initially testing a new board –
software debug tools will not work until OS
is running
• May require a special connector or
processor socket on the PCB
Figure 11.5 This hardware debug tool is tracing the instruction
execution of an XScale (ARM family) processor using a general
purpose Tektronix logic analyzer. The logic analyzer is running
Windows XP. Special software disassembles code to provide the
XScale assembly language mnemonics. Image courtesy of Nexus
Technology.
Figure 11.6 This hardware debug tool is tracing the instruction execution of a P4
(X86 family) target processor using an in-target probe (ITP) adapter box with
special software running on a desktop PC running Windows XP. It disassembles
code to provide the X86 assembly language mnemonics and displays register
contents. Image courtesy of American Arium.
Porting Application Code to a new OS
• Can recompile source code – but it is not
that easy!
• OS APIs are different and need to be
changed to new OS APIs
• Can be easy - or so hard that starting over
might make sense. Depends on the
application
• A complex user interface (GUI) is typically
the main problem area
Other Issues to Consider
• Network Security
• Is Safety Critical Coding needed?
• Code Analysis Tools
• Legal Issues and License Agreements for new
designs
• Software Management and Scheduling
• Companies are under constant pressure to reduce
product development time and cost so they often
do not put enough time and effort into resolving
these all of these issues!
IoT Device Security
• IoT devices have major security issues to consider
– Hackers can and will attack any device with a network
connection
• Privacy concerns about data collection and on
devices with audio and video
• Automobile wrecks could result
• Medical devices could kill patients
• Power grids could be taken down
• More effort will be needed in this area in the future
Safety Critical Coding
• Applications that can endanger people or
cause catastrophic failure are “safety critical”
• Extra precautions must be taken
• Special safety-critical coding rules and extra
testing typically used in such cases
• Expect to reduce productivity 80% or more!
• Examples: Biomedical, Aircraft, Vehicles, &
Industrial Equipment
Rules for Safety Critical Coding
1. Restrict all code to very simple control constructs –
no goto statements, direct, or indirect recursion.
2. Give all loop structures a fixed upper bound. This
does not include non-terminating loops.
3. Do not use dynamic memory allocation after
initialization. Memory allocators can sometimes
have unpredictable behavior.
4. Functions should only be 1 page long. When
humans cross a page boundary the error rate
increases.
5. The average function should have around two
assertions. Assertions must be used to check for
anomalous conditions.
6. Declare all data objects at the smallest
possible level of scope.
7. Each calling function must check the error
codes returned and each called function
must check for valid parameters.
8. The preprocessor must only be used for
header files and simple macros.
9. The use of pointers must be limited. No more
than one level of dereferencing.
10. All code must compile with all compiler
warnings enabled. Code analysis tools
should also be used.
Code Analysis Tools
• Windows Visual Studio includes a static
code analysis tool.
• Log report for a module is generated
during build with errors and warnings
(more than compilers detect!)
• Detects buffer overruns, memory use
problems, and more
Other Tools
• Unified Modeling Language (UML) is a
developmental modeling language that is
intended to provide a standard way to
visualize and specify the design of a
system.
• Labview and Matlab Simulink can also be
used to develop real-time embedded
systems with graphical blocks.
Dependable Systems
An interesting discussion of software reliability
issues and famous failures for safety critical
systems can be found in:
Software for Dependable Systems:
Sufficient Evidence? Edited by Daniel Jackson,
Martyn Thomas, and Lynette I. Millett, Editors,
Committee on Certifiably Dependable Software
Systems, National Research Council 2007
http://www.nap.edu/catalog.php?record_id=11923
License and IP Issues
• Any legal issues will take longer to resolve than
you expect!
• May need legal agreements approved on OS,
the development of custom device drivers, and
applications developed by third parties
• Need to determine any such costs or license
fees early on and include in product cost
estimates
• Even Open Source Licenses may still have
some restrictions – read carefully
Choosing an Open Source Software
License in Commercial Context: A
Managerial perspective
Juho Lindman, Anna Paajanen and Matti
Rossi
Information System Science, Aalto
University School of Economics Helsinki,
Finland
Software Project Management
A discussion of software engineering
coding practices and project
management can be found in:
Code Complete, Second Edition
by Steve McConnell and
Software Estimation: Demystifying
the Black Art by Steve McConnell.

You might also like