r/stm32 • • 18d ago

Help request with parallel-in serial-out shift register

I am using a PCA9701PW on my PCB, which seems to be a normal SPI shift register. It is connected to an STM32G431. When I attempt to read it, I get ones for all 16 bits from it. I have tried all clock polarity and phase combinations, and even when I pull the Chip Select pin low to load the data into the register and reset the interrupt trigger, I get no interrupt. I have checked for shorts between all pins and there are none, I also checked that the pins are wired correctly, SDOUT to MISO and CS to CS etc. My SPI clock is at 625KBits/S. I would appreciate any ideas about what to check next, thank you.

int timeTest = 0;

static uint8_t allOnes[2] = {0xFF, 0xFF};

static uint8_t expanderInput[2] = {0, 0};

...

while(1){

if(timeTest >= 10000){

HAL_GPIO_WritePin(GPIO_CS_GPIO_Port, GPIO_CS_Pin, RESET);

	`HAL_SPI_TransmitReceive(&hspi1, &allOnes, expanderInput, 2, 20);`

	`HAL_GPIO_WritePin(GPIO_CS_GPIO_Port, GPIO_CS_Pin, SET);`

timeTest = 0;

}

else {

timeTest++;

}

}

...
static void MX_SPI1_Init(void)

{

/* USER CODE BEGIN SPI1_Init 0 */

/* USER CODE END SPI1_Init 0 */

/* USER CODE BEGIN SPI1_Init 1 */

/* USER CODE END SPI1_Init 1 */

/* SPI1 parameter configuration*/

hspi1.Instance = SPI1;

hspi1.Init.Mode = SPI_MODE_MASTER;

hspi1.Init.Direction = SPI_DIRECTION_2LINES;

hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;

hspi1.Init.NSS = SPI_NSS_SOFT;

hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;

hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi1.Init.TIMode = SPI_TIMODE_DISABLE;

hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

hspi1.Init.CRCPolynomial = 7;

hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;

hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;

if (HAL_SPI_Init(&hspi1) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN SPI1_Init 2 */

/* USER CODE END SPI1_Init 2 */

}

2 Upvotes

3 comments sorted by

1

u/Hangoverman 18d ago

I'm curious why you are using the TransmitReceive function and not just Receive? I'd start by simplifying that. Then, look at the clock and data out pins from the SR with a scope if you can to confirm 16 clock pulses and the expected data coming out of the SR.

You may also want to verify that concurrent SPI operations aren't interrupting each other. You are using the while loop itself to increment the counter and the actual time that takes will depend on various things. HAL_Delays are blocking, but could provide more predictable timing intervals during early stage troubleshooting like this. I'd use SysTick for non-blocking, interrupt based timers, but that may be overkill.

1

u/RepulsiveStrawberry5 17d ago

Sorry for the late response, I was using TransmitReceive in case the shift register might need data going into it, I was just trying something new after Receive didn't work. I did disable all other SPI, and try it in a blank project, I am starting to think it is just some kind of hardware issue, I might try a different IC. My last thing to try right now will be to implement it without using the SPI peripheral. I don't have a scope so I can't check right now, but maybe I can use an arduino or nucleo to do that at this speed. Thanks for the help.

1

u/dgendreau 17d ago

There is no reason you should be flying blind. If money is an issue, at least pick up a cheap logic analyzer. EG: https://www.sparkfun.com/usb-logic-analyzer-24mhz-8-channel.html

That one works with the open source Sigrok viewer.