SPI communication with POWERSTEP – receiving same data as transmitted
Hello everyone,
I’m trying to communicate with a POWERSTEP motor driver via SPI using an STM32 (STM32F4) and the HAL library. I have a basic test where I send three bytes and read back the same number of bytes, but I always receive exactly the same data that I transmit (i.e., a loopback effect). The POWERSTEP does not seem to respond with its expected status bytes.
SPI configuration (from CubeMX):
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_HIGH; // CPOL = 1
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE; // CPHA = 1 => SPI mode 3
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
Code snippet (simplified):
uint8_t tx[3];
uint8_t rx[3];
tx[0] = 0x3B; // example command
tx[1] = 0x00;
tx[2] = 0x00;
// Reset chip
HAL_GPIO_WritePin(rstPOWERSTEP_GPIO_Port, rstPOWERSTEP_Pin, GPIO_PIN_RESET);
HAL_Delay(5);
HAL_GPIO_WritePin(rstPOWERSTEP_GPIO_Port, rstPOWERSTEP_Pin, GPIO_PIN_SET);
HAL_Delay(5);
HAL_GPIO_WritePin(csPOWERSTEP_GPIO_Port, csPOWERSTEP_Pin, GPIO_PIN_SET);
HAL_Delay(10);
uint8_t i = 0;
while (i < 3)
{
// CS Low
HAL_GPIO_WritePin(csPOWERSTEP_GPIO_Port, csPOWERSTEP_Pin, GPIO_PIN_RESET);
// Send 1 byte and receive 1 byte
HAL_SPI_Transmit(&hspi1, &tx[i], 1, 100);
HAL_SPI_Receive(&hspi1, &rx[i], 1, 100);
// CS High
HAL_GPIO_WritePin(csPOWERSTEP_GPIO_Port, csPOWERSTEP_Pin, GPIO_PIN_SET);
HAL_Delay(100);
++i;
}
