2022-02-23 03:27 AM
Hi all,
I am using STM32F334 for SPI communication with a TDC7201 chip from TI.
I am using the Low-Level Drivers to initialize the driver and communicate.
Here is my initialisation routine:
__HAL_RCC_SPI1_CLK_ENABLE();
LL_SPI_InitTypeDef SPI1InitStruct;
SPI1InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV4;
SPI1InitStruct.ClockPhase = LL_SPI_PHASE_2EDGE;
SPI1InitStruct.ClockPolarity = LL_SPI_POLARITY_HIGH;
SPI1InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
SPI1InitStruct.DataWidth = LL_SPI_DATAWIDTH_16BIT;
SPI1InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
SPI1InitStruct.BitOrder = LL_SPI_MSB_FIRST;
SPI1InitStruct.Mode = LL_SPI_MODE_MASTER;
SPI1InitStruct.NSS = LL_SPI_NSS_SOFT;
LL_SPI_Init(SPI1, &SPI1InitStruct);
LL_SPI_Enable(SPI1);
I have initialized the port PIns so:
/**
* PB3 - SCK
* PB4 - MISO
* PB5 - MOSI
* PA15 - NSS
*/
if (RESET == __HAL_RCC_GPIOB_IS_CLK_ENABLED())
{
__HAL_RCC_GPIOB_CLK_ENABLE();
}
LL_GPIO_InitTypeDef gpioSPIInitStr;
gpioSPIInitStr.Alternate = LL_GPIO_AF_5;
gpioSPIInitStr.Pin = LL_GPIO_PIN_3 | LL_GPIO_PIN_5;
gpioSPIInitStr.Mode = LL_GPIO_MODE_ALTERNATE;
gpioSPIInitStr.Speed = LL_GPIO_SPEED_FREQ_HIGH;
gpioSPIInitStr.Pull = LL_GPIO_PULL_NO; // LL_GPIO_PULL_NO; //
// LL_GPIO_PULL_UP
gpioSPIInitStr.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
LL_GPIO_SetAFPin_0_7(GPIOB, LL_GPIO_PIN_3, LL_GPIO_AF_5);
LL_GPIO_SetAFPin_0_7(GPIOB, LL_GPIO_PIN_4, LL_GPIO_AF_5);
LL_GPIO_SetAFPin_0_7(GPIOB, LL_GPIO_PIN_5, LL_GPIO_AF_5);
LL_GPIO_Init(GPIOB, &gpioSPIInitStr);
// Pin B4 (MISO) must be configured as as input pull-up
LL_GPIO_StructInit(&gpioSPIInitStr);
gpioSPIInitStr.Pin = LL_GPIO_PIN_4;
gpioSPIInitStr.Mode = LL_GPIO_MODE_INPUT;
gpioSPIInitStr.Pull = LL_GPIO_PULL_NO;
gpioSPIInitStr.Speed = LL_GPIO_SPEED_FREQ_HIGH;
LL_GPIO_Init(GPIOA, &gpioSPIInitStr);
/*NSS Pin Config */
LL_GPIO_InitTypeDef SPI_SSPIN_Struct;
SPI_SSPIN_Struct.Mode = LL_GPIO_MODE_OUTPUT;
SPI_SSPIN_Struct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
SPI_SSPIN_Struct.Pin = LL_GPIO_PIN_15;
SPI_SSPIN_Struct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
LL_GPIO_Init(GPIOA, &SPI_SSPIN_Struct);
LL_GPIO_SetOutputPin(GPIOA, LL_GPIO_PIN_15);
This is how my "read_register " routine looks like:
uint16_t read_register(uint8_t address)
{
uint16_t data;
uint8_t raddr;
if (!LL_SPI_IsEnabled(SPI1))
{
LL_SPI_Enable(SPI1);
}
raddr = address; // set addr bits 7 & 6 to 0 for read
CHIP_SEL_LOW(); // slave select (low)
while (!LL_SPI_IsActiveFlag_TXE(SPI1))
; // wait while not empty
LL_SPI_TransmitData16(SPI1, (uint16_t)(raddr<<8));
while (!LL_SPI_IsActiveFlag_RXNE(SPI1))
;
data = LL_SPI_ReceiveData16(SPI1);
while (LL_SPI_IsActiveFlag_BSY(SPI1))
;
CHIP_SEL_HIGH(); // slave deselect (high)
if (LL_SPI_IsEnabled(SPI1))
{
LL_SPI_Disable(SPI1);
}
return data;
}
I try to read the contents in 2 registers periodically in the main routine.
var1 = read_register(0x00);
Delay_us(500);
var2 = read_register(0x03);
Delay_us(500);
I read 0xFFFF in both var1 and var2 , always. When i know for sure that the content is different.
This is how my transaction looks on the scope. Clock (Red), CS (Blue), MOSI(Green), MISO (Yellow).
The signal on MISO and the received data are not as expected, I don't understand why. I believe i am doing something wrong in the initialization stage. Any useful tips or pointers would be helpful. Thanks.
2022-02-24 03:56 AM
Something is confusing here.
The SPI init comments specify Port A, and the GPIO init comments specify Port B.
@AT.8 can you please clarify which pins are used for the SPI ?
What @Community member said is that line 32 should be LL_GPIO_MODE_ALTERNATE.
On top of that, the 334 Discovery uses PB3 and PB4 for communication with the ST-Link/V2-1 .
Did you change the solder bridges accordingly ? Otherwise the ST-Link Tx wants to keep it high.
2022-02-24 04:03 AM
Yes, its confusing because i forgot to remove the comments in the SPI init routine. Actually, i configured the PB3, PB4, PB5 and PA15 for SPI1 as AF. No i did not change the solder bridges.
2022-02-24 04:38 AM
Do you measure the waveforms directly on the TI board?
JW
2022-02-24 04:49 AM
No, measured it from ST. I will try measuring from TI board.
2022-02-24 06:05 AM
This screenshot is from the TI side. SCK (RED), Data IN (Green), Data Out (Yellow), CS (Blue)
I believe the chip itself might be damaged.
2022-02-24 02:33 PM
Try with the original TI master, measuring at the same place, compare/post waveforms.
JW