You are here
Home > Solved >

ESP32 IDF STPM32/33 SPI Issue

This post discusses ESP32 IDF STPM32/33 SPI Issue and its solution, There are lot of issues reported on GitHub and aurdinio forum. Well ESP32 is from Espressif Systems and is a popular IoT controller used in embedded systems. While COVID-19 had accelerated most of the touchless and wireless features in any sort of product, these controllers like ESP32, ESP8266 had proven their time to market.

In our product, While using SPI of ESP32, We are using esp32 wroom32. In our Smart Meter IoT Application Software, we need to read the internal registers of STPM32/33. As ESP32 is interfaced with STPM32/33- a popular metering module – over SPI. For that purpose, we are using SPI protocol. STPM32/33 has SPI peripheral which is configured as SPI slave device and esp32 is also having SPI peripheral which is acting as SPI master. This interface was not working and we were getting ‘FF’ as a response from SPI slave device.

Let’s get started with the first step.

Select SPI Modes

This is tricky and it should be done carefully. You need to read the sensor or slave datasheet to understand which SPI mode to set up on the master side. Generally, there are 3-4 modes on SPI master side.

mode0 

Here CPOL=0, CPHA=0, Idel state of SPI, the clock output is logic low. This means data will change on the falling edge of the SPI clock. So it should be sampled on the rising edge.

mode1 

Here CPOL=0, CPHA=1, In SPI idle state, the clock output is logic low. But in this mode data change will happen on the rising edge of the SPI clock, So data should be sampled on the falling edge.

mode2 

Here CPOL=1, CPHA=0, In SPI idle state, the clock output is logic high, and the data changes on the rising edge of the SPI clock, so the data should be sampled on the falling edge.

mode3 

Here CPOL=1, CPHA=1, In SPI idle state, the clock output is logic high; data changes on the falling edge of the SPI clock and is sampled on the rising edge.

You will need a logic analyzer or oscilloscope to confirm the clock and data sent from SPI master.

SPI master clock at 1MHz
SPI master Read SFDP Register (5Ah) Command Waveform
Expected response for SFDP register

ESP-IDF is Espressif’s official IoT Development Framework for the ESP32 and ESP32-S series of SoCs. It provides a self-sufficient SDK for any generic application development on these platforms, using programming languages such as C and C++.

The STPM3x is an ASSP family designed for high accuracy measurement of power and energies in power line systems using the Rogowski coil, current transformer or shunt current sensors. The STPM3x provides instantaneous voltage and current waveforms and calculates RMS values of voltage and currents, active, reactive and apparent power and energies.
We are creating this interface on esp idf. Created one project referring esp32 programming guide. Then trying to read the default value of the first register from STPM33 (Address 0x00) which should be 040000A0. But the issue was that we were getting the value as FFFFFFFF, which is not correct.

After a lot of searches finally, we found the issue.. that was in spi_device_interface_config_t structure. This struct is defined in esp-idf/blob/master/components/driver/include/driver/spi_master.h file. We configured this structure as below:

spi_device_interface_config_t devcfg= { .command_bits=0, .address_bits=0, .dummy_bits=0, .flags = 0, .mode=3, //SPI mode 3 .duty_cycle_pos=128, .clock_speed_hz=1000000, //Clock out frequency (Hz) .queue_size=1, .spics_io_num=PIN_NUM_CS };

The issue was in spics_io_num member. When we set spics_io_num value >=0, we are using hardware chip select i.e. activation/deactivation of CS will be done by SPI hardware.

But in our code, we are manually controlling CS. (Making CS low before sending SPI transaction and making it high after completion of the transaction). We need software CS control. For that, we have to make spics_io_num = -1.

Use spics_io_num=-1 instead of .spics_io_num=PIN_NUM_CS in above structure. After this change, we are getting the default value of 0th register from STPM33 which is 040000A0.

Hope you have enjoyed reading ESP32 IDF STPM32/33 SPI Issue and its solution.

Leave a Reply

Top
%d bloggers like this: