0% found this document useful (0 votes)
70 views90 pages

SmartDevices Chap1-2

1. A smart device is an embedded device capable of autonomous computing, connecting to other devices wirelessly or wired, and adapting its behaviors based on information about its environment. 2. Smart devices contain sensors, microprocessors, memory, and wireless connectivity that allow them to perform tasks autonomously without direct user input. 3. Smart devices operate at the edge of networks or as endpoints, and can process data locally without needing to report to the cloud. They are powered devices that integrate into the Internet of Things (IoT).

Uploaded by

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

SmartDevices Chap1-2

1. A smart device is an embedded device capable of autonomous computing, connecting to other devices wirelessly or wired, and adapting its behaviors based on information about its environment. 2. Smart devices contain sensors, microprocessors, memory, and wireless connectivity that allow them to perform tasks autonomously without direct user input. 3. Smart devices operate at the edge of networks or as endpoints, and can process data locally without needing to report to the cloud. They are powered devices that integrate into the Internet of Things (IoT).

Uploaded by

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

Chap 1

Smart Devices : Introduction

Emir DAMERGI
INSAT 2021/22
Smart Devices (Smart Embedded Devices)
• An embedded device is an object that contains a special-purpose computing (processing)
system : Generally, an embedded device's Firmware (Software) will only run a single
application which helps the device to do its job.

• An embedded system is a processor-based computer hardware system with software that is


designed to perform a dedicated function

• Embedded processors (Proessors


for embedded systems) represent
98% of processors market in 2019.
• 2% for PC/Mac processors.

2
DAMERGI Emir – INSAT 2021
Smart Devices (Smart Embedded Devices)

A Smart Device is an embedded device made intelligent ???

The definition has evolved over Time. The definition proposed in [1] will be retained:

“A smart device is a context-aware electronic device capable of

performing autonomous computing and connecting to other devices

wire or wirelessly for data exchange”

[1] Silverio-Fernández et al. « What is a smart device? - a conceptualisation within the paradigm of the internet of things
Visualization in Engineering (2018) - Springer

3
DAMERGI Emir – INSAT 2021
Smart Devices

Context-awareness [1]
The ability of a system or system component to gather information about its environment
at any given time and adapt behaviors accordingly.

Connectivity [1]
The ability to establish a connection to a network of any size; Sometimes the main
purpose might be gaining internet access, other times it might be sharing information with
other devices on the network. (Ethernet, Bluetooth, Zigbee, NFC, Wi-Fi, 5G, etc., )

Autonomous computing [1]


The ability of a device or multiple devices performing tasks autonomously without the
direct command of the user.

[1] Silverio-Fernández et al. « What is a smart device? - a conceptualisation within the paradigm of the internet of things
Visualization in Engineering (2018) – Springer
4
DAMERGI Emir – INSAT 2021
Smart Devices
This definition complies with the main idea of the IoT which is that Any “thing” can be
part of the IoT.

What Are Smart Devices? (www.arm.com/glossary/smart-devices)


Smart devices are all of the everyday objects made intelligent with advanced compute, including AI
and machine learning (not necessary), and networked to form the internet of things (IoT).
Smart devices can operate at the edge of the network or on very small endpoints, and are powerful
enough to process data without having to report back into the cloud

What is IoT? (www.oracle.com/internet-of-things/what-is-iot/)


The Internet of Things (IoT) describes the network of physical objects—“things”—that are
embedded with sensors, software, and other technologies for the purpose of connecting and
exchanging data with other devices and systems over the internet.

Thing (IoT) = Smart Device

5
DAMERGI Emir – INSAT 2021
Smart Devices: IoT Edge and Endpoint Devices

Internet Cloud (Servers)

Internet Access

The Edge

Gateway

Edge Device Network Entry point to the IoT Cloud.


Bluetooth, Zigbee, Lorawan, …
Endpoint Device
The most remote element in an IoT communication channel
(thing)

• An endpoint can also be an Edge device.


• In addition to the Gateway function, Edge devices can perform complex tasks
(Data agregation, processing , filtering, secutity, etc..) 6
DAMERGI Emir – INSAT 2021
Smart Devices
Every thing can become smart and part of the IoT if we add a sensor, a tiny bit of computing
and network connectivity

Smart chair

7
DAMERGI Emir – INSAT 2021
Smart Devices

Smart Shoes

Computing system
Pressure sensors
Accelerometer
Temperature sensor
Battery
BlueTooth Connectivity

8
DAMERGI Emir – INSAT 2021
Smart Devices

 A smart device is an embedded  An embedded system is a


system with: processor-based computer
• context awareness hardware system with
• Autonomous Computing software that is designed to
• Connectivity perform a dedicated function

Sensors Computing
Wired or Wireless
(embedded) Network
Interface
Actuators System
Connectivity
Context Autonomous
Awarness computing

9
DAMERGI Emir – INSAT 2021
Smart Devices : computing System

Serves as Interface to enable communication between the processing device and external peripherals
P.I *
(display, keyboards), sensors, actuators,
Periph examples: GPIO (Digital), Analog, Ethernet, USB , Serial, etc….

* : Peripheral Interface ( Interface de périphérique)


10
DAMERGI Emir – INSAT 2021
Smart Devices :

• Sensors Processing
• Actuators device D
• Display Devices Flash,
Eeprom RAM
BUS
(Code) (Variables)

P.I • Connectivity
P.I
B P.I
P.I
C
P.I
Computing System A

A (following in this chapter): Computing Systems for Smart Devices

B Chapter 2: Input/Output Operations in Smart Devices (context Awareness)


B
C Chapter 3: Smart Devices and Connectivity
3
D Chapter : Smart Devices and edge Computing
DAMERGI Emir – INSAT 2021 11
Smart Devices : Programming

User application

High Level (Object Oriented Library)

MiddleWare (Protocols, File System, Scheduler)

Firmware or HARDWARE ABSTRACTION LAYER


(HAL or Firmware Library)

Smart Device (Computing System + Sensors + Connectivity +..)

DAMERGI Emir – INSAT 2021 12


Smart Devices :

B A A B
E
Chapter 2,3 and D (Arduino)

D
C

C Chapter 4: Low level Input/output – Access (Cortex-M bassed SOC)

D Chapter 5: Firmware Library

E Chapter 6: Object Oriented Layer Programming


13
DAMERGI Emir – INSAT 2021
Computing System for
Smart Devices
Smart Devices : Computing System

A Sytem on a chip, also known as an SoC, is an integrated circuit that takes a

single platform and integrates an entire electronic or computer system onto it. It

is, exactly an entire system on a single chip. SoCs are generally designed for a

specific application (SoCs for Smartphones, for Digital Receivers, etc..)

A microcontroller (µc, or MCU for microcontroller unit) is a small computer on a integrated circuit (IC)

chip. It contains one or more CPUs (processor cores) along with memory and programmable

input/output peripherals.

15
DAMERGI Emir – INSAT 2021
Smart Devices : Computing Systems
The two terms (MCU, SoC) are often used interchangeably. The main difference is that

• A SoC, in addition to standard peripherals embedded in a MCU (Serial, SPI, I2C, USB, Timers,

etc..), typically contains a greater number of onboard advanced peripherals that make it specific to a

given applications Field. And it can be used with minimal number of additional external circuits.

Bluetooth connectivity
embedded

UART
SPI
I2C Bluetooth Sensors
Sensors
Module Actuators
Actuators

STM32F4 MCU STM32WB55 SOC

Using ST MCU (STM32F4) in a Smart Device Using ST SOC (STM32WB55) in a Smart


with Bluetooth connectivity Device with Bluetooth connectivity

16
DAMERGI Emir – INSAT 2021
Smart Devices : Computing Systems – Socs/MCUs

SOC / Processor Flash Wifi BLE Std Zigbee OO IDE Company


MCU (MB) BL
ESP8266 Tensilica Standard 1 to 4 Yes No No No Arduino Espressif (CN)
106Micro @80Mhz

ESP32 Tensilica Xtensa LX6 1 to 4 Yes Yes Yes No Espressif (CN)


@160MHz Arduino
(1/ 2 Cores )
NINA- 1 No Yes No No Arduino U-blox (CH)
B306 ARM Cortex-M4 +
ARM MBED
STM32 ARM Cortex-M4 1 No Yes No Yes Arduino ST-
WB55 + + Microelectronics
ARM Cortex-M0 ARM MBED (FR/ITA)
STM32F4 1 No No No No Arduino ST-
(MCU) ARM Cortex-M4 + Microelectronics
ARM MBED (FR/ITA)

BLE: BlueTooth Low Energy (5.0)


Std BL: Standard BlueTooth (up to 4.
OO IDE: Object Oriented Integrated development environment

17
DAMERGI Emir – INSAT 2021
Smart Devices : Computing Systems - Boards

NodeMCU (ESP8266)
NodeMCU (ESP32)

Nano 33 BLE Sense P-Nucleo-WB55 Nucleo-F401


(Nina-B306) (STM32WB55) (STM32F401 MCU)

18
DAMERGI Emir – INSAT 2021
Smart Devices : Debug Access Port and In-System Programming (ISP)
IDE PC Target
Source Flash Mem
Code Processing (Code)
device

Compiler RAM Flash,


(Variables)
BUS User code
(Code)
(Application)
Binary P.I
File
P.I Bootloader
Debugger/Programmer P.I
Formats: SWD P.I
.bin DAP
.hex
.elf

The Flash memory of a SoC conatians User code


User Code can be updated by using a
and Bootloader code (company).
Debugger/programmer probe that communicates
Both are called Firmware (a specific class of software
with the DAP peripheral (Debug Access Port).
that provides the low-level control for a device's specific
The DAP has access to Flash memory and can
hardware)
update the user code The Bootloader is the first code executed after system
Reset. It jumps to the Application.
19
DAMERGI Emir – INSAT 2021
Smart Devices : Bootloader and In Application Programming (IAP)

Bootloader) is the code executed just after


SoC Reset. It is the BIOS (PC) equivalent Update User
code from UART
in embedded systems. It :
• Initializes some peripherals parameters.
Update User
• Polls for update user code update code from CAN
request ?
 If yes, the user code is updated through
Update User
one of the available peripherals (UART,
code from USB
SPI; I2C, CAN, USB).
And the SoC is reset.
Update User
code from I2C
 If not, a jump to user code is executed.
Jump to user
code
Update User
code from SPI
For some SoCs, Code update wirelessly is
possible. It is called OTA (Over the Air)
RESET
code update or FOTA (Firmware Over The SoC
Air).
STM32F4x Bootloader Activity Diagram
Examples: STM32WB55 and Nano 33
support FOTA (Bluetooth).
20
DAMERGI Emir – INSAT 2021
Smart Devices
Development Environment
Smart Devices : Programming Tools/IDE

22

DAMERGI Emir – INSAT 2021


Smart Devices : Programming Tools/IDE

Low Level (Register API based High Level


Access) (ST HAL) (OO: Arduino /MBED)

User Expertrise Very high High Low to medium

Dev Time Very Long Long Very short

Code Size Very Short Short Large

Performance Optimized ~Optimized ~ poor

Code Easy Easy Difficult


Control/modification

Users Professionals Professionals Hobbyists


Professionals *)

*)- For Fast Prototyping


- For products without Performance/memory Size
23
constraints (Expl: Sonoff – ESP8266/Arduino)

DAMERGI Emir – INSAT 2021


Smart Devices : ARM MBED IDE

Mbed Studio is a free IDE for Mbed OS application and library development (Win, MacOS, Linux).
Supports C and C++ programming Languages.
Mbed supports only MCUs and SoCs based on the Cortex Processors

24
DAMERGI Emir – INSAT 2021
Smart Devices : Arduino IDE

The Arduino IDE is an open-source cross-platform application (Windows, Linux and


MacOS), written in JAVA language, and developed by Arduino Company.
• For Programming Languages, It supports the C and C++.
• Originally, it was used for only the boards developed by the Arduino company.
• Many other Microcontrollers and Boards Manufacturer, have developed their own
Libraries (Arduino compatible) and added them to the IDE.
 ESP8266 and ESP32 SOCs (Espressif)
 STM32 MCUs (ST Microelectronics) “ARM Cortex-M”
 Nordic MCUs (Nordic). “ARM Cortex-M”
 MSP MCUs (Texas Instruments). “ARM Cortex-M”
 NINA SoCs (UBLOX). “ARM Cortex-M”

25
DAMERGI Emir – INSAT 2021
Smart Devices : Arduino Sketchs Structure

The minimal Arduino project (sketch) contains:

1- Global Variables declarations, defines and


library including will be put at the beginning.

2- Setup function : This function will be


executed once at program start. Contains the
code to initialize the variables, pin modes, start
using libraries, etc.

3- Loop function: will be run continiuously as


the board is powered. The code controlling the
application behavior must be added here.

26
DAMERGI Emir – INSAT 2021
Chap 2
Smart Devices : Context Awareness
Emir DAMERGI
• Digital I/O INSAT 2021/22
• Serial (UART) Communication
• Analog Data Acquisition (Analog Sensors)
• Digital Data Acquisition (Digital Sensors)
• Data Storage
Smart Devices
Digital Input /output
Digital I/O: GPIOs
SoC
Flash RAM
Processor

GPIO
GPIOs P.I pins
pins P.I

GPIO
P.I

All SoC pins (except Power/Gnd pins) are connected to the


GPIO Peripherals «General Purpose Input Output».
Pins provide an electric connection
between the SoC peripherals and The Processor has direct access (Read/Write) to the

external circuits. GPIO pins.

All Communications between a SoC Perpiheral and


external devices must go through the GPIOs.
*) Generally 16 pins for each GPIO Peripheral
24
DAMERGI Emir – INSAT 2021
Digital I/O : GPIO Pins & Ardunio Numbering

Arduino Connector
When programming SoCs Input/output operations,
the used pin(s) must be identified.
For Arduino IDE, the connector pinout identifiers
can be used D0..D15 (Digital) and A0..A5 (Analog)
GPIO4

#define GreenLed D12

For Pins that are not tied to the Arduino Connector,


the ardunio pin numbers must be known. For each
Example for Arduino Pin
installed board, a pins_arduino.h file contains the
numbering (ESP8266)
pin mapping.

#define PushButton 4

It is possible to use Arduino pin Numbers for connector pins.

30
DAMERGI Emir – INSAT 2021
Digital I/O: Arduino Programming

Pin Configuration Write a Value to Pin Read Pin Value

Arduino Connector
Example:
An application that continously :
Tests if the PushButton is pressed (Low level).
 If yes : The Led is Turned ON
GPIO4
 No: the Led is Turend OFF

31
DAMERGI Emir – INSAT 2021
Digital I/O: Arduino Programming
Arduino Connector

GPIO4

Example:
An application that continously :
Tests if the PushButton is pressed
(Low level).
 If yes : The Led is Turned ON
 No: the Led is Turend OFF

32
DAMERGI Emir – INSAT 2021
Digital I/O: power Interface

~ mA
5V

SoC pins can only support low current (~30 mA max) and Voltage (5V).

To control High Current/Voltage Devices, a Power Interface must be used.

Example: Relay, power Transistor.

33
DAMERGI Emir – INSAT 2021
Smart Devices
UART Communications
USART:

Synchronous

Asynchronous

STM32F4: USART1, USART2, USART3, UART4, UART5, USART6


DAMERGI Emir – INSAT 2021
UART: Communication with modules / PC

Bluetooth

Zigbee
TX

Wifi
RX

GPS
PC
Terminal

TX USB/Serial Converter
serial USB
(CH340, PL2110, ST-
Link)
RX

DAMERGI Emir – INSAT 2021


UART: TX/RX Pins

As stated, all communications between


Flash RAM Processing
device Peripheral interfaces and external
UART1_TX

GPIO
devices must go through GPIO Pins.
It is obviously, the case of UART
UART 1 peripheral. Each UART uses two GPIO
UART1_RX pins: one for sending (TX) and one for
UART 2 GPIO UART2_TX receiveng serial data (RX) .

UART i UART2_RX
Generally, before configuring and using
UART peripherals, the correspending TX
and RX pins must be configured.
Generally, for each UART, more than one
In Android, this is done automatically
choice (2 or 3) are available for the TX
when invoking the UART to use
and RX pins. It depends on SoC
architecture.

DAMERGI Emir – INSAT 2021


UART Communications : Serial Object
Serial Object Creation and Communication parameters configuration:

If config is not specified, the default value is used :


SERIAL_8N1: 8 data bits, No Parity bits and 1 Stop bit

Setup() { Generally, the UART Pins are specified and


pre-configured.
Serial.begin (115200); //initialize Serial Port with 115200 bits/sec

while ( !Serial) {//wait for Serial port Initialization The default Serial object names in arduino are:
} Serial (the UART connected to the USB/Serial

} Converter)
Serial1 for the second UART, Serial2, etc…

38
DAMERGI Emir – INSAT 2021
UART Communications: Sending Data

The Arduino Default Total


Buffer size is 64 bytes.
If more bytes are sent, the
write operation can be blocked. Examples:
Serial.print (" HELLO !!") ;
int X=17; Example:
Serial.print (X);  17 Char Msg[ ] = " HELLO !!";
Serial.print (X,BIN);  10001 Serial.print (Msg , 7) ;
Serial.print(X,HEX);  11

Float Accel = 35,26548


Serial.print (Accel);  35,26
Serial.print (Accel,4);  35,2654

39
DAMERGI Emir – INSAT 2021
UART Communications; Receiving Data

const byte numChars = 32;


char receivedChars [numChars];
Byte nbr;

while (Serial.available() > 0 && nbr < numChars)


{

char rc;
rc = Serial.read();
receivedChars[nbr] = rc;
nbr++;
}

40
DAMERGI Emir – INSAT 2021
Smart Devices
Analog Sensors
Processing Analog quantities ?
Processor

Binary Data Voltage Temperature, Speed,


Digital
Processing Analog Quantity Sensor Humidity, light

Sampling
Analog Digital
Conversion

Quantization
DAMERGI Emir – INSAT 2021 42
Analog to Digital Conversion
Processor
Analog Digital Temperature, Speed,
Binary Data Voltage Sensor
Digital Converter
Processing Analog Quantity Humidity, light
ADC

Quantized Quantization Sampled


Coding Sampling
signal signal

Sampled signal
Quantized signal
Vmax V111
max
Quantization Error (max = ΔQ/2)
110
101
ΔQ: Quantum
100
011
010

V001
min x
Vmin 000x
100 111 011 011 101 000

DAMERGI Emir – INSAT 2021 43


Sampling Period

time
Fourier Transform
frequency

Fmax

Sampling Frequency min= 2* Fmax

 Sampling Period max =1/2*Fmax

Tconv :Conversion Time

Sampling Quantization Sampling Quantization


+ Coding + Coding

Sampling Period min= Tconv


Tconv < Sampling Period < 1/(2*Fmax)

DAMERGI Emir – INSAT 2021 44


Conversion Result

Nin (N bits) Vin = Vref - + Nin * ΔQ


2N-1=111…1

Quantization Step (Quantum)


=
ΔQ = (Vref+ - Vref-) / 2N

2 = 00..010
1 = 00..001
0 = 00..000 Vin
Vref- Vref- Vref +
Vref- Vref+
- ΔQ Emir – INSAT 2021
+ΔQ +2*ΔQ DAMERGI 45
Conversion Result
Processor
Analog Digital Temperature, Speed,
Binary Data Voltage Sensor
Digital Converter
Processing Analog Quantity Humidity, light
Nin ADC Vin
G

Vin = Vref - + Nin * ΔQ


Vin (Voltage)

Δy slope
Vmes
Δx = Δy/Δx

G = ( Vref - + (Nin * ΔQ) – VMES ) /slope + Gmes


Gmes G
(quantity to
measure)
ΔQ = (Vref+ - Vref-) / 2N
G = ( (Vin – Vmes )/ slope ) + Gmes

DAMERGI Emir – INSAT 2021 46


Analog I/O: Arduino programming

It specifies the Vref+


source (It depends on Returns a value from 0 to 1023
the board). Generally, (for a 10 bits resolution:
default value is 5V default).
Vref- is tied to ground To exact voltage can be
(0V) calculated (see last diapo)

47
DAMERGI Emir – INSAT 2021
Analog I/O: Arduino Programming

A SoC with:
• D12 conneceted to a LED.
• D3 connected to a Button (Pressed =LOW)
• A1 connected to an TMP36 (Temperature Sensor)
• ADC with 10 bits max resolution, Vref++5V, Vref-=0V

The Application must each second:


- Test if the Push Button is pressed/
 If yes:
- Convert Temperature and send conversion result to
serial port.
- Calculate Temperature and send value to Serial Port.
- If (Temp<16) or (Temp>28): Led ON

48
DAMERGI Emir – INSAT 2021
Analog I/O: Arduino Programming

G = ( Vref - + (Nin * ΔQ) – VMES ) /slope + Gmes

Vref-= 0V
0,5
ΔQ = (Vref+ - Vref-) / 2N = 5000/1024

Δy = 1,2 – 0,5 V = 0,7 V = 700 mV, Δx = 75 – 0 = 75°C

Slope = 700mv /75 °C = 9,33 mV / °C


0

49
DAMERGI Emir – INSAT 2021
Analog I/O: Arduino Programming

𝑁𝑖𝑛 ∗ 5000
( ) − 500
𝑇𝑒𝑚𝑝 𝑚𝑉 = 1024
9,33

50
DAMERGI Emir – INSAT 2021
Analog I/O: Arduino programming (PWM)

Vmoy = 0

Generates a Periodic ON/OFF Digital


signal with duty cycle: Ton / T = value Vmoy = 1,25V

Vmoy = 2,5 V
PWM Signal
T = Period
Vmoy = 3,75V
T=Ton + Toff

V
Vmoy = 5 V
Ton Toff

Vmoy = Ton/T * V

51
DAMERGI Emir – INSAT 2021
Analog I/O: Arduino programming (PWM - DC Motor Control)
DC* Motors are drived by PWM signals  Motor Speed = fct (Vmoy) = fct (Value)

DC Moror : Bidirectionnal
DC Moror : One Direction

Digital out
PWM signal

Digital out

PWM signal
(Speed)

H-Bridge

*) Direct Current : courant continu 52


DAMERGI Emir – INSAT 2021
Analog I/O: Arduino programming (PWM - Servo Motor Control)
#include <ServoESP32.h> for
ESP32

+V

PWM

53
DAMERGI Emir – INSAT 2021
Smart Devices
Digital Sensors
Digital Sensors: I2C/SPI communications

Analog signals are much affected by external noise and create errors in the output signal.

Digital signals have higher noise Immunity. So, digital sensors are preferred over analog ones.

However, given that all measured quantities are analog, Analog to Digital Conversion is Inevitable. But it is

executed inside the Digital sensor itself.

Registers
Digital Sensors contain some Registers used to store
ADC
• A/D Converter and Communication configuration
Analog
• Conversion Results (binary data)
Sensor

Each Register has its address. Communication Analog/Digital


Interface Converter

Digital Sensor

55
DAMERGI Emir – INSAT 2021
Digital Sensors: I2C/SPI communications

Generally, Digital Sensors support both SPI (Serial Peripheral Interface) and I2C (Inter-Integrated Circuit)

serial communication protocols

SoC
Registers
SPI SPI
or I2C or I2C
ADC

Analog
Sensor
The SoC, through Peripheral Interfaces (SPI/I12C),
Communication Analog/Digital
has access to the Digital Sensor Registers.
Interface Converter

The SoC, is so able to configure the Sensor (ADC) by Digital Sensor

writing to configuration results and to read the

conversion result by reading the corresponding

Register(s).
56
DAMERGI Emir – INSAT 2021
Smart Devices
Digital Sensors : SPI
Serial Connectivity: SPI
SPI MASTER SPI SLAVE

SPC
SDI
SDO
/SS

• Developped by Motorola (1980s)


• Synchronous and Full Duplex communication (Clock generated By Matser)
• High Speed (100 Mbps @ 0,1m)
• 4 wires*:
SCLK: Serial CLOCK  SPC: Serial Peripheral Clock
MOSI: Master Output Slave Input  SDI : Serial Data In
MISO: Master Input Slave Output  SDO: Serial Data Output
/SS: Slave Select (Active Low Level)

* : 3 Wires Communication is possible (SDO=SDI)  Half Duplex


DAMERGI Emir – INSAT 2021 58
Serial Connectivity: SPI
SPI MASTER SPI SLAVES

A point (Master) to Multipoint


(Slaves) communication is
possible:

Slave Select Signal • 3+N Wires are needed for


for each Slave controlling N Slaves
• At a given time, only one
unique Slave must be active.

DAMERGI Emir – INSAT 2021 59


Serial Connectivity: SPI

SDI / WR + Address i Data to Slave ……… Data to Slave Data to Slave


(WRITE) (WRITE) (WRITE)

SDO /

time
Generally, the sent data are written to Reg @i, @i+1, @i+2, etc…
But the slave can be configured to write to the same Register @i
DAMERGI Emir – INSAT 2021 60
Serial Connectivity: SPI

SDI / Ctrl + Address Dummy Data ………… Dummy Data Dummy Data

SDO / Data from slave …………… Data from slave Data from slave
(READ) (READ) (READ)
time

To read Data, The master must continue to send dummy data to activate the clock signal

Generally, the data are READ from Reg @i, @i+1, @i+2, etc…
But the slave can be configured to read the same Register @i
DAMERGI Emir – INSAT 2021 61
SPI: Arduino Programming - Configuration/Initilaization

Flash RAM Processing The Initialization of the SPI object is put in the setup
device
SPI1_SCK section (begin method).

GPIO
SPI1_MOSI
The Slave Select pin will be configured in OUTPUT
mode and set HIGH to disable SPI Slave device at
SPI 1
beginning
SPI1_MISO
SPI 2
GPIO

SS

For installed Boards, SPI Peripherals are


declared and the 3 pins (SCK, MOSI and MISO)
are defined.
The user can choose any free pin for the Slave
Select signal that will be controlled by the user
code (LOW, HIGH)

DAMERGI Emir – INSAT 2021 62


SPI: Arduino Programming - Write to slave

Write 1 Byte in Reg


(@Reg=i+1)

SPI Master 0 @Reg = *@Reg= Value (DI7..DI0)


i+1
SoC
SCK SPI Slave )
(Digital Sensor
MOSI Value
SPI SPI
MISO @ Reg
MySPI_SS /CS
i
ADC
i+1
Analog
digitalWrite (MySPI_SS, LOW); i+2
Sensor

SPI.transfer (i+1); //send address …


I2C Interface Analog/Digital
SPI.transfer (Value); //send value 8 bits Converter

digitalWrite (MySPI_SS, HIGH);

Rq: Register adresses are generally <=63 (0111 1111), so that the MSB (bit 7) is 0 (Write operation)
DAMERGI Emir – INSAT 2021 63
SPI: Arduino Programming - Write to slave

Write many Bytes (2) in Reg


(@Reg=i+1, i+2)

0 @Reg = *@Reg i+1= Value1 (DI 7..DI 0)


i+1
*@Reg i+2= Value2 (DI 15..DI 8)

digitalWrite (MySPI_SS, LOW);

SPI.transfer (i+1); //Send address

SPI.transfer(Value1); //Send first value  @ i+1

SPI.transfer(Value2); //Send second value  @i+2

digitalWrite(MySPI_SS, HIGH);

Rq: Generally, the Slave Device will increment the address after each byte reception. If the Regsiter
address is X, the sent values will be written to registers @X, @X+1, @X+2, ….@X+n.

DAMERGI Emir – INSAT 2021 64


SPI: Arduino Programming - Read From slave

Read 1 Byte:

Var = *@Reg = DO7..DO0


1

Const int Reg_Addr = …

digitalWrite(MySPI_SS, LOW);

SPI.transfer(Reg_Addr | 080 ); //Send address with MSB (bit 7 = 1): Read

Uint8_t Rcv_Data = SPI.transfer(0x00); //Send dummy Data to activate clk signal and read Data from Slave

digitalWrite(MySPI_SS, HIGH);

DAMERGI Emir – INSAT 2021 65


SPI: Arduino Programming - Read From slave

Read 2 Bytes:

Var = *@Reg = DO7..DO0


1

Const int Reg_Addr = …

digitalWrite(MySPI_SS, LOW);

SPI.transfer(Reg_Addr | 080 ); //Send address with MSB (bit 7 = 1): Read

Uint8_t Rcv_Data1 = SPI.transfer(0x00); // Read Data from Slave (@Reg_Addr)

Uint8_t Rcv_Data2 = SPI.transfer(0x00); // Read Data from Slave (@Reg_Addr + 1)

digitalWrite(MySPI_SS, HIGH);

RQ: It is possible to read multiple bytes from successive Address registers @X, @X+1, @X+2, ….@X+n.

DAMERGI Emir – INSAT 2021 66


SPI: Arduino Programming - Example

The Sensor HTS221 (Temperature, pressure) is


connected to the SPI bus of an Arduino compatible
Bit 7 of CTRL_REG1 = 1
Board (see Arduino Numbers)
to power HTS

Bit 0 of CTRL_REG2 = 1
to start a Temperature
conversion

Bit 0 of STATUS_REG = 1
 Conversion Finished.
 New Values available.

• The Conversion result is 16 bits long and placed in the


2 *8bit registers; TEMP_OUT_L and TEMP_OUT_H.
• The Temperarue is calculated: conversion result*slope
+ Zero_Value.

DAMERGI Emir – INSAT 2021 67


Smart Devices
Digital Sensors : I2C (Inter-Integrated Circuits)
Serial Connectivity: I2C
Invented
by philips
Serial Clock (SCL)
1982 Generated by Master

Serial Data (SDA)

I2C is a 2-Wire Half Duplex Bus (Clock+Data) :

• Multiple Masters can be connected to the Bus.

• Only Masters can start /Stop a communication.

• Each Slave must have a unique Address.

• A device can act a Master and Slave. But not at the same time.

• Max Number of Slaves is 128 (7 bits addressing) and 1024 (10 bits addressing)

• Collision Detection Mechanism


DAMERGI Emir – INSAT 2021 69
Serial Connectivity: I2C

 Data transfer between ICs and systems at relatively low rates


 “Classic” I2C is rated to 100K bits/second

 “Fast Mode” devices support up to 400K bits/second

 A “High Speed Mode” is defined for operation up to 3.4M bits/second

DAMERGI Emir – INSAT 2021 70


I2C: Write to slave

Byte 1 Byte 2 Byte n


b7 b6 b5 b4 b3 b2 b1 b0
S SLAVE Addr /W A DATA A DATA A P
A6 A5 … …. A1 A0 R /W

1 1 1 1 0 A9 A8 R /W A7 A6 A5 A4 A3 A2 A1 A0
10 bit slave SLAVE Addr R/ SLAVE Addr
address S Part 1 W A Part 2
A

From Matser to Slave A: Acknowlege bit (SDA Low)


From Slave to Master /A: Not Acknowlege bit (SDA High)
DAMERGI Emir – INSAT 2021 71
I2C: Write to slave
Byte 1 Byte 2 Byte n
b7 b6 b5 b4 b3 b2 b1 b0
S SLAVE Addr /W A DATA A DATA A P

Data Formats and meaning depends on the communication context:

• A user designed protocol (SOC to SOC).

• An order to the slave to perform an action.

• A Register Address + Data to be written

(I2C Peripheral Interfaces)

DAMERGI Emir – INSAT 2021 72


I2C: Read from slave (I2C Peripheral Interfaces)
Before reading, the Matser must start by sending (write) the register address

SLAVE Register SLAVE R


S /W A A P S A DATA A DATA /A P
Addr Addr Addr d

Start after Stop: not supported by all Devices


Another master can ocupy the bus.

SLAVE Register SLAVE R


S /W A A S A DATA A DATA /A P
Addr Addr Addr d

Restart Condition
Without Stop

/A: Not Acknowlege bit (SDA High)


From Matser to Slave
the master orders the slave to
From Slave to Master stop sending
DAMERGI Emir – INSAT 2021 73
I2C: Slave Addresses
MMA 7660 Lis3SDH 24LC64
Accelerometer Accelerometer 64 Kbits EEPROM

1 Fixed 7 bits
Address:
2 configurable 7
1001 100 bits Addresses: 8 configurable 7
bits Addresses:
0011 1xx

0011 101 if sel=1 1 0 1 0 A2 A1 A0

0011 110 if sel=0

DAMERGI Emir – INSAT 2021 74


I2C: Arduino Programming - Configuration/Initilaization

An I2C object is an instance of TwoWire Class.


Flash RAM Processing
device To Declare an I2C object, the SDA and SCL must be
I2C1_SDA

GPIO
passed as parameters.c

# 𝑖𝑛𝑐𝑙𝑢𝑑𝑒 𝒘𝒊𝒓𝒆. 𝒉
I2C 1
I2C1_SCL
I2C 0
GPIO
I2C0_SDA

I2C0_SCL

The Initialization of the I2C object is put in the setup


section (begin method)

Rq: For many Boards, the I2C objects can be


used without ddeclaration:
Wire1 for I2C1
Wire2 for I2C2,
Etc…

DAMERGI Emir – INSAT 2021 75


I2C: Arduino Programming - Write to slave

Reg Value to be
S SLAVE Addr /W A
Addr
A
written
A P

I2C Master
I2C Slave (Digital Sensor )
SoC Value
@ Reg
I2C I2C
i
ADC
i+1
Analog
i+2
MySensors_I2C.beginTransmission (Slave_Addr); Sensor

MySensors_I2C.write(Reg_Addr);
I2C Interface Analog/Digital
MySensors_I2C.write(Value); 8 bits
Converter

MySensors_I2C. endTransmission(true);

To catch I2C communication if (MySensors_I2C.endTransmission(true) != 0) {


Errors, endTransmission return Serial.println ("problem writing to I2C device");
exit(0);
value can be tested. (!=0)
}
DAMERGI Emir – INSAT 2021 76
I2C: Arduino Programming - Read From slave

Restart Condition Without Stop

SLAVE Register SLAVE R


S /W A A S A DATA /A P
Addr Addr Addr d

MySensors_I2C.beginTransmission (Slave_Addr);

MySensors_I2C.write(Reg_Addr);

MySensors_I2C. endTransmission(false);

MySensors_I2C.requestFrom(Slave_Addr , 0x01); //Read 1 byte

uint8_t Rcv_byte = MySensors_I2C.read();

MySensors_I2C. endTransmission(true);

DAMERGI Emir – INSAT 2021 77


I2C/SPI Devices
Additinally to sensors, many other devices have I2C and SPI Interfaces:

OLED SSD EEPROM


Dispaly Memory LCD Dispaly Motor Control
(characters)

Bluetooth BLE
GPS Module
Module 4G/LTE
LORA Module Module

DAMERGI Emir – INSAT 2021 78


I2C: Arduino Programming - Application with Accelerometer Lis3SDH (@0x6b)

SDA = 21 LIS3DSH is a 3 axis accelerometer MEMS sensor with +/- 6G Full Scale.
SCL = 22
• The results for each axis are 16 bits integer and are stored in 2
registers (2’s complement format).
• WHO AM I Register (Generally exists in each I2C device) and helps
identifying the device. For the LIS3SDH the value is 0x3F

DAMERGI Emir – INSAT 2021 79


Smart Devices
Data Storage / Manipulation
Data Storage / Manipulation
In Smart Devices, persistent data storage is necessary to store

• Configuration parameters (AP SSID, AP Pwd, etc…)

• Collected data

Variables are not suitable. They are stored in RAM and are lost when the system is powered off.

A part of the code memory (Flash) is used for DATA storage.

Difficulty: Code for Accessing Flash to store Data in file like structures.

Managing Code and Data Zones.

Tools* for :

- splitting the Flash memory in Code and data zone


+ uploading Files to data zone . 1M of a total size of 4 MByte are reserved for data

Libraries* (File System) to manipulate Files: read, update, write, create, etc…

*) They are not compatible. For each Board (SoC or MCU), suitable tools/Librairies must be used (if available)
81
DAMERGI Emir – INSAT 2021
Data Storage / Manipulation : File System for ESP in Arduino

For ESP8266 and ESP32, SPIFFS* File System :

1) Include the Library: #include « SPIFFS.h » (« FS.h » for ESP8266)

2) Create and Initialize SPIFFS Object: SPIFFS.begin();

82
DAMERGI Emir – INSAT 2021
Data Storage / Manipulation : File System for ESP in Arduino

Example 1: Read a txt file and send the Example 2: writes a string (TEST
content (character by char) to serial port SPIFFS) in a file

83
DAMERGI Emir – INSAT 2021
Data Storage / Manipulation: JSON Format
Manipulating txt files to store, recuperate, update Data and Connexion parameters (structured Data) is
complicated.
JSON (JavaScript Object Notation) is an open standard file format and data interchange
format that uses human-readable text to store and transmit data.

• Objects are described in a hierarchical structure.


Object
• The hierarchy level is thetheoretically unlimited.
ParamName1
• Each parameter has a value or a value Array or is an
Object itself.

ParamName2 • Parameters that are not the Last node in Hierarchy are
ParamName2_1 objects.

ParamName2_2

ParamName2_2_1

ParamName2_2_2
ParamName3

Rq: There are other formats (expl: XML) but JSON is easier to
read, consumes less characters and easier to parse. 84
DAMERGI Emir – INSAT 2021
Data Storage / Manipulation: JSON Format

{ ‘’ParamName1’’ : Value1 ,

• Strings are placed between


‘’ParamName2’’ : {’ParamName2_1’’ : Value2_1 , ‘’.
‘’ParamName2_2’’ : {‘’ParamName2_2_1’’ : value2_2_1 ,
‘’ParamName2_2_2’’ : value2_2_2 }, • Parameter Names are
Strings.
},
‘’ParamName3’ : [ value3a , value3b, value3c ] • Arrays are grouped between
} [ and separated by , .
85
DAMERGI Emir – INSAT 2021
Data Storage / Manipulation: JSON Format

Example of a JSON File describing a Person (Object)

{ "firstName": "INSAT",
"lastName": "INSAT",
"age": 25,

"address": { "streetAddress": "Boulevard de la terre",


"city": "Zone Urbaine Nord",
"postalCode": 1080

},

"phoneNumbers": ["71800800" , "71800801" ]

86
DAMERGI Emir – INSAT 2021
Data Storage / Manipulation: JSON Manipulation In Arduino

JSON File : serie of characters(Text File)


{"firstName": "INSAT", "lastName": "INSAT", "age": 25, "address": { "streetAddress": "Boulevard de la terre", "city": "Zone Urbaine
Nord","postalCode": 1080 },"phoneNumbers": ["71800800" , "71800801" ]}

Deserialization: extract Serialization: Convert json


objects, parameter structure in a json formatted text
names and values file (brackets, etc…

JsonDoucment
firstName : INSAT
lastName : INSAT

age: 25
address:
streetAddress : Boulevard de la terre
city : Zone Urbaine Nord
postalCode": 1080
phoneNumbers: [ 71800800 , 71800801 ]
87
DAMERGI Emir – INSAT 2021
Data Storage / Manipulation: JSON Manipulation In Arduino

{
"city":"Tunis",
"weather":"Sunny",
"humidity":[80.5,27.75],
"temp": {
"max":49,
"min":28
}
}

88
DAMERGI Emir – INSAT 2021
Data Storage / Manipulation : JSON Deserialization

3- Get data from JsonDocument  Structure


1- Open the json file

2- Deserialize the json file  to jsonDocument

4- Close File and ends SPIFFS

89
DAMERGI Emir – INSAT 2021
Data Storage / Manipulation : JSON Serialization

90
DAMERGI Emir – INSAT 2021

You might also like