2020-04-14 03:45 AM
Hello,
I am using STM32F767ZI with a motor driver from TI (DRV8908) which works via SPI in 16-bit mode and with 5MHz and has the following requirements:
1. SCLK pin should be low when the nSCS pin transitions from high to low and low to high.
2. nSCS pin should be pulled high for at least 400 ns between words.
3. Data is captured on the falling edge of SCLK and data is propagated on the rising edge of SCLK.
4. The MSB is shifted in and out first.
5. A full 16 SCLK cycles must occur for transaction to be valid.
To make the MCU compatible with it, I made the following setting on SPI1 (in STM32CubeIDE):
* Frame Format: Motorola
* Data Size: 16 Bits
* First Bit: MSB First
* Baud Rate: 3.375 MBits/s
* Clock Polarity (CPOL): Low
* Clock Phase (CPHA): 2 Edge
* NSS Signal Type: Software
In the code for transmitting the data, I'm using, the following code:
uint16_t value = spiTxBuffer[0] << 8 | spiTxBuffer[1];
uint16_t return_value = 0;
HAL_GPIO_WritePin(GPIOG, MOTOR_NSS_Pin, GPIO_PIN_RESET);
if(HAL_SPI_Transmit(&hspi1, (uint8_t*)&value, 1, 50) != HAL_OK) {
// error
}
HAL_GPIO_WritePin(GPIOG, MOTOR_NSS_Pin, GPIO_PIN_SET);
And for receiving, the following:
uint16_t value = spiTxBuffer[0] << 8 | spiTxBuffer[1];
uint16_t return_value = 0;
HAL_GPIO_WritePin(GPIOG, MOTOR_NSS_Pin, GPIO_PIN_RESET);
if(HAL_SPI_TransmitReceive(&hspi1, (uint8_t*)&value, (uint8_t*)&return_value, 1, 50) != HAL_OK)
return_value = 0xFFFF;
HAL_GPIO_WritePin(GPIOG, MOTOR_NSS_Pin, GPIO_PIN_SET);
I was not able to successfully get it to work. Could someone tell me what is it that I am doing incorrectly? I am suspicious about the SPI setting (mainly regarding the CPOL and CPHASE).
Thanks,
Naveen.