Skip to main content
Ashish Sharma
Associate
July 7, 2018
Question

Problem in SPI Interfacing of LTC1859 with STM32F401RE

  • July 7, 2018
  • 1 reply
  • 1261 views
Posted on July 07, 2018 at 08:45

Hello,

I am using STM32F401RE microcontroller board and trying to interface LTC1859 with it. I am using STM32CUBEMX to generate code with following parameters for SPI:

Data size : 8 bits

Prescaler : 8

Clock Frequency : Changed from 84 MHz (default) to 16 MHz

First Bit : MSB first

CPOL : Low

CPHA : 2 Edge

I have configured HAL_SPI and generated the source code for ARM_V5 (keil). Following is the snippet of code used for making SPI connection between STM32 and LTC1859:

OPTION 1:

// Read Process

// 1. Pull CS or CONVST Low : Activate

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET);

// 2. Tranfer Data Read Command on MOSI

spiTxBuf[1] = 0x88;//LTC1859_CH0 | LTC1859_UNIPOLAR_MODE | LTC1859_LOW_RANGE_MODE | LTC1859_NORMAL_MODE;

spiTxBuf[0] = 0x00;

HAL_SPI_Transmit(&hspi1, &spiTxBuf[1], 1, 50);

// 3. Receive Data

HAL_SPI_Receive(&hspi1, spiRxBuf, 2, 50);

// 4. Pull CS or CONVST High : Deactivate

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);

OPTION 2:

// Read Process

// 1. Pull CS or CONVST Low : Activate

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET);

// 2. Tranfer Data Read Command on MOSI

spiTxBuf[0] = 0x88;//LTC1859_CH0 | LTC1859_UNIPOLAR_MODE | LTC1859_LOW_RANGE_MODE | LTC1859_NORMAL_MODE;

spiTxBuf[1] = 0x00;

HAL_SPI_TransmitReceive(&hspi1, spiTxBuf, spiRxBuf, 2, 50);

// 3. Pull CS or CONVST High : Deactivate

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);

Physical Connections are as follows:

AVDD, DVDD : Connected to 5V through 10uF (tantalum) and 0.1uF (Ceramic) capacitor

OVDD : Connected to 3.3V through 1

0uF (tantalum)

COM : Connected to GND

CH0 : Input through 1000pF Ceramic capactior

SDI : SPI1_MOSI

SDO : SPI1_MISO

SCK : SPI1_SCK

CONVST & ~RD : GPIO_PIN_9

All Grounds are common.

 

The problem I am facing is that the device is getting the communication lines in right format but not responding with ''correct'' data. Some garbage data keeps coming which is nowhere close to the data to be read. Any help would be appreciable.

All the necessary pictures are attached herewith.

0690X0000060CFCQA2.png   0690X00000604klQAA.jpg 

0690X00000604kbQAA.jpg0690X00000604JDQAY.jpg0690X00000604l5QAA.jpg0690X00000604lKQAQ.jpg

#hal-spi #ltc1859-adc #stm32f401re-spi-keil-cubemx
This topic has been closed for replies.

1 reply

T J
Senior III
July 7, 2018
Posted on July 08, 2018 at 01:12

I use this style of coding :

void SPI_t::transfer(unsigned short data) {

// drop nCS

//    Clear_SPI1_nSS_Out();

    char RxSPI;

    while (!(hspi1.Instance->SR  & SPI_FLAG_TXE))         // wait while not empty

        ;

    *((__IO uint8_t *)&hspi1.Instance->DR) =  data;         // send data

    while (!(hspi1.Instance->SR  & SPI_FLAG_TXE))      // wait while not empty

        ;

    while ((hspi1.Instance->SR  & SPI_FLAG_BSY))   // wait while still transferring to pin

        ;

    while ((hspi1.Instance->SR  & SPI_FLAG_RXNE))   //take all bytes in the receiver, we only want the last one.

        RxSPI = hspi1.Instance->DR;

//    Set_SPI1_nSS_Out();        

}
Ashish Sharma
Associate
July 8, 2018
Posted on July 08, 2018 at 08:08

Thanks for reply.

As I see, set and reset of nSS pin would mean to simple HAL_GPIO_WritePin to CONVST (for this particular ADC).

Next the ADC responds with 2 bytes of data in the current communication cycle:

1. While transmitting 1 byte on MOSI, first byte is received on MISO.

2. Next byte is received in next set of 8-bit SCLK of the same communication cycle.

In the snippet you provided, first it is waiting for Tx to complete on MOSI and then read the data of MISO. How to make it a duplex transmission/reception? (by removing 

while ((hspi1.Instance->SR  & SPI_FLAG_BSY))  ??)

As in my code I was using HAL_SPI_TransmitReceive() send and receive at the same time on a particular communication cycle.

For 2 byte reception shall I make RxSPI a uint16_t type of variable?