cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F334 SPI PROBLEM

ASSAAD.ASSAAD
Associate II
Posted on October 31, 2015 at 08:48

Hello ;

I am using spi with external ADC ; when I use bitbang spi I can communicate with the ADC and it is ok ; But I need to use harware SPI ; when I am using the internal SPI1 I cant communicate with the ADC ; it seems it is a problem in my SPI configuration but I still could not sort it here my code

void InitSPI1(void) 
{
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;
// enable clock for used IO pins
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);
// // connect SPI1 pins to SPI alternate function
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_5); //SCK
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_5); //MISO
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_5); //MOSI
// enable peripheral clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* configure SPI1 in Mode 0 
* CPOL = 0 --> clock is low when idle
* CPHA = 0 --> data is sampled at the first edge
*/
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // one line transmission
SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has to be always high
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 16 bits wide
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; // clock is low when idle
SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge; // data sampled at first edge
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft ; // set the NSS management to hardware
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; // SPI frequency is APB2 frequency / 4 = 21MHz
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
SPI_Init(SPI1, &SPI_InitStruct); 
SPI_Cmd(SPI1, ENABLE); // enable SPI1
// ensure RXNE is initially clear
SPI_ReceiveData8(SPI1); 
}
//-----------------------
unsigned char receive_spi(void )
{
unsigned char data; 
SPI1->DR=0x00;
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE == 0) );
data = SPI_ReceiveData8(SPI1); // Receive data
return data;
}
//-------
void send_spi(char data)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE == 0) ); // Wait for TX buffer to be not empty. ie data loaded
SPI_SendData8(SPI1, data); // Send data
}

Thank you inadvanced
8 REPLIES 8
ASSAAD.ASSAAD
Associate II
Posted on November 02, 2015 at 11:52

Any one could  share SPI code for STM32f334 ; please ?

I still could not make the hardware SPI work

krzysztofrybak6
Associate II
Posted on November 02, 2015 at 14:21

What ADC are You going to connect?

Do You have some logs from logic analyzer if there is clock, chip select active?

If hardware, why You configure this: SPI_InitStruct.SPI_NSS = SPI_NSS_Soft?

ASSAAD.ASSAAD
Associate II
Posted on November 02, 2015 at 14:31

SPI_InitStruct.SPI_NSS = SPI_NSS_Soft: means  I control the chip select in software and not hardware function of the SPI ; right ?

when I use bitbang SPI it works ; I only have problem when I want to use the hardware SPI .

Thank you

AvaTar
Lead
Posted on November 02, 2015 at 14:44

when I use bitbang SPI it works ; I only have problem when I want to use the hardware SPI .

 

Then either your SPI setup is incorrect, you don't treat NSS correctly, or you are too fast.

How about taking a scope, and comparing the working bitbang-waveform with the SPI waveform (and the spec. for the ADC) ?

krzysztofrybak6
Associate II
Posted on November 02, 2015 at 16:06

When NSS is software, as configured in this case, when is it toggled to activate slave? What ADC You use + waveform from logic analyzer :)

ASSAAD.ASSAAD
Associate II
Posted on November 05, 2015 at 16:00

ADS1291 ;

when I use the bitbang spi even faster clock ; it work ;

when I move to the hardware SPI it fails .

Posted on November 06, 2015 at 01:44

Not sure I see the value of separating the send/receive and what the unqualified write to DR will do.

If you have something that works, and something that doesn't, compare them on the scope, it will be more apparent what's actually different.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
krzysztofrybak6
Associate II
Posted on November 06, 2015 at 14:27

In general, Your previous software method works, but it is not recommended in any kind of applications.

There is dedicated core for SPI communication, which You incorrectly configured.

The easiest way is to compare signals on SPI bus when: works fine (despite the method used) and now, when fails.

There is, for example, chip select signal, which You configure to be software configured:

SPI_NSS_Soft 

. This means that You should provide this pin toggling, but it doesn't happen anywhere. 

Brief checking adc datasheet suggests that the chip requires CS input.