In this article, I will try to communicate with SPI using Mbed.
I wrote an article about SPI using HAL at here, but this time I will use Mbed.
Here is the development environment at the time of submission.
PC: Windows 10 OS
IDE: STM32CubeIDE Version1.3.0
Board: STM32Nucleo-F401RE
Overview of SPI communication
SPI stands for Serial Peripheral Interface.
Like I2C, it is a way to communicate between a microcontroller and peripheral devices (in general) on a printed circuit board.
While I2C has a single data bus for both transmission and reception, SPI has a separate data bus.
This means that full-duplex communication is possible, and therefore the communication speed is faster.
Communication methods developed by Motorola
Four communication lines (SCK, MISO, MOSI, and SS) can be used for bi-directional communication.
Communication speed is faster than I2C.
There is a master and a slave.
For more information about SPI, please refer to here.
Targets that are slaves
I used the STM32 (controlled by the master) and the following devices for the slaves.
barometric pressure sensor module.
The price is 600 yen and I bought it using the internet.
I am afraid it is made in Japan, but I want to blog about the results of checking the operation.
The manufacturer is STMicro. The datasheet for the device is here.
Connection
The connections are as shown in the table below.
You can choose whether you want to connect this module via I2C or SPI.
Since we will be using SPI, we will connect pin 5 to the chip select signal.
Since we will not be using interrupts, we have not connected the INT pin.
For SPI communication, the J1 and J2 solder jumpers on the back side must be open.

Mbed OS2 project files
You can download the Mbed OS2 project here.
However, please note that the boards and environments are limited to the ones listed above.
Try coding
For how to use SPI with Mbed, I referred to the site at here.
Since the timer needs to be initialized, the code should be written after SystemClock_Config() in the maini() function.
/* Configure the system clock */ SystemClock_Config();
SystemClock_Config();
/* USER CODE BEGIN 2 */ SystemClock_Config()
#define WHO_AM_I 0x0f
#define CTRL_REG1 0x20
#define WAKE_UP 0x90
#define P_ADRS 0x28
int err, l, m, h;
char buf[16];
uint32_t press;
SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK);
spi.format(8, 0);
spi.frequency(1000000);
DigitalOut cs(PA_4);
buf[0] = WHO_AM_I | 0x80; // Who am I ?
cs = 0;
int ret;
spi.write(buf[0]);
ret = spi.write(0x00);
cs = 1;
if (0xbd == ret)
{
err = 0;
}
else
{
err = 1;
}
buf[0] = CTRL_REG1; // Control Register1
buf[1] = WAKE_UP;
cs = 0;
spi.write(buf[0]);
spi.write(buf[1]);
cs = 1;
HAL_Delay(3000);
buf[0] = P_ADRS | 0xc0;
cs = 0;
spi.write(buf[0]);
l = spi.write(0);
m = spi.write(0);
h = spi.write(0);
cs = 1;
press = h << 16 | m << 8 | l;
press /= 4096;
/* USER CODE END 2 */
Explanation of the code
With Mbed, the initialization is done in the constructor, so it's easy and hassle-free.
For chip select, I have defined GPIO(PA4) in DigitalOut class.
We read the value of the register WHO_AM_I, and if it is 0xbd, the device is recognized.
Write WAKE_UP(0x90) to control register 1 to release the power down mode.
Read the barometric pressure measurements from the registers 0x28 (P_ADRS), 0x29, and 0x2a.
By setting P_ADRS | 0xc0, the address is auto-incremented and can be read one after another.
However, the mbed SPI does not have a function to read continuously, so I used the write() function repeatedly to read.
By dividing the read value by 4096, we can get the air pressure in units [hPa].
The air pressure was 1013 [hPa].





