AnsweredAssumed Answered

STM32F334 SPI PROBLEM

Question asked by ASSAAD on Oct 31, 2015
Latest reply on Nov 6, 2015 by rybak.krzysztof
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 

Outcomes