24.12.2019 How to use SPI wih STM32 » ContrallersTach
How to use SPI with STM32
© August 3, 2018 admin © ARM, Embedded, I2C, STM32 2 4 Comments
ST-Link V2 Mini Nucleo-64 oreoaee sm
Programlayici- STM32F411RE , 1
isti STM32F4 Discc
STM32... Gelistirme...
26,56 146,63 Gelistirme... 95,4
: " 61.207,08
Description
Thave written many posts about interfacing 12C devices with STM32 but there are some
devices which require only SPI to work i.e. SD card reader, TFT display etc. So today in this
post, we are going to learn how to use SPI with STM32.
CE
SPI (Serial Peripheral Interface) generally requires 4 wires as shown above. The names are as
FoiTgysPsite uses cookies to improve your experience. Ifyou continue to use this site, you agree with it. Privacy Policy.
Ok
hitps:/icantollerstech.comihow-o-use-spl-with-stm32/ ane24.12.2019 How to use SPI with STM22 » ContolarsTech
SCK -> Serial Clock
MOSI -> Master out Slave In is used to send data to slave
MISO —> Master In Slave Out is used to receive data from slave
CE/CS -> Chip Select is used for selecting the slave
SPI is not very different from 12C. It just require more wires and the process of selecting the
slave is a little different. In order to enable a slave device, we need to pull the CS pin low and
after our read or write is complete, just pull the pin high again. This will disable the slave
device.
HOW TO
Lam going to show you working with an actual hardware and because of lack of many SPI
devices, Iwill work with whatever I have and that is ADXL345. I have already wrote a tutorial
about How to use this device with I2C. Do check it out because I am not going to explain the
register part but only focus on How to read and write data using SPI.
So before start setting up the CubeMx, Let’s check the datasheet of ADXL345 to understand
the requirements for the SPI.
jown in
the connection diagrams in Figure 34 and Figure 35. Clearing the
SPI bit (Bit D6) in the DATA_FORMAT register (Address 0331)
selects 4-wire mode, whereas setting the SPI bit selects 3-wire
SroenemarTnTe Sel clock speed is 5 MHz.with 100 pF
maximum loading, and the timing scheme follows clock polarity
(CPOL) = 1 and clock phase (CPHA) = 1 If power is applied to
For SPI, either 3- or 4-wire configuration is possible, 2
‘According to the figure above, We need to setup SPI with clock speed less than SMHz and also
CPOL =1 and CPHA =1. I will be using the 4 wire mode so let’s set it up.. Below is the
screenshot of the SPI setup window
This website uses cookies to improve your experience. If you continue to use this site, you agree with it, Privacy Policy.
Ok
hitps:/icantollerstech.comihow-o-use-spl-with-stm32/ ana24.12.2019 How to use SPI with STM2 » ContolarsTech
Cees
Saree meat
ere ela te |__ oe) cane
Squarespace Website Builder
Make and manage your own profes
with Squarespace's all-in-one platform.
Squarespace
Some Insight into the CODE
ADXL WRITE FUNCTION
void adxl_write (uint8_t address, uint8_t value)
{
uinte_t data[2];
data[o] = address|oxeo; // multibyte write
data[1] = value;
HAL_GPIO_WritePin (GPIOB, GPIO_PIN.6, GPIO_PIN_RESET); // pull the cs pin low
HAL_SPI_Teansmit (&hspi1, data, 2, 100); // write data to register
HAL_GPIO_NritePin (GPIOB, GPIO_PIN.6, GPIO_PIN_SET); // pull the cs pin high
y
We are using following to write dat
This website Uses cookies to improve your experience. If you continue to use this site, you agree with it. Privacy Policy,
* pull the CS low to enable the slave ok
hitps:/icantollerstech.comihow-o-use-spl-with-stm32/ ana24.12.2019 How to use SPI with STM22 » ContolarsTech
* transmit the address to which we want to write data
* transmit the data
* pull the CS pin high to disable the slave
NOTE that in data[0], address is OR with 0x40. This is for multibyte writing. It informs ADXL
that we want to transfer more than one byte ina single transmission. According to ADXL
datasheet, this byte should be high if you want to do that.
To read or write multiple bytes in a single transmission, the
multiple-byte bit, located after the R/W bit in the first byte transfer
(MB in Figure 37 to Figure 39), must be setJ After the register
Sai athe Sat STE gueet tot nk
pulses (eight clock pulses) causes the ADXL345 to point to the
next register for a read or write. This shifting continues until the
clock pulses cease and CS is deasserted. To perform reads or writes
on different, nonsequential registers, CS must be deasserted
between transmissions and the new register must be addressed
separately.
ADXL READ FUNCTION
void adxl_read (uint8_t address)
{
address |= 0x89; // read operation
address |= 0x49; // multibyte read
vinte_s
HAL_GPIO_WritePin (GPIOB, GPIO_PIN.6, GPIO_PIN_RESET); // pull the pin low
HAL_SPI_Transmit (Bhspit, address, 1, 108); // send address
HAL_SPI_Receive (&hspil, data_rec, 6, 100); // receive 6 bytes data
HAL_GPIO_MritePin (GPIOB, GPIO_PIN.6, GPIO_PIN_SET); // pull the pin high
Here we are reading using the following steps:~
* pull the CS low to enable the slave
* transmit the address from where we want to read data
* receive data. 6 bytes in this case
* pull the CS high to disable the slave.
Fis PALADS SS Rin Og te sae ns AVF you continue to use this site, you agree with it. rivacy Policy
Ok
hitps:/icantollerstech.comihow-o-use-spl-with-stm32/ ana24.12.2019 How to use SPI wih STM32 » ContrallersTach
NOTE that address is OR with 0x80 and 0x40. That’s because according to ADXL datasheet, If
we want to read data, we need to set the last bit HIGH and also for multibyte read/write the 6th
bit must be HIGH
. =f
aw tea Ma Ter jae el ea
ene
: Pyke X_* =X ar
se Tonfesens—SOSCS™S~S~S~S
so xX XX X= xX « X X= i
Figure 37.5P14-Wire Write
a
= ae ea ellie
so eT Nt X= _X X= *_X X=
to ooneSsars SS” tose
0 xX» X_* X Y= _ X= X Y=
“en jetty ae ae fost far nee toa os
a
tees pe He houe oto
1210 aK Ye X= X Xe
ADXL INITIALISATION FUNCTION
void adxl_anit (void)
{
adxl_write (0x31, 0x01); // data_format range= +- 4g
adxl_write (8x24, @x@@); // reset all bits
adxl_write (@x2d, @x@8); // power_cntl measure and wake up 8hz
his website uses cookies to improve your experience. If you continue to use this site, you agree with it. Privacy Policy
Ok
hitps:/icantollerstech.comihow-o-use-spl-with-stm32/ sia24.12.2019 How to use SPI with STM22 » ContolarsTech
This is explained in my previous tutorial in detail. Anyway comments are self explanatory. This
is it guys, You can download the code below
Squarespace
Website Builder
Make and manage your own
professional website with
Squarespace's all-in-one
platform.
Connection
uueat con/sta32nucleo
fr
This website uses cookies to improve your experience. If you continue to use this site, you agree with it, Privacy Policy
Ok
hitps:/icantollerstech.comihow-o-use-spl-with-stm32/ ana24.12.2019 How to use SPI with STM2 » ContolarsTech
Triple Ais
Accelerometer
Breakout,
siopauvoy ounpsy DXA
‘Morph Connectors
Result
‘SPI in STM32 interfacing ADXL 345 || LCD || CubeMX || HAL || SW4STM
This website uses cookies to improve your experience. If you continue to use this site, you agree with it, Privacy Policy
He
hitps:/icantollerstech.comihow-o-use-spl-with-stm32/
ma24.12.2019 How to use SPI with STM22 » ContolarsTech
DOWNLOAD
‘Aynt Gin Kargo!
You can buy me a eeffee sensor ©)
DONATE HERE
download the CODE below
J adxl345, example, i2c, miso, mosi, spi, STM32, stm32f103, stm32f4, tutorial
4 Leave a Reply
THIS website uses cookies td improve your experience. If you continue to use this site, you agree with it, Privacy Policy
Ok
hitps:/icantollerstech.comihow-o-use-spl-with-stm32/ ana24.12.2019 How to use SPI with STM22 » ContolarsTech
o Join the discussion...
QM2an05 @ e: @a0
& Subscribe & newest # oldest ® most voted
Lara
Managing Multiple UARTs in STM32
December 18, 2019
> Multiplexer 74HC4051 and STM32
lecember 6, 201'
TRSGERRE Se Ob Ries to improve your experience. Ifyou continue to use this site, you agree with it. Privacy Policy
> Free RTOS Tutorial 2.0 with STM32 Ok
November 27, 2019
hitpsficantollerstech.comhow-o-use-spl-with-stm32/ ona24.12.2019 How to use SPI with STM22 » ContolarsTech
> DAC in STM32
November 17, 2019
> Interface TFT display with STM32
October 6, 2019
> Introduction to Free RTOS in STM32
September 28, 2019
Squarespace Webs
Builder
Squarespace
Make and manage your own profe
website with Squarespace's all-in
platform,
Subscribe for alerts
Name *
Jon Snow
Email *
[email protected]
REGEN NRE. improve your experience you continue to use this sie, you agree witht. Privacy Policy
Ok
sora24.12.2019 How to use SPI with STM2 » ContolarsTech
FIBER TUS 1ULUTIAE 2.0 [Lo
GOTO
> EMBEDDED
> HOME
> LPC2148
> PIC
> Search Result Page
> STM32
search this Site
Search . Q|
Contact us
Name *
Email *
[email protected]
Comment or Message *
This website uses cookies to improve your experience. If you continue to use this site, you agree with it, Privacy Policy
Ok
hitps/leantollerstech.comhow-o-use-spl-with-stm32/ ne24.12.2019
How to use SPI with STM22 » ContolarsTech
5451 TL
emt
60.1
614
This website uses cookies to improve your experience. If you continue to use this site, you agree with it, Priva
hitps:/icantollerstech.comihow-o-use-spl-with-stm32/
Ok
rane