Getting Started with STM32H5 ARM Cortex M33: SPI in TX Mode

The SPI (Serial Peripheral Interface) on the STM32H5 microcontroller provides a fast and flexible means of serial communication, ideal for connecting peripherals such as sensors or memory devices. In this guide, we focus on configuring the SPI peripheral in Transmit Only (TX) mode, enabling the microcontroller to send data to external devices.

In this guide, we shall cover the following:

  • What is SPI.
  • SPI configuration.
  • Firmware development.
  • Results.

1. What is SPI:

SPI, which stands for Serial Peripheral Interface, is a standard with a very specific hardware interface. A connection is between a master and a slave, with the master typical being a processor, and the slave being a peripheral such as a sensor, flash memory device, or a modem chip.   It can also be used for processor to processor communications, but in this case, an additional handshake signal is often used. There are normally four signals between a master and a slave, the first is a clock signal, and this signal is always driven by the master, regard which device is transmitting.  The second line is a data line for data going from the master to the slave, and this is designated as the master output, slave input line, are MOSI for short. It connects the SPI data out connection on the master and to the SPI data in connection on the slave.

The next conductor is for data in the opposite direction and is labelled as the master input slave output line, are MISO for short. The last conductor is slave select with the slave chip selecting input, and is active low.

pastedImage_0.png

If you have more than one slave, with the first being perhaps a sensor of some kind, the slave will be dedicated to slave 1. If you add a second sensor, the top 3 interface line will be shared, but it dedicates for slave’s line will be required for the second device, and the same is true of course for each of additional slave device.

pastedImage_1.png

Most processors have a maxim of 4 slave selected lines.  The four lines could be used effectively as a multiplexed address lines to access more than 4 slaves. You cannot have more than one master on the bus,   since the interface is not support coordination between two masters as to which one is controlling the bus.

Transmissions are typically sent as a sequence of bytes, but without a formal protocol, there is nothing restricting communication being   byte based. Typical by frame sizes, are in 8 to 32 bits range.  Also note the bytes and packets are not acknowledged as they are in i2c, and you could have a master synching communicating with the slave but, you don’t really know of your communications are being received OK. However, some slave devices will echo bytes sent to it, which provides an acknowledgement to the master.  it is how the data lines are synchronized with a clock signal.

Clock Polarity and Phasing

There are four different modes available, one mode each combination clocking in a low state and high state, with a data being read in a rising edge or falling edge of the clock signal.  For modes 0 and 1, the clock is low in idle, which is referred to  as clock and 0,  For modes 2 and 3 then the clock is in high state when idle, so it has  polarity  , one , For modes 0 and 2, the data will be sampled by the receiving device  on the leading edge of a clock signal.  Relative to the idle state, which is referred to a clock phase of zero. So for mode 0, this means the rising edge of the clock and for mode 2, means the following edge of the clock, the other two modes, use a clock phase 1 which means that trailing edge of clock as a returns to an  idle state.  And this translates to a following edge for mode 1 and the rising edge for mode 3.  Mode 0 is the most commonly supported setting. If multiple slaves in the same bus, you may have to reconfigure the settings for the master to which modes when you want to communicate with a different slave.

pastedImage_2.png

2. SPI Configuration:

We start off by creating new project with name of SPI_TX. For how to create a new project, please refer to this guide here.

After the project creating, since this guide uses STM32H563ZI Nucleo-14, it has Arduino pins. From the STM32H5 Nucleo-144 board user manual (UM3115), we can find that SPI pins as following:

  • PA5 for SPI SCK
  • PG9 for SPI MISO.
  • PB5 for SPI MOSI.

PA6 has been nominated as CS (Chip Select) pin.

Hence, we cam configure the pins as following:

Note: Set PA6 as GPIO output.

After the GPIO configuration, we can configure the SPI as following:

From connectivity from left menu, select SPI1 and enable it as Full Duplex Master.

Configure the SPI as following:

  • Frame Format: Motorola.
  • Data Size: 8bits.
  • First bit is MSB.
  • Set the prescaler ot 64 to reduce the speed to 1Mbps.
  • Keep the rest as default.

Save the project and this shall generate the code.

3. Firmware Development:

Once the project is generated, the main.c file will be opened.

In mian.c, in user code begin PV (Private Variable), declare the following:

#define dataSize 4

uint8_t data[dataSize]={0x01,0x02,0x03,0x04};

Define the data size of 4 in this example.

buffer to hold the 4 bytes to be transmitted.

In user code begin 3 in while loop, we shall transmit the data.

	  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET);

	  HAL_SPI_Transmit(&hspi1, data, dataSize, 100);

	  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET);

	  HAL_Delay(10);

First, set the CS pin to low.

Transmit data using HAL_SPI_Transmit function. This function shall take the following parameters:

  • Instant to spi to be used which is hspi1 in this case.
  • data variable which points to the data buffer to be transmitted.
  • Buffer size to be transmitted.
  • Timeout management which is 100ms in this case.

Then set the CS line to high to indicate end of transmission.

Delay by 10ms to give some time.

Save the project, build it and run it on your STM32H563Zi board.

4. Results:

Using logic analyzer, probe the PA5, PB5, PG9 and PA6 then set the mode to decode is SPI and you should get the following:

Later, we shall use full duplex mode to communicate with a sensor.

Happy coding 😉

Add Comment

Your email address will not be published. Required fields are marked *