SPI Bus Communication with Sensortechnics` Digital

SPI Bus Communication with Sensortechnics’ Digital
HCE Pressure Sensors
1
INTRODUCTION
Sensortechnics’ digital pressure sensors are
calibrated and temperature compensated with
an on-board ASIC, which provides a corrected
digital pressure value with up to 15 bit
resolution. Additionally an analog voltage output
is available at the same time. The response
time of the sensor depends on the adjusted
internal resolution. For 12 bit it is typ. 0.5 ms.
Sensortechnics’ digital sensors can be
configured to comply with SPI or I²C bus
protocol.
NOTE:
This application note only refers to SPI
bus communication and specifically
discusses Sensortechnics’ HCE pressure
sensors with SPI compatible protocol.
The Serial Peripheral Interface (SPI) is a simple
bus system for synchronous serial
communication between one master and one or
more slaves (theoretically any amount of slaves
would be possible). It operates in full-duplex
mode allowing communication to happen in
both directions simultaneously. The master
device initiates an information transfer on the
bus and generates clock and control signals.
Slave devices are controlled by the master
through individual slave select lines and are
active only when selected.
For the data transmission there needs to be two
signal and two data lines. These are:
·
·
·
·
Slave Select (SS)
Signal Clock (SCK)
Master Out - Slave In (MOSI)
Master In - Slave Out (MISO)
All bus lines are unidirectional.
E / 11156 / C
1/11
www.sensortechnics.com
SPI Bus Communication with Sensortechnics’ Digital
HCE Pressure Sensors
2
BUS ARCHITECTURE
2.1
SCK (Signal Clock)
2.3
MOSI (Master Out - Slave In)
The clock signal is generated by the master
and is connected to all slaves. It is used to
synchronise all data transfer.
The MOSI line transfers data from the master to
the slave.
2.2
2.4
SS (Slave Select)
Slave devices are addressed and controlled by
the master through individual slave select lines.
The master selects a slave by pulling the Slave
Select line of that slave to LOW. The limiting
factor for the amount of slaves in a system is
the possible number of Slave Select lines to the
master device.
Master
MISO (Master In - Slave Out)
The MISO line transfers data from the slave to
the master.
SCK
MOSI
MISO
SS1
SS2
SS3
SCK
MOSI
Slave 1
MISO
SS
SCK
MOSI
Slave 2
MISO
SS
SCK
MOSI
Slave 3
MISO
SS
Fig. 1:
Sample SPI configuration with one master and multiple slaves
E / 11156 / C
2/11
www.sensortechnics.com
SPI Bus Communication with Sensortechnics’ Digital
HCE Pressure Sensors
3
SPI BUS PROTOCOL
3.1
Data Transmission
To start communication, the master first selects
a slave by pulling the Slave Select line of this
slave down to LOW. The master then writes the
data to be transferred into its data transmission
register and, after a short delay, transmits the
clock signal (see 4.5 Timing).
Data transfer is organised in full duplex mode
by using shift registers in both, master and
slave devices. With each clock cycle data is
pushed from master to slave on the MOSI line
while the slave itself pushes data to the master
on the MISO line at the same time (see Fig. 2).
A data transmission will be finished when the
Slave Select line is pulled up to HIGH again.
NOTE:
For Sensortechnics’ digital pressure
sensors with SPI bus a MOSI line is
not necessary because there is no
need for data transmission to the
sensor (slave). Therefore, for some
applications it makes sense not to
connect the sensors MOSI line with the
master-microcontroller but to pull up
this line to HIGH level with a resistor on
the sensor side (see 5 Application
Circuit).
Master
Slave
SS
7
Fig. 2:
SS
SCK
SCK
MOSI
MOSI
MISO
MISO
0
7
0
Data transfer using master and slave shift registers
E / 11156 / C
3/11
www.sensortechnics.com
SPI Bus Communication with Sensortechnics’ Digital
HCE Pressure Sensors
3.2
Communication Modes
3.2.1 CPOL (Clock Polarity)
The SPI protocol specifies the clock signal by
two parameters, the clock polarity (CPOL) and
the clock phase (CPHA). This results into four
possible communication modes (see Table 1).
These settings define which clock edge the
data will be transmitted and which time the data
is allowed to change.
It is important to set these parameters to the
same values in both, master and slave devices
to ensure proper communication. CPOL and
CPHA can be adjusted via two control bits in the
SPI control registers.
SPI mode
CPOL
C PH A
0
0
0
1
0
1
2
1
0
3
1
1
The clock polarity specifies whether the clock
signal is LOW (CPOL=0) or HIGH (CPOL=1) in
its idle state.
3.2.2 CPHA (Clock Phase)
The clock phase defines at which clock edge
the first data is accepted as valid. CPHA=0
means that the data is valid with the first
(leading) clock edge. CPHA=1 means that the
data is valid with the second (trailing) clock
edge.
Table 1: SPI communication modes
NOTE:
Sensortechnics’ digital pressure
sensors are programmed to CPHA = 0
and CPOL = 0 by default. Generally
they support all four different modes.
Please contact Sensortechnics for
more information.
E / 11156 / C
4/11
www.sensortechnics.com
SPI Bus Communication with Sensortechnics’ Digital
HCE Pressure Sensors
3.3
Transfer format
NOTE:
Fig. 3 and 4 show the theoretical SPI
bus data transfer. For the specific
communication characteristics of the
HCE pressure sensors please refer to
4.5 Timing.
3.3.1 CPHA = 0
If the clock phase is set to 0 data is valid with
the first clock edge. The state of the clock
polarity defines whether this first clock edge is a
rising or falling edge.
For CPOL=0 the clock value is LOW in its idle
state and rises to HIGH with the first clock edge.
For CPOL=1 the clock value is HIGH in its idle
state and falls to LOW with the first clock edge.
However, the clock polarity does not influence
the moment when the first data bit is valid and
therefore does not influence the transfer format
which is shown in Fig. 3.
SCK cycle
1
2
3
4
5
6
7
8
SCK
(CPOL=0)
SCK
(CPOL=1)
MOSI
MISO
MSB
MSB
LSB
LSB
SS
Sampling
End of idle state
Fig. 3:
Begin of idle state
Example of a 1 byte SPI data transfer for CPHA=0
E / 11156 / C
5/11
www.sensortechnics.com
SPI Bus Communication with Sensortechnics’ Digital
HCE Pressure Sensors
3.3.2 CPHA = 1
If the clock phase is set to 1 data is valid with
the second clock edge. The state of the clock
polarity defines whether this second clock edge
is a rising or falling edge.
For CPOL=0 the clock value is in its HIGH state
after the first clock edge and is falling to LOW
with the second edge.
For CPOL=1 the clock value is in its LOW state
after the first clock edge and is rising to HIGH
with the second edge.
The clock polarity does not influence the
moment when the first data bit is valid and
therefore does not influence the transfer format
which is shown in Fig. 4.
SCK cycle
1
2
3
4
5
6
7
8
SCK
(CPOL=0)
SCK
(CPOL=1)
MOSI
MSB
LSB
MISO
MSB
LSB
SS
Sampling
End of idle state
Fig. 4:
Begin of idle state
Example of a 1 byte SPI data transfer for CPHA=1
E / 11156 / C
6/11
www.sensortechnics.com
SPI Bus Communication with Sensortechnics’ Digital
HCE Pressure Sensors
4
SPI BUS DATA TRANSFER WITH HCE PRESSURE SENSORS
4.1
Pressure reading
NOTE1:
Sensortechnics’ HCE sensors are
configured for a 2 or 3 byte data
transfer. Therefore, the Slave Select
line must not be pulled up in between
the individual bytes of a data stream
since this would terminate the
communication!
To start communication the master pulls down
the Slave Select line to LOW level. The
pressure information will be transferred as a 15 bit
word within a 3 byte data stream (see Fig. 5).
Sensortechnics’ digital pressure sensors are not
designed for data reception. Therefore, it is
recommended to send only a HIGH level to the
sensor so as not to cause any problems or
undefined actions. As the master might still send
some order to the slave at the beginning of the
transfer there is a first byte without any data
transmitted by the sensor (the sensor will typically
send 0xFFh). The following 2nd and 3rd data bytes
contain the current pressure information starting
with the most significant bit (MSB) and ending
with the least significant bit (LSB). The data
transmission will be terminated when the master
pulls up the Slave Select to HIGH again.
NOTE2:
With a clock frequency of 500 kHz the
exchange of the first 3 data bytes takes
about 50 µs. The following 2 byte
“online” pressure values take another
32 µs each. However, the internal HCE
sensor conversion cycle to obtain a new
pressure value is 500 µs for 14 bit
resolution. Therefore, if the sensor is
not deactivated it will send the same
digital pressure value at least
14 times before a new reading can be
obtained. For further information please
contact Sensortechnics.
The sensor is also able to send pressure values
“online”. That is, if the master does not pull up
the Slave Select line after the third exchanged
byte the slave will go on sending the last
available pressure value when it is clocked.
These “online” values are then only 2 bytes long.
4.2
transmitted as a 4th and 5th byte after the
pressure value. If the master does not pull up
the Slave Select Line after the 5th byte the
sensor will continue sending alternating 4 byte
long pressure and temperature values until it is
deactivated.
Optional temperature reading
As an option the sensor can be factory
configured to deliver an additional 15 bit
temperature reading. This will then be
Data Byte 1
Data Byte 2
1
1
1
1
1
Data Byte 3
MOSI
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
MISO
X
X
X
X
X
X
X
X
X P14 P13 P12 P11 P10 P9 P8 P7 P6 P5 P4 P3 P2 P1 P0
Relevant data
Fig. 5:
3 byte data stream containing the pressure value as a 15 bit information
E / 11156 / C
7/11
www.sensortechnics.com
SPI Bus Communication with Sensortechnics’ Digital
HCE Pressure Sensors
4.3
Calculation of the actual pressure
value from the digital pressure word
For an actual digital pressure reading of e.g.
P counts = 20608 counts decimal
The following formulas show how to calculate
the actual pressure value from the digital
sensor output:
Definitions:
S
= Sensitivity [counts/mbar]
Outmax = Output @ max. pressure [counts]
Outmin = Output @ min. pressure [counts]
Pmax
= Max. value of pressure range [mbar]
Pmin
= Min. value of pressure range [mbar]
P
= Pressure reading [mbar]
Pcounts = Digital pressure reading [counts]
S=
Out max − Out min
Pmax − Pmin
the actual pressure in mbar can be calculated
from equation (2) to be
P=
P = 44.73 mbar
This pressure reading is calculated with the
typical calibration values, not taking into
account that the individual sensor calibrations
might differ within the tolerances specified in
the HCE data sheet.
(1)
4.4
P=
Pcounts − Out min
+ Pmin
S
(2)
The following example shows the calculation for
a HCEM100DB... device (pressure range
0…±100 mbar bidirectional). Please refer to the
HCE data sheet for the specified calibration
values.
Outmin (-100 mbar) = 0666 hex = 1638 dec
Outmax (+100 mbar) = 6CCC hex = 27852 dec
With equation (1) the sensitivity of the sensor
gives
S=
20608 counts − 1638 counts
+ ( −100 mbar )
131.07 counts / mbar
Resolution of data
Each temperature and pressure value will be
transmitted as a 15 bit word. However, the
actual resolution can be less than this
depending on how the internal A/D-converter is
configured. Also, internal calculations and
signal windowing will reduce the effective
resolution. For the HCE pressure sensor the
standard resolution for pressure measurement
is typ. 14 bits. For temperature measurement
the limiting factor is the sensitivity of the sensing
element. For further information please contact
Sensortechnics.
27852 counts − 1638 counts
100 mbar − ( −100 mbar )
S = 131 .07 counts/mba r
E / 11156 / C
8/11
www.sensortechnics.com
SPI Bus Communication with Sensortechnics’ Digital
HCE Pressure Sensors
Parameter
Symbol
Min.
Typ.
Max.
Input high level
90
100
Input low level
0
10
Output low level
% of V s
10
Pull-up resistor
Ω
500
Load capacitance @ MISO
CMISO
400
Input capacitance @ each pin
CSPI_IN
10
fSCK
100*
MISO hold time after SCK sample slope
tSPI_HD_MISO
200
MOSI setup time before SCK sample slope
tSPI_SU_MOSI
2/fCLK
tSPI_SU_SS
10
Signal clock frequency
/SS setup time before SCK sample slope
Unit
pF
640
kHz
ns
ns
tSPI_HD_SS
1/fCLK
/SS hold time after SCK sample slope
* recommended (see note on page 10)
Table 2: SPI bus timing and communication parameters for Sensortechnics’ digital pressure sensors
4.5
Timing
t
t
SPI_SU_SS
To ensure correct communication the sensor
must be able to detect the start condition (pull
down of Slave Select line) before the master
sends the first clock signal. Therefore, a
minimum delay time tSPI_SU_SS is required prior to
the first clock edge (see Table 2 and Fig. 6).
Further, after the last clock edge of the data
transfer a minimum delay time tSPI_HD_SS is
required before the master pulls up the Slave
Select line to terminate the communication.
SPI_HD_SS
t
SPI_SU_MOSI
t
SCK
(CPOL=0)
SPI_HD_MISO
MOSI
MISO
SS
The correct timing has to be controlled by the
master and is influenced by the following
conditions:
· the SPI communication mode
· the communication speed
· the application circuit
(e.g. the values of the pull-up-resistors)
· the load capacitances and impedances of
the SPI bus lines
… and others
E / 11156 / C
Fig. 6:
SPI bus timing characteristics
The HCE pressure sensors change their data
after tSPI_HD_MISO very shortly after the valid clock
edge (see Fig. 6). This does not correspond to
the theoretical transfer format as shown in Fig.
3, page 5. Therefore, especially for low
communication speeds there can be the wrong
impression as if the data transfer would take
place with the falling clock edge (CPAH=1,
CPOL=0, compare Fig. 4 page 6).
9/11
www.sensortechnics.com
SPI Bus Communication with Sensortechnics’ Digital
HCE Pressure Sensors
4.6
Communication Parameters
If both digital and analog interfaces are used in
parallel it is recommended to separate these
lines as far as possible from each other.
Further, decoupling capacitors of 220 nF
between supply and ground and 15 nF between
the analog output and ground are beneficial. It
is important to place the capacitors as close to
the pins as possible.
The maximum allowed communication speed
depends on the configured internal clock
frequency of the sensor which varies with
temperature and production conditions. For the
worst case scenario the maximum allowed
clock frequency in standard configuration is
640 kHz. As a special configuration this can be
increased to 730 kHz.
5
4.7
APPLICATION CIRCUIT
Signal Noise due to Communication
As the pulses transmitted on the bus lines have
very sharp edges, this can cause some electromagnetic interference. Especially for very low
pressures and small PCB designs, these spikes
can influence the analog millivolt measurement of
the sensor bridge and downgrade signal quality.
All bus lines have to be connected to the supply
voltage via pull-up resistors (see Fig. 7).
Resistors of about 4.7 kΩ are recommended.
Additionally, 330 Ω serial resistors should be
used in each communication line.
NOTE:
To prevent signal noise Sensortechnics
recommends a min. clock frequency of
100 kHz (max. 640 kHz) and transmission
breaks of min. 500 µs between two
pressure readings. This is especially
valid for low pressure devices up to
25 mbar. Please contact Sensortechnics
for more information.
Fig. 7:
4k7
4k7
4k7
4k7
+Vs
MOSI
330
MOSI
MISO
330
MISO
SS
330
SS
SCK
330
SCK
HCE
SPI bus application circuit for Sensortechnics’ HCE pressure sensors
E / 11156 / C
10/11
www.sensortechnics.com
SPI Bus Communication with Sensortechnics’ Digital
HCE Pressure Sensors
6
SAMPLE PROGRAM CODE
byte dont_care;
byte byte_msb, byte_lsb;
int16 pressure;
// 8bit values
// 8bit values
// 16bit value
// If your controller does not control the /SS pin, you'll need to configure it yourself
// Set output value to 1/high state (SPI in idle state)
pin_ss = 1;
// Set pin to output
set_inout_mode(pin_ss, PIN_OUT);
// Set SPI unit to SPI master mode, clock speed 100 kHz and SPI mode 0 (CPHA = 0 & CPOL = 0)
configureSPI (SPI_MASTER | CLK_SPEED_100KHZ | SPI_MODE_0);
// Set /SS Pin low (Start communication)
pin_ss = 0;
// Transfer one byte over SPI bus (send 0xFF, write received data to dont_care (discard first byte))
SPI_transfer (0xFF, &dont_care);
// Transfer one byte over SPI bus (send 0xFF, write received data to byte_msb)
SPI_transfer (0xFF, &byte_msb);
// Transfer one byte over SPI bus (send 0xFF, write received data to byte_lsb)
SPI_transfer (0xFF, &byte_lsb);
// Set /SS pin high (End communication)
pin_ss = 1;
// Put both values together
pressure = ((int16)byte_msb << 8) | byte_lsb;
7
TROUBLESHOOTING
7.2
7.1
Sensor does not respond
· Check the used SPI communication mode.
By default Sensortechnics’ HCE pressure
sensors are programmed to CPAH=0 and
CPOL=0.
· Check the signal levels at the SCK and
Slave Select pins of the sensor:
- Are the edges sharp enough?
- Are the HIGH and LOW levels within the
specified range (compare Table 2)?
· Check the analog output signal of the sensor.
If it responds to pressure changes the sensor
works.
Sensor supplies unstable data
· Have the advices regarding signal noise
under point 4.7 been followed?
For more information please contact
Sensortechnics:
www.sensortechnics.com/contact
[email protected]
E / 11156 / C
11/11
www.sensortechnics.com