Communication Protocols 2
Communication Protocols 2
BRAC University
1
Communication Protocol
• A communication protocol is a system of rules that allow two or more
entities of a communication system to transmit information via any
kind of variation of a physical quantity.
2
.
3
.
4
.
5
.
Timing diagram
6
.
7
.
Although the system becomes more simple, the time taken for the data
to travel to the destined node increases. 8
.
9
.
SPI
Advantages:
Full duplex protocol. Separate MISO and MOSI lines, so data can be sent
and received at the same time
No start and stop bits, so the data can be streamed continuously without
interruption
No complicated slave addressing system like I2C
Higher data transfer rate than I2C (almost twice as fast)
Not Limited to 8 bit words in case of bit transferring
Arbitrary choice of message size, content and Purpose
Low Power
10
.
SPI
Disadvantages:
Requires more pins than I2C
No hardware flow control
No Slave Acknowledgement
Multi Master Difficult to Implement
No form of error checking like the parity bit in UART
It usually requires separate SS lines to each slave, which can be
problematic if numerous slaves are needed.
11
.
SPI
SPI Peripherals:
Converters (ADC, DAC)
Memories (EEPROM, RAM’s, Flash)
Sensors (Temperature, Humidity, Pressure)
Real Time Clocks
Misc.- Potentiometers, LCD controllers, UART’s, USB controller, CAN
controller, amplifiers
12
.
13
.
UART
Introduction:
Universal Asynchronous Receiver/Transmitter
It also called Serial Communication Interface(SCI)
Full-duplex communication.
Asynchronous communication.
Compatible with PC .
The Standard bit rates are: 100, 200, 300, 1200, 2400, 4800, 9600,
19200, 38400, 57600, 115200 bps.
Example: Connecting GPS modules, Bluetooth modules, and RFID card
reader modules to Raspberry Pi, Arduino, or other microcontrollers.
14
.
UART
A UART’s main purpose is to transmit and receive serial data.
One of the best things about UART is that it only uses two wires to
transmit data between devices
Why UART?
A UART may be used when:-
• High speed is not required
• An inexpensive communication link between two devices is required.
UART communication is very cheap:-
• Single wire for each direction(and ground wire).
• Simple hardware.
15
.
UART
In UART communication, two UARTs communicate directly with each
other. Data flows from the Tx pin of the transmitting UART to the Rx pin
of the receiving UART:
16
.
UART
17
.
UART
The transmitting UART converts parallel data from a controlling device
like a CPU into serial form, transmits it in serial to the receiving UART,
which then converts the serial data back into parallel data for the
receiving device.
The UART that is going to transmit data receives the data from a data
bus. The data bus is used to send data to the UART by another device like
a CPU, memory, or microcontroller. Data is transferred from the data bus
to the transmitting UART in parallel form. After the transmitting UART
gets the parallel data from the data bus, it adds a start bit, a parity bit,
and a stop bit, creating the data packet.
18
.
UART
Next, the data packet is output serially, bit by bit at the Tx pin. The
receiving UART reads the data packet bit by bit at its Rx pin. The
receiving UART then converts the data back into parallel form and
removes the start bit, parity bit, and stop bits. Finally, the receiving UART
transfers the data packet in parallel to the data bus on the receiving end.
When the receiving UART detects a start bit, it starts to read the
incoming bits at a specific frequency known as the baud rate. Baud rate
is a measure of the speed of data transfer, expressed in bits per second
(bps).
Both UARTs must operate at about the same baud rate. The baud rate
between the transmitting and receiving UARTs can only differ by about
10% before the timing of bits gets too far off.
19
.
START BIT: The UART data transmission line is normally held at a high voltage level when it’s
not transmitting data.
• To start the transfer of data, the transmitting UART pulls the transmission line from high to
low for one clock cycle.
• When the receiving UART detects the high to low voltage transition, it begins reading the
bits in the data frame at the frequency of the baud rate.
20
.
DATA FRAME: The data frame contains the actual data being transferred. It can be 5 bits up to 8 bits long if a
parity bit is used.
If no parity bit is used, the data frame can be 9 bits long. In most cases, the data is sent with the least
significant bit first.
21
.
STOP BITS: To signal the end of the data packet, the sending UART drives the data transmission line from a
low voltage to a high voltage for at least two bit durations
23
.
24
.
25
.
26
.
27
.
28
.
UART
ADVANTAGES
• Only uses two wires
• No clock signal is necessary
• Has a parity bit to allow for error checking
• The structure of the data packet can be changed as long as both sides are set up for it
• Well documented and widely used method
DISADVANTAGES
• The size of the data frame is limited to a maximum of 9 bits
• Doesn’t support multiple slave or multiple master systems
29
(Inter-Integrated Circuit)
I2C Bus
• I2C is not only used on single boards but also to connect
components which are linked via cable. Simplicity and flexibility are
key characteristics that make this bus attractive to many
applications.
Most significant features include:
• Only two bus lines are required: SDA (Serial Data), SCL (Serial
Clock)
• No strict baud rate requirements like for instance with RS232, the
master generates a bus clock
• Simple master/slave relationships exist between all components
Each device connected to the bus is software-addressable by a
unique address
• I2C is a true multi-master bus providing arbitration and collision
detection
Timing Diagram of I2C data transmission
I2C Bus
I2C Protocol (Basics) Messages are broken up into two types of
frame:
• Address frame: The master indicates the slave to which the
message is being sent
• Data frame: These are 8-bit data messages passed from master to
slave or vice versa.
Data is placed on the SDA line after SCL goes low, and is sampled
after the SCL line goes high.
I2C Bus
Start Condition
• To initiate the address frame, the master device leaves SCL high
and pulls SDA low.
• This puts all slave devices on notice that a transmission is about to
start.
• If two master devices wish to take ownership of the bus at one
time, whichever device pulls SDA low first wins the race and gains
control of the bus.
• It is possible to issue repeated starts, initiating a new
communication sequence without relinquishing control of the bus
to other masters
I2C Bus
Address Frame
• The address frame is always first in any new communication
sequence.
• For a 7-bit address, the address is clocked out most significant bit
(MSB) first, followed by a R/W bit indicating whether this is a read
(1) or write (0) operation.
I2C Bus
NACK/ACK bit
• It is the 9th bit of the frame
• This is the case for all frames (data or address).
• Once the first 8 bits of the frame are sent, the receiving device is
given control over SDA.
• If the receiving device does not pull the SDA line low before the
9th clock pulse, it can be inferred that the receiving device either
did not receive the data or did not know how to parse the message.
• In that case, the exchange halts, and it’s up to the master of the
system to decide how to proceed.
I2C Bus
Data Frames
• After the address frame has been sent, data can begin being transmitted.
• The master will simply continue generating clock pulses at a regular interval,
and the data will be placed on SDA by either the master or the slave, depending
on whether the R/W bit indicated a read or write operation.
Stop condition
• Once all the data frames have been sent, the master will generate a stop
condition.
• Stop conditions are defined by a 0->1 (low to high) transition on SDA after a
0->1 transition on SCL, with SCL remaining high.
• During normal data writing operation, the value on SDA should not change
when SCL is high, to avoid false stop conditions.
.
STEPS OF I2C DATA TRANSMISSION
1. The master sends the start condition to every connected slave by switching
the SDA line from a high voltage level to a low voltage level before switching the
SCL line from high to low.
2. The master sends each slave the 7 or 10 bit address of the slave it wants to
communicate with, along with the read/write bit.
3. Each slave compares the address sent from the master to its own address. If
the address matches, the slave returns an ACK bit by pulling the SDA line low for
one bit. If the address from the master does not match the slave’s own address,
the slave leaves the SDA line high.
4. The master sends or receives the data frame.
5. After each data frame has been transferred, the receiving device returns
another ACK bit to the sender to acknowledge successful receipt of the frame.
6. To stop the data transmission, the master sends a stop condition to the slave
by switching SCL high before switching SDA high.
Single Master – Multiple Slave Multiple Master – Multiple Slave
Advantages
• I2C communication or protocol has a significant edge over its peers such as serial
port communication and SPI. Let us have a look into the various advantages that
renders the I2C protocol so effective for short distance intra-board
communication.
• 1. Flexibility – The I2C protocol supports multi-master, multi-slave
communication, which implies you can add a lot of functionality to your design.
More than one master IC controlling and communicating with the slave ICs can
speed things up and add functionalities to the embedded system.
• 2. Addressing feature – Yet another advantage of the I2C protocol lies in its
inherent ability to use chip addressing. It means that you can easily add
components to the bus without any complexity. It eliminates the necessity of SS
(Slave select) lines.
• 3. Simplicity – I2C protocol doesn’t complicate the design. It requires only two
bidirectional signal lines to establish communication among multiple devices.
Further, the pin count is low as well.
• 4. Better error handling mechanism – To improve the error detection and
correction mechanism, the I2C protocol relies on ACK/NACK feature, which is a
robust error correction feature. ACK stands for Acknowledgement whereas NACK
means No Acknowledgement.
• 5. Adaptable – The I2C protocol is adaptable in the sense that it can work well
with both slow ICs and fast ICs.
Disadvantages
1. Conflicts – Due to chip addressing, there’s always a possibility of an
address conflict.
2. Slower speeds – Slower data transfer rate than SPI
3. Requires more space – More complicated hardware needed to
implement than SPI
4. Size of Data Frame - Limited to 8 bits.
.
Thank You
For Your Attention
46
STM32 Microcontroller
Prepared by Md. Khalilur Rahman PhD, Professor and Umme Jannat Taposhi, Lecturer
Department of Computer Science and Engineering, BRAC University
STM32 Nucleo Board
● 144 pin board
STM32 Nucleo Board
● 64 pin board
STM32F446RE
ARM Cortex-M4: A 32-bit RISC processor core designed for high performance and low power
consumption.
DSP Instructions: The Cortex-M4 includes a set of digital signal processing (DSP) instructions that
enhance its performance in applications requiring complex mathematical computations.
Floating Point Unit (FPU): Supports single-precision floating-point operations, which is beneficial for
applications involving arithmetic operations with real numbers.
Key Features of the ARM Cortex-M4 Core in STM32F446RE
2. Performance
Clock Speed: Can operate at up to 180 MHz, providing high processing power for complex
applications.
Harvard Architecture: Features separate instruction and data buses, allowing simultaneous access
to memory and instructions.
3. Memory
Flash Memory: 512 KB of on-chip Flash memory for program storage.
SRAM: 128 KB of SRAM for data storage.
Memory Protection Unit (MPU): Enhances security by allowing control over memory access
permissions.
Key Features of the ARM Cortex-M4 Core in STM32F446RE
4. Interrupt System
SysTick Timer: A dedicated timer for generating system ticks, useful for real-time operating systems
5. Peripherals
Timers: Multiple general-purpose timers, advanced-control timers, and basic timers for timing and
counting applications.
SPI: For synchronous serial communication with peripherals like sensors and memory devices.
Embedded Trace Macrocell (ETM): Provides real-time trace capabilities for debugging complex
applications.
Serial Wire Debug (SWD): A two-pin protocol for debugging, offering a reduced pin count compared
to JTAG.
STM32F446RE Block Diagram of the System Architecture
peripheral.jpg
System Architecture [Simplified - Only AHB1]
• DMA: Direct Memory Access
• AHB: Advanced High-performance Bus
• APB: Advanced Peripheral Bus
• RCC: Reset and Clock Control
• CRC: Cyclic Redundancy Check
• PWM: Pulse width Modulation
• USART: universal synchronous/
asynchronous receiver/transmitter
• SPI: Serial Peripheral Interface
• I2C: Inter-Integrated Circuit
• ADC: Analog to Digital Converter
Memory & Bus Architecture
● I-bus: This bus connects the Instruction bus of the Cortex M4 with FPU core to the
Bus Matrix. Fetches instructions from memory to the CPU.
● D-bus: This bus connects the databus of the Cortex M4 with FPU core to the Bus
Matrix. Handles data read/write operations between CPU and memory.The target
of this bus is a memory containing code or data.
● S-bus: This bus connects the system bus of the Cortex M4 with FPU core to the Bus
Matrix. This bus is used to access data located in a peripheral or in SRAM. Accesses
system-level resources, peripherals, and control register (less efficient than I-bus).
Memory & Bus Architecture
● DMA, Flash MEM, External MEM, SRAM
● AHB Bus Matrix
● APB Buses: The two APB bridges, APB1 and APB2, provide full synchronous connections
between the AHB and the two APB buses, allowing flexible selection of the peripheral
frequency. After each device reset, all peripheral clocks are disabled (except for the SRAM and
Flash memory interface). Before using a peripheral you have to enable its clock in the
RCC_AHBxENR or RCC_APBxENR register.
CRC & RCC
Cyclic Redundancy Check (CRC)
The CRC calculation unit is used to get a CRC code from a 32-bit data word and a
fixed generator polynomial. Among other applications, CRC-based techniques are
used to verify data transmission or storage integrity.
Reset and Clock Control (RCC)
There are three types of reset
1. System Reset
2. Power Reset
3. Backup domain Reset
To use different IO ports, NEED TO CONFIGURE DIFFERENT REGISTERS
…….. and so on
General Purpose I/0 [GPIO]
Each general-purpose I/O port has -
● A four 32-bit configuration registers
● GPIOx_MODER: Mode Register
● GPIOx_OTYPER : Output Type Register
● GPIOx_OSPEEDR : Output Speed Register
● GPIOx_PUPDR) : Pull-up, Pull-Down Register
● Two 32-bit data registers
● GPIOx_IDR: Input Data Register
● GPIOx_ODR: Output Data Register
● A 32-bit set/reset register (GPIOx_BSRR),
● A 32-bit locking register (GPIOx_LCKR)
● Two 32-bit alternate function selection
● GPIOx_AFRH: Alternetive Register High
● GPIOx_AFRL : Alternetive Register Low
Main features of GPIO
● Up to 16 I/Os under control
● Output states: push-pull or open drain + pull-up/down
● Output data from output data register (GPIOx_ODR) or peripheral (alternate function output)
● Speed selection for each I/O
● Input states: floating, pull-up/down, analog
● Input data to input data register (GPIOx_IDR) or peripheral (alternate function input)
● Bit set and reset register (GPIOx_BSRR) for bitwise write access to GPIOx_ODR
● Locking mechanism (GPIOx_LCKR) provided to freeze the I/O configuration
● Analog function
● Alternate function input/output selection registers (at most 16 AFs per I/O)
● Fast toggle capable of changing every two clock cycles
● Highly flexible pin multiplexing allows the use of I/O pins as GPIOs or as one of several periphe
functions
Internal Interfacing Unit of each GPIO PORT
GPIO
Alternate
Input Output
Function
Communic
Digital Analog Digital Time
ation
Input Input Output (PWM)
Protocol
Speed
Input Input Input-pull-d (Low,
USART SPI I2C CAN
floating pull-up own Medium,
Fast, High)
No pull
Pull-up Pull-down
up/down
1. GPIO Configuration Registers (IO Mode Register)
GPIOx_MODER ( x = A..H ) : These bits configure the I/O direction mode.
Note: For atomic bit set/reset, the ODR bits can be individually set and
reset by writing to the GPIOx_BSRR register (x = A..H).
3. Set/Rest Register (Bit Set Reset Register)
GPIOx_BSRR (x=A..H): These bits are write-only and can be accessed in word, half-word or byte mode.
Note: If both BSx and BRx are set, BSx has priority.
4. Locking register Register
GPIOx_LCKR: (x=A..H): Using this register, you can freeze the GPIO configurations. Once you do the proper lock
key write sequence, it will lock the GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR, GPIOx_PUPDR, GPIOx_AFRL, and
GPIOx_AFRH registers.
Bits [15:0] – LCKy: Port Set Bit, (y = 0 … 15)
● 0 – Port configuration is not locked
● 1 – Port configuration is locked
Bits [16] – LCKK: Lock Key
● 0 – Port configuration lock key is not active
● 1 – Port configuration lock key is not active
5. Alternate function selection Register
GPIOx_AFRL: (x=A..H) , GPIOx_AFRH: (x=A..H): Each GPIO pin has around sixteen alternative
functions like SPI, I2C, UART, etc. So we can tell the STM32 to use our required functions.
● GPIOx_AFRL 32-bit register is grouped by 4 bits. So This GPIOx_AFRL register is used to select the
alternate functions of Pin 0 to Pin 7
● GPIOx_AFRH 32-bit register is also grouped by 4 bits. So This GPIOx_AFRH register is used to select
the alternate functions of Pin 8 to Pin 15
● .
● .
THANK YOU!