cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Code Help

pranathi1091
Associate II
Posted on October 19, 2012 at 10:18

Hello All,

 I am using STM32Fs series and I am writing a code for SPI2 . I have to read and write from the slave device. I have to read the data when RX buffer is full and write data continuoulsy to another device. I have wrtitten a small application. Which could read and write. I am new to this domain. It would be of great help, if any could check and let me know if any changes needed

void

 

SPIInit(

SPI_InitTypeDef

*SPI_InitStruct)

 

{

 

 

//Initialize the SPI_Direction member

SPI_InitStruct->SPI_Direction = SPI_Direction_2Lines_FullDuplex;

//initialize the SPI_Mode member

SPI_InitStruct->SPI_Mode = SPI_Mode_Master;

//initialize the SPI_DataSize member

SPI_InitStruct->SPI_DataSize = SPI_DataSize_8b;

//Initialize the SPI_CPOL member

SPI_InitStruct->SPI_CPOL = SPI_CPOL_High;

//Initialize the SPI_CPHA member

SPI_InitStruct->SPI_CPHA = SPI_CPHA_2Edge;

//Initialize the SPI_NSS member

SPI_InitStruct->SPI_NSS = SPI_NSS_Soft;

//Initialize the SPI_BaudRatePrescaler member

SPI_InitStruct->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;

//Initialize the SPI_FirstBit member */

SPI_InitStruct->SPI_FirstBit = SPI_FirstBit_MSB;

// Initialize the SPI_CRCPolynomial member

SPI_InitStruct->SPI_CRCPolynomial = 7;

// Configure SPI2

SPI_Init(SPI2, SPI_InitStruct);

// Enable SPI2 Receive and Transmit interrupts

SPI_I2S_ITConfig(SPI2, SPI_IT_TXE,

ENABLE

);

SPI_I2S_ITConfig(SPI2, SPI_IT_RXNE,

ENABLE

);

//Enable SPI2

SPI_Cmd(SPI2,

ENABLE

);

}

uint8_t

SpiWrite(

uint16_t

Data)

{

uint8_t

SlaveData;

while

(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) ==

RESET

);

SPI_I2S_SendData(SPIx, Data);

while

(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) ==

RESET

);

SlaveData=SPI_I2S_ReceiveData(SPIx);

return

SUCCESS;

}

uint8_t

SpiRead(

uint8_t

*BufferPtr)

{

uint16_t

RxBuffer=0;

while

(SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_RXNE ));

RxBuffer=SPI_I2S_ReceiveData(SPIx);

return

RxBuffer;

}

Thanks

#c-code #spi-stm32
0 REPLIES 0