cancel
Showing results for 
Search instead for 
Did you mean: 

HTS221 with Half duplex SPI

SSchm.16
Associate

Hello,

I'm currently programing a self designed sensor node with a HTS221. I've decided to use SPI to communicate with it (SPI half-duplex master)

Here are the settings:

static void MX_SPI2_Init(void)
{
 
  /* USER CODE BEGIN SPI2_Init 0 */
 
  /* USER CODE END SPI2_Init 0 */
 
  /* USER CODE BEGIN SPI2_Init 1 */
 
  /* USER CODE END SPI2_Init 1 */
  /* SPI2 parameter configuration*/
  hspi2.Instance = SPI2;
  hspi2.Init.Mode = SPI_MODE_MASTER;
  hspi2.Init.Direction = SPI_DIRECTION_1LINE;
  hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi2.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi2.Init.NSS = SPI_NSS_SOFT;
  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi2.Init.CRCPolynomial = 7;
  hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi2.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  if (HAL_SPI_Init(&hspi2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI2_Init 2 */
 
  /* USER CODE END SPI2_Init 2 */
 
}

Since I couldn't get the content of the WHO_I_AM register, I decided to check the exchanges with my oscilloscope.

I discovered that the CLK level was between 1V and 200mV. And due to some poor design choices, this line take some noise from the SWDCLK.

Here is the way I (try) to communicate with it:

  status = HAL_SPI_Transmit(&hspi2, (uint8_t*)&spiTX,1,1000);
  if (status != HAL_OK){
	  //printf(status);
	  printf("PROBLEM");
  }
  HAL_Delay(10);
  
  HAL_SPI_Receive(&hspi2, (uint8_t*)&spiRX,1,1000);
  if (status != HAL_OK){
	  //printf(status);
	  printf("PROBLEM");
  }

I would like to know if there is something I missed. After some researches, I realised that half duplex SPI seem to be subject to some turnarounds for some designers.

Thank you

3 REPLIES 3
S.Ma
Principal

Have you checked that the device is configured in SPI 4 wires interface? Otherwise, reading by maybe a challenge.

I would reccomend I2C bus for humidity sensor as its response time and refresh speed hardly requires a high speed interface.

SSchm.16
Associate

0690X000006DReOQAW.pngThe device is configured (by CubeMX) as half-duplex SPI (3 wires) since it was the only SPI option available for HTS221. I have one software NSS, a CLK and a MOSI (which is supposed to be switching role during a transfert).

I'm not sure to understand how a 4 wires interface would change something.

Would it be for this kind of architecture : ?

Thank you for your help.

S.Ma
Principal

As SPI 3 wires for HTS, try to short STM32 MISO and MOSI pins and use the mcu SPI in bidirectional mode. When reading data, simply turn MOSI as digital input pin to cut the peripheral from expressing its output data while sending SCK clock pulses.

If this works, then you can try to use the other SPI specifics modes of the peripheral.

And remember that the sensor listen to the lines as I2C slave when NSS is high. So if this SPI peripheral has multiple NSS on the MCU side, make sure signals are "clean".