cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103C8T6 SPI communication with ADS1220 stops

bla vbla
Associate
Posted on March 01, 2017 at 01:17

Hello,

I'am using and STM32F103C8T6 development board to communicate via SPI with ADS1220(Analog to Digital Converter). My code needs to read the buffer of the ADS1220 at certain times. When i run the code the SPI works only for a few minutes(max 14 min) before it stops. The SPI code i am using is :

void SPIx_Init()

{

// Initialization struct

SPI_InitTypeDef SPI_InitStruct;

GPIO_InitTypeDef GPIO_InitStruct;

// Step 1: Initialize SPI

RCC_APB2PeriphClockCmd(SPIx_RCC, ENABLE);

SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;

SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge; 

SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;

SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;

SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStruct.SPI_Mode = SPI_Mode_Master;

//SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;

SPI_Init(SPIx, &SPI_InitStruct); 

SPI_Cmd(SPIx, ENABLE); // Enables SPIx peripheral.

// Step 2: Initialize GPIO

RCC_APB2PeriphClockCmd(SPI_GPIO_RCC, ENABLE);

// GPIO pins for MOSI and SCK

GPIO_InitStruct.GPIO_Pin = SPI_PIN_MOSI | SPI_PIN_SCK;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;

GPIO_Init(SPI_GPIO, &GPIO_InitStruct);

// GPIO pins for MOSI, MISO

GPIO_InitStruct.GPIO_Pin = SPI_PIN_MISO;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;

GPIO_Init(SPI_GPIO, &GPIO_InitStruct);

}

uint8_t SPIx_Receive(uint8_t data)

{

// Write data to be transmitted to the SPI data register

SPIx->DR = data;

// Wait until transmit complete

while (!(SPIx->SR & (SPI_I2S_FLAG_TXE)));

// Wait until receive complete

while (!(SPIx->SR & (SPI_I2S_FLAG_RXNE)));

// Wait until SPI is not busy anymore

while (SPIx->SR & (SPI_I2S_FLAG_BSY));

//Return received data from SPI data register

return SPIx->DR;

}

The program always stops at this line of code :

while (!(SPIx->SR & (SPI_I2S_FLAG_TXE)));

Before the code stops, it has done at least 1000 reads(usually more than a 1000,maybe even 50000) of the buffer successfully. Why does my code stop after successfully running for so long time. Is there a problem with the SPI buffer, some data not being send. The data I am transmitting is a dummy byte 0x00.

Any help would be greatly appreciated. 

3 REPLIES 3
Posted on March 01, 2017 at 11:27

//SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;

So NSS is undefined, then?

JW

Posted on March 01, 2017 at 14:55

I mean, you MUST initialize it somehow in the SPI_InitStruct, otherwise - as it's a local variable - it's sort of random what will it be set to.

And, if the pin is not used, use Soft and set it to low.

JW

Posted on March 01, 2017 at 14:46

Yes, i don't need it. The chip select of the ADS1220 is tied low. I am only communicating with one slave on the SPI.