Nothing Special   »   [go: up one dir, main page]

Chapter5_Driver_Firmware

Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

Firmware and

Device Driver
Tien Pham Van, Dr. rer. nat.
Hanoi University of Science and Technology
Compiled with reference to other presentations

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Course Objectives

• System software
• Device driver
• Firmware
• Middleware
• System building

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Fundamentals

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Software

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
System tiers

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
I/O and Devices

Processor Device table


dispatcher Device Driver
X
(interrupt handler)

Bus
command status data 0
data 1
Device Controller
(firmware and logic) ...
Device X data N-1

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology 6
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
What is firmware?

• A computer program that is written to work


directly on specific custom hardware and it
lives in non-volatile memory such as a flash
chip and it is executed directly from it.
• Firmware takes the physical hardware and
presents it to the operating system, which
then builds on top of it. In other words,
Firmware can be thought of as the glasses
through which the operating system can see
the actual hardware.
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Device Drivers

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
What are device drivers ?

• As the name says, they help in driving the


device, i.e. most of the embedded hardware
requires initialization and software management
system
• Device Drivers refer to the software code that
directly controls and interacts with the
peripherals. They form an abstraction layer
between the user interface and the hardware.
• It forms a connecting bridge between the
operating system and the hardware, without the
OS having any knowledge about the internal
details of the hardware.
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Types of drivers

• Device drivers are typically considered either


architecture-specific or generic drivers.
• A device that is architecture-specific manages the
hardware that is integrated into the master processor (the
architecture). Examples include on-chip memory,
integrated Memory Managing Units (MMUs) and
floating-point hardware.
• A generic device driver manages hardware that is located
on the board and not integrated into the master processor.
It manages hardware that is not particular to the
processor. This allows it to run on a variety of
architectures. Examples include board buses like I2C, PCI,
off-chip memory, off-chip IO’s.

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
I/O System

USB
Network
Device driver SSD
driver
Driver
driver

SSD USB
Network
Device controller controller Dev
controller adapter

SSD USB LAN


drive Bus

I/O programming - abstracted by OS


Embedded2/16Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Microkernel

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Functions of Device Driver

• Startup/ Shutdown of Hardware allows the


initialization of the hardware upon power-on or reset or
configuring hardware into its power-off state.
• Disable/Enable of Hardware allows other software
to disable or enable the hardware.
• Release/Acquire of Hardware allows other
software to free (unlock) the hardware or to gain
singular (locking) access to hardware.
• Read/Write of Hardware allows other software to
read/write data from the hardware.
• Install/Uninstall of Hardware allows other
software to install new hardware or to remove
hardware.

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Driver - from RTOS perspective
◼ Locating the right device for the I/O request
◼ Locating the right device driver for the device
◼ Issuing the request to the device driver
◼ Ensure synchronized access to the device
◼ Facilitate an abstraction that hides both the device
characteristics and specifics from the application
developers

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
• Device Drivers - RTOS | Coursera
https://www.coursera.org/lecture/iot-
architecture/device-drivers-AL7YG

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
3 I/O Data Transfer techniques

• Polling
• Interrupt-driven I/O
– CPU transfers data between memory and I/O
– Time consuming for high-speed devices
• Direct memory access (DMA)
– OS provides start info: memory address, control
– DMA controller transfers to/from memory
autonomously - direct
– Controller interrupts on completion / error

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
I/O Mechanisms - Polling

• Periodically check I/O status


– If device ready, do operation
– If error, take action
• Small or low-performance real-time
embedded systems
– Predictable timing
– Low hardware cost
• Wastes CPU time

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Interrupts - review

• When device ready or error


– Interrupt → CPU, checked every instruction
• CPU acknowledges interrupt ; saves the state
– interrupt handler dispatched
– determines cause, services the device and clears
the interrupt
– Context switch time
• Priority interrupts
– higher priority devices get more attention
– Can interrupt lower priority device

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
• Linux device driver lecture 11 : Hello world
module and module build system – YouTube
https://www.youtube.com/watch?v=fWw7gQzYTWc

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Port-Mapped vs. Memory-Mapped
 All IO devices must be initialized through device
control registers which located on the CPU board or
in the devices themselves
 During operation, the device registers are accessed
again and are programmed to process data transfer
requests
 To access these devices, it is necessary for the
developer to determine if the device is port mapped
or memory mapped

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Port-Mapped I/O

The I/O device address space is separate from the system memory address
space, special processor instructions, such as the IN and OUT instructions
offered

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Memory-Mapped IO

The device address is part of the system


memory address space. You can access
by any memory access instructions

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
I/O Subsystem and the Layered Design

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
IO Functions
Function Description
Create Creates a virtual instance of an I/O device

Destroy Deletes a virtual instance of an I/O device


Open Prepares an I/O device for use.
Close Communicates to the device that its services are no
longer required, which typically initiates device-specific
cleanup operations.
Read Reads data from an I/O device
Write Writes data into an I/O device
Ioctl Issues control commands to the I/O device (I/O control)

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
C Structure Defining the
Uniform IO API Set
typedef struct
{
int (*Create)( );
int (*Open) ( );
int (*Read)( );
int (*Write) ( );
int (*Close) ( );
int (*Ioctl) ( );
int (*Destroy) ( );
} UNIFORM_IO_DRV;

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Uniform IO Driver Table

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Device tree

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Device tree
https://www.youtube.com/watch?v=Nz6aBff
v-Ek&ab_channel=Bootlin

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
User space driver

• Not part of kernel (Linux); easier to write and debug


• Can be found in two broad categories: those
controlled through files in sysfs, including GPIO
and LEDs, and serial buses that expose a generic
interface through a device node, such as I2C
• http://www.embeddedlinux.org.cn/essentiallinuxde
vicedrivers/final/ch19.html
• Embedded Linux device drivers: Device drivers in
user space - Embedded.com
https://www.embedded.com/embedded-linux-device-drivers-
device-drivers-in-user-space/

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
User space driver

• How to Avoid Writing Device Drivers for


Embedded Linux - Chris Simmonds, 2net –
YouTube
https://www.youtube.com/watch?v=QIO2pJqMxjE

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Firmware

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Features

• Provides the low-level control for a device's


specific hardware
• May contain basic functions of a device, and
may provide hardware abstraction services to
higher-level software such as OS
• May act as the device's complete operating
system, performing all control, monitoring
and data manipulation functions
• Stored in non-volatile memory e.g. ROM,
EPROM, EEPROM, and Flash memory
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Features

• Firmware may run


independently at an
external device (by its
controller), or at an entire
computer system, such as
Hypervisor of server
• In the world of
embedded systems,
firmware stored inside a
board may refer to the
whole software running
by the microcontroller
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
In co-design

• IDE designing tools support porting firmware


to the target

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
C programming for microcontrollers

• Compile and link codes with libraries in IDE


• Download the binary into the target machine
– Inserting the target to an external device programmer
– Programming interface in the HW board, and program
the microcontroller while it is already attached to its
hardware. This method is usually referred to as In-System
Programming, or ISP
– Through one of its peripherals, usually a USART, while the
microcontroller is already running a boot-loader

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Key steps

1. Identify requirements
2. Differentiate the architecture from the design
3. Manage time constraints (soft, firm, hard real-
time)
4. Design for testing in different levels
5. Foresee customizing plans

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Tools for firmware developments

1. PyCharm
2. WebStorm
3. Qt Creator
4. MPLAB X
5. Visual Studio
6. Eclipse
7. NetBeans
8. MATLAB
9. Arduino
10.ARM Keil
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Middleware

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Concepts

• Software that provides services to software


applications beyond those available from the
operating system
• Middleware makes it easier for software developers
to implement communication and input/output, so
they can focus on the specific purpose of their
application
• The term is most commonly used for software that
enables communication and management of data in
distributed applications

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Popular middlewares in embedded world

• OSEK/VDX COM: APIs for communication within and


between ECUs

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Popular middlewares in embedded world

• CORBA (Common Object Request Broker


Architecture): facilitates the access to remote
services. RT-CORBA is to provide end-to-end
predictability of timeliness in a fixed priority system

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
POSIX threads

• APIs to provide multi-threading at the OS


level

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
UPnP and DPWS

• Universal Plug and Play (UPnP) is a set of


networking protocols that permits networked
devices, such as PC, printers, Wi-Fi APs, etc to
seamlessly discover each other's presence on the
network and establish functional network services.
• Devices Profile for Web Services (DPWS) specifies
services for discovering devices connected to a
network, for exchanging information about available
services, and for publishing and subscribing to
events
• Suitable to IoT world

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Miscellaneous middlewares

• MPI (Message Passing Interface) is designed


for message passing-based communication. It
has recently been extended to also support-
shared memory-based communication
• Mobile middleware offers various
transparencies that hide the complexities of
mobile environments: location, transport
protocol, etc. It typically involves services like
RPC, resource discovery, transactions,
directory, security, storage services and data
synchronization
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
System
building

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Challenges in building embedded systems
• Systems are huge
• Dependency Hell is a thing
• Builds take a long time
• Builds take a lot of
resources
• Embedded applications
require significant
customization
• Developers need to
modify from defaults
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Build System Defined

_Is_ _Is Not_


● Mechanism to specify and build ● An IDE
○ Define hardware/BSP ● A Distribution
(firmware, drivers as ● A deployment and
well) components provisioning tool
○ Integrate user-space ● An out-of-the-box
applications; including solution
custom codes
● Need reproducibility
● Must support multiple
developers
● Allow for parallel processing
● (Cross) Toolchains
● License Management
Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Yocto Project
Organized into independent layers:
● Separation of functionality
● Allows different release schedules
● Expandability
○ Recipes developed in python and bash
SDK mechanism
● Separation of system and application devs
● Easily allows multiple developers to contribute
Optimizations:
● Faster build time reusing prebuilt binaries
● Parallel builds

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Yocto Project - Getting Started
$ git clone -b rocko \
git://git.yoctoproject.org/poky.git
$ source poky/oe-init-build-env
$ MACHINE=qemux86 bitbake \
core-image-minimal
$ runqemu qemux86

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Buildroot -Overview
“Buildroot is a simple, efficient and easy-to-use tool to
generate embedded Linux systems through cross-
compilation.”1
● Primary output: boot images
● Does not support rpm-style package mgmt
● “Firmware Generator”
● Builds all components from source
● Focus on simplicity

Products:
● Root filesystem image
● Kernel, Bootloader, Toolchain
1 See more at https://buildroot.org/

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Buildroot - Getting Started
$ git clone -b 2018.02 https://git.buildroot.net/buildroot
$ cd buildroot
$ make qemu_arm_vexpress_defconfig
$ make
$ eval $(grep qemu-system-arm board/qemu/arm-vexpress/readme.txt)

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
OpenWRT -Overview

“OpenWrt provides a fully writable filesystem with


package management.”1

Primary focus isnetworking


○ Replacement firmware for consumer devices
○ Primarily a binary distribution
○ On-device package management

Products:
○ Firmware image in device-specific format
○ Network availablepackage repositories
1See more at https://openwrt.org/

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
OpenWRT - Build System

● Consists of Makefiles and patches


● Generates a cross-toolchain and root filesystem image
● Uses kconfig, like Buildroot
● More details here:
○ https://openwrt.org/docs/guide-developer/build-system

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Desktop Distros
• Use installer from favorite
distro
• Increased usage (Raspberry Pi)
• Slim down to meet your needs

• Dependent on distro vendor decisions


• Likely not targeted at embedded
applications
• May not be cross-development friendly

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596
Summary

Yocto Project Buildroot OpenWRT Desktop Distro

Expandability

Configurability

Ease of Getting
Started

Package
Availability

Industry Support

Embedded Networking Research Group School of Elec. and Telecom - Hanoi University of Science and Technology
Email: tien.phamvan1@hust.edu.vn C9-411, Dai Co Viet str. 1, HBT, Hanoi Tel: +84-243-8693596

You might also like