2013-05-27 04:51 AM
Hello!
This is my code for SPI:RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
SPI_InitTypeDef spi;
spi.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
spi.SPI_Mode = SPI_Mode_Master;
spi.SPI_DataSize = SPI_DataSize_8b;//16
spi.SPI_CPOL = SPI_CPOL_High;
spi.SPI_CPHA = SPI_CPHA_2Edge;
spi.SPI_NSS = SPI_NSS_Soft;
spi.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
spi.SPI_FirstBit = SPI_FirstBit_MSB;
spi.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &spi);
NVIC_EnableIRQ(SPI2_IRQn);
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
SPI_Cmd(SPI2, ENABLE);
SPI_NSSInternalSoftwareConfig(SPI2, SPI_NSSInternalSoft_Set);
and IRQ Handler:
void SPI2_IRQHandler(){
if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET){
uint16_t spi_data16 = SPI_I2S_ReceiveData16(SPI2);
}
}
According to stm32f30x_spi.c there are several SPI modes: ''Interrupt mode'', ''Polling mode'' and ''DMA mode''.
In ''Interrupt mode'' I receive some data from my connected device.
In ''Polling mode'' (setup is the same as in Interrupt mode but without NVIC_EnableIRQ andSPI_I2S_ITConfig?)I made a function to send/receive 8 bits:
uint8_t SPI_SendRead8(uint8_t ToSend8){
while((SPI2->SR & (uint8_t)SPI_I2S_FLAG_TXE) == RESET){;}
SPI2->DR = ToSend8;
while ((SPI2->SR & (uint8_t)SPI_I2S_FLAG_RXNE) == RESET) {;}
return SPI2->DR;
}
but I receive invalid data. I want to simultaneously readwrite to know what data I receive at this moment. There is an error in my code?
AlsoSPI_DataSize_8b; andSPI_DataSize_16b; SPI_BaudRatePrescaler parameters - which should I use with the
http://www.analog.com/static/imported-files/data_sheets/AD77pdf
?